www.pudn.com > ChatUseIOCP.rar > ChatProtocol.h
#pragma once
#include "../DtLibrary/DtServerSocketClient.h"
#define STX 0x02
#define ETX 0x03
#define CHARSIZE sizeof(char)
#define USHORTSIZE sizeof(USHORT)
#define DWORDSIZE sizeof(DWORD)
#define CHAT_HEADER_SIZE (CHARSIZE+USHORTSIZE+DWORDSIZE+CHARSIZE) //1+2+4+1 //stx+USHORT+DWORD+char
#ifdef __SERVER_SIDE__
class ChatProtocol : public Datatal::DtServerSocketClient
#else
class ChatProtocol : public Datatal::DtIpSocket
#endif
{
public:
ChatProtocol(void);
~ChatProtocol(void);
/// In this enum we define all our diffrent transaction codes.
enum TRANS_CODES
{
TC_LOGIN, /// want to login
TC_LIST_CHANNELS, /// List all channels
TC_LIST_USERS, /// list all users / users on a specific channel
TC_SEND_MESSAGE, /// Set a message to a channel/user
TC_SEND_MESSAGE_OUT /// Outgoing message, (sent to client)
};
enum TRANS_STATUS
{
TS_OK, /// Everything went ok
TS_NO_DATA, /// Everything went ok, but we got no data
TS_INVALID, /// Invalid transaction
TS_ERROR, /// Something went wrong, problaly invalid data.
TS_NO_ACCESS, /// The user do not have the required
TS_EXCEPTION /// Something unexpected happened.
};
/// Packet Structure - The heart of the protocol.
struct Packet
{
//functioncode
USHORT nFunctionCode;
//size of the data stored in pData
DWORD dwDataSize;
//statuscode. 0 = ok;
char Status;
//Buffer
char* pData;
//maxsize of pData (allocated size)
DWORD dwBufferSize;
~Packet()
{
if (pData) delete[] pData;
};
Packet()
{
pData = NULL;
nFunctionCode = 0;
dwDataSize = 0;
dwBufferSize = 0;
Status = 0;
};
Packet(int FunctionCode)
{
pData = NULL;
nFunctionCode = FunctionCode;
dwDataSize = 0;
dwBufferSize = 0;
Status = 0;
};
};
protected:
Packet* m_pInPacket; /// The inpacket that we are working with.
public:
#ifdef __SERVER_SIDE__
/// New data has arrived.
/// This function is called by the CServerClientBase class when some new data has arrived.
/// @param pInBuffer Pointer to the inputbuffer
/// @param dwBufSize Size of the inputbuffer
/// @returns The number of bytes that were proccessed by this function, the returned number of bytes will be removed by baseclass.
void OnReceive(const char* pInBuffer, size_t nBufSize);
#else
/// New data has arrived.
/// This function is called by the CClientSocket class when some new data has arrived.
/// @param pInBuffer Pointer to the inputbuffer
/// @param dwBufSize Size of the inputbuffer
/// @returns The number of bytes that were proccessed by this function, the returned number of bytes will be removed by baseclass.
void HandleReceive(const char* pInBuffer, size_t nBufSize);
#endif
/// Virtual function that will handle complete incomming packets
/// This function is called by OnReceive when a complete packet have arrived.
/// @param InPacket The newly arrived packet.
/// @see OnRecieve
virtual void HandlePacket(Packet* pInPacket) = 0;
/// Sends a packet back to the client
/// This function is called from the class that derives this one.
/// @param OutPacket The packet that we should send
/// @returns TRUE if the sending succeeds.
bool Send(Packet& OutPacket);
/// Creates a packet buffer from a std::String that contains the data.
/// @param ThePacket A reference to a packet that we should create a buffer in.
/// @param data A reference to the data buffer that we should copy.
static void SetPacketBuffer(Packet& ThePacket, const char* szData, DWORD dwSize);
#ifdef _STRING_
/// Creates a packet buffer from a std::string that contains the data.
/// @param ThePacket A reference to a packet that we should create a buffer in.
/// @param strBuffer A reference to the data buffer that we should copy.
static void SetPacketBuffer(Packet& ThePacket, std::string& strBuffer);
#endif
#ifdef __AFX_H__
/// Creates a packet buffer from a CString that contains the data.
/// @param ThePacket A reference to a packet that we should create a buffer in.
/// @param strBuffer A reference to the strBuffer buffer that we should copy.
static void SetPacketBuffer(Packet& ThePacket, CString& strBuffer);
#endif
};