Added dummy serialization functions
This commit is contained in:
@@ -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));
|
||||
}
|
||||
@@ -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"
|
||||
|
||||
@@ -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<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
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "config_utility.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "network_packet.hpp"
|
||||
#include "serial.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
//networking
|
||||
#include "network_packet.hpp"
|
||||
#include "udp_network_utility.hpp"
|
||||
#include "serial.hpp"
|
||||
|
||||
//APIs
|
||||
#include "sqlite3/sqlite3.h"
|
||||
|
||||
Reference in New Issue
Block a user