Encapsulated SerialPacket, and made adjustments to accomodate it

This commit is contained in:
Kayne Ruse
2014-08-27 15:35:04 +10:00
parent 426c3a52c2
commit 4b5194918b
6 changed files with 43 additions and 29 deletions
+2 -3
View File
@@ -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)
+1 -1
View File
@@ -1,5 +1,5 @@
#config
INCLUDES+=. ../packet ../../gameplay ../../map ../../utilities
INCLUDES+=. ..
LIBS+=
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
+17 -7
View File
@@ -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;
+18 -11
View File
@@ -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
+1 -1
View File
@@ -30,7 +30,7 @@ enum class SerialPacketType {
//default: there is something wrong
NONE = 0,
//keep alive
//heartbeat
//ping => pong
PING = 1,
PONG = 2,
+4 -6
View File
@@ -21,8 +21,6 @@
*/
#include "udp_network_utility.hpp"
#include "serial.hpp"
#include <stdexcept>
//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) {