From a850d6b1af693d8a68d1f0a65a33fbac6007bf6b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 6 Mar 2014 00:20:31 +1100 Subject: [PATCH] Added dummy serialization functions --- client/scenes/in_world.cpp | 20 +++++++++++++++----- client/scenes/in_world.hpp | 1 + client/scenes/lobby_menu.cpp | 10 +++++++--- client/scenes/lobby_menu.hpp | 1 + common/network/serial.cpp | 32 ++++++++++++++++++++++++++++++++ common/network/serial.hpp | 30 ++++++++++++++++++++++++++++++ server/server_application.cpp | 22 ++++++++++++++++------ server/server_application.hpp | 1 + 8 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 common/network/serial.cpp create mode 100644 common/network/serial.hpp diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index cefcc86..9a8ba5b 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -65,11 +65,14 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet packet.playerInfo.motion = {0,0}; //send it - network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); //request a sync packet.meta.type = NetworkPacket::Type::SYNCHRONIZE; - network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket)); + serialize(&packet, buffer); + network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); } InWorld::~InWorld() { @@ -295,7 +298,10 @@ void InWorld::SendState() { packet.playerInfo.position = localCharacter->GetPosition(); packet.playerInfo.motion = localCharacter->GetMotion(); - network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + + network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); } void InWorld::RequestDisconnect() { @@ -303,7 +309,9 @@ void InWorld::RequestDisconnect() { NetworkPacket packet; packet.meta.type = NetworkPacket::Type::DISCONNECT; packet.clientInfo.index = clientIndex; - network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); } void InWorld::RequestShutDown() { @@ -311,5 +319,7 @@ void InWorld::RequestShutDown() { NetworkPacket packet; packet.meta.type = NetworkPacket::Type::SHUTDOWN; packet.clientInfo.index = clientIndex; - network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + network.Send(Channels::SERVER, buffer, sizeof(NetworkPacket)); } \ No newline at end of file diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 666437a..b3b74f9 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -27,6 +27,7 @@ #include "config_utility.hpp" #include "udp_network_utility.hpp" #include "network_packet.hpp" +#include "serial.hpp" #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 9f91b68..dfed9e4 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -80,7 +80,7 @@ void LobbyMenu::Update(double delta) { //suck in all waiting packets NetworkPacket packet; while(network.Receive()) { - memcpy(&packet, network.GetInData(), sizeof(NetworkPacket)); + deserialize(&packet, network.GetInData()); packet.meta.srcAddress = network.GetInPacket()->address; HandlePacket(packet); } @@ -128,7 +128,9 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { //broadcast to the network, or a specific server NetworkPacket packet; packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST; - network.Send(config["server.host"].c_str(), config.Int("server.port"), reinterpret_cast(&packet), sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + network.Send(config["server.host"].c_str(), config.Int("server.port"), buffer, sizeof(NetworkPacket)); //reset the server list serverInfo.clear(); @@ -139,7 +141,9 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { //join the selected server NetworkPacket packet; packet.meta.type = NetworkPacket::Type::JOIN_REQUEST; - network.Send(&selection->address, &packet, sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + network.Send(&selection->address, buffer, sizeof(NetworkPacket)); selection = nullptr; } diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index fc3551a..36cfbe7 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -31,6 +31,7 @@ #include "config_utility.hpp" #include "udp_network_utility.hpp" #include "network_packet.hpp" +#include "serial.hpp" #include diff --git a/common/network/serial.cpp b/common/network/serial.cpp new file mode 100644 index 0000000..73f92c1 --- /dev/null +++ b/common/network/serial.cpp @@ -0,0 +1,32 @@ +/* Copyright: (c) Kayne Ruse 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 + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "serial.hpp" + +#include + +void serialize(NetworkPacket* const packet, void* buffer) { + memcpy(buffer, packet, sizeof(NetworkPacket)); +} + +void deserialize(NetworkPacket* const packet, void* buffer) { + memcpy(packet, buffer, sizeof(NetworkPacket)); +} \ No newline at end of file diff --git a/common/network/serial.hpp b/common/network/serial.hpp new file mode 100644 index 0000000..ef0c291 --- /dev/null +++ b/common/network/serial.hpp @@ -0,0 +1,30 @@ +/* Copyright: (c) Kayne Ruse 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 + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef SERIAL_HPP_ +#define SERIAL_HPP_ + +#include "network_packet.hpp" + +void serialize(NetworkPacket* const, void*); +void deserialize(NetworkPacket* const, void*); + +#endif diff --git a/server/server_application.cpp b/server/server_application.cpp index c2f79a4..9136327 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -89,7 +89,7 @@ void ServerApplication::Loop() { //suck in the waiting packets & process them try { while(network.Receive()) { - memcpy(&packet, network.GetInData(), sizeof(NetworkPacket)); + deserialize(&packet, network.GetInData()); packet.meta.srcAddress = network.GetInPacket()->address; HandlePacket(packet); } @@ -158,7 +158,9 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) { //send back the server's name packet.meta.type = NetworkPacket::Type::BROADCAST_RESPONSE; snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str()); - network.Send(&packet.meta.srcAddress, &packet, sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + network.Send(&packet.meta.srcAddress, buffer, sizeof(NetworkPacket)); } void ServerApplication::HandleJoinRequest(NetworkPacket packet) { @@ -174,7 +176,9 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) { //send the client their info packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE; packet.clientInfo.index = clientCounter; - network.Send(&newClient.address, &packet, sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + network.Send(&newClient.address, buffer, sizeof(NetworkPacket)); //finished this routine clientCounter++; @@ -183,7 +187,9 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) { void ServerApplication::HandleDisconnect(NetworkPacket packet) { //disconnect the specified client - network.Send(&clientMap[packet.clientInfo.index].address, &packet, sizeof(NetworkPacket)); + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); + network.Send(&clientMap[packet.clientInfo.index].address, buffer, sizeof(NetworkPacket)); clientMap.erase(packet.clientInfo.index); //delete players @@ -209,6 +215,7 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) { void ServerApplication::HandleSynchronize(NetworkPacket packet) { //send all the server's data to this client NetworkPacket newPacket; + char buffer[sizeof(NetworkPacket)]; //players newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE; @@ -218,7 +225,8 @@ void ServerApplication::HandleSynchronize(NetworkPacket packet) { snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str()); newPacket.playerInfo.position = it.second.position; newPacket.playerInfo.motion = it.second.motion; - network.Send(&clientMap[packet.clientInfo.index].address, &newPacket, sizeof(NetworkPacket)); + serialize(&newPacket, buffer); + network.Send(&clientMap[packet.clientInfo.index].address, buffer, sizeof(NetworkPacket)); } } @@ -294,7 +302,9 @@ void ServerApplication::HandlePlayerUpdate(NetworkPacket packet) { void ServerApplication::PumpPacket(NetworkPacket packet) { //send this packet to all clients + char buffer[sizeof(NetworkPacket)]; + serialize(&packet, buffer); for (auto& it : clientMap) { - network.Send(&it.second.address, &packet, sizeof(NetworkPacket)); + network.Send(&it.second.address, buffer, sizeof(NetworkPacket)); } } \ No newline at end of file diff --git a/server/server_application.hpp b/server/server_application.hpp index 073d480..a339c77 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -25,6 +25,7 @@ //networking #include "network_packet.hpp" #include "udp_network_utility.hpp" +#include "serial.hpp" //APIs #include "sqlite3/sqlite3.h"