diff --git a/client/client_application.cpp b/client/client_application.cpp index cbf2f68..cb225c6 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -56,7 +56,7 @@ void ClientApplication::Init(int argc, char** argv) { if (SDLNet_Init()) { throw(std::runtime_error("Failed to initialize SDL_net")); } - network.Open(0, PACKET_BUFFER_SIZE); + network.Open(0); } void ClientApplication::Proc() { diff --git a/client/in_combat.hpp b/client/in_combat.hpp index 44e91ee..ebd34c7 100644 --- a/client/in_combat.hpp +++ b/client/in_combat.hpp @@ -24,8 +24,6 @@ //network #include "udp_network_utility.hpp" -#include "serial_packet.hpp" -#include "serial.hpp" //graphics #include "image.hpp" diff --git a/client/in_world.cpp b/client/in_world.cpp index 6914681..0caddff 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -96,9 +96,7 @@ void InWorld::Update(double delta) { SerialPacket packet; //suck in all waiting packets - while(network.Receive()) { - deserialize(&packet, network.GetInData()); - packet.meta.srcAddress = network.GetInPacket()->address; + while(network.Receive(&packet)) { HandlePacket(packet); } @@ -354,7 +352,6 @@ void InWorld::HandleCharacterDelete(SerialPacket packet) { void InWorld::RequestSynchronize() { SerialPacket packet; - char buffer[PACKET_STRING_SIZE]; //request a sync packet.meta.type = SerialPacket::Type::SYNCHRONIZE; @@ -362,13 +359,11 @@ void InWorld::RequestSynchronize() { packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.characterIndex = characterIndex; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + network.SendTo(Channels::SERVER, &packet); } void InWorld::SendPlayerUpdate() { SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; //pack the packet packet.meta.type = SerialPacket::Type::CHARACTER_UPDATE; @@ -378,47 +373,43 @@ void InWorld::SendPlayerUpdate() { packet.characterInfo.position = localCharacter->position; packet.characterInfo.motion = localCharacter->motion; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + network.SendTo(Channels::SERVER, &packet); } void InWorld::RequestDisconnect() { SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; //send a disconnect request packet.meta.type = SerialPacket::Type::DISCONNECT; packet.clientInfo.clientIndex = clientIndex; packet.clientInfo.accountIndex = accountIndex; packet.clientInfo.characterIndex = characterIndex; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + + network.SendTo(Channels::SERVER, &packet); } void InWorld::RequestShutDown() { SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; //send a shutdown request packet.meta.type = SerialPacket::Type::SHUTDOWN; packet.clientInfo.clientIndex = clientIndex; packet.clientInfo.accountIndex = accountIndex; 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) { SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; //pack the region's data packet.meta.type = SerialPacket::Type::REGION_REQUEST; packet.regionInfo.mapIndex = mapIndex; packet.regionInfo.x = x; packet.regionInfo.y = y; - serialize(&packet, buffer); - network.Send(Channels::SERVER, buffer, PACKET_BUFFER_SIZE); + + network.SendTo(Channels::SERVER, &packet); } //------------------------- diff --git a/client/in_world.hpp b/client/in_world.hpp index 2062d3e..c245e75 100644 --- a/client/in_world.hpp +++ b/client/in_world.hpp @@ -29,8 +29,6 @@ //networking #include "udp_network_utility.hpp" -#include "serial_packet.hpp" -#include "serial.hpp" //graphics #include "image.hpp" diff --git a/client/lobby_menu.cpp b/client/lobby_menu.cpp index cf7732d..f411ba6 100644 --- a/client/lobby_menu.cpp +++ b/client/lobby_menu.cpp @@ -86,11 +86,9 @@ void LobbyMenu::FrameStart() { } void LobbyMenu::Update(double delta) { - //suck in all waiting packets + //suck in and process all waiting packets SerialPacket packet; - while(network.Receive()) { - deserialize(&packet, network.GetInData()); - packet.meta.srcAddress = network.GetInPacket()->address; + while(network.Receive(&packet)) { HandlePacket(packet); } } @@ -149,14 +147,10 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (search.MouseButtonUp(button) == Button::State::HOVER) { - //the vars - SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; - //broadcast to the network, or a specific server + SerialPacket packet; packet.meta.type = SerialPacket::Type::BROADCAST_REQUEST; - serialize(&packet, buffer); - network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, PACKET_BUFFER_SIZE); + network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); //reset the server list 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) { - //the vars - SerialPacket packet; - char buffer[PACKET_BUFFER_SIZE]; - //pack the packet + SerialPacket packet; packet.meta.type = SerialPacket::Type::JOIN_REQUEST; 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.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE); //join the selected server - serialize(&packet, buffer); - network.Send(&selection->address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&selection->address, &packet); selection = nullptr; } diff --git a/client/lobby_menu.hpp b/client/lobby_menu.hpp index 9c858cb..02a20b9 100644 --- a/client/lobby_menu.hpp +++ b/client/lobby_menu.hpp @@ -30,8 +30,6 @@ //network #include "udp_network_utility.hpp" -#include "serial_packet.hpp" -#include "serial.hpp" //client #include "base_scene.hpp" diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index e97c711..8b648a8 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -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 * warranty. In no event will the authors be held liable for any damages diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index 5f828c7..5fb669e 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -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 * warranty. In no event will the authors be held liable for any damages @@ -21,34 +21,33 @@ */ #include "udp_network_utility.hpp" +#include "serial.hpp" + #include -void UDPNetworkUtility::Open(int port, int packSize) { - if (!(socket = SDLNet_UDP_Open(port))) { - Close(); - throw(std::runtime_error("Failed to open a UDP socket")); - } +//DOCS: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network +//NOTE: don't confuse SerialPacket with UDPpacket - 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(); - throw(std::runtime_error("Failed to allocate the out packet")); - } - - if (!(packIn = SDLNet_AllocPacket(packSize))) { - Close(); - throw(std::runtime_error("Failed to allocate the in packet")); + throw(std::runtime_error("Failed to open UDPNetworkUtility")); } } void UDPNetworkUtility::Close() { SDLNet_UDP_Close(socket); - SDLNet_FreePacket(packOut); - SDLNet_FreePacket(packIn); + SDLNet_FreePacket(packet); socket = nullptr; - packOut = nullptr; - packIn = nullptr; + packet = nullptr; } +//------------------------- +//bind to a channel +//------------------------- + int UDPNetworkUtility::Bind(const char* ip, int port, int channel) { IPaddress add; 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 ret = SDLNet_UDP_Bind(socket, channel, add); - if (ret == -1) { + if (ret < 0) { throw(std::runtime_error("Failed to bind to a channel")); } @@ -72,25 +71,29 @@ void UDPNetworkUtility::Unbind(int 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; if (SDLNet_ResolveHost(&add, ip, port) == -1) { 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) { - if (len > packOut->maxlen) { - throw(std::runtime_error("Failed to copy the data into the packet")); +int UDPNetworkUtility::SendTo(IPaddress* add, void* data, int len) { + if (len > packet->maxlen) { + throw(std::runtime_error("The buffer is to large for the UDPpacket")); } - memset(packOut->data, 0, packOut->maxlen); - memcpy(packOut->data, data, len); - packOut->len = len; - packOut->address = *add; + memset(packet->data, 0, packet->maxlen); + memcpy(packet->data, data, len); + packet->len = len; + packet->address = *add; - int ret = SDLNet_UDP_Send(socket, -1, packOut); + int ret = SDLNet_UDP_Send(socket, -1, packet); if (ret <= 0) { throw(std::runtime_error("Failed to send a packet")); @@ -99,15 +102,15 @@ int UDPNetworkUtility::Send(IPaddress* add, void* data, int len) { return ret; } -int UDPNetworkUtility::Send(int channel, void* data, int len) { - if (len > packOut->maxlen) { - throw(std::runtime_error("Failed to copy the data into the packet")); +int UDPNetworkUtility::SendTo(int channel, void* data, int len) { + if (len > packet->maxlen) { + throw(std::runtime_error("The buffer is to large for the UDPpacket")); } - memset(packOut->data, 0, packOut->maxlen); - memcpy(packOut->data, data, len); - packOut->len = len; + memset(packet->data, 0, packet->maxlen); + memcpy(packet->data, data, len); + packet->len = len; - int ret = SDLNet_UDP_Send(socket, channel, packOut); + int ret = SDLNet_UDP_Send(socket, channel, packet); if (ret <= 0) { throw(std::runtime_error("Failed to send a packet")); @@ -116,29 +119,30 @@ int UDPNetworkUtility::Send(int channel, void* data, int len) { return ret; } -int UDPNetworkUtility::SendAll(void* data, int len) { - if (len > packOut->maxlen) { - throw(std::runtime_error("Failed to copy the data into the packet")); +int UDPNetworkUtility::SendToAllChannels(void* data, int len) { + if (len > packet->maxlen) { + throw(std::runtime_error("The buffer is to large for the UDPpacket")); } - memset(packOut->data, 0, packOut->maxlen); - memcpy(packOut->data, data, len); - packOut->len = len; + memset(packet->data, 0, packet->maxlen); + memcpy(packet->data, data, len); + packet->len = len; 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, packOut); + sent += SDLNet_UDP_Send(socket, i, packet); } } return sent; } +//TODO: put a void* and int* parameter list here int UDPNetworkUtility::Receive() { - memset(packIn->data, 0, packIn->maxlen); - int ret = SDLNet_UDP_Recv(socket, packIn); + memset(packet->data, 0, packet->maxlen); + int ret = SDLNet_UDP_Recv(socket, packet); if (ret < 0) { throw(std::runtime_error("Unknown network error occured")); @@ -146,3 +150,75 @@ int UDPNetworkUtility::Receive() { 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) { + throw(std::runtime_error("Unknown network error occured")); + } + + return ret; +} \ No newline at end of file diff --git a/common/network/udp_network_utility.hpp b/common/network/udp_network_utility.hpp index 9ba7842..4ebdd98 100644 --- a/common/network/udp_network_utility.hpp +++ b/common/network/udp_network_utility.hpp @@ -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 * warranty. In no event will the authors be held liable for any damages @@ -24,12 +24,14 @@ #include "SDL/SDL_net.h" +#include "serial_packet.hpp" + class UDPNetworkUtility { public: UDPNetworkUtility() = default; ~UDPNetworkUtility() = default; - void Open(int port, int packSize); + void Open(int port); void Close(); //bind to a channel @@ -41,31 +43,30 @@ public: return SDLNet_UDP_GetPeerAddress(socket, channel); } - int Send(const char* ip, int port, void* data, int len); - int Send(IPaddress* add, void* data, int len); - int Send(int channel, void* data, int len); - int SendAll(void* data, int len); + //send a buffer + int SendTo(const char* ip, int port, void* data, int len); + int SendTo(IPaddress* add, void* data, int len); + int SendTo(int channel, void* data, int len); + int SendToAllChannels(void* data, int len); int Receive(); - void* GetOutData() const { - return reinterpret_cast(packOut->data); - }; - void* GetInData() const { - return reinterpret_cast(packIn->data); - }; - UDPpacket* GetOutPacket() const { - return packOut; - } - UDPpacket* GetInPacket() const { - return packIn; + //send a SerialPacket + int SendTo(const char* ip, int port, SerialPacket* serialPacket); + int SendTo(IPaddress* add, SerialPacket* serialPacket); + int SendTo(int channel, SerialPacket* serialPacket); + int SendToAllChannels(SerialPacket* serialPacket); + int Receive(SerialPacket* serialPacket); + + //accessors + UDPpacket* GetPacket() const { + return packet; } UDPsocket GetSocket() const { return socket; } private: UDPsocket socket = nullptr; - UDPpacket* packOut = nullptr; - UDPpacket* packIn = nullptr; + UDPpacket* packet = nullptr; }; #endif diff --git a/server/server_application.hpp b/server/server_application.hpp index 0437e1b..cbc7511 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -35,9 +35,7 @@ #include "region_pager.hpp" //networking -#include "serial_packet.hpp" #include "udp_network_utility.hpp" -#include "serial.hpp" //common #include "config_utility.hpp" diff --git a/server/server_connections.cpp b/server/server_connections.cpp index 1e771c3..11e99ce 100644 --- a/server/server_connections.cpp +++ b/server/server_connections.cpp @@ -36,9 +36,7 @@ void ServerApplication::HandleBroadcastRequest(SerialPacket packet) { packet.serverInfo.playerCount = characterMap.size(); //bounce this packet - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); - network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&packet.meta.srcAddress, &packet); } void ServerApplication::HandleJoinRequest(SerialPacket packet) { @@ -70,9 +68,7 @@ void ServerApplication::HandleJoinRequest(SerialPacket packet) { packet.clientInfo.characterIndex = characterIndex; //bounce this packet - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); - network.Send(&newClient.address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&newClient.address, &packet); //send the new character to all clients 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 SerialPacket newPacket; - char buffer[PACKET_BUFFER_SIZE]; //characters newPacket.meta.type = SerialPacket::Type::CHARACTER_UPDATE; @@ -108,8 +103,7 @@ void ServerApplication::HandleSynchronize(SerialPacket packet) { newPacket.characterInfo.motion = it.second.motion; newPacket.characterInfo.stats = it.second.stats; - serialize(&newPacket, buffer); - network.Send(&clientMap[packet.clientInfo.clientIndex].address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&clientMap[packet.clientInfo.clientIndex].address, &newPacket); } } @@ -117,9 +111,7 @@ void ServerApplication::HandleDisconnect(SerialPacket packet) { //TODO: authenticate who is disconnecting/kicking //forward to the specified client - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); - network.Send(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&clientMap[accountMap[packet.clientInfo.accountIndex].clientIndex].address, &packet); //unload client and server-side characters for (std::map::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); //send the content - char buffer[PACKET_BUFFER_SIZE]; - serialize(&packet, buffer); - network.Send(&packet.meta.srcAddress, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&packet.meta.srcAddress, &packet); } void ServerApplication::PumpPacket(SerialPacket packet) { //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) { - network.Send(&it.second.address, buffer, PACKET_BUFFER_SIZE); + network.SendTo(&it.second.address, &packet); } } diff --git a/server/server_internals.cpp b/server/server_internals.cpp index d75c9c3..1cfeb4c 100644 --- a/server/server_internals.cpp +++ b/server/server_internals.cpp @@ -22,6 +22,7 @@ #include "server_application.hpp" #include "sql_utility.hpp" +#include "serial.hpp" #include #include @@ -52,7 +53,7 @@ void ServerApplication::Init(int argc, char** argv) { if (SDLNet_Init()) { 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; //Init SQL @@ -119,12 +120,7 @@ void ServerApplication::Proc() { SerialPacket packet; while(running) { //suck in the waiting packets & process them - while(network.Receive()) { - //get the packet - deserialize(&packet, network.GetInData()); - //cache the source address - packet.meta.srcAddress = network.GetInPacket()->address; - //we need to go deeper + while(network.Receive(&packet)) { HandlePacket(packet); } //update the internals