Added dummy serialization functions

This commit is contained in:
Kayne Ruse
2014-03-06 00:20:31 +11:00
parent 7703c4b0ad
commit a850d6b1af
8 changed files with 103 additions and 14 deletions
+15 -5
View File
@@ -65,11 +65,14 @@ InWorld::InWorld(ConfigUtility* const argConfig, UDPNetworkUtility* const argNet
packet.playerInfo.motion = {0,0}; packet.playerInfo.motion = {0,0};
//send it //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 //request a sync
packet.meta.type = NetworkPacket::Type::SYNCHRONIZE; 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() { InWorld::~InWorld() {
@@ -295,7 +298,10 @@ void InWorld::SendState() {
packet.playerInfo.position = localCharacter->GetPosition(); packet.playerInfo.position = localCharacter->GetPosition();
packet.playerInfo.motion = localCharacter->GetMotion(); 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() { void InWorld::RequestDisconnect() {
@@ -303,7 +309,9 @@ void InWorld::RequestDisconnect() {
NetworkPacket packet; NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::DISCONNECT; packet.meta.type = NetworkPacket::Type::DISCONNECT;
packet.clientInfo.index = clientIndex; 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() { void InWorld::RequestShutDown() {
@@ -311,5 +319,7 @@ void InWorld::RequestShutDown() {
NetworkPacket packet; NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::SHUTDOWN; packet.meta.type = NetworkPacket::Type::SHUTDOWN;
packet.clientInfo.index = clientIndex; 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));
} }
+1
View File
@@ -27,6 +27,7 @@
#include "config_utility.hpp" #include "config_utility.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "network_packet.hpp" #include "network_packet.hpp"
#include "serial.hpp"
#include "image.hpp" #include "image.hpp"
#include "raster_font.hpp" #include "raster_font.hpp"
#include "button.hpp" #include "button.hpp"
+7 -3
View File
@@ -80,7 +80,7 @@ void LobbyMenu::Update(double delta) {
//suck in all waiting packets //suck in all waiting packets
NetworkPacket packet; NetworkPacket packet;
while(network.Receive()) { while(network.Receive()) {
memcpy(&packet, network.GetInData(), sizeof(NetworkPacket)); deserialize(&packet, network.GetInData());
packet.meta.srcAddress = network.GetInPacket()->address; packet.meta.srcAddress = network.GetInPacket()->address;
HandlePacket(packet); HandlePacket(packet);
} }
@@ -128,7 +128,9 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
//broadcast to the network, or a specific server //broadcast to the network, or a specific server
NetworkPacket packet; NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST; packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST;
network.Send(config["server.host"].c_str(), config.Int("server.port"), reinterpret_cast<void*>(&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 //reset the server list
serverInfo.clear(); serverInfo.clear();
@@ -139,7 +141,9 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
//join the selected server //join the selected server
NetworkPacket packet; NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::JOIN_REQUEST; 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; selection = nullptr;
} }
+1
View File
@@ -31,6 +31,7 @@
#include "config_utility.hpp" #include "config_utility.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "network_packet.hpp" #include "network_packet.hpp"
#include "serial.hpp"
#include <vector> #include <vector>
+32
View File
@@ -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 <cstring>
void serialize(NetworkPacket* const packet, void* buffer) {
memcpy(buffer, packet, sizeof(NetworkPacket));
}
void deserialize(NetworkPacket* const packet, void* buffer) {
memcpy(packet, buffer, sizeof(NetworkPacket));
}
+30
View File
@@ -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
+16 -6
View File
@@ -89,7 +89,7 @@ void ServerApplication::Loop() {
//suck in the waiting packets & process them //suck in the waiting packets & process them
try { try {
while(network.Receive()) { while(network.Receive()) {
memcpy(&packet, network.GetInData(), sizeof(NetworkPacket)); deserialize(&packet, network.GetInData());
packet.meta.srcAddress = network.GetInPacket()->address; packet.meta.srcAddress = network.GetInPacket()->address;
HandlePacket(packet); HandlePacket(packet);
} }
@@ -158,7 +158,9 @@ void ServerApplication::HandleBroadcastRequest(NetworkPacket packet) {
//send back the server's name //send back the server's name
packet.meta.type = NetworkPacket::Type::BROADCAST_RESPONSE; packet.meta.type = NetworkPacket::Type::BROADCAST_RESPONSE;
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str()); 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) { void ServerApplication::HandleJoinRequest(NetworkPacket packet) {
@@ -174,7 +176,9 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) {
//send the client their info //send the client their info
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE; packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = clientCounter; 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 //finished this routine
clientCounter++; clientCounter++;
@@ -183,7 +187,9 @@ void ServerApplication::HandleJoinRequest(NetworkPacket packet) {
void ServerApplication::HandleDisconnect(NetworkPacket packet) { void ServerApplication::HandleDisconnect(NetworkPacket packet) {
//disconnect the specified client //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); clientMap.erase(packet.clientInfo.index);
//delete players //delete players
@@ -209,6 +215,7 @@ void ServerApplication::HandleDisconnect(NetworkPacket packet) {
void ServerApplication::HandleSynchronize(NetworkPacket packet) { void ServerApplication::HandleSynchronize(NetworkPacket packet) {
//send all the server's data to this client //send all the server's data to this client
NetworkPacket newPacket; NetworkPacket newPacket;
char buffer[sizeof(NetworkPacket)];
//players //players
newPacket.meta.type = NetworkPacket::Type::PLAYER_UPDATE; 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()); snprintf(newPacket.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
newPacket.playerInfo.position = it.second.position; newPacket.playerInfo.position = it.second.position;
newPacket.playerInfo.motion = it.second.motion; 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) { void ServerApplication::PumpPacket(NetworkPacket packet) {
//send this packet to all clients //send this packet to all clients
char buffer[sizeof(NetworkPacket)];
serialize(&packet, buffer);
for (auto& it : clientMap) { for (auto& it : clientMap) {
network.Send(&it.second.address, &packet, sizeof(NetworkPacket)); network.Send(&it.second.address, buffer, sizeof(NetworkPacket));
} }
} }
+1
View File
@@ -25,6 +25,7 @@
//networking //networking
#include "network_packet.hpp" #include "network_packet.hpp"
#include "udp_network_utility.hpp" #include "udp_network_utility.hpp"
#include "serial.hpp"
//APIs //APIs
#include "sqlite3/sqlite3.h" #include "sqlite3/sqlite3.h"