Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

BaseClient

juhgiyo edited this page · 14 revisions

Base Client Usage

  • Idea of the "Base Client" is communicating the packets with the server whatever the way you prefer.

Linking

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

Client Declaration Example

// SomeClient.h
class SomeClient:public BaseClient
{
public:
   SomeClient(const TCHAR * hostName, const TCHAR * port);
   SomeClient(const SomeClient& b);
   virtual ~SomeClient();
   SomeClient & operator=(const SomeClient&b)
   {
      if(this!=&b)
      {
         BaseClient::operator =(b);
      }
      return *this;
   }
   virtual BasePacketParser* createNewPacketParser();
};

Client Implementation Example

// SomeClient.cpp
SomeClient::SomeClient(const TCHAR * hostName, const TCHAR * port):BaseClient(hostName,port)
{
}

SomeClient::SomeClient(const SomeClient& b):BaseClient(b)
{
}

SomeClient::~SomeClient()
{
}

BasePacketParser* SomeClient::createNewPacketParser()
{
   return EP_NEW SomePacketParser(DEFAULT_WAITTIME); // Wait time for Parser Thread to terminate
                                                     // DEFAULT_WAITTIME == WAITTIME_INFINITE
}
  • * Note that SomePacketParser object will be created/destroyed automatically by BaseClient. For the details of SomePacketParser class, see BasePacketParser

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;
};

Client Connect/Disconnect Example

#include "SomePacketInfo.h"
#include "SomeClient.h"
SomeClient m_client(_T("11.222.33.44"),_T("1020"));
...
if(m_client.Connect())
{
   // connected to server
   PacketContainer<SendPacket> packetContainer=PacketContainer<SendPacket>();
   packetContainer.GetPacketPtr()->packetType=SEND_PACKET_TYPE_DOSOMETHING;
   Packet sendPacket=Packet(reinterpret_cast<const void*>(packetContainer.GetPacketPtr()),packetContainer.GetPacketByteSize(),false);
    if(!m_client.Send(sendPacket))
    {
       // send failed
    }
}
else
{
   // connection failed
}

...
if(m_client.IsConnected()) // check if connected to server
{
   m_client.Disconnect();  // Disconnect from the server
}

Client Send Packet Example

#include "SomePacketInfo.h"
#include "SomeClient.h"
SomeClient m_client;
...
// Connect to the Server
// See example above...
...
if(m_client.IsConnected()) // check if connected to server
{
    PacketContainer<SendPacketType> packetContainer=PacketContainer<SendPacketType>();
    packetContainer.GetPacketPtr()->packetType=SEND_PACKET_TYPE_DOSOMETHING;
    Packet sendPacket=Packet(reinterpret_cast<const void*>(packetContainer.GetPacketPtr()),packetContainer.GetPacketByteSize(),false);
    if(!m_client.Send(sendPacket))
    {
       // Send failed
    }
}
...
  • * For the details of PacketContainer class, see PacketContainer
  • * For the details of Packet class, see Packet

Client Receive Packet Details

  • When client receives a packet, the client will create new PacketParser object for each packet, it receives.
  • PacketParser will be run as a new thread, and the parsing will take place in the newly created thread.
  • For more detail, please see BasePacketParser
Something went wrong with that request. Please try again.