2012-06-14 13:06:06 +00:00
# pragma once
# include "../cSocket.h"
# include "../PacketID.h"
2012-08-17 10:18:07 +00:00
# include "../ByteBuffer.h"
2012-06-14 13:06:06 +00:00
# define PACKET_INCOMPLETE -2
# define PACKET_ERROR -1
2012-08-17 10:18:07 +00:00
# define PACKET_OK 1
2012-06-14 13:06:06 +00:00
// Use this macro to simplify handling several ReadXXX in a row. It assumes that you want [a_Data, a_Size] parsed (which is true for all Parse() functions)
# define HANDLE_PACKET_READ(Proc, Var, TotalBytes) \
{ \
2012-08-17 10:18:07 +00:00
if ( ! a_Buffer . Proc ( Var ) ) \
2012-06-14 13:06:06 +00:00
{ \
2012-08-17 10:18:07 +00:00
return PACKET_INCOMPLETE ; \
2012-06-14 13:06:06 +00:00
} \
2012-08-17 10:18:07 +00:00
TotalBytes = PACKET_OK ; \
2012-06-14 13:06:06 +00:00
}
class cPacket
{
public :
cPacket ( )
: m_PacketID ( 0 )
{ }
virtual ~ cPacket ( ) { }
2012-08-17 10:18:07 +00:00
/// Called to parse the packet. Packet type has already been read and the correct packet type created. Return PACKET_INCOMPLETE for incomplete data, PACKET_ERROR for error, any positive number for success
virtual int Parse ( cByteBuffer & a_Buffer )
2012-06-14 13:06:06 +00:00
{
2012-08-17 10:18:07 +00:00
// There are packets that are sent S->C only, those don't have a parsing function
UNUSED ( a_Buffer ) ;
LOGERROR ( " Packet type 0x%02x has no parser defined! " , m_PacketID ) ;
ASSERT ( ! " Unparsed packet type! " ) ;
return PACKET_ERROR ;
2012-06-14 13:06:06 +00:00
}
/// Called to serialize the packet into a string. Append all packet data to a_Data, including the packet type!
virtual void Serialize ( AString & a_Data ) const
{
2012-08-17 10:18:07 +00:00
// There are packets that are sent C->S only, those don't have a serializing function
2012-08-03 11:53:11 +00:00
UNUSED ( a_Data ) ;
2012-08-17 10:18:07 +00:00
LOGERROR ( " Packet type 0x%02x has no serializer defined! " , m_PacketID ) ;
ASSERT ( ! " Unserialized packet " ) ;
2012-06-14 13:06:06 +00:00
}
virtual cPacket * Clone ( ) const = 0 ;
unsigned char m_PacketID ;
protected :
// These append the data into the a_Dst string:
static void AppendString ( AString & a_Dst , const AString & a_String ) ;
static void AppendString16 ( AString & a_Dst , const AString & a_String ) ;
static void AppendShort ( AString & a_Dst , short a_Short ) ;
static void AppendShort ( AString & a_Dst , unsigned short a_Short ) ;
static void AppendInteger ( AString & a_Dst , int a_Integer ) ;
static void AppendInteger ( AString & a_Dst , unsigned int a_Integer ) ;
static void AppendFloat ( AString & a_Dst , float a_Float ) ;
static void AppendDouble ( AString & a_Dst , const double & a_Double ) ;
static void AppendByte ( AString & a_Dst , char a_Byte ) ;
static void AppendLong ( AString & a_Dst , const long long & a_Long ) ;
static void AppendBool ( AString & a_Dst , bool a_Bool ) ;
static void AppendData ( AString & a_Dst , const char * a_Data , unsigned int a_Size ) ;
} ;
typedef std : : list < cPacket * > PacketList ;
typedef std : : deque < cPacket * > PacketQueue ;