From da60fa8f944d88fb44db347459a0f0d07af4476e Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Jun 2014 20:46:05 +1000 Subject: [PATCH 1/5] Began reworking the network code This commit devides SerialPacket into a series of different structures, all decended from a common base class. Using a union was not a good idea. --- common/gameplay/combat_data.hpp | 11 +- common/gameplay/room_data.hpp | 6 + common/gameplay/sanity_check.cpp | 3 - common/network/packet/character_packet.hpp | 53 ++++++ common/network/packet/client_packet.hpp | 34 ++++ common/network/packet/combat_packet.hpp | 46 +++++ common/network/packet/enemy_packet.hpp | 39 ++++ common/network/packet/makefile | 37 ++++ common/network/packet/region_packet.hpp | 38 ++++ common/network/packet/sanity_check.cpp | 28 +++ common/network/packet/serial_packet.hpp | 34 ++++ common/network/packet/serial_packet_base.hpp | 46 +++++ common/network/packet/serial_packet_type.hpp | 98 ++++++++++ common/network/packet/server_packet.hpp | 34 ++++ common/network/serial.cpp | 8 +- common/network/serial_packet.hpp | 188 ------------------- todo.txt | 3 + 17 files changed, 505 insertions(+), 201 deletions(-) create mode 100644 common/network/packet/character_packet.hpp create mode 100644 common/network/packet/client_packet.hpp create mode 100644 common/network/packet/combat_packet.hpp create mode 100644 common/network/packet/enemy_packet.hpp create mode 100644 common/network/packet/makefile create mode 100644 common/network/packet/region_packet.hpp create mode 100644 common/network/packet/sanity_check.cpp create mode 100644 common/network/packet/serial_packet.hpp create mode 100644 common/network/packet/serial_packet_base.hpp create mode 100644 common/network/packet/serial_packet_type.hpp create mode 100644 common/network/packet/server_packet.hpp delete mode 100644 common/network/serial_packet.hpp diff --git a/common/gameplay/combat_data.hpp b/common/gameplay/combat_data.hpp index db87851..3c45b78 100644 --- a/common/gameplay/combat_data.hpp +++ b/common/gameplay/combat_data.hpp @@ -35,11 +35,11 @@ //std namespace #include -#include +#include #include -#define COMBAT_MAX_CHARACTER_COUNT 12 -#define COMBAT_MAX_ENEMY_COUNT 12 +#define COMBAT_MAX_CHARACTERS 12 +#define COMBAT_MAX_ENEMIES 12 struct CombatData { enum class Terrain { @@ -50,9 +50,8 @@ struct CombatData { typedef std::chrono::steady_clock Clock; - //combatants, point to the std::map's internal pairs - std::list*> characterList; - std::list*> enemyList; + std::array characterArray; + std::array enemyArray; //world interaction int mapIndex = 0; diff --git a/common/gameplay/room_data.hpp b/common/gameplay/room_data.hpp index ae49465..6826314 100644 --- a/common/gameplay/room_data.hpp +++ b/common/gameplay/room_data.hpp @@ -30,6 +30,12 @@ struct RoomData { FORESTS, CAVES, }; + + /* TODO: more + * "multiple rooms system" using this structure + * Pager + * collision map + */ }; #endif diff --git a/common/gameplay/sanity_check.cpp b/common/gameplay/sanity_check.cpp index 1e20ccf..719db9f 100644 --- a/common/gameplay/sanity_check.cpp +++ b/common/gameplay/sanity_check.cpp @@ -31,7 +31,4 @@ * Since most/all of the files in this directory are header files, I've created * this source file as a "sanity check", to ensure that the above header files * are written correctly via make. - * - * Oddly enough, I'm pretty sure this is the first directory compiled in a - * clean build. */ \ No newline at end of file diff --git a/common/network/packet/character_packet.hpp b/common/network/packet/character_packet.hpp new file mode 100644 index 0000000..3b44d9b --- /dev/null +++ b/common/network/packet/character_packet.hpp @@ -0,0 +1,53 @@ +/* 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 + * 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 CHARACTERPACKET_HPP_ +#define CHARACTERPACKET_HPP_ + +#include "serial_packet_base.hpp" + +#include "vector2.hpp" +#include "statistics.hpp" + +struct CharacterPacket : SerialPacketBase { + //identify the character + int characterIndex; + char handle[PACKET_STRING_SIZE]; + char avatar[PACKET_STRING_SIZE]; + + //the owner + int accountIndex; + + //location + int roomIndex; + Vector2 origin; + Vector2 motion; + + //gameplay + Statistics stats; + + //TODO: equipment + //TODO: items + //TODO: buffs + //TODO: debuffs +}; + +#endif \ No newline at end of file diff --git a/common/network/packet/client_packet.hpp b/common/network/packet/client_packet.hpp new file mode 100644 index 0000000..995bc4a --- /dev/null +++ b/common/network/packet/client_packet.hpp @@ -0,0 +1,34 @@ +/* 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 + * 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 CLIENTPACKET_HPP_ +#define CLIENTPACKET_HPP_ + +#include "serial_packet_base.hpp" + +struct ClientPacket : SerialPacketBase { + int clientIndex; + int accountIndex; + char username[PACKET_STRING_SIZE]; + char password[PACKET_STRING_SIZE]; //hashed, not currently used +}; + +#endif \ No newline at end of file diff --git a/common/network/packet/combat_packet.hpp b/common/network/packet/combat_packet.hpp new file mode 100644 index 0000000..a95ca25 --- /dev/null +++ b/common/network/packet/combat_packet.hpp @@ -0,0 +1,46 @@ +/* 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 + * 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 COMBATPACKET_HPP_ +#define COMBATPACKET_HPP_ + +#include "serial_packet_base.hpp" + +#include "combat_data.hpp" + +struct CombatPacket : SerialPacketBase { + //identify the combat instance + int combatIndex; + int difficulty; + CombatData::Terrain terrainType; + + //combatants + int characterArray[COMBAT_MAX_CHARACTERS]; + int enemyArray[COMBAT_MAX_ENEMIES]; + + //location + int mapIndex; + Vector2 origin; + + //TODO: rewards +}; + +#endif \ No newline at end of file diff --git a/common/network/packet/enemy_packet.hpp b/common/network/packet/enemy_packet.hpp new file mode 100644 index 0000000..bbe68c9 --- /dev/null +++ b/common/network/packet/enemy_packet.hpp @@ -0,0 +1,39 @@ +/* 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 + * 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 ENEMYPACKET_HPP_ +#define ENEMYPACKET_HPP_ + +#include "serial_packet_base.hpp" + +struct EnemyPacket : SerialPacketBase { + //identify the enemy + int enemyIndex; + char handle[PACKET_STRING_SIZE]; + char avatar[PACKET_STRING_SIZE]; + + //gameplay + Statistics stats; + + //TODO: rewards +}; + +#endif \ No newline at end of file diff --git a/common/network/packet/makefile b/common/network/packet/makefile new file mode 100644 index 0000000..f993f8b --- /dev/null +++ b/common/network/packet/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../../gameplay ../../map ../../utilities +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=../../.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/common/network/packet/region_packet.hpp b/common/network/packet/region_packet.hpp new file mode 100644 index 0000000..8a9085e --- /dev/null +++ b/common/network/packet/region_packet.hpp @@ -0,0 +1,38 @@ +/* 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 + * 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 REGIONPACKET_HPP_ +#define REGIONPACKET_HPP_ + +#include "serial_packet_base.hpp" + +#include "region.hpp" + +struct RegionPacket : SerialPacketBase { + //location/identify the region + int roomIndex; + int x, y; + + //the data + Region* region; +}; + +#endif \ No newline at end of file diff --git a/common/network/packet/sanity_check.cpp b/common/network/packet/sanity_check.cpp new file mode 100644 index 0000000..f250d69 --- /dev/null +++ b/common/network/packet/sanity_check.cpp @@ -0,0 +1,28 @@ +/* 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 + * 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_packet.hpp" + +/* DOCS: Sanity check, read more + * Since most/all of the files in this directory are header files, I've created + * this source file as a "sanity check", to ensure that the above header files + * are written correctly via make. +*/ diff --git a/common/network/packet/serial_packet.hpp b/common/network/packet/serial_packet.hpp new file mode 100644 index 0000000..7b7137f --- /dev/null +++ b/common/network/packet/serial_packet.hpp @@ -0,0 +1,34 @@ +/* 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 + * 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 SERIALPACKET_HPP_ +#define SERIALPACKET_HPP_ + +#include "character_packet.hpp" +#include "client_packet.hpp" +#include "combat_packet.hpp" +#include "enemy_packet.hpp" +#include "region_packet.hpp" +#include "server_packet.hpp" + +//NOTE: SerialPacket is defined in serial_packet_base.hpp + +#endif diff --git a/common/network/packet/serial_packet_base.hpp b/common/network/packet/serial_packet_base.hpp new file mode 100644 index 0000000..742c4db --- /dev/null +++ b/common/network/packet/serial_packet_base.hpp @@ -0,0 +1,46 @@ +/* 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 + * 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 SERIALPACKETBASE_HPP_ +#define SERIALPACKETBASE_HPP_ + +#ifndef SERIALPACKET_HPP_ + #error Cannot include this file without 'serial_packet.hpp' +#endif + +#include "serial_packet_type.hpp" + +#include "SDL/SDL_net.h" + +#define PACKET_STRING_SIZE 100 + +struct SerialPacketBase { + //members + SerialPacketType type; + IPaddress srcAddress; + + typedef SerialPacketType Type; +}; + +typedef SerialPacketBase* SerialPacket; +typedef SerialPacketType PacketType; + +#endif diff --git a/common/network/packet/serial_packet_type.hpp b/common/network/packet/serial_packet_type.hpp new file mode 100644 index 0000000..8fbcd17 --- /dev/null +++ b/common/network/packet/serial_packet_type.hpp @@ -0,0 +1,98 @@ +/* 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 + * 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 SERIALPACKETTYPE_HPP_ +#define SERIALPACKETTYPE_HPP_ + +enum class SerialPacketType { + //default: there is something wrong + NONE = 0, + + //keep alive + PING, + PONG, + + //searching for a server to join + BROADCAST_REQUEST, + BROADCAST_RESPONSE, + BROADCAST_REJECTION, + + //try to join the server + JOIN_REQUEST, + JOIN_RESPONSE, + JOIN_REJECTION, + + //mass update + SYNCHRONIZE, + + //disconnect from the server + DISCONNECT, + + //shut down the server + SHUTDOWN, + + //map data + REGION_REQUEST, + REGION_CONTENT, + REGION_REJECTION, + + //combat data + COMBAT_NEW, + COMBAT_DELETE, + COMBAT_UPDATE, + + COMBAT_ENTER_REQUEST, + COMBAT_ENTER_RESPONSE, + + COMBAT_EXIT_REQUEST, + COMBAT_EXIT_RESPONSE, + + //TODO: COMBAT info + + COMBAT_REJECTION, + + //character data + CHARACTER_NEW, + CHARACTER_DELETE, + CHARACTER_UPDATE, + + CHARACTER_STATS_REQUEST, + CHARACTER_STATS_RESPONSE, + + CHARACTER_REJECTION, + + //enemy data + ENEMY_NEW, + ENEMY_DELETE, + ENEMY_UPDATE, + + ENEMY_STATS_REQUEST, + ENEMY_STATS_RESPONSE, + + ENEMY_REJECTION, + + //more packet types go here + + //not used + LAST, +}; + +#endif \ No newline at end of file diff --git a/common/network/packet/server_packet.hpp b/common/network/packet/server_packet.hpp new file mode 100644 index 0000000..fcd4a49 --- /dev/null +++ b/common/network/packet/server_packet.hpp @@ -0,0 +1,34 @@ +/* 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 + * 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 SERVERPACKET_HPP_ +#define SERVERPACKET_HPP_ + +#include "serial_packet_base.hpp" + +struct ServerPacket : SerialPacketBase { + //identify the server + char name[PACKET_STRING_SIZE]; + int playerCount; + int version; +}; + +#endif \ No newline at end of file diff --git a/common/network/serial.cpp b/common/network/serial.cpp index 961afd4..6e75c5a 100644 --- a/common/network/serial.cpp +++ b/common/network/serial.cpp @@ -103,8 +103,8 @@ void serializeCombat(SerialPacket* packet, char* buffer) { SERIALIZE(buffer, &packet->combatInfo.terrainType, sizeof(CombatData::Terrain)); //arrays - SERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTER_COUNT); - SERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMY_COUNT); + SERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTERS); + SERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMIES); //position SERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); @@ -247,8 +247,8 @@ void deserializeCombat(SerialPacket* packet, char* buffer) { DESERIALIZE(buffer, &packet->combatInfo.terrainType, sizeof(CombatData::Terrain)); //arrays - DESERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTER_COUNT); - DESERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMY_COUNT); + DESERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTERS); + DESERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMIES); //position DESERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp deleted file mode 100644 index d378fde..0000000 --- a/common/network/serial_packet.hpp +++ /dev/null @@ -1,188 +0,0 @@ -/* 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 - * 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 SERIALPACKET_HPP_ -#define SERIALPACKET_HPP_ - -#include "vector2.hpp" -#include "region.hpp" -#include "statistics.hpp" -#include "combat_data.hpp" - -#include "SDL/SDL_net.h" - -#define NETWORK_VERSION 20140601 -#define PACKET_STRING_SIZE 100 - -//TODO: would it be possible to serialize structures directly? -union SerialPacket { - //types of packets - enum class Type { - //default: there is something wrong - NONE = 0, - - //keep alive - PING, - PONG, - - //searching for a server to join - BROADCAST_REQUEST, - BROADCAST_RESPONSE, - BROADCAST_REJECTION, - - //try to join the server - JOIN_REQUEST, - JOIN_RESPONSE, - JOIN_REJECTION, - - //mass update - SYNCHRONIZE, - - //disconnect from the server - DISCONNECT, - - //shut down the server - SHUTDOWN, - - //map data - REGION_REQUEST, - REGION_CONTENT, - REGION_REJECTION, - - //combat data - COMBAT_NEW, - COMBAT_DELETE, - COMBAT_UPDATE, - - COMBAT_ENTER_REQUEST, - COMBAT_ENTER_RESPONSE, - - COMBAT_EXIT_REQUEST, - COMBAT_EXIT_RESPONSE, - - //TODO: COMBAT info - - COMBAT_REJECTION, - - //character data - CHARACTER_NEW, - CHARACTER_DELETE, - CHARACTER_UPDATE, - - CHARACTER_STATS_REQUEST, - CHARACTER_STATS_RESPONSE, - - CHARACTER_REJECTION, - - //enemy data - ENEMY_NEW, - ENEMY_DELETE, - ENEMY_UPDATE, - - ENEMY_STATS_REQUEST, - ENEMY_STATS_RESPONSE, - - ENEMY_REJECTION, - - //more packet types go here - - //not used - LAST, - }; - - //metadata on the packet itself - struct Metadata { - Type type; - IPaddress srcAddress; - }meta; - - //info about the server - struct ServerInformation { - Metadata meta; - int networkVersion; - char name[PACKET_STRING_SIZE]; - int playerCount; - }serverInfo; - - //info about the client - struct ClientInformation { - Metadata meta; - int clientIndex; - int accountIndex; - int characterIndex; - char username[PACKET_STRING_SIZE]; - //TODO: password - char handle[PACKET_STRING_SIZE]; - char avatar[PACKET_STRING_SIZE]; - }clientInfo; - - //info about a region - struct RegionInformation { - Metadata meta; - int mapIndex; - int x, y; - Region* region; - }regionInfo; - - //info about a combat scenario - struct CombatInformation { - Metadata meta; - int combatIndex; - int difficulty; - CombatData::Terrain terrainType; - int characterArray[COMBAT_MAX_CHARACTER_COUNT]; - int enemyArray[COMBAT_MAX_ENEMY_COUNT]; - int mapIndex; - Vector2 origin; - //TODO: rewards - }combatInfo; - - //info about a character - struct CharacterInformation { - Metadata meta; - int clientIndex; - int accountIndex; - int characterIndex; - char handle[PACKET_STRING_SIZE]; - char avatar[PACKET_STRING_SIZE]; - int mapIndex; - Vector2 origin; - Vector2 motion; - Statistics stats; - }characterInfo; - - //info about an enemy - struct EnemyInformation { - Metadata meta; - char handle[PACKET_STRING_SIZE]; - char avatar[PACKET_STRING_SIZE]; - Statistics stats; - //TODO: rewards - }enemyInfo; - - //defaults - SerialPacket() { - meta.type = Type::NONE; - meta.srcAddress = {0,0}; - } -}; - -#endif diff --git a/todo.txt b/todo.txt index f03386a..fa7d60d 100644 --- a/todo.txt +++ b/todo.txt @@ -1,3 +1,6 @@ +TODO: Modulate this god class +TODO: Segment SerialPacket? +TODO: Not all structures in common/gameplay are needed by the client TODO: I need to keep the documentation up to date. Namely, the GDD is getting out of date. TODO: I completely forgot about status ailments TODO: Time delay for requesting region packets From 23364b28109d33ee995e1592bd277ea84901a25d Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Jun 2014 23:03:24 +1000 Subject: [PATCH 2/5] Split up the serial code, but had to gut it in the process Since the contents of the packets are different than before, I decided to gut the serialization code. I'll reimplement the internals soon. --- common/network/packet/serial_packet_base.hpp | 5 +- common/network/serial.cpp | 473 ------------------ common/network/serial/makefile | 37 ++ common/network/serial/serial.cpp | 188 +++++++ common/network/serial/serial.hpp | 65 +++ common/network/serial/serial_character.cpp | 44 ++ common/network/serial/serial_client.cpp | 36 ++ common/network/serial/serial_combat.cpp | 36 ++ common/network/serial/serial_enemy.cpp | 44 ++ common/network/serial/serial_region.cpp | 89 ++++ common/network/serial/serial_server.cpp | 36 ++ common/network/serial/serial_statistics.cpp | 65 +++ .../{serial.hpp => serial/serial_util.hpp} | 19 +- 13 files changed, 649 insertions(+), 488 deletions(-) delete mode 100644 common/network/serial.cpp create mode 100644 common/network/serial/makefile create mode 100644 common/network/serial/serial.cpp create mode 100644 common/network/serial/serial.hpp create mode 100644 common/network/serial/serial_character.cpp create mode 100644 common/network/serial/serial_client.cpp create mode 100644 common/network/serial/serial_combat.cpp create mode 100644 common/network/serial/serial_enemy.cpp create mode 100644 common/network/serial/serial_region.cpp create mode 100644 common/network/serial/serial_server.cpp create mode 100644 common/network/serial/serial_statistics.cpp rename common/network/{serial.hpp => serial/serial_util.hpp} (60%) diff --git a/common/network/packet/serial_packet_base.hpp b/common/network/packet/serial_packet_base.hpp index 742c4db..16279c8 100644 --- a/common/network/packet/serial_packet_base.hpp +++ b/common/network/packet/serial_packet_base.hpp @@ -38,9 +38,10 @@ struct SerialPacketBase { IPaddress srcAddress; typedef SerialPacketType Type; + + virtual ~SerialPacketBase(); }; -typedef SerialPacketBase* SerialPacket; -typedef SerialPacketType PacketType; +typedef SerialPacketBase SerialPacket; #endif diff --git a/common/network/serial.cpp b/common/network/serial.cpp deleted file mode 100644 index 6e75c5a..0000000 --- a/common/network/serial.cpp +++ /dev/null @@ -1,473 +0,0 @@ -/* 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 "map_allocator.hpp" -#include "statistics.hpp" - -#include - -//------------------------- -//Convenience Macros -//------------------------- - -#define SERIALIZE(buffer, data, size) memcpy(buffer, data, size); buffer += size; -#define DESERIALIZE(buffer, data, size) memcpy(data, buffer, size); buffer += size; - -//------------------------- -//internal serialization functions -//------------------------- - -void serializeType(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); -} - -void serializeServer(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //server info - SERIALIZE(buffer, &packet->serverInfo.networkVersion, sizeof(int)); - SERIALIZE(buffer, packet->serverInfo.name, PACKET_STRING_SIZE); - SERIALIZE(buffer, &packet->serverInfo.playerCount, sizeof(int)); -} - -void serializeClient(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //indexes - SERIALIZE(buffer, &packet->clientInfo.clientIndex, sizeof(int)); - SERIALIZE(buffer, &packet->clientInfo.accountIndex, sizeof(int)); - SERIALIZE(buffer, &packet->clientInfo.characterIndex, sizeof(int)); - - //texts - SERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE); - //TODO: password - SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); -} - -void serializeRegionFormat(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //format - SERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int)); - SERIALIZE(buffer, &packet->regionInfo.x, sizeof(int)); - SERIALIZE(buffer, &packet->regionInfo.y, sizeof(int)); -} - -void serializeRegionContent(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //format - SERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int)); - SERIALIZE(buffer, &packet->regionInfo.x, sizeof(int)); - SERIALIZE(buffer, &packet->regionInfo.y, sizeof(int)); - - //content - for (register int i = 0; i < REGION_WIDTH; i++) { - for (register int j = 0; j < REGION_HEIGHT; j++) { - for (register int k = 0; k < REGION_DEPTH; k++) { - *reinterpret_cast(buffer) = packet->regionInfo.region->GetTile(i, j, k); - buffer += sizeof(Region::type_t); - } - } - } -} - -void serializeCombat(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //integers - SERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int)); - SERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int)); - - SERIALIZE(buffer, &packet->combatInfo.terrainType, sizeof(CombatData::Terrain)); - - //arrays - SERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTERS); - SERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMIES); - - //position - SERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); - SERIALIZE(buffer, &packet->combatInfo.origin.x, sizeof(double)); - SERIALIZE(buffer, &packet->combatInfo.origin.y, sizeof(double)); - - //TODO: rewards -} - -void serializeStatistics(Statistics* stats, char* buffer) { - //integers - SERIALIZE(buffer, &stats->level, sizeof(int)); - SERIALIZE(buffer, &stats->exp, sizeof(int)); - SERIALIZE(buffer, &stats->maxHP, sizeof(int)); - SERIALIZE(buffer, &stats->health, sizeof(int)); - SERIALIZE(buffer, &stats->maxMP, sizeof(int)); - SERIALIZE(buffer, &stats->mana, sizeof(int)); - SERIALIZE(buffer, &stats->attack, sizeof(int)); - SERIALIZE(buffer, &stats->defence, sizeof(int)); - SERIALIZE(buffer, &stats->intelligence, sizeof(int)); - SERIALIZE(buffer, &stats->resistance, sizeof(int)); - SERIALIZE(buffer, &stats->speed, sizeof(int)); - - //floats - SERIALIZE(buffer, &stats->accuracy, sizeof(float)); - SERIALIZE(buffer, &stats->evasion, sizeof(float)); - SERIALIZE(buffer, &stats->luck, sizeof(float)); -} - -void serializeCharacter(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //indexes - SERIALIZE(buffer, &packet->characterInfo.clientIndex, sizeof(int)); - SERIALIZE(buffer, &packet->characterInfo.accountIndex, sizeof(int)); - SERIALIZE(buffer, &packet->characterInfo.characterIndex, sizeof(int)); - - //texts - SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); - - //vectors - SERIALIZE(buffer, &packet->characterInfo.origin.x, sizeof(double)); - SERIALIZE(buffer, &packet->characterInfo.origin.y, sizeof(double)); - SERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); - SERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); - - //stats structure - serializeStatistics(&packet->characterInfo.stats, buffer); - buffer += sizeof(Statistics); -} - -void serializeEnemy(SerialPacket* packet, char* buffer) { - SERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //texts - SERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); - - //stats structure - serializeStatistics(&packet->characterInfo.stats, buffer); - buffer += sizeof(Statistics); -} - -//------------------------- -//internal deserialization functions -//------------------------- - -void deserializeType(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); -} - -void deserializeServer(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //server info - DESERIALIZE(buffer, &packet->serverInfo.networkVersion, sizeof(int)); - DESERIALIZE(buffer, packet->serverInfo.name, PACKET_STRING_SIZE); - DESERIALIZE(buffer, &packet->serverInfo.playerCount, sizeof(int)); -} - -void deserializeClient(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //indexes - DESERIALIZE(buffer, &packet->clientInfo.clientIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->clientInfo.accountIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->clientInfo.characterIndex, sizeof(int)); - - //texts - DESERIALIZE(buffer, packet->clientInfo.username, PACKET_STRING_SIZE); - //TODO: password - DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); -} - -void deserializeRegionFormat(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //format - DESERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->regionInfo.x, sizeof(int)); - DESERIALIZE(buffer, &packet->regionInfo.y, sizeof(int)); -} - -void deserializeRegionContent(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //format - DESERIALIZE(buffer, &packet->regionInfo.mapIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->regionInfo.x, sizeof(int)); - DESERIALIZE(buffer, &packet->regionInfo.y, sizeof(int)); - - //an object to work on - BlankAllocator().Create( - &packet->regionInfo.region, - packet->regionInfo.x, - packet->regionInfo.y - ); - - //content - for (register int i = 0; i < REGION_WIDTH; i++) { - for (register int j = 0; j < REGION_HEIGHT; j++) { - for (register int k = 0; k < REGION_DEPTH; k++) { - packet->regionInfo.region->SetTile(i, j, k, *reinterpret_cast(buffer)); - buffer += sizeof(Region::type_t); - } - } - } -} - - -void deserializeCombat(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //integers - DESERIALIZE(buffer, &packet->combatInfo.combatIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->combatInfo.difficulty, sizeof(int)); - - DESERIALIZE(buffer, &packet->combatInfo.terrainType, sizeof(CombatData::Terrain)); - - //arrays - DESERIALIZE(buffer, &packet->combatInfo.characterArray, COMBAT_MAX_CHARACTERS); - DESERIALIZE(buffer, &packet->combatInfo.enemyArray, COMBAT_MAX_ENEMIES); - - //position - DESERIALIZE(buffer, &packet->combatInfo.mapIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->combatInfo.origin.x, sizeof(double)); - DESERIALIZE(buffer, &packet->combatInfo.origin.y, sizeof(double)); - - //TODO: rewards -} - - -void deserializeStatistics(Statistics* stats, char* buffer) { - //integers - DESERIALIZE(buffer, &stats->level, sizeof(int)); - DESERIALIZE(buffer, &stats->exp, sizeof(int)); - DESERIALIZE(buffer, &stats->maxHP, sizeof(int)); - DESERIALIZE(buffer, &stats->health, sizeof(int)); - DESERIALIZE(buffer, &stats->maxMP, sizeof(int)); - DESERIALIZE(buffer, &stats->mana, sizeof(int)); - DESERIALIZE(buffer, &stats->attack, sizeof(int)); - DESERIALIZE(buffer, &stats->defence, sizeof(int)); - DESERIALIZE(buffer, &stats->intelligence, sizeof(int)); - DESERIALIZE(buffer, &stats->resistance, sizeof(int)); - DESERIALIZE(buffer, &stats->speed, sizeof(int)); - - //floats - DESERIALIZE(buffer, &stats->accuracy, sizeof(float)); - DESERIALIZE(buffer, &stats->evasion, sizeof(float)); - DESERIALIZE(buffer, &stats->luck, sizeof(float)); -} - -void deserializeCharacter(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //indexes - DESERIALIZE(buffer, &packet->characterInfo.clientIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->characterInfo.accountIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->characterInfo.characterIndex, sizeof(int)); - - //texts - DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); - - //vectors - DESERIALIZE(buffer, &packet->characterInfo.origin.x, sizeof(double)); - DESERIALIZE(buffer, &packet->characterInfo.origin.y, sizeof(double)); - DESERIALIZE(buffer, &packet->characterInfo.motion.x, sizeof(double)); - DESERIALIZE(buffer, &packet->characterInfo.motion.y, sizeof(double)); - - //stats structure - deserializeStatistics(&packet->characterInfo.stats, buffer); - buffer += sizeof(Statistics); -} - -void deserializeEnemy(SerialPacket* packet, char* buffer) { - DESERIALIZE(buffer, &packet->meta.type, sizeof(SerialPacket::Type)); - - //texts - DESERIALIZE(buffer, packet->clientInfo.handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, packet->clientInfo.avatar, PACKET_STRING_SIZE); - - //stats structure - deserializeStatistics(&packet->characterInfo.stats, buffer); - buffer += sizeof(Statistics); -} - -//------------------------- -//the interface functions -//------------------------- - -void serialize(SerialPacket* packet, void* buffer) { - switch(packet->meta.type) { - //no extra data - case SerialPacket::Type::NONE: - case SerialPacket::Type::PING: - case SerialPacket::Type::PONG: - case SerialPacket::Type::BROADCAST_REQUEST: - - //all rejections - case SerialPacket::Type::BROADCAST_REJECTION: - case SerialPacket::Type::JOIN_REJECTION: - case SerialPacket::Type::REGION_REJECTION: - case SerialPacket::Type::CHARACTER_REJECTION: - case SerialPacket::Type::ENEMY_REJECTION: - case SerialPacket::Type::COMBAT_REJECTION: - - serializeType(packet, reinterpret_cast(buffer)); - break; - - //server info - case SerialPacket::Type::BROADCAST_RESPONSE: - serializeServer(packet, reinterpret_cast(buffer)); - break; - - //client info - case SerialPacket::Type::JOIN_REQUEST: - case SerialPacket::Type::JOIN_RESPONSE: - case SerialPacket::Type::SYNCHRONIZE: - case SerialPacket::Type::DISCONNECT: - case SerialPacket::Type::SHUTDOWN: - serializeClient(packet, reinterpret_cast(buffer)); - break; - - //region info - case SerialPacket::Type::REGION_REQUEST: - serializeRegionFormat(packet, reinterpret_cast(buffer)); - break; - - case SerialPacket::Type::REGION_CONTENT: - serializeRegionContent(packet, reinterpret_cast(buffer)); - break; - - //combat info - case SerialPacket::Type::COMBAT_NEW: - case SerialPacket::Type::COMBAT_DELETE: - case SerialPacket::Type::COMBAT_UPDATE: - - //TODO: is this the best fit? - case SerialPacket::Type::COMBAT_ENTER_REQUEST: - case SerialPacket::Type::COMBAT_ENTER_RESPONSE: - case SerialPacket::Type::COMBAT_EXIT_REQUEST: - case SerialPacket::Type::COMBAT_EXIT_RESPONSE: - - serializeCombat(packet, reinterpret_cast(buffer)); - break; - - //character info - case SerialPacket::Type::CHARACTER_NEW: - case SerialPacket::Type::CHARACTER_DELETE: - case SerialPacket::Type::CHARACTER_UPDATE: - case SerialPacket::Type::CHARACTER_STATS_REQUEST: - case SerialPacket::Type::CHARACTER_STATS_RESPONSE: - serializeCharacter(packet, reinterpret_cast(buffer)); - break; - - //enemy info - case SerialPacket::Type::ENEMY_NEW: - case SerialPacket::Type::ENEMY_DELETE: - case SerialPacket::Type::ENEMY_UPDATE: - case SerialPacket::Type::ENEMY_STATS_REQUEST: - case SerialPacket::Type::ENEMY_STATS_RESPONSE: - serializeEnemy(packet, reinterpret_cast(buffer)); - break; - } -} - -void deserialize(SerialPacket* packet, void* buffer) { - //find the type, so that you can actually deserialize the packet! - deserializeType(packet, reinterpret_cast(buffer)); - switch(packet->meta.type) { - //no extra data - case SerialPacket::Type::NONE: - case SerialPacket::Type::PING: - case SerialPacket::Type::PONG: - case SerialPacket::Type::BROADCAST_REQUEST: - - //all rejections - case SerialPacket::Type::BROADCAST_REJECTION: - case SerialPacket::Type::JOIN_REJECTION: - case SerialPacket::Type::REGION_REJECTION: - case SerialPacket::Type::CHARACTER_REJECTION: - case SerialPacket::Type::ENEMY_REJECTION: - case SerialPacket::Type::COMBAT_REJECTION: - - //NOTHING - break; - - //server info - case SerialPacket::Type::BROADCAST_RESPONSE: - deserializeServer(packet, reinterpret_cast(buffer)); - break; - - //client info - case SerialPacket::Type::JOIN_REQUEST: - case SerialPacket::Type::JOIN_RESPONSE: - case SerialPacket::Type::SYNCHRONIZE: - case SerialPacket::Type::DISCONNECT: - case SerialPacket::Type::SHUTDOWN: - deserializeClient(packet, reinterpret_cast(buffer)); - break; - - //region info - case SerialPacket::Type::REGION_REQUEST: - deserializeRegionFormat(packet, reinterpret_cast(buffer)); - break; - - case SerialPacket::Type::REGION_CONTENT: - deserializeRegionContent(packet, reinterpret_cast(buffer)); - break; - - //combat info - case SerialPacket::Type::COMBAT_NEW: - case SerialPacket::Type::COMBAT_DELETE: - case SerialPacket::Type::COMBAT_UPDATE: - - //TODO: is this the best fit? - case SerialPacket::Type::COMBAT_ENTER_REQUEST: - case SerialPacket::Type::COMBAT_ENTER_RESPONSE: - case SerialPacket::Type::COMBAT_EXIT_REQUEST: - case SerialPacket::Type::COMBAT_EXIT_RESPONSE: - - serializeCombat(packet, reinterpret_cast(buffer)); - break; - - //character info - case SerialPacket::Type::CHARACTER_NEW: - case SerialPacket::Type::CHARACTER_DELETE: - case SerialPacket::Type::CHARACTER_UPDATE: - case SerialPacket::Type::CHARACTER_STATS_REQUEST: - case SerialPacket::Type::CHARACTER_STATS_RESPONSE: - deserializeCharacter(packet, reinterpret_cast(buffer)); - break; - - //enemy info - case SerialPacket::Type::ENEMY_NEW: - case SerialPacket::Type::ENEMY_DELETE: - case SerialPacket::Type::ENEMY_UPDATE: - case SerialPacket::Type::ENEMY_STATS_REQUEST: - case SerialPacket::Type::ENEMY_STATS_RESPONSE: - serializeEnemy(packet, reinterpret_cast(buffer)); - break; - } -} \ No newline at end of file diff --git a/common/network/serial/makefile b/common/network/serial/makefile new file mode 100644 index 0000000..564b228 --- /dev/null +++ b/common/network/serial/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. ../packet ../../gameplay ../../map ../../utilities +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=../../.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/common/network/serial/serial.cpp b/common/network/serial/serial.cpp new file mode 100644 index 0000000..c73b2bc --- /dev/null +++ b/common/network/serial/serial.cpp @@ -0,0 +1,188 @@ +/* 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 "serial_util.hpp" + +//simple type functions +void serializeType(SerialPacketBase* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +} + +void deserializeType(SerialPacketBase* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +} + +//main switch functions +void serializePacket(SerialPacketBase* packet, void* buffer) { + switch(packet->type) { + //no extra data + case SerialPacketType::NONE: + case SerialPacketType::PING: + case SerialPacketType::PONG: + case SerialPacketType::BROADCAST_REQUEST: + + //all rejections + case SerialPacketType::BROADCAST_REJECTION: + case SerialPacketType::JOIN_REJECTION: + case SerialPacketType::REGION_REJECTION: + case SerialPacketType::CHARACTER_REJECTION: + case SerialPacketType::ENEMY_REJECTION: + case SerialPacketType::COMBAT_REJECTION: + + serializeType(packet, buffer); + break; + + //character info + case SerialPacketType::CHARACTER_NEW: + case SerialPacketType::CHARACTER_DELETE: + case SerialPacketType::CHARACTER_UPDATE: + case SerialPacketType::CHARACTER_STATS_REQUEST: + case SerialPacketType::CHARACTER_STATS_RESPONSE: + serializeCharacter(dynamic_cast(packet), buffer); + break; + + //client info + case SerialPacketType::JOIN_REQUEST: + case SerialPacketType::JOIN_RESPONSE: + case SerialPacketType::SYNCHRONIZE: + case SerialPacketType::DISCONNECT: + case SerialPacketType::SHUTDOWN: + serializeClient(dynamic_cast(packet), buffer); + break; + + //combat info + case SerialPacketType::COMBAT_NEW: + case SerialPacketType::COMBAT_DELETE: + case SerialPacketType::COMBAT_UPDATE: + + //TODO: is this the best fit? + case SerialPacketType::COMBAT_ENTER_REQUEST: + case SerialPacketType::COMBAT_ENTER_RESPONSE: + case SerialPacketType::COMBAT_EXIT_REQUEST: + case SerialPacketType::COMBAT_EXIT_RESPONSE: + + serializeCombat(dynamic_cast(packet), buffer); + break; + + //enemy info + case SerialPacketType::ENEMY_NEW: + case SerialPacketType::ENEMY_DELETE: + case SerialPacketType::ENEMY_UPDATE: + case SerialPacketType::ENEMY_STATS_REQUEST: + case SerialPacketType::ENEMY_STATS_RESPONSE: + serializeEnemy(dynamic_cast(packet), buffer); + break; + + //region info + case SerialPacketType::REGION_REQUEST: + serializeRegionFormat(dynamic_cast(packet), buffer); + break; + + case SerialPacketType::REGION_CONTENT: + serializeRegionContent(dynamic_cast(packet), buffer); + break; + + //server info + case SerialPacketType::BROADCAST_RESPONSE: + serializeServer(dynamic_cast(packet), buffer); + break; + } +} + +void deserializePacket(SerialPacketBase* packet, void* buffer) { + //find the type, so that you can actually deserialize the packet! + deserializeType(packet, buffer); + switch(packet->type) { + //no extra data + case SerialPacketType::NONE: + case SerialPacketType::PING: + case SerialPacketType::PONG: + case SerialPacketType::BROADCAST_REQUEST: + + //all rejections + case SerialPacketType::BROADCAST_REJECTION: + case SerialPacketType::JOIN_REJECTION: + case SerialPacketType::REGION_REJECTION: + case SerialPacketType::CHARACTER_REJECTION: + case SerialPacketType::ENEMY_REJECTION: + case SerialPacketType::COMBAT_REJECTION: + + //NOTHING + break; + + //character info + case SerialPacketType::CHARACTER_NEW: + case SerialPacketType::CHARACTER_DELETE: + case SerialPacketType::CHARACTER_UPDATE: + case SerialPacketType::CHARACTER_STATS_REQUEST: + case SerialPacketType::CHARACTER_STATS_RESPONSE: + deserializeCharacter(dynamic_cast(packet), buffer); + break; + + //client info + case SerialPacketType::JOIN_REQUEST: + case SerialPacketType::JOIN_RESPONSE: + case SerialPacketType::SYNCHRONIZE: + case SerialPacketType::DISCONNECT: + case SerialPacketType::SHUTDOWN: + deserializeClient(dynamic_cast(packet), buffer); + break; + + //combat info + case SerialPacketType::COMBAT_NEW: + case SerialPacketType::COMBAT_DELETE: + case SerialPacketType::COMBAT_UPDATE: + + //TODO: is this the best fit? + case SerialPacketType::COMBAT_ENTER_REQUEST: + case SerialPacketType::COMBAT_ENTER_RESPONSE: + case SerialPacketType::COMBAT_EXIT_REQUEST: + case SerialPacketType::COMBAT_EXIT_RESPONSE: + + serializeCombat(dynamic_cast(packet), buffer); + break; + + //enemy info + case SerialPacketType::ENEMY_NEW: + case SerialPacketType::ENEMY_DELETE: + case SerialPacketType::ENEMY_UPDATE: + case SerialPacketType::ENEMY_STATS_REQUEST: + case SerialPacketType::ENEMY_STATS_RESPONSE: + serializeEnemy(dynamic_cast(packet), buffer); + break; + + //region info + case SerialPacketType::REGION_REQUEST: + deserializeRegionFormat(dynamic_cast(packet), buffer); + break; + + case SerialPacketType::REGION_CONTENT: + deserializeRegionContent(dynamic_cast(packet), buffer); + break; + + //server info + case SerialPacketType::BROADCAST_RESPONSE: + deserializeServer(dynamic_cast(packet), buffer); + break; + } +} \ No newline at end of file diff --git a/common/network/serial/serial.hpp b/common/network/serial/serial.hpp new file mode 100644 index 0000000..02d5dba --- /dev/null +++ b/common/network/serial/serial.hpp @@ -0,0 +1,65 @@ +/* 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 SERIALIZE_HPP_ +#define SERIALIZE_HPP_ + +#include "serial_packet.hpp" + +#include "region.hpp" +#include "statistics.hpp" + +//Primary interface functions +void serializePacket(SerialPacketBase*, void* dest); +void deserializePacket(SerialPacketBase*, void* src); + +void serializeType(SerialPacketBase*, void*); +void deserializeType(SerialPacketBase*, void*); + +//utility functions, exposed +void serializeCharacter(CharacterPacket*, void*); +void serializeClient(ClientPacket*, void*); +void serializeCombat(CombatPacket*, void*); +void serializeEnemy(EnemyPacket*, void*); +void serializeRegionFormat(RegionPacket*, void*); +void serializeRegionContent(RegionPacket*, void*); +void serializeServer(ServerPacket*, void*); +void serializeStatistics(Statistics*, void*); + +void deserializeCharacter(CharacterPacket*, void*); +void deserializeClient(ClientPacket*, void*); +void deserializeCombat(CombatPacket*, void*); +void deserializeEnemy(EnemyPacket*, void*); +void deserializeRegionFormat(RegionPacket*, void*); +void deserializeRegionContent(RegionPacket*, void*); +void deserializeServer(ServerPacket*, void*); +void deserializeStatistics(Statistics*, void*); + +/* DOCS: Keep the PACKET_BUFFER_SIZE up to date + * DOCS: REGION_CONTENT is currently the largest type of packet, read more + * map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t) + * map format: sizeof(int) * 3 + * metadata: sizeof(SerialPacket::Type) +*/ + +constexpr int PACKET_BUFFER_SIZE = REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacketType); + +#endif \ No newline at end of file diff --git a/common/network/serial/serial_character.cpp b/common/network/serial/serial_character.cpp new file mode 100644 index 0000000..033944a --- /dev/null +++ b/common/network/serial/serial_character.cpp @@ -0,0 +1,44 @@ +/* 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 "serial_util.hpp" + +void serializeCharacter(CharacterPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO + + //stats structure + serializeStatistics(&packet->stats, buffer); + buffer = reinterpret_cast(buffer) + sizeof(Statistics); +} + +void deserializeCharacter(CharacterPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO + + //stats structure + deserializeStatistics(&packet->stats, buffer); + buffer = reinterpret_cast(buffer) + sizeof(Statistics); +} diff --git a/common/network/serial/serial_client.cpp b/common/network/serial/serial_client.cpp new file mode 100644 index 0000000..4a818cf --- /dev/null +++ b/common/network/serial/serial_client.cpp @@ -0,0 +1,36 @@ +/* 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 "serial_util.hpp" + +void serializeClient(ClientPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} + +void deserializeClient(ClientPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} diff --git a/common/network/serial/serial_combat.cpp b/common/network/serial/serial_combat.cpp new file mode 100644 index 0000000..6fbb053 --- /dev/null +++ b/common/network/serial/serial_combat.cpp @@ -0,0 +1,36 @@ +/* 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 "serial_util.hpp" + +void serializeCombat(CombatPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} + +void deserializeCombat(CombatPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} diff --git a/common/network/serial/serial_enemy.cpp b/common/network/serial/serial_enemy.cpp new file mode 100644 index 0000000..4059ea5 --- /dev/null +++ b/common/network/serial/serial_enemy.cpp @@ -0,0 +1,44 @@ +/* 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 "serial_util.hpp" + +void serializeEnemy(EnemyPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO + + //stats structure + serializeStatistics(&packet->stats, buffer); + buffer = reinterpret_cast(buffer) + sizeof(Statistics); +} + +void deserializeEnemy(EnemyPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO + + //stats structure + deserializeStatistics(&packet->stats, buffer); + buffer = reinterpret_cast(buffer) + sizeof(Statistics); +} diff --git a/common/network/serial/serial_region.cpp b/common/network/serial/serial_region.cpp new file mode 100644 index 0000000..e0860ac --- /dev/null +++ b/common/network/serial/serial_region.cpp @@ -0,0 +1,89 @@ +/* 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 "serial_util.hpp" + +#include "map_allocator.hpp" + +void serializeRegionFormat(RegionPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //format + SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + SERIALIZE(buffer, &packet->x, sizeof(int)); + SERIALIZE(buffer, &packet->y, sizeof(int)); +} + +void serializeRegionContent(RegionPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //format + SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + SERIALIZE(buffer, &packet->x, sizeof(int)); + SERIALIZE(buffer, &packet->y, sizeof(int)); + + //content + for (register int i = 0; i < REGION_WIDTH; i++) { + for (register int j = 0; j < REGION_HEIGHT; j++) { + for (register int k = 0; k < REGION_DEPTH; k++) { + *reinterpret_cast(buffer) = packet->region->GetTile(i, j, k); + buffer = reinterpret_cast(buffer) + sizeof(Region::type_t); + } + } + } +} + +void deserializeRegionFormat(RegionPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //format + DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->x, sizeof(int)); + DESERIALIZE(buffer, &packet->y, sizeof(int)); +} + +void deserializeRegionContent(RegionPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //format + DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->x, sizeof(int)); + DESERIALIZE(buffer, &packet->y, sizeof(int)); + + //an object to work on + BlankAllocator().Create( + &packet->region, + packet->x, + packet->y + ); + + //content + for (register int i = 0; i < REGION_WIDTH; i++) { + for (register int j = 0; j < REGION_HEIGHT; j++) { + for (register int k = 0; k < REGION_DEPTH; k++) { + packet->region->SetTile(i, j, k, *reinterpret_cast(buffer)); + buffer = reinterpret_cast(buffer) + sizeof(Region::type_t); + } + } + } +} \ No newline at end of file diff --git a/common/network/serial/serial_server.cpp b/common/network/serial/serial_server.cpp new file mode 100644 index 0000000..1f94dd4 --- /dev/null +++ b/common/network/serial/serial_server.cpp @@ -0,0 +1,36 @@ +/* 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 "serial_util.hpp" + +void serializeServer(ServerPacket* packet, void* buffer) { + SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} + +void deserializeServer(ServerPacket* packet, void* buffer) { + DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); + + //TODO +} diff --git a/common/network/serial/serial_statistics.cpp b/common/network/serial/serial_statistics.cpp new file mode 100644 index 0000000..6a594a8 --- /dev/null +++ b/common/network/serial/serial_statistics.cpp @@ -0,0 +1,65 @@ +/* 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 "serial_util.hpp" + +void serializeStatistics(Statistics* stats, void* buffer) { + //integers + SERIALIZE(buffer, &stats->level, sizeof(int)); + SERIALIZE(buffer, &stats->exp, sizeof(int)); + SERIALIZE(buffer, &stats->maxHP, sizeof(int)); + SERIALIZE(buffer, &stats->health, sizeof(int)); + SERIALIZE(buffer, &stats->maxMP, sizeof(int)); + SERIALIZE(buffer, &stats->mana, sizeof(int)); + SERIALIZE(buffer, &stats->attack, sizeof(int)); + SERIALIZE(buffer, &stats->defence, sizeof(int)); + SERIALIZE(buffer, &stats->intelligence, sizeof(int)); + SERIALIZE(buffer, &stats->resistance, sizeof(int)); + SERIALIZE(buffer, &stats->speed, sizeof(int)); + + //floats + SERIALIZE(buffer, &stats->accuracy, sizeof(float)); + SERIALIZE(buffer, &stats->evasion, sizeof(float)); + SERIALIZE(buffer, &stats->luck, sizeof(float)); +} + + +void deserializeStatistics(Statistics* stats, void* buffer) { + //integers + DESERIALIZE(buffer, &stats->level, sizeof(int)); + DESERIALIZE(buffer, &stats->exp, sizeof(int)); + DESERIALIZE(buffer, &stats->maxHP, sizeof(int)); + DESERIALIZE(buffer, &stats->health, sizeof(int)); + DESERIALIZE(buffer, &stats->maxMP, sizeof(int)); + DESERIALIZE(buffer, &stats->mana, sizeof(int)); + DESERIALIZE(buffer, &stats->attack, sizeof(int)); + DESERIALIZE(buffer, &stats->defence, sizeof(int)); + DESERIALIZE(buffer, &stats->intelligence, sizeof(int)); + DESERIALIZE(buffer, &stats->resistance, sizeof(int)); + DESERIALIZE(buffer, &stats->speed, sizeof(int)); + + //floats + DESERIALIZE(buffer, &stats->accuracy, sizeof(float)); + DESERIALIZE(buffer, &stats->evasion, sizeof(float)); + DESERIALIZE(buffer, &stats->luck, sizeof(float)); +} diff --git a/common/network/serial.hpp b/common/network/serial/serial_util.hpp similarity index 60% rename from common/network/serial.hpp rename to common/network/serial/serial_util.hpp index 29b9c2d..a0910b8 100644 --- a/common/network/serial.hpp +++ b/common/network/serial/serial_util.hpp @@ -19,20 +19,13 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef SERIAL_HPP_ -#define SERIAL_HPP_ +#ifndef SERIALIZEUTIL_HPP_ +#define SERIALIZEUTIL_HPP_ -#include "serial_packet.hpp" +#include -/* NOTE: Keep the PACKET_BUFFER_SIZE up to date - * NOTE: REGION_CONTENT is currently the largest type of packet - * map content: REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizoeof(region::type_t) - * map format: sizeof(int) * 3 - * metadata: sizeof(SerialPacket::Type) -*/ -#define PACKET_BUFFER_SIZE REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH * sizeof(Region::type_t) + sizeof(int) * 3 + sizeof(SerialPacket::Type) - -void serialize(SerialPacket* const, void* dest); -void deserialize(SerialPacket* const, void* src); +//NOTE: The strange assignments here used in order to move the void* parameter +#define SERIALIZE(buffer, data, size) memcpy(buffer, data, size); buffer = reinterpret_cast(buffer) + size; +#define DESERIALIZE(buffer, data, size) memcpy(data, buffer, size); buffer = reinterpret_cast(buffer) + size; #endif From 46dff9b97bb276947e60f9c238298c1d797d6cd0 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Jun 2014 23:08:52 +1000 Subject: [PATCH 3/5] Implemented the minor changes into UDPNetworkPacket --- common/network/makefile | 4 +++- common/network/udp_network_utility.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/common/network/makefile b/common/network/makefile index 7053c0f..618d925 100644 --- a/common/network/makefile +++ b/common/network/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../gameplay ../map ../utilities +INCLUDES+=. packet serial ../gameplay ../map ../utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) @@ -17,6 +17,8 @@ OUT=$(addprefix $(OUTDIR)/,libcommon.a) #targets all: $(OBJ) $(OUT) ar -crs $(OUT) $(OBJ) + $(MAKE) -C packet + $(MAKE) -C serial $(OBJ): | $(OBJDIR) diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index 5fb669e..275c1c8 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -166,7 +166,7 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPack int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) { memset(packet->data, 0, packet->maxlen); - serialize(serialPacket, packet->data); + serializePacket(serialPacket, packet->data); packet->len = PACKET_BUFFER_SIZE; packet->address = *add; @@ -181,7 +181,7 @@ int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) { int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) { memset(packet->data, 0, packet->maxlen); - serialize(serialPacket, packet->data); + serializePacket(serialPacket, packet->data); packet->len = PACKET_BUFFER_SIZE; int ret = SDLNet_UDP_Send(socket, channel, packet); @@ -195,7 +195,7 @@ int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) { int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) { memset(packet->data, 0, packet->maxlen); - serialize(serialPacket, packet->data); + serializePacket(serialPacket, packet->data); packet->len = PACKET_BUFFER_SIZE; int sent = 0; @@ -213,8 +213,8 @@ int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) { 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; + deserializePacket(serialPacket, packet->data); + serialPacket->srcAddress = packet->address; if (ret < 0) { throw(std::runtime_error("Unknown network error occured")); From 5966d7b51ab083df9b35277d2467ec0595d4e743 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 4 Jun 2014 23:30:20 +1000 Subject: [PATCH 4/5] Fleshed out the serialization internals --- common/gameplay/enemy_data.hpp | 2 ++ common/network/packet/enemy_packet.hpp | 5 +++ common/network/serial/serial_character.cpp | 40 ++++++++++++++++++++-- common/network/serial/serial_client.cpp | 10 ++++-- common/network/serial/serial_combat.cpp | 32 +++++++++++++++-- common/network/serial/serial_enemy.cpp | 26 ++++++++++++-- common/network/serial/serial_server.cpp | 10 ++++-- 7 files changed, 115 insertions(+), 10 deletions(-) diff --git a/common/gameplay/enemy_data.hpp b/common/gameplay/enemy_data.hpp index 62de98c..9fd96e2 100644 --- a/common/gameplay/enemy_data.hpp +++ b/common/gameplay/enemy_data.hpp @@ -46,6 +46,8 @@ struct EnemyData { //TODO: buffs //TODO: debuffs + //TODO: rewards + //active gameplay members //NOTE: these are lost when unloaded #ifdef GRAPHICS diff --git a/common/network/packet/enemy_packet.hpp b/common/network/packet/enemy_packet.hpp index bbe68c9..4a3d343 100644 --- a/common/network/packet/enemy_packet.hpp +++ b/common/network/packet/enemy_packet.hpp @@ -33,6 +33,11 @@ struct EnemyPacket : SerialPacketBase { //gameplay Statistics stats; + //TODO: equipment + //TODO: items + //TODO: buffs + //TODO: debuffs + //TODO: rewards }; diff --git a/common/network/serial/serial_character.cpp b/common/network/serial/serial_character.cpp index 033944a..3805702 100644 --- a/common/network/serial/serial_character.cpp +++ b/common/network/serial/serial_character.cpp @@ -26,19 +26,55 @@ void serializeCharacter(CharacterPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + //identify the character + SERIALIZE(buffer, &packet->characterIndex, sizeof(int)); + SERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); + SERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); + + //the owner + SERIALIZE(buffer, &packet->accountIndex, sizeof(int)); + + //location + SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + SERIALIZE(buffer, &packet->origin.x, sizeof(double)); + SERIALIZE(buffer, &packet->origin.y, sizeof(double)); + SERIALIZE(buffer, &packet->motion.x, sizeof(double)); + SERIALIZE(buffer, &packet->motion.y, sizeof(double)); //stats structure serializeStatistics(&packet->stats, buffer); buffer = reinterpret_cast(buffer) + sizeof(Statistics); + + //TODO: equipment + //TODO: items + //TODO: buffs + //TODO: debuffs } void deserializeCharacter(CharacterPacket* packet, void* buffer) { DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + //identify the character + DESERIALIZE(buffer, &packet->characterIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); + DESERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); + + //the owner + DESERIALIZE(buffer, &packet->accountIndex, sizeof(int)); + + //location + DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->origin.x, sizeof(double)); + DESERIALIZE(buffer, &packet->origin.y, sizeof(double)); + DESERIALIZE(buffer, &packet->motion.x, sizeof(double)); + DESERIALIZE(buffer, &packet->motion.y, sizeof(double)); //stats structure deserializeStatistics(&packet->stats, buffer); buffer = reinterpret_cast(buffer) + sizeof(Statistics); + + //TODO: equipment + //TODO: items + //TODO: buffs + //TODO: debuffs } diff --git a/common/network/serial/serial_client.cpp b/common/network/serial/serial_client.cpp index 4a818cf..bbc07b9 100644 --- a/common/network/serial/serial_client.cpp +++ b/common/network/serial/serial_client.cpp @@ -26,11 +26,17 @@ void serializeClient(ClientPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + SERIALIZE(buffer, &packet->clientIndex, sizeof(int)); + SERIALIZE(buffer, &packet->accountIndex, sizeof(int)); + SERIALIZE(buffer, &packet->username, sizeof(PACKET_STRING_SIZE)); + SERIALIZE(buffer, &packet->password, sizeof(PACKET_STRING_SIZE)); } void deserializeClient(ClientPacket* packet, void* buffer) { DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + DESERIALIZE(buffer, &packet->clientIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->accountIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->username, sizeof(PACKET_STRING_SIZE)); + DESERIALIZE(buffer, &packet->password, sizeof(PACKET_STRING_SIZE)); } diff --git a/common/network/serial/serial_combat.cpp b/common/network/serial/serial_combat.cpp index 6fbb053..881db97 100644 --- a/common/network/serial/serial_combat.cpp +++ b/common/network/serial/serial_combat.cpp @@ -26,11 +26,39 @@ void serializeCombat(CombatPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + //identify the combat instance + SERIALIZE(buffer, &packet->combatIndex, sizeof(int)); + SERIALIZE(buffer, &packet->difficulty, sizeof(int)); + SERIALIZE(buffer, &packet->terrainType, sizeof(CombatData::Terrain)); + + //combatants + SERIALIZE(buffer, &packet->characterArray, sizeof(int) * COMBAT_MAX_CHARACTERS); + SERIALIZE(buffer, &packet->enemyArray, sizeof(int) * COMBAT_MAX_ENEMIES); + + //location + SERIALIZE(buffer, &packet->mapIndex, sizeof(int)); + SERIALIZE(buffer, &packet->origin.x, sizeof(double)); + SERIALIZE(buffer, &packet->origin.y, sizeof(double)); + + //TODO: rewards } void deserializeCombat(CombatPacket* packet, void* buffer) { DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + //identify the combat instance + DESERIALIZE(buffer, &packet->combatIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->difficulty, sizeof(int)); + DESERIALIZE(buffer, &packet->terrainType, sizeof(CombatData::Terrain)); + + //combatants + DESERIALIZE(buffer, &packet->characterArray, sizeof(int) * COMBAT_MAX_CHARACTERS); + DESERIALIZE(buffer, &packet->enemyArray, sizeof(int) * COMBAT_MAX_ENEMIES); + + //location + DESERIALIZE(buffer, &packet->mapIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->origin.x, sizeof(double)); + DESERIALIZE(buffer, &packet->origin.y, sizeof(double)); + + //TODO: rewards } diff --git a/common/network/serial/serial_enemy.cpp b/common/network/serial/serial_enemy.cpp index 4059ea5..363ca1f 100644 --- a/common/network/serial/serial_enemy.cpp +++ b/common/network/serial/serial_enemy.cpp @@ -26,19 +26,41 @@ void serializeEnemy(EnemyPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + //identify the enemy + SERIALIZE(buffer, &packet->enemyIndex, sizeof(int)); + SERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); + SERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); + + //gameplay //stats structure serializeStatistics(&packet->stats, buffer); buffer = reinterpret_cast(buffer) + sizeof(Statistics); + + //TODO: equipment + //TODO: items + //TODO: buffs + //TODO: debuffs + + //TODO: rewards } void deserializeEnemy(EnemyPacket* packet, void* buffer) { DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + //identify the enemy + DESERIALIZE(buffer, &packet->enemyIndex, sizeof(int)); + DESERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); + DESERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); //stats structure deserializeStatistics(&packet->stats, buffer); buffer = reinterpret_cast(buffer) + sizeof(Statistics); + + //TODO: equipment + //TODO: items + //TODO: buffs + //TODO: debuffs + + //TODO: rewards } diff --git a/common/network/serial/serial_server.cpp b/common/network/serial/serial_server.cpp index 1f94dd4..12a2f34 100644 --- a/common/network/serial/serial_server.cpp +++ b/common/network/serial/serial_server.cpp @@ -26,11 +26,17 @@ void serializeServer(ServerPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + //identify the server + SERIALIZE(buffer, &packet->name, PACKET_STRING_SIZE); + SERIALIZE(buffer, &packet->playerCount, sizeof(int)); + SERIALIZE(buffer, &packet->version, sizeof(int)); } void deserializeServer(ServerPacket* packet, void* buffer) { DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - //TODO + //identify the server + DESERIALIZE(buffer, &packet->name, PACKET_STRING_SIZE); + DESERIALIZE(buffer, &packet->playerCount, sizeof(int)); + DESERIALIZE(buffer, &packet->version, sizeof(int)); } From 2c9b0fc3e76bee67466cefc87da46c74317ce9f9 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 6 Jun 2014 21:12:46 +1000 Subject: [PATCH 5/5] Committing some comment tweaks --- client/in_combat.cpp | 2 +- client/in_world.cpp | 2 +- client/lobby_menu.cpp | 2 +- common/network/packet/client_packet.hpp | 2 +- common/network/packet/serial_packet_type.hpp | 27 +++++++++++++++----- common/network/serial/serial.cpp | 4 --- common/network/serial/serial_client.cpp | 4 +-- server/server_internals.cpp | 2 +- 8 files changed, 28 insertions(+), 17 deletions(-) diff --git a/client/in_combat.cpp b/client/in_combat.cpp index 6be762f..207b97e 100644 --- a/client/in_combat.cpp +++ b/client/in_combat.cpp @@ -161,7 +161,7 @@ void InCombat::HandlePacket(SerialPacket packet) { break; //handle errors default: - throw(std::runtime_error(std::string() + "Unknown SerialPacket::Type encountered in InCombat: " + to_string_custom(int(packet.meta.type)))); + throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InCombat: " + to_string_custom(int(packet.meta.type)))); break; } } diff --git a/client/in_world.cpp b/client/in_world.cpp index 01a8ed0..6aa6a5a 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -267,7 +267,7 @@ void InWorld::HandlePacket(SerialPacket packet) { break; //handle errors default: - throw(std::runtime_error(std::string() + "Unknown SerialPacket::Type encountered in InWorld: " + to_string_custom(int(packet.meta.type)))); + throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InWorld: " + to_string_custom(int(packet.meta.type)))); break; } } diff --git a/client/lobby_menu.cpp b/client/lobby_menu.cpp index fa1925e..ddc6998 100644 --- a/client/lobby_menu.cpp +++ b/client/lobby_menu.cpp @@ -231,7 +231,7 @@ void LobbyMenu::HandlePacket(SerialPacket packet) { //handle errors default: - throw(std::runtime_error(std::string() + "Unknown SerialPacket::Type encountered in LobbyMenu: " + to_string_custom(int(packet.meta.type)))); + throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in LobbyMenu: " + to_string_custom(int(packet.meta.type)))); break; } } \ No newline at end of file diff --git a/common/network/packet/client_packet.hpp b/common/network/packet/client_packet.hpp index 995bc4a..f191b11 100644 --- a/common/network/packet/client_packet.hpp +++ b/common/network/packet/client_packet.hpp @@ -28,7 +28,7 @@ struct ClientPacket : SerialPacketBase { int clientIndex; int accountIndex; char username[PACKET_STRING_SIZE]; - char password[PACKET_STRING_SIZE]; //hashed, not currently used +// char password[PACKET_STRING_SIZE]; //hashed, not currently used }; #endif \ No newline at end of file diff --git a/common/network/packet/serial_packet_type.hpp b/common/network/packet/serial_packet_type.hpp index 8fbcd17..dceffaa 100644 --- a/common/network/packet/serial_packet_type.hpp +++ b/common/network/packet/serial_packet_type.hpp @@ -22,39 +22,49 @@ #ifndef SERIALPACKETTYPE_HPP_ #define SERIALPACKETTYPE_HPP_ +/* Key for the comments: + * request => response +*/ + enum class SerialPacketType { //default: there is something wrong NONE = 0, //keep alive + //ping => pong PING, PONG, - //searching for a server to join + //search for the server list + //none => server name, player count, version info (and source address) BROADCAST_REQUEST, BROADCAST_RESPONSE, - BROADCAST_REJECTION, //try to join the server + //username, and password => client index, account index JOIN_REQUEST, JOIN_RESPONSE, JOIN_REJECTION, - //mass update + //mass update of all surrounding content + //character.x, character.y => packet barrage SYNCHRONIZE, //disconnect from the server + //autentication, account index => disconnect that account DISCONNECT, //shut down the server + //autentication => disconnect, shutdown SHUTDOWN, //map data + //room index, region.x, region.y => room index, region.x, region.y, region content REGION_REQUEST, REGION_CONTENT, - REGION_REJECTION, //combat data + //TODO: system incomplete COMBAT_NEW, COMBAT_DELETE, COMBAT_UPDATE, @@ -70,16 +80,20 @@ enum class SerialPacketType { COMBAT_REJECTION, //character data + //character data => etc. CHARACTER_NEW, CHARACTER_DELETE, CHARACTER_UPDATE, + //authentication, character index => character stats CHARACTER_STATS_REQUEST, CHARACTER_STATS_RESPONSE, + //character new => character rejection, disconnect? CHARACTER_REJECTION, //enemy data + //enemy data => etc. ENEMY_NEW, ENEMY_DELETE, ENEMY_UPDATE, @@ -87,12 +101,13 @@ enum class SerialPacketType { ENEMY_STATS_REQUEST, ENEMY_STATS_RESPONSE, + //enemy index => enemy doens't exist ENEMY_REJECTION, - //more packet types go here + //NOTE: more packet types go here //not used - LAST, + LAST }; #endif \ No newline at end of file diff --git a/common/network/serial/serial.cpp b/common/network/serial/serial.cpp index c73b2bc..423f6c1 100644 --- a/common/network/serial/serial.cpp +++ b/common/network/serial/serial.cpp @@ -42,9 +42,7 @@ void serializePacket(SerialPacketBase* packet, void* buffer) { case SerialPacketType::BROADCAST_REQUEST: //all rejections - case SerialPacketType::BROADCAST_REJECTION: case SerialPacketType::JOIN_REJECTION: - case SerialPacketType::REGION_REJECTION: case SerialPacketType::CHARACTER_REJECTION: case SerialPacketType::ENEMY_REJECTION: case SerialPacketType::COMBAT_REJECTION: @@ -120,9 +118,7 @@ void deserializePacket(SerialPacketBase* packet, void* buffer) { case SerialPacketType::BROADCAST_REQUEST: //all rejections - case SerialPacketType::BROADCAST_REJECTION: case SerialPacketType::JOIN_REJECTION: - case SerialPacketType::REGION_REJECTION: case SerialPacketType::CHARACTER_REJECTION: case SerialPacketType::ENEMY_REJECTION: case SerialPacketType::COMBAT_REJECTION: diff --git a/common/network/serial/serial_client.cpp b/common/network/serial/serial_client.cpp index bbc07b9..c96449d 100644 --- a/common/network/serial/serial_client.cpp +++ b/common/network/serial/serial_client.cpp @@ -29,7 +29,7 @@ void serializeClient(ClientPacket* packet, void* buffer) { SERIALIZE(buffer, &packet->clientIndex, sizeof(int)); SERIALIZE(buffer, &packet->accountIndex, sizeof(int)); SERIALIZE(buffer, &packet->username, sizeof(PACKET_STRING_SIZE)); - SERIALIZE(buffer, &packet->password, sizeof(PACKET_STRING_SIZE)); +// SERIALIZE(buffer, &packet->password, sizeof(PACKET_STRING_SIZE)); } void deserializeClient(ClientPacket* packet, void* buffer) { @@ -38,5 +38,5 @@ void deserializeClient(ClientPacket* packet, void* buffer) { DESERIALIZE(buffer, &packet->clientIndex, sizeof(int)); DESERIALIZE(buffer, &packet->accountIndex, sizeof(int)); DESERIALIZE(buffer, &packet->username, sizeof(PACKET_STRING_SIZE)); - DESERIALIZE(buffer, &packet->password, sizeof(PACKET_STRING_SIZE)); +// DESERIALIZE(buffer, &packet->password, sizeof(PACKET_STRING_SIZE)); } diff --git a/server/server_internals.cpp b/server/server_internals.cpp index 4ee835f..c5298fc 100644 --- a/server/server_internals.cpp +++ b/server/server_internals.cpp @@ -184,7 +184,7 @@ void ServerApplication::HandlePacket(SerialPacket packet) { break; //handle errors default: - throw(std::runtime_error("Unknown SerialPacket::Type encountered")); + throw(std::runtime_error("Unknown SerialPacketType encountered")); break; } }