Refactored UDPNetworkUtility, and tied it to SerialPacket

This commit is contained in:
Kayne Ruse
2014-05-28 23:22:00 +10:00
parent de7da81102
commit 7b3bf24e5d
12 changed files with 166 additions and 132 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ void ClientApplication::Init(int argc, char** argv) {
if (SDLNet_Init()) { if (SDLNet_Init()) {
throw(std::runtime_error("Failed to initialize SDL_net")); throw(std::runtime_error("Failed to initialize SDL_net"));
} }
network.Open(0, PACKET_BUFFER_SIZE); network.Open(0);
} }
void ClientApplication::Proc() { void ClientApplication::Proc() {
-2
View File
@@ -24,8 +24,6 @@
//network //network
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "serial.hpp"
//graphics //graphics
#include "image.hpp" #include "image.hpp"
+9 -18
View File
@@ -96,9 +96,7 @@ void InWorld::Update(double delta) {
SerialPacket packet; SerialPacket packet;
//suck in all waiting packets //suck in all waiting packets
while(network.Receive()) { while(network.Receive(&packet)) {
deserialize(&packet, network.GetInData());
packet.meta.srcAddress = network.GetInPacket()->address;
HandlePacket(packet); HandlePacket(packet);
} }
@@ -354,7 +352,6 @@ void InWorld::HandleCharacterDelete(SerialPacket packet) {
void InWorld::RequestSynchronize() { void InWorld::RequestSynchronize() {
SerialPacket packet; SerialPacket packet;
char buffer[PACKET_STRING_SIZE];
//request a sync //request a sync
packet.meta.type = SerialPacket::Type::SYNCHRONIZE; packet.meta.type = SerialPacket::Type::SYNCHRONIZE;
@@ -362,13 +359,11 @@ void InWorld::RequestSynchronize() {
packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.accountIndex = accountIndex;
packet.clientInfo.characterIndex = characterIndex; packet.clientInfo.characterIndex = characterIndex;
serialize(&packet, buffer); network.SendTo(Channels::SERVER, &packet);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
} }
void InWorld::SendPlayerUpdate() { void InWorld::SendPlayerUpdate() {
SerialPacket packet; SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//pack the packet //pack the packet
packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE; packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
@@ -378,47 +373,43 @@ void InWorld::SendPlayerUpdate() {
packet.characterInfo.position = localCharacter->position; packet.characterInfo.position = localCharacter->position;
packet.characterInfo.motion = localCharacter->motion; packet.characterInfo.motion = localCharacter->motion;
serialize(&packet, buffer); network.SendTo(Channels::SERVER, &packet);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE);
} }
void InWorld::RequestDisconnect() { void InWorld::RequestDisconnect() {
SerialPacket packet; SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//send a disconnect request //send a disconnect request
packet.meta.type = SerialPacket::Type::DISCONNECT; packet.meta.type = SerialPacket::Type::DISCONNECT;
packet.clientInfo.clientIndex = clientIndex; packet.clientInfo.clientIndex = clientIndex;
packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.accountIndex = accountIndex;
packet.clientInfo.characterIndex = characterIndex; packet.clientInfo.characterIndex = characterIndex;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); network.SendTo(Channels::SERVER, &packet);
} }
void InWorld::RequestShutDown() { void InWorld::RequestShutDown() {
SerialPacket packet; SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//send a shutdown request //send a shutdown request
packet.meta.type = SerialPacket::Type::SHUTDOWN; packet.meta.type = SerialPacket::Type::SHUTDOWN;
packet.clientInfo.clientIndex = clientIndex; packet.clientInfo.clientIndex = clientIndex;
packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.accountIndex = accountIndex;
packet.clientInfo.characterIndex = characterIndex; packet.clientInfo.characterIndex = characterIndex;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); network.SendTo(Channels::SERVER, &packet);
} }
void InWorld::RequestRegion(int mapIndex, int x, int y) { void InWorld::RequestRegion(int mapIndex, int x, int y) {
SerialPacket packet; SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//pack the region's data //pack the region's data
packet.meta.type = SerialPacket::Type::REGION_REQUEST; packet.meta.type = SerialPacket::Type::REGION_REQUEST;
packet.regionInfo.mapIndex = mapIndex; packet.regionInfo.mapIndex = mapIndex;
packet.regionInfo.x = x; packet.regionInfo.x = x;
packet.regionInfo.y = y; packet.regionInfo.y = y;
serialize(&packet, buffer);
network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); network.SendTo(Channels::SERVER, &packet);
} }
//------------------------- //-------------------------
-2
View File
@@ -29,8 +29,6 @@
//networking //networking
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "serial.hpp"
//graphics //graphics
#include "image.hpp" #include "image.hpp"
+6 -16
View File
@@ -86,11 +86,9 @@ void LobbyMenu::FrameStart() {
} }
void LobbyMenu::Update(double delta) { void LobbyMenu::Update(double delta) {
//suck in all waiting packets //suck in and process all waiting packets
SerialPacket packet; SerialPacket packet;
while(network.Receive()) { while(network.Receive(&packet)) {
deserialize(&packet, network.GetInData());
packet.meta.srcAddress = network.GetInPacket()->address;
HandlePacket(packet); HandlePacket(packet);
} }
} }
@@ -149,14 +147,10 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (search.MouseButtonUp(button) == Button::State::HOVER) { if (search.MouseButtonUp(button) == Button::State::HOVER) {
//the vars
SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//broadcast to the network, or a specific server //broadcast to the network, or a specific server
SerialPacket packet;
packet.meta.type = SerialPacket::Type::BROADCAST_REQUEST; packet.meta.type = SerialPacket::Type::BROADCAST_REQUEST;
serialize(&packet, buffer); network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet);
network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, PACKET_BUFFER_SIZE);
//reset the server list //reset the server list
serverInfo.clear(); serverInfo.clear();
@@ -164,19 +158,15 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
} }
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) { else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) {
//the vars
SerialPacket packet;
char buffer[PACKET_BUFFER_SIZE];
//pack the packet //pack the packet
SerialPacket packet;
packet.meta.type = SerialPacket::Type::JOIN_REQUEST; packet.meta.type = SerialPacket::Type::JOIN_REQUEST;
strncpy(packet.clientInfo.username, config["client.username"].c_str(), PACKET_STRING_SIZE); strncpy(packet.clientInfo.username, config["client.username"].c_str(), PACKET_STRING_SIZE);
strncpy(packet.clientInfo.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE); strncpy(packet.clientInfo.handle, config["client.handle"].c_str(), PACKET_STRING_SIZE);
strncpy(packet.clientInfo.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE); strncpy(packet.clientInfo.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE);
//join the selected server //join the selected server
serialize(&packet, buffer); network.SendTo(&selection->address, &packet);
network.Send(&selection->address, buffer, PACKET_BUFFER_SIZE);
selection = nullptr; selection = nullptr;
} }
-2
View File
@@ -30,8 +30,6 @@
//network //network
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial_packet.hpp"
#include "serial.hpp"
//client //client
#include "base_scene.hpp" #include "base_scene.hpp"
+1 -1
View File
@@ -1,4 +1,4 @@
/* Copyright: (c) Kayne Ruse 2013 /* Copyright: (c) Kayne Ruse 2013, 2014
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
+120 -44
View File
@@ -1,4 +1,4 @@
/* Copyright: (c) Kayne Ruse 2013 /* Copyright: (c) Kayne Ruse 2013, 2014
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
@@ -21,34 +21,33 @@
*/ */
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial.hpp"
#include <stdexcept> #include <stdexcept>
void UDPNetworkUtility::Open(int port, int packSize) { //DOCS: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network
if (!(socket = SDLNet_UDP_Open(port))) { //NOTE: don't confuse SerialPacket with UDPpacket
Close();
throw(std::runtime_error("Failed to open a UDP socket"));
}
if (!(packOut = SDLNet_AllocPacket(packSize))) { void UDPNetworkUtility::Open(int port) {
socket = SDLNet_UDP_Open(port);
packet = SDLNet_AllocPacket(PACKET_BUFFER_SIZE);
if (!socket || !packet) {
Close(); Close();
throw(std::runtime_error("Failed to allocate the out packet")); throw(std::runtime_error("Failed to open UDPNetworkUtility"));
}
if (!(packIn = SDLNet_AllocPacket(packSize))) {
Close();
throw(std::runtime_error("Failed to allocate the in packet"));
} }
} }
void UDPNetworkUtility::Close() { void UDPNetworkUtility::Close() {
SDLNet_UDP_Close(socket); SDLNet_UDP_Close(socket);
SDLNet_FreePacket(packOut); SDLNet_FreePacket(packet);
SDLNet_FreePacket(packIn);
socket = nullptr; socket = nullptr;
packOut = nullptr; packet = nullptr;
packIn = nullptr;
} }
//-------------------------
//bind to a channel
//-------------------------
int UDPNetworkUtility::Bind(const char* ip, int port, int channel) { int UDPNetworkUtility::Bind(const char* ip, int port, int channel) {
IPaddress add; IPaddress add;
if (SDLNet_ResolveHost(&add, ip, port) == -1) { if (SDLNet_ResolveHost(&add, ip, port) == -1) {
@@ -61,7 +60,7 @@ int UDPNetworkUtility::Bind(const char* ip, int port, int channel) {
int UDPNetworkUtility::Bind(IPaddress* add, int channel) { int UDPNetworkUtility::Bind(IPaddress* add, int channel) {
int ret = SDLNet_UDP_Bind(socket, channel, add); int ret = SDLNet_UDP_Bind(socket, channel, add);
if (ret == -1) { if (ret < 0) {
throw(std::runtime_error("Failed to bind to a channel")); throw(std::runtime_error("Failed to bind to a channel"));
} }
@@ -72,25 +71,29 @@ void UDPNetworkUtility::Unbind(int channel) {
SDLNet_UDP_Unbind(socket, channel); SDLNet_UDP_Unbind(socket, channel);
} }
int UDPNetworkUtility::Send(const char* ip, int port, void* data, int len) { //-------------------------
//send a buffer
//-------------------------
int UDPNetworkUtility::SendTo(const char* ip, int port, void* data, int len) {
IPaddress add; IPaddress add;
if (SDLNet_ResolveHost(&add, ip, port) == -1) { if (SDLNet_ResolveHost(&add, ip, port) == -1) {
throw(std::runtime_error("Failed to resolve a host")); throw(std::runtime_error("Failed to resolve a host"));
} }
Send(&add, data, len); SendTo(&add, data, len);
} }
int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) { int UDPNetworkUtility::SendTo(IPaddress* add, void* data, int len) {
if (len > packOut->maxlen) { if (len > packet->maxlen) {
throw(std::runtime_error("Failed to copy the data into the packet")); throw(std::runtime_error("The buffer is to large for the UDPpacket"));
} }
memset(packOut->data, 0, packOut->maxlen); memset(packet->data, 0, packet->maxlen);
memcpy(packOut->data, data, len); memcpy(packet->data, data, len);
packOut->len = len; packet->len = len;
packOut->address = *add; packet->address = *add;
int ret = SDLNet_UDP_Send(socket, -1, packOut); int ret = SDLNet_UDP_Send(socket, -1, packet);
if (ret <= 0) { if (ret <= 0) {
throw(std::runtime_error("Failed to send a packet")); throw(std::runtime_error("Failed to send a packet"));
@@ -99,15 +102,15 @@ int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) {
return ret; return ret;
} }
int UDPNetworkUtility::Send(int channel, void* data, int len) { int UDPNetworkUtility::SendTo(int channel, void* data, int len) {
if (len > packOut->maxlen) { if (len > packet->maxlen) {
throw(std::runtime_error("Failed to copy the data into the packet")); throw(std::runtime_error("The buffer is to large for the UDPpacket"));
} }
memset(packOut->data, 0, packOut->maxlen); memset(packet->data, 0, packet->maxlen);
memcpy(packOut->data, data, len); memcpy(packet->data, data, len);
packOut->len = len; packet->len = len;
int ret = SDLNet_UDP_Send(socket, channel, packOut); int ret = SDLNet_UDP_Send(socket, channel, packet);
if (ret <= 0) { if (ret <= 0) {
throw(std::runtime_error("Failed to send a packet")); throw(std::runtime_error("Failed to send a packet"));
@@ -116,29 +119,102 @@ int UDPNetworkUtility::Send(int channel, void* data, int len) {
return ret; return ret;
} }
int UDPNetworkUtility::SendAll(void* data, int len) { int UDPNetworkUtility::SendToAllChannels(void* data, int len) {
if (len > packOut->maxlen) { if (len > packet->maxlen) {
throw(std::runtime_error("Failed to copy the data into the packet")); throw(std::runtime_error("The buffer is to large for the UDPpacket"));
} }
memset(packOut->data, 0, packOut->maxlen); memset(packet->data, 0, packet->maxlen);
memcpy(packOut->data, data, len); memcpy(packet->data, data, len);
packOut->len = len; packet->len = len;
int sent = 0; int sent = 0;
//send to all bound channels //send to all bound channels
for (int i = 0; i < SDLNET_MAX_UDPCHANNELS; i++) { for (int i = 0; i < SDLNET_MAX_UDPCHANNELS; i++) {
if (SDLNet_UDP_GetPeerAddress(socket, i)) { if (SDLNet_UDP_GetPeerAddress(socket, i)) {
sent += SDLNet_UDP_Send(socket, i, packOut); sent += SDLNet_UDP_Send(socket, i, packet);
} }
} }
return sent; return sent;
} }
//TODO: put a void* and int* parameter list here
int UDPNetworkUtility::Receive() { int UDPNetworkUtility::Receive() {
memset(packIn->data, 0, packIn->maxlen); memset(packet->data, 0, packet->maxlen);
int ret = SDLNet_UDP_Recv(socket, packIn); int ret = SDLNet_UDP_Recv(socket, packet);
if (ret < 0) {
throw(std::runtime_error("Unknown network error occured"));
}
return ret;
}
//-------------------------
//send a SerialPacket
//-------------------------
int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPacket) {
IPaddress add;
if (SDLNet_ResolveHost(&add, ip, port) == -1) {
throw(std::runtime_error("Failed to resolve a host"));
}
SendTo(&add, serialPacket);
}
int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) {
memset(packet->data, 0, packet->maxlen);
serialize(serialPacket, packet->data);
packet->len = PACKET_BUFFER_SIZE;
packet->address = *add;
int ret = SDLNet_UDP_Send(socket, -1, packet);
if (ret <= 0) {
throw(std::runtime_error("Failed to send a packet"));
}
return ret;
}
int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) {
memset(packet->data, 0, packet->maxlen);
serialize(serialPacket, packet->data);
packet->len = PACKET_BUFFER_SIZE;
int ret = SDLNet_UDP_Send(socket, channel, packet);
if (ret <= 0) {
throw(std::runtime_error("Failed to send a packet"));
}
return ret;
}
int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) {
memset(packet->data, 0, packet->maxlen);
serialize(serialPacket, packet->data);
packet->len = PACKET_BUFFER_SIZE;
int sent = 0;
//send to all bound channels
for (int i = 0; i < SDLNET_MAX_UDPCHANNELS; i++) {
if (SDLNet_UDP_GetPeerAddress(socket, i)) {
sent += SDLNet_UDP_Send(socket, i, packet);
}
}
return sent;
}
int UDPNetworkUtility::Receive(SerialPacket* serialPacket) {
memset(packet->data, 0, packet->maxlen);
int ret = SDLNet_UDP_Recv(socket, packet);
deserialize(serialPacket, packet->data);
serialPacket->meta.srcAddress = packet->address;
if (ret < 0) { if (ret < 0) {
throw(std::runtime_error("Unknown network error occured")); throw(std::runtime_error("Unknown network error occured"));
+20 -19
View File
@@ -1,4 +1,4 @@
/* Copyright: (c) Kayne Ruse 2013 /* Copyright: (c) Kayne Ruse 2013, 2014
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
@@ -24,12 +24,14 @@
#include "SDL/SDL_net.h" #include "SDL/SDL_net.h"
#include "serial_packet.hpp"
class UDPNetworkUtility { class UDPNetworkUtility {
public: public:
UDPNetworkUtility() = default; UDPNetworkUtility() = default;
~UDPNetworkUtility() = default; ~UDPNetworkUtility() = default;
void Open(int port, int packSize); void Open(int port);
void Close(); void Close();
//bind to a channel //bind to a channel
@@ -41,31 +43,30 @@ public:
return SDLNet_UDP_GetPeerAddress(socket, channel); return SDLNet_UDP_GetPeerAddress(socket, channel);
} }
int Send(const char* ip, int port, void* data, int len); //send a buffer
int Send(IPaddress* add, void* data, int len); int SendTo(const char* ip, int port, void* data, int len);
int Send(int channel, void* data, int len); int SendTo(IPaddress* add, void* data, int len);
int SendAll(void* data, int len); int SendTo(int channel, void* data, int len);
int SendToAllChannels(void* data, int len);
int Receive(); int Receive();
void* GetOutData() const { //send a SerialPacket
return reinterpret_cast<void*>(packOut->data); int SendTo(const char* ip, int port, SerialPacket* serialPacket);
}; int SendTo(IPaddress* add, SerialPacket* serialPacket);
void* GetInData() const { int SendTo(int channel, SerialPacket* serialPacket);
return reinterpret_cast<void*>(packIn->data); int SendToAllChannels(SerialPacket* serialPacket);
}; int Receive(SerialPacket* serialPacket);
UDPpacket* GetOutPacket() const {
return packOut; //accessors
} UDPpacket* GetPacket() const {
UDPpacket* GetInPacket() const { return packet;
return packIn;
} }
UDPsocket GetSocket() const { UDPsocket GetSocket() const {
return socket; return socket;
} }
private: private:
UDPsocket socket = nullptr; UDPsocket socket = nullptr;
UDPpacket* packOut = nullptr; UDPpacket* packet = nullptr;
UDPpacket* packIn = nullptr;
}; };
#endif #endif
-2
View File
@@ -35,9 +35,7 @@
#include "region_pager.hpp" #include "region_pager.hpp"
//networking //networking
#include "serial_packet.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial.hpp"
//common //common
#include "config_utility.hpp" #include "config_utility.hpp"
+6 -18
View File
@@ -36,9 +36,7 @@ void ServerApplication::HandleBroadcastRequest(SerialPacket packet) {
packet.serverInfo.playerCount = characterMap.size(); packet.serverInfo.playerCount = characterMap.size();
//bounce this packet //bounce this packet
char buffer[PACKET_BUFFER_SIZE]; network.SendTo(&packet.meta.srcAddress, &packet);
serialize(&packet, buffer);
network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
} }
void ServerApplication::HandleJoinRequest(SerialPacket packet) { void ServerApplication::HandleJoinRequest(SerialPacket packet) {
@@ -70,9 +68,7 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) {
packet.clientInfo.characterIndex = characterIndex; packet.clientInfo.characterIndex = characterIndex;
//bounce this packet //bounce this packet
char buffer[PACKET_BUFFER_SIZE]; network.SendTo(&newClient.address, &packet);
serialize(&packet, buffer);
network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE);
//send the new character to all clients //send the new character to all clients
packet.meta.type = SerialPacket::Type::CHARACTER_NEW; packet.meta.type = SerialPacket::Type::CHARACTER_NEW;
@@ -94,7 +90,6 @@ void ServerApplication::HandleSynchronize(SerialPacket packet) {
//send all the server's data to this client //send all the server's data to this client
SerialPacket newPacket; SerialPacket newPacket;
char buffer[PACKET_BUFFER_SIZE];
//characters //characters
newPacket.meta.type = SerialPacket::Type::CHARACTER_UPDATE; newPacket.meta.type = SerialPacket::Type::CHARACTER_UPDATE;
@@ -108,8 +103,7 @@ void ServerApplication::HandleSynchronize(SerialPacket packet) {
newPacket.characterInfo.motion = it.second.motion; newPacket.characterInfo.motion = it.second.motion;
newPacket.characterInfo.stats = it.second.stats; newPacket.characterInfo.stats = it.second.stats;
serialize(&newPacket, buffer); network.SendTo(&clientMap[packet.clientInfo.clientIndex].address, &newPacket);
network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE);
} }
} }
@@ -117,9 +111,7 @@ void ServerApplication::HandleDisconnect(SerialPacket packet) {
//TODO: authenticate who is disconnecting/kicking //TODO: authenticate who is disconnecting/kicking
//forward to the specified client //forward to the specified client
char buffer[PACKET_BUFFER_SIZE]; network.SendTo(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, &packet);
serialize(&packet, buffer);
network.Send(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, buffer, PACKET_BUFFER_SIZE);
//unload client and server-side characters //unload client and server-side characters
for (std::map<int, CharacterData>::iterator it = characterMap.begin(); it != characterMap.end(); /* EMPTY */ ) { for (std::map<int, CharacterData>::iterator it = characterMap.begin(); it != characterMap.end(); /* EMPTY */ ) {
@@ -175,17 +167,13 @@ void ServerApplication::HandleRegionRequest(SerialPacket packet) {
packet.regionInfo.region = regionPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y); packet.regionInfo.region = regionPager.GetRegion(packet.regionInfo.x, packet.regionInfo.y);
//send the content //send the content
char buffer[PACKET_BUFFER_SIZE]; network.SendTo(&packet.meta.srcAddress, &packet);
serialize(&packet, buffer);
network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE);
} }
void ServerApplication::PumpPacket(SerialPacket packet) { void ServerApplication::PumpPacket(SerialPacket packet) {
//NOTE: I don't really like this, but it'll do for now //NOTE: I don't really like this, but it'll do for now
char buffer[PACKET_BUFFER_SIZE];
serialize(&packet, buffer);
for (auto& it : clientMap) { for (auto& it : clientMap) {
network.Send(&it.second.address, buffer, PACKET_BUFFER_SIZE); network.SendTo(&it.second.address, &packet);
} }
} }
+3 -7
View File
@@ -22,6 +22,7 @@
#include "server_application.hpp" #include "server_application.hpp"
#include "sql_utility.hpp" #include "sql_utility.hpp"
#include "serial.hpp"
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
@@ -52,7 +53,7 @@ void ServerApplication::Init(int argc, char** argv) {
if (SDLNet_Init()) { if (SDLNet_Init()) {
throw(std::runtime_error("Failed to initialize SDL_net")); throw(std::runtime_error("Failed to initialize SDL_net"));
} }
network.Open(config.Int("server.port"), PACKET_BUFFER_SIZE); network.Open(config.Int("server.port"));
std::cout << "Initialized SDL_net" << std::endl; std::cout << "Initialized SDL_net" << std::endl;
//Init SQL //Init SQL
@@ -119,12 +120,7 @@ void ServerApplication::Proc() {
SerialPacket packet; SerialPacket packet;
while(running) { while(running) {
//suck in the waiting packets & process them //suck in the waiting packets & process them
while(network.Receive()) { while(network.Receive(&packet)) {
//get the packet
deserialize(&packet, network.GetInData());
//cache the source address
packet.meta.srcAddress = network.GetInPacket()->address;
//we need to go deeper
HandlePacket(packet); HandlePacket(packet);
} }
//update the internals //update the internals