Skip to content

BasePacketParser

juhgiyo edited this page Nov 14, 2012 · 7 revisions

BasePacketParser Usage

Packet Type Declaration

// SomePacketInfo.h
...
typedef enum _sendPacketType{
SEND_PACKET_TYPE_DIDSOMETHING=0,
SEND_PACKET_TYPE_DIDSOMETHING2,
SEND_PACKET_TYPE_DIDSOMETHING3,
SEND_PACKET_TYPE_ENDOFPROCESS,
}SendPacketType;

typedef enum _receivePacketType{
RECEIVE_PACKET_TYPE_DOSOMETHING=0,
RECEIVE_PACKET_TYPE_DOSOMETHING2,
RECEIVE_PACKET_TYPE_DOSOMETHING3,
}ReceivePacketType;

struct SendPacket{
   SendPacketType packetType;
};

struct ReceivePacket{
   ReceivePacketType packetType;
   unsigned int magicNum;
};

Linking

// SomePacketParser.h
#include "epse.h"
using namespace epl;
using namespace epse;

Packet Parser Declaration Example

// SomePacketParser.h
class SomePacketParser:public BasePacketParser
{
public:
   SomePacketParser(unsigned int waitTimeMilliSec=DEFAULT_WAITTIME); // Parser Thread Termination Wait time
   SomePacketParser(const SomePacketParser& b);
   virtual ~SomePacketParser();
   SomePacketParser& operator=(const SomePacketParser&b)
   {
      if(this!=&b)
      {
         BasePacketParser::operator =(b);
      }
      return *this;
   }
   virtual void ParsePacket(const Packet &packet );
};

Packet Parser Implementation Example

// SomePacketParser.cpp
#include "SomePacketParser.h"

SomePacketParser::SomePacketParser(unsigned int waitTimeMilliSec):BasePacketParser(waitTimeMilliSec)
{
}

SomePacketParser::SomePacketParser(const SomePacketParser& b):BasePacketParser(b)
{
}

SomePacketParser::~SomePacketParser()
{
}

void SomePacketParser::ParsePacket(const Packet &packet )
{
   Packet sendPacket;
   PacketContainer<SendPacket> packetContainer;
   PacketContainer<ReceivePacket> container=PacketContainer<ReceivePacket>(reinterpret_cast<const void*>(packet.GetPacket()),packet.GetPacketByteSize(),false);
   switch(container.GetPacketPtr()->packetType)
   {
   case RECEIVE_PACKET_TYPE_DOSOMETHING:
      packetContainer=PacketContainer<SendPacket>();
      packetContainer.GetPacketPtr()->packetType=SEND_PACKET_TYPE_DIDSOMETHING;
      CString someString=_T("Hello");
      packetContainer.SetArray(reinterpret_cast<const char*>(someString.GetString()),(someString.GetLength()+1)*sizeof(TCHAR));
      sendPacket=Packet(reinterpret_cast<const void*>(packetContainer.GetPacketPtr()),packetContainer.GetPacketByteSize(),false);
      Send(sendPacket);
      packetContainer=PacketContainer<SendPacket>();
      packetContainer.GetPacketPtr()->packetType=SEND_PACKET_TYPE_ENDOFPROCESS;
      sendPacket=Packet(reinterpret_cast<const void*>(packetContainer.GetPacketPtr()),packetContainer.GetPacketByteSize(),false);
      Send(sendPacket);
      break;
   case RECEIVE_PACKET_TYPE_DOSOMETHING2:
      PacketProcessor.Process(container.GetPacketPtr()->magicNum);    // Process with received data
      packetContainer=PacketContainer<SendPacket>();
      packetContainer.GetPacketPtr()->packetType=SEND_PACKET_TYPE_DIDSOMETHING2;
      CString someString=_T("Hello");
      packetContainer.SetArray(reinterpret_cast<const char*>(someString.GetString()),(someString.GetLength()+1)*sizeof(TCHAR));
      sendPacket=Packet(reinterpret_cast<const void*>(packetContainer.GetPacketPtr()),packetContainer.GetPacketByteSize(),false);
      Send(sendPacket);
      packetContainer=PacketContainer<SendPacket>();
      packetContainer.GetPacketPtr()->packetType=SEND_PACKET_TYPE_ENDOFPROCESS;
      sendPacket=Packet(reinterpret_cast<const void*>(packetContainer.GetPacketPtr()),packetContainer.GetPacketByteSize(),false);
      Send(sendPacket);
      break;
   case RECEIVE_PACKET_TYPE_DOSOMETHING3:
      ...
      break;
   default:
      //TODO: Message Output for Error
      break;
   }
}
  • * Note that for UDP communication, the packet byte size, sending must NOT be greater than size returned from GetMaxPacketByteSize().
  • * For the details of PacketContainer class, see PacketContainer
  • * For the details of Packet class, see Packet
Something went wrong with that request. Please try again.