Skip to content

PacketContainer

juhgiyo edited this page Jan 24, 2013 · 7 revisions

Packet Container Usage

  • Idea of the "Packet Container" is that for variadic length packet, add variadic length array after the packet structure.

Linking

#include "epse.h"
#include "PacketContainer.h"
using namespace epl;
using namespace epse;

Packet Type Declaration

// SomePacketInfo.h
...
typedef enum _receivePacketType{
RECEIVE_PACKET_TYPE_DIDSOMETHING=0,
RECEIVE_PACKET_TYPE_DIDSOMETHING2,
RECEIVE_PACKET_TYPE_DIDSOMETHING3,
RECEIVE_PACKET_TYPE_ENDOFPROCESS,
}ReceivePacketType;

typedef enum _sendPacketType{
SEND_PACKET_TYPE_DOSOMETHING=0,
SEND_PACKET_TYPE_DOSOMETHING2,
SEND_PACKET_TYPE_DOSOMETHING3,
}SendPacketType;

struct SendPacket{
   SendPacketType packetType;
   unsigned int magicNum;
};

struct ReceivePacket{
   ReceivePacketType packetType;
};

Packet Instantiation

#include "epPacket.h"
#include "epPacketContainer.h"
using namespace epl;
using namespace epse;
...
PacketContainer<SendPacket> packetContainer;
Packet sendPacket;

packetContainer=PacketContainer<SendPacket>(0           // default array size addition to SendPacket
                                            , true      // flag whether to allocation memory
                                                        //   within PacketContainer object or not
                                           );
packetContainer.GetPacketPtr()->packetType=SEND_PACKET_TYPE_DIDSOMETHING;
CString someString=_T("Hello");

// Add given array after the SendPacket struct.
packetContainer.SetArray(reinterpret_cast<const char*>(someString.GetString()),(someString.GetLength()+1)*sizeof(TCHAR));

// Transfer to Packet object to send the packet with variadic array to Server/Client.
sendPacket=Packet(reinterpret_cast<const void*>(
   packetContainer.GetPacketPtr())      // returns the pointer to the SendPacket object followed by array set above.
   ,packetContainer.GetPacketByteSize() // returns the byte size of SendPacket Struct with array.
   ,false                               // Since the memory is allocated in PacketContainer already,
                                        //   don't allocate in Packet Object for Performance increase
   );
    • Note that since copying memory for packet is critical on the performance in Server development, you can choose whether to allocate the memory for the Packet object, or just hold the pointer to the data within the Packet/Packet Container object according to the situation.