From 4b5194918b82ae6fd4db8d839341b3d95cb6b1a0 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 27 Aug 2014 15:35:04 +1000 Subject: [PATCH] Encapsulated SerialPacket, and made adjustments to accomodate it --- common/network/makefile | 5 ++--- common/network/packet_types/makefile | 2 +- common/network/serial_packet.hpp | 24 ++++++++++++++------- common/network/serial_packet_base.hpp | 29 ++++++++++++++++---------- common/network/serial_packet_type.hpp | 2 +- common/network/udp_network_utility.cpp | 10 ++++----- 6 files changed, 43 insertions(+), 29 deletions(-) diff --git a/common/network/makefile b/common/network/makefile index 618d925..903ab42 100644 --- a/common/network/makefile +++ b/common/network/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. packet serial ../gameplay ../map ../utilities +INCLUDES+=. packet_types ../utilities ../gameplay ../map LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) @@ -17,8 +17,7 @@ OUT=$(addprefix $(OUTDIR)/,libcommon.a) #targets all: $(OBJ) $(OUT) ar -crs $(OUT) $(OBJ) - $(MAKE) -C packet - $(MAKE) -C serial + $(MAKE) -C packet_types $(OBJ): | $(OBJDIR) diff --git a/common/network/packet_types/makefile b/common/network/packet_types/makefile index 564b228..6ade54b 100644 --- a/common/network/packet_types/makefile +++ b/common/network/packet_types/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. ../packet ../../gameplay ../../map ../../utilities +INCLUDES+=. .. LIBS+= CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/network/serial_packet.hpp b/common/network/serial_packet.hpp index e25a855..24a3d23 100644 --- a/common/network/serial_packet.hpp +++ b/common/network/serial_packet.hpp @@ -22,6 +22,11 @@ #ifndef SERIALPACKET_HPP_ #define SERIALPACKET_HPP_ +/* DOCS: serial_packet.hpp is used to define a number of required constants. + * These are used extensively by the server and client +*/ + +#include "serial_packet_base.hpp" #include "character_packet.hpp" #include "client_packet.hpp" #include "combat_packet.hpp" @@ -29,9 +34,14 @@ #include "region_packet.hpp" #include "server_packet.hpp" -//NOTE: SerialPacket is defined in serial_packet_base.hpp +//SerialPacketBase is defined in serial_packet_base.hpp +typedef SerialPacketBase SerialPacket; -union MaxPacket { +//DOCS: NETWORK_VERSION is used to discern compatible servers and clients +constexpr int NETWORK_VERSION = 20140827; + +//_MaxPacket Should not be used +union _MaxPacket { CharacterPacket a; ClientPacket b; CombatPacket c; @@ -39,17 +49,17 @@ union MaxPacket { RegionPacket e; ServerPacket f; }; -constexpr int MAX_PACKET_SIZE = sizeof(MaxPacket); + +constexpr int MAX_PACKET_SIZE = sizeof(_MaxPacket); /* DOCS: PACKET_BUFFER_SIZE is the memory required to store serialized data * DOCS: SerialPacketType::REGION_CONTENT is currently the largest packet type - * Serialized packet structure: + * Serialized RegionPacket structure: * SerialPacketType - * room index - * X & Y position + * room index (int) + * X & Y position (int) * tile data (3 layers) * solid data (bitset) - * The constants declared here are used for networking ONLY */ constexpr int REGION_TILE_FOOTPRINT = sizeof(Region::type_t) * REGION_WIDTH * REGION_HEIGHT * REGION_DEPTH; diff --git a/common/network/serial_packet_base.hpp b/common/network/serial_packet_base.hpp index 2f82092..2faee3d 100644 --- a/common/network/serial_packet_base.hpp +++ b/common/network/serial_packet_base.hpp @@ -22,25 +22,32 @@ #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" -constexpr int NETWORK_VERSION = 20140701; +//The packets use a char array for string storage constexpr int PACKET_STRING_SIZE = 100; -struct SerialPacketBase { - //members +class SerialPacketBase { +public: + SerialPacketType SetType(SerialPacketType t) { return type = t; } + SerialPacketType GetType() { return type; } + + IPaddress GetSourceAddress() { return srcAddress; } + +protected: + friend class UDPNetworkUtility; + + SerialPacketBase() = default; + virtual ~SerialPacketBase() = default; + + virtual void Serialize(const void* buffer) = 0; + virtual void Deserialize(const void* buffer) = 0; + +private: SerialPacketType type; IPaddress srcAddress; - - virtual ~SerialPacketBase() {}; }; -typedef SerialPacketBase SerialPacket; - #endif diff --git a/common/network/serial_packet_type.hpp b/common/network/serial_packet_type.hpp index 9142dcc..4ac4beb 100644 --- a/common/network/serial_packet_type.hpp +++ b/common/network/serial_packet_type.hpp @@ -30,7 +30,7 @@ enum class SerialPacketType { //default: there is something wrong NONE = 0, - //keep alive + //heartbeat //ping => pong PING = 1, PONG = 2, diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index ad97a1c..ccd51b1 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -21,8 +21,6 @@ */ #include "udp_network_utility.hpp" -#include "serial.hpp" - #include //BUGFIX: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network @@ -166,7 +164,7 @@ int UDPNetworkUtility::SendTo(const char* ip, int port, SerialPacket* serialPack int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) { memset(packet->data, 0, packet->maxlen); - serializePacket(serialPacket, packet->data); + serialPacket->Serialize(packet->data); packet->len = PACKET_BUFFER_SIZE; packet->address = *add; @@ -181,7 +179,7 @@ int UDPNetworkUtility::SendTo(IPaddress* add, SerialPacket* serialPacket) { int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) { memset(packet->data, 0, packet->maxlen); - serializePacket(serialPacket, packet->data); + serialPacket->Serialize(packet->data); packet->len = PACKET_BUFFER_SIZE; int ret = SDLNet_UDP_Send(socket, channel, packet); @@ -195,7 +193,7 @@ int UDPNetworkUtility::SendTo(int channel, SerialPacket* serialPacket) { int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) { memset(packet->data, 0, packet->maxlen); - serializePacket(serialPacket, packet->data); + serialPacket->Serialize(packet->data); packet->len = PACKET_BUFFER_SIZE; int sent = 0; @@ -213,7 +211,7 @@ int UDPNetworkUtility::SendToAllChannels(SerialPacket* serialPacket) { int UDPNetworkUtility::Receive(SerialPacket* serialPacket) { memset(packet->data, 0, packet->maxlen); int ret = SDLNet_UDP_Recv(socket, packet); - deserializePacket(serialPacket, packet->data); + serialPacket->Deserialize(packet->data); serialPacket->srcAddress = packet->address; if (ret < 0) {