diff --git a/common/network/makefile b/common/network/makefile index 903ab42..2caa8d4 100644 --- a/common/network/makefile +++ b/common/network/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. packet_types ../utilities ../gameplay ../map +INCLUDES+=. packet_types ../gameplay ../map ../utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/network/packet_types/character_packet.cpp b/common/network/packet_types/character_packet.cpp index 2754f56..aa5119a 100644 --- a/common/network/packet_types/character_packet.cpp +++ b/common/network/packet_types/character_packet.cpp @@ -19,56 +19,54 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "serial.hpp" +#include "character_packet.hpp" -#include "serial_util.hpp" +#include "serial_utility.hpp" -void serializeCharacter(CharacterPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void CharacterPacket::Serialize(void* buffer) { + serialize(&buffer, &type, sizeof(SerialPacketType)); //identify the character - SERIALIZE(buffer, &packet->characterIndex, sizeof(int)); - SERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); - SERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); + serialize(&buffer, &characterIndex, sizeof(int)); + serialize(&buffer, &handle, PACKET_STRING_SIZE); + serialize(&buffer, &avatar, PACKET_STRING_SIZE); //the owner - SERIALIZE(buffer, &packet->accountIndex, sizeof(int)); + serialize(&buffer, &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)); + serialize(&buffer, &roomIndex, sizeof(int)); + serialize(&buffer, &origin.x, sizeof(double)); + serialize(&buffer, &origin.y, sizeof(double)); + serialize(&buffer, &motion.x, sizeof(double)); + serialize(&buffer, &motion.y, sizeof(double)); //stats structure - serializeStatistics(&packet->stats, buffer); - buffer = reinterpret_cast(buffer) + sizeof(Statistics); + serializeStatistics(&buffer, &stats); //TODO: gameplay components: equipment, items, buffs, debuffs } -void deserializeCharacter(CharacterPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void CharacterPacket::Deserialize(void* buffer) { + deserialize(&buffer, &type, sizeof(SerialPacketType)); //identify the character - DESERIALIZE(buffer, &packet->characterIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->handle, PACKET_STRING_SIZE); - DESERIALIZE(buffer, &packet->avatar, PACKET_STRING_SIZE); + deserialize(&buffer, &characterIndex, sizeof(int)); + deserialize(&buffer, &handle, PACKET_STRING_SIZE); + deserialize(&buffer, &avatar, PACKET_STRING_SIZE); //the owner - DESERIALIZE(buffer, &packet->accountIndex, sizeof(int)); + deserialize(&buffer, &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)); + deserialize(&buffer, &roomIndex, sizeof(int)); + deserialize(&buffer, &origin.x, sizeof(double)); + deserialize(&buffer, &origin.y, sizeof(double)); + deserialize(&buffer, &motion.x, sizeof(double)); + deserialize(&buffer, &motion.y, sizeof(double)); //stats structure - deserializeStatistics(&packet->stats, buffer); - buffer = reinterpret_cast(buffer) + sizeof(Statistics); + deserializeStatistics(&buffer, &stats); //TODO: gameplay components: equipment, items, buffs, debuffs } diff --git a/common/network/packet_types/character_packet.hpp b/common/network/packet_types/character_packet.hpp index b6042f3..a3451a3 100644 --- a/common/network/packet_types/character_packet.hpp +++ b/common/network/packet_types/character_packet.hpp @@ -27,11 +27,49 @@ #include "vector2.hpp" #include "statistics.hpp" -struct CharacterPacket : SerialPacketBase { +#include + +class CharacterPacket : public SerialPacketBase { +public: + CharacterPacket() {} + ~CharacterPacket() {} + + //indentity + int SetCharacterIndex(int i) { return characterIndex = i; } + const char* SetHandle(const char* s) + { return strncpy(handle, s, PACKET_STRING_SIZE); } + const char* SetAvatar(const char* s) + { return strncpy(handle, s, PACKET_STRING_SIZE); } + + int SetAccountIndex(int i) { return accountIndex = i; } + + int GetCharacterIndex() { return characterIndex; } + const char* GetHandle() { return handle; } + const char* GetAvatar() { return avatar; } + + int GetAccountIndex() { return accountIndex; } + + //location + int SetRoomIndex(int i) { return roomIndex = i; } + Vector2 SetOrigin(Vector2 v) { return origin = v; } + Vector2 SetMotion(Vector2 v) { return motion = v; } + + int GetRoomIndex() { return roomIndex; } + Vector2 GetOrigin() { return origin; } + Vector2 GetMotion() { return motion; } + + //gameplay + Statistics* GetStatistics() { return &stats; } + +protected: + virtual void Serialize(void* buffer) override; + virtual void Deserialize(void* buffer) override; + +private: //identify the character int characterIndex; - char handle[PACKET_STRING_SIZE]; - char avatar[PACKET_STRING_SIZE]; + char handle[PACKET_STRING_SIZE+1]; + char avatar[PACKET_STRING_SIZE+1]; //the owner int accountIndex; diff --git a/common/network/packet_types/client_packet.cpp b/common/network/packet_types/client_packet.cpp index 96bc881..05f01a6 100644 --- a/common/network/packet_types/client_packet.cpp +++ b/common/network/packet_types/client_packet.cpp @@ -19,24 +19,22 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "serial.hpp" +#include "client_packet.hpp" -#include "serial_util.hpp" +#include "serial_utility.hpp" -void serializeClient(ClientPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void ClientPacket::Serialize(void* buffer) { + serialize(&buffer, &type, sizeof(SerialPacketType)); - SERIALIZE(buffer, &packet->clientIndex, sizeof(int)); - SERIALIZE(buffer, &packet->accountIndex, sizeof(int)); - SERIALIZE(buffer, &packet->username, PACKET_STRING_SIZE); -// SERIALIZE(buffer, &packet->password, PACKET_STRING_SIZE); + serialize(&buffer, &clientIndex, sizeof(int)); + serialize(&buffer, &accountIndex, sizeof(int)); + serialize(&buffer, username, PACKET_STRING_SIZE); } -void deserializeClient(ClientPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void ClientPacket::Deserialize(void* buffer) { + deserialize(&buffer, &type, sizeof(SerialPacketType)); - DESERIALIZE(buffer, &packet->clientIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->accountIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->username, PACKET_STRING_SIZE); -// DESERIALIZE(buffer, &packet->password, PACKET_STRING_SIZE); + deserialize(&buffer, &clientIndex, sizeof(int)); + deserialize(&buffer, &accountIndex, sizeof(int)); + deserialize(&buffer, username, PACKET_STRING_SIZE); } diff --git a/common/network/packet_types/client_packet.hpp b/common/network/packet_types/client_packet.hpp index f191b11..5e36a2e 100644 --- a/common/network/packet_types/client_packet.hpp +++ b/common/network/packet_types/client_packet.hpp @@ -24,10 +24,31 @@ #include "serial_packet_base.hpp" -struct ClientPacket : SerialPacketBase { +#include + +class ClientPacket : public SerialPacketBase { +public: + ClientPacket() {} + ~ClientPacket() {} + + //accessors & mutators + int SetClientIndex(int i) { return clientIndex = i; } + int SetAccountIndex(int i) { return accountIndex = i; } + const char* SetUsername(const char* s) + { return strncpy(username, s, PACKET_STRING_SIZE); } + + int GetClientIndex() { return clientIndex; } + int GetAccountIndex() { return accountIndex; } + const char* GetUsername() { return username; } + +protected: + virtual void Serialize(void* buffer) override; + virtual void Deserialize(void* buffer) override; + +private: int clientIndex; int accountIndex; - char username[PACKET_STRING_SIZE]; + char username[PACKET_STRING_SIZE+1]; // char password[PACKET_STRING_SIZE]; //hashed, not currently used }; diff --git a/common/network/packet_types/combat_packet.cpp b/common/network/packet_types/combat_packet.cpp deleted file mode 100644 index 318779f..0000000 --- a/common/network/packet_types/combat_packet.cpp +++ /dev/null @@ -1,64 +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 "serial_util.hpp" - -void serializeCombat(CombatPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //identify the combat instance - SERIALIZE(buffer, &packet->combatIndex, sizeof(int)); - SERIALIZE(buffer, &packet->difficulty, sizeof(int)); - SERIALIZE(buffer, &packet->terrainType, sizeof(TerrainType)); - - //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: gameplay components: rewards -} - -void deserializeCombat(CombatPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //identify the combat instance - DESERIALIZE(buffer, &packet->combatIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->difficulty, sizeof(int)); - DESERIALIZE(buffer, &packet->terrainType, sizeof(TerrainType)); - - //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: gameplay components: rewards -} diff --git a/common/network/packet_types/combat_packet.hpp b/common/network/packet_types/combat_packet.hpp deleted file mode 100644 index 6b068df..0000000 --- a/common/network/packet_types/combat_packet.hpp +++ /dev/null @@ -1,46 +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 COMBATPACKET_HPP_ -#define COMBATPACKET_HPP_ - -#include "serial_packet_base.hpp" - -#include "combat_defines.hpp" - -struct CombatPacket : SerialPacketBase { - //identify the combat instance - int combatIndex; - int difficulty; - TerrainType terrainType; - - //combatants - int characterArray[COMBAT_MAX_CHARACTERS]; - int enemyArray[COMBAT_MAX_ENEMIES]; - - //location - int mapIndex; - Vector2 origin; - - //TODO: gameplay components: rewards -}; - -#endif \ No newline at end of file diff --git a/common/network/packet_types/enemy_packet.cpp b/common/network/packet_types/enemy_packet.cpp deleted file mode 100644 index 4861d83..0000000 --- a/common/network/packet_types/enemy_packet.cpp +++ /dev/null @@ -1,56 +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 "serial_util.hpp" - -void serializeEnemy(EnemyPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //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: gameplay components: equipment, items, buffs, debuffs, rewards -} - -void deserializeEnemy(EnemyPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); - - //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: gameplay components: equipment, items, buffs, debuffs, rewards -} diff --git a/common/network/packet_types/enemy_packet.hpp b/common/network/packet_types/enemy_packet.hpp deleted file mode 100644 index d3fb7c5..0000000 --- a/common/network/packet_types/enemy_packet.hpp +++ /dev/null @@ -1,39 +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 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: gameplay components: equipment, items, buffs, debuffs, rewards -}; - -#endif \ No newline at end of file diff --git a/common/network/packet_types/makefile b/common/network/packet_types/makefile index 6ade54b..21223de 100644 --- a/common/network/packet_types/makefile +++ b/common/network/packet_types/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. .. +INCLUDES+=. .. ../../gameplay ../../map ../../utilities LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/network/packet_types/region_packet.cpp b/common/network/packet_types/region_packet.cpp index 54c4b6b..802e876 100644 --- a/common/network/packet_types/region_packet.cpp +++ b/common/network/packet_types/region_packet.cpp @@ -19,71 +19,61 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "serial.hpp" +#include "region_packet.hpp" -#include "serial_util.hpp" +#include "serial_utility.hpp" -void serializeRegionFormat(RegionPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void RegionPacket::Serialize(void* buffer) { + serialize(&buffer, &type, sizeof(SerialPacketType)); //format - SERIALIZE(buffer, &packet->roomIndex, sizeof(int)); - SERIALIZE(buffer, &packet->x, sizeof(int)); - SERIALIZE(buffer, &packet->y, sizeof(int)); -} + serialize(&buffer, &roomIndex, sizeof(int)); + serialize(&buffer, &x, sizeof(int)); + serialize(&buffer, &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)); + if (type != SerialPacketType::REGION_CONTENT) { + return; + } //tiles for (int i = 0; i < REGION_WIDTH; i++) { for (int j = 0; j < REGION_HEIGHT; j++) { for (int k = 0; k < REGION_DEPTH; k++) { - *reinterpret_cast(buffer) = packet->region->GetTile(i, j, k); + *reinterpret_cast(buffer) = region->GetTile(i, j, k); buffer = reinterpret_cast(buffer) + sizeof(Region::type_t); } } } //solids - SERIALIZE(buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); + serialize(&buffer, region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); } -void deserializeRegionFormat(RegionPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void RegionPacket::Deserialize(void* buffer) { + deserialize(&buffer, &type, sizeof(SerialPacketType)); //format - DESERIALIZE(buffer, &packet->roomIndex, sizeof(int)); - DESERIALIZE(buffer, &packet->x, sizeof(int)); - DESERIALIZE(buffer, &packet->y, sizeof(int)); -} + deserialize(&buffer, &roomIndex, sizeof(int)); + deserialize(&buffer, &x, sizeof(int)); + deserialize(&buffer, &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)); + if (type != SerialPacketType::REGION_CONTENT) { + return; + } //an object to work on - packet->region = new Region(packet->x, packet->y); + region = new Region(x, y); //tiles for (int i = 0; i < REGION_WIDTH; i++) { for (int j = 0; j < REGION_HEIGHT; j++) { for (int k = 0; k < REGION_DEPTH; k++) { - packet->region->SetTile(i, j, k, *reinterpret_cast(buffer)); + region->SetTile(i, j, k, *reinterpret_cast(buffer)); buffer = reinterpret_cast(buffer) + sizeof(Region::type_t); } } } //solids - DESERIALIZE(buffer, packet->region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); + deserialize(&buffer, region->GetSolidBitset(), REGION_SOLID_FOOTPRINT); } \ No newline at end of file diff --git a/common/network/packet_types/region_packet.hpp b/common/network/packet_types/region_packet.hpp index 8a9085e..fe0f966 100644 --- a/common/network/packet_types/region_packet.hpp +++ b/common/network/packet_types/region_packet.hpp @@ -26,7 +26,37 @@ #include "region.hpp" -struct RegionPacket : SerialPacketBase { +#include +#include + +//define the memory footprint for the region's members +constexpr int REGION_TILE_FOOTPRINT = sizeof(Region::type_t) * REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH; +constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0); +constexpr int REGION_METADATA_FOOTPRINT = sizeof(int) * 3; + +class RegionPacket : public SerialPacketBase { +public: + RegionPacket() {} + ~RegionPacket() {} + + //location + int SetRoomIndex(int i) { return roomIndex = i; } + int SetX(int i) { return x = i; } + int SetY(int i) { return y = i; } + + int GetRoomIndex() { return roomIndex; } + int GetX() { return x; } + int GetY() { return y; } + + //the region itself + Region* SetRegion(Region* r) { return region = r; } + Region* GetRegion() { return region; } + +protected: + virtual void Serialize(void* buffer) override; + virtual void Deserialize(void* buffer) override; + +private: //location/identify the region int roomIndex; int x, y; diff --git a/common/network/packet_types/server_packet.cpp b/common/network/packet_types/server_packet.cpp index 12a2f34..622b61b 100644 --- a/common/network/packet_types/server_packet.cpp +++ b/common/network/packet_types/server_packet.cpp @@ -19,24 +19,24 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#include "serial.hpp" +#include "server_packet.hpp" -#include "serial_util.hpp" +#include "serial_utility.hpp" -void serializeServer(ServerPacket* packet, void* buffer) { - SERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void ServerPacket::Serialize(void* buffer) { + serialize(&buffer, &type, sizeof(SerialPacketType)); //identify the server - SERIALIZE(buffer, &packet->name, PACKET_STRING_SIZE); - SERIALIZE(buffer, &packet->playerCount, sizeof(int)); - SERIALIZE(buffer, &packet->version, sizeof(int)); + serialize(&buffer, &name, PACKET_STRING_SIZE); + serialize(&buffer, &playerCount, sizeof(int)); + serialize(&buffer, &version, sizeof(int)); } -void deserializeServer(ServerPacket* packet, void* buffer) { - DESERIALIZE(buffer, &packet->type, sizeof(SerialPacketType)); +void ServerPacket::Deserialize(void* buffer) { + deserialize(&buffer, &type, sizeof(SerialPacketType)); //identify the server - DESERIALIZE(buffer, &packet->name, PACKET_STRING_SIZE); - DESERIALIZE(buffer, &packet->playerCount, sizeof(int)); - DESERIALIZE(buffer, &packet->version, sizeof(int)); + deserialize(&buffer, &name, PACKET_STRING_SIZE); + deserialize(&buffer, &playerCount, sizeof(int)); + deserialize(&buffer, &version, sizeof(int)); } diff --git a/common/network/packet_types/server_packet.hpp b/common/network/packet_types/server_packet.hpp index fcd4a49..27602ff 100644 --- a/common/network/packet_types/server_packet.hpp +++ b/common/network/packet_types/server_packet.hpp @@ -24,9 +24,29 @@ #include "serial_packet_base.hpp" -struct ServerPacket : SerialPacketBase { +#include + +class ServerPacket : public SerialPacketBase { +public: + ServerPacket() {} + ~ServerPacket() {} + + const char* SetName(const char* s) + { return strncpy(name, s, PACKET_STRING_SIZE); } + int SetPlayerCount(int i) { return playerCount = i; } + int SetVersion(int i) { return version = i; } + + const char* GetName() { return name; } + int GetPlayerCount() { return playerCount; } + int GetVersion() { return version; } + +protected: + virtual void Serialize(void* buffer) override; + virtual void Deserialize(void* buffer) override; + +private: //identify the server - char name[PACKET_STRING_SIZE]; + char name[PACKET_STRING_SIZE+1]; int playerCount; int version; }; diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index 24a3d23..cc23940 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -29,8 +29,6 @@ #include "serial_packet_base.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" @@ -44,10 +42,8 @@ constexpr int NETWORK_VERSION = 20140827; union _MaxPacket { CharacterPacket a; ClientPacket b; - CombatPacket c; - EnemyPacket d; - RegionPacket e; - ServerPacket f; + RegionPacket c; + ServerPacket d; }; constexpr int MAX_PACKET_SIZE = sizeof(_MaxPacket); @@ -62,8 +58,10 @@ constexpr int MAX_PACKET_SIZE = sizeof(_MaxPacket); * solid data (bitset) */ -constexpr int REGION_TILE_FOOTPRINT = sizeof(Region::type_t) * REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH; -constexpr int REGION_SOLID_FOOTPRINT = ceil(REGION_WIDTH * REGION_HEIGHT / 8.0); -constexpr int PACKET_BUFFER_SIZE = sizeof(SerialPacketType) + sizeof(int) * 3 + REGION_TILE_FOOTPRINT + REGION_SOLID_FOOTPRINT; +constexpr int PACKET_BUFFER_SIZE = + sizeof(SerialPacketType) + + REGION_METADATA_FOOTPRINT + + REGION_TILE_FOOTPRINT + + REGION_SOLID_FOOTPRINT; #endif diff --git a/common/network/serial_packet_base.hpp b/common/network/serial_packet_base.hpp index 2faee3d..e731763 100644 --- a/common/network/serial_packet_base.hpp +++ b/common/network/serial_packet_base.hpp @@ -39,13 +39,12 @@ public: protected: friend class UDPNetworkUtility; - SerialPacketBase() = default; - virtual ~SerialPacketBase() = default; + SerialPacketBase() {}; + virtual ~SerialPacketBase() {}; - virtual void Serialize(const void* buffer) = 0; - virtual void Deserialize(const void* buffer) = 0; + virtual void Serialize(void* buffer) = 0; + virtual void Deserialize(void* buffer) = 0; -private: SerialPacketType type; IPaddress srcAddress; }; diff --git a/common/network/packet_types/serial_utility.cpp b/common/network/serial_utility.cpp similarity index 100% rename from common/network/packet_types/serial_utility.cpp rename to common/network/serial_utility.cpp diff --git a/common/network/packet_types/serial_utility.hpp b/common/network/serial_utility.hpp similarity index 89% rename from common/network/packet_types/serial_utility.hpp rename to common/network/serial_utility.hpp index 2c7669e..9af2a50 100644 --- a/common/network/packet_types/serial_utility.hpp +++ b/common/network/serial_utility.hpp @@ -25,8 +25,8 @@ #include "statistics.hpp" //raw memcpy -inline void serialize(void** bufferHead, void* data, int size); -inline void deserialize(void** bufferHead, void* data, int size); +void serialize(void** bufferHead, void* data, int size); +void deserialize(void** bufferHead, void* data, int size); void serializeStatistics(void** bufferHead, Statistics* stats); void deserializeStatistics(void** bufferHead, Statistics* stats);