// *** I assume this to make it compile
typedef char byte;
typedef int u32;
#define CSIM_PROTOCOL_VERSION1 0x01
typedef enum {BAD_COMMAND,
CLIENTINIT_COMMAND,
DELETEALIAS_COMMAND,
ERROR_COMMAND,
GROUPSEARCH_COMMAND,
GROUPSEARCHRESPONSE_COMMAND,
LOGIN_COMMAND,
MESSAGE_COMMAND,
MODIFYALIAS_COMMAND,
MSGHISTORY_COMMAND,
RENAMEALIAS_COMMAND,
SENDMSGHISTORY_COMMAND,
UPDATESTATUS_COMMAND,
USERSEARCH_COMMAND,
USERSEARCHRESPONSE_COMMAND,
MAKE_PCOMMANDS_ENUM_32BIT = 1<<30} PCommands;
#define SERVER_UID 0x00
class PacketException
{
public:
//Constructors/Destructors
PacketException() {};
// *** The line below makes copying these things illegal; the arg
should
// be const, or better yet, just comment it out and let the implicit
one
// work
// PacketException(PacketException &s) {};
virtual ~PacketException() {};
Quote:
};
class Packet
{
protected:
//Data Members
u32 pVersion; /* Protocol Version */
PCommands CommandType; /* Packet Command */
//Constructors
Packet(u32 vProtocol, PCommands Command);
//Member Functions
virtual u32 GetBinaryDataLength() = 0;
virtual bool GenerateBinaryData(byte *bin_data, u32 *length) = 0;
//Static Member Functions
static char* GetStringField(const byte *bin_data, u32 length, u32
&counter) throw(PacketException);
static u32 GetIntField(const byte *bin_data, u32 length, u32
&counter) throw(PacketException);
static bool GetBooleanField(const byte *bin_data, u32 length, u32
&counter) throw(PacketException);
static u32 SetField(byte *bin_data, const void *field, u32 length);
public:
//Member Functions
u32 GetPacketLength();
bool GeneratePacketData(byte *bin_data, u32 *length);
PCommands IdentifyPacket() const;
u32 GetProtocolVersion() const;
//Static Member Functions
static PCommands IdentifyPacket(byte **bin_data, u32 &length, u32
*pVersion);
static bool IsGroupID(u32 uid);
static bool IsUserID(u32 uid);
static bool IsSystemID(u32 uid);
Quote:
};
#include <stdlib.h>
#include <memory.h>
/***********************************************************************
*****
* CONSTRUCTORS AND DESTRUCTORS
************************************************************************
***/
Packet::Packet(u32 pVersion, PCommands Command)
{
this->pVersion = pVersion;
this->CommandType = Command;
Quote:
}
/***********************************************************************
*****
* MEMBER FUNCTIONS
************************************************************************
***/
/**
* Finds the length (in bytes) of the Packet that is about to be
converted
* into binary data. Use this function to allocate enough space for
the
* binary data buffer before calling GeneratePacketData()
bytes
*/
u32 Packet::GetPacketLength()
{
u32 bin_length;
bin_length = GetBinaryDataLength();
if(bin_length > 0)
{
return sizeof(pVersion) + sizeof(u32) + bin_length;
}
else
{
return 0;
}
Quote:
}
/**
* Converts a Packet into binary
the
* binary data
length
* of the binary data
*/
bool Packet::GeneratePacketData(byte *bin_data, u32 *length)
{
u32 counter, c;
//Add Protocol Version
memcpy(bin_data, &pVersion, sizeof(pVersion));
counter = sizeof(pVersion);
//Add Command Type
memcpy(&bin_data[counter], (u32 *)&CommandType, sizeof(u32));
counter += sizeof(u32);
//Add Data
if(GenerateBinaryData(&bin_data[counter], &c) == true)
{
counter += c;
*length = counter;
return true;
}
return false;
Quote:
}
/**
* Identifies the command that the Packet object describes
that
* the Packet object describes
*/
PCommands Packet::IdentifyPacket() const
{
return CommandType;
Quote:
}
/**
* Identifies the protocol version of the Packet object
*/
u32 Packet::GetProtocolVersion() const
{
return pVersion;
Quote:
}
/***********************************************************************
*****
* STATIC FUNCTIONS
************************************************************************
***/
/**
* Identifies command that the binary data describes
data
* with packet information
* version of the Packet that the binary data
* describes
that
* the Packet object describes
*
* NOTE: The pointer to the binary data is incremented to move over
the
* part of the data that contains general Packet information,
* such as Protocol Version and Command Type. This pointer
should
* then be sent toa command-specific GetClass() function call.
* The length parameter is also adjusted accordingly.
*/
PCommands Packet::IdentifyPacket(byte **bin_data, u32 &length, u32
*pVersion)
{
PCommands Command;
//Get Protocol Version
if(length < sizeof(pVersion))
{
return BAD_COMMAND;
}
length -= sizeof(*pVersion);
memcpy(pVersion, *bin_data, sizeof(*pVersion));
*bin_data += sizeof(*pVersion);
//Get Command Type
if(length < sizeof(u32))
{
return BAD_COMMAND;
}
length -= sizeof(u32);
memcpy((u32 *)&Command, *bin_data, sizeof(u32));
*bin_data += sizeof(u32);
return Command;
Quote:
}
/**
* Checks whether a UserID represents a valid Group ID
* FALSE otherwise
*/
bool Packet::IsGroupID(u32 uid)
{
return (!IsSystemID(uid)) && ((uid & 0x80000000) == 0);
Quote:
}
/**
* Checks whether a UserID represents a valid User ID
* FALSE otherwise
*/
bool Packet::IsUserID(u32 uid)
{
return (uid & 0x80000000) != 0;
Quote:
}
/**
* Checks whether a UserID represents a valid System Administrtor ID
Administrator ID,
* FALSE otherwise
*/
bool Packet::IsSystemID(u32 uid)
{
return uid == SERVER_UID;
Quote:
}
/***********************************************************************
*****
* PRIVATE MEMBER FUNCTIONS
************************************************************************
***/
char* Packet::GetStringField(const byte *bin_data, u32 length, u32
&counter) throw(PacketException)
{
u32 len;
char *szField;
if(length < (counter + sizeof(len)) )
{
throw PacketException();
}
//Get Field Length
memcpy(&len, &bin_data[counter], sizeof(len));
counter += sizeof(len);
if(len <= 0)
{
return NULL;
}
if(length < (counter + len) )
{
throw PacketException();
}
//Get Field
szField = (char *)malloc(len);
if(szField == NULL)
{
throw PacketException();
}
memcpy(szField, &bin_data[counter], len);
counter += len;
return szField;
Quote:
}
u32 Packet::GetIntField(const byte *bin_data, u32 length, u32 &counter)
throw(PacketException)
{
u32 len;
u32 iField;
if(length < (counter + sizeof(len)) )
{
throw PacketException();
}
//Get Field Length
memcpy(&len, &bin_data[counter], sizeof(len));
counter += sizeof(len);
if(len != sizeof(iField) || (length < (counter + len)))
{
throw PacketException();
}
//Get Field
memcpy(&iField, &bin_data[counter], len);
counter += len;
return iField;
Quote:
}
bool Packet::GetBooleanField(const byte *bin_data, u32 length, u32
&counter) throw(PacketException)
{
u32 len;
bool bField;
if(length < (counter + sizeof(len)) )
{
throw PacketException();
}
//Get Field Length
memcpy(&len, &bin_data[counter], sizeof(len));
counter += sizeof(len);
if(len != sizeof(bField) || (length < (counter + len)))
{
throw PacketException();
}
//Get Field
memcpy(&bField, &bin_data[counter], len);
counter += len;
return bField;
Quote:
}
u32 Packet::SetField(byte *bin_data, const void *field, u32 length)
{
u32 counter = 0;
//Copy field length
memcpy(bin_data, &length, sizeof(length));
counter = sizeof(length);
if(length > 0)
{
//Copy field
memcpy(&bin_data[counter], field, length);
counter += length;
}
return counter;
Quote:
}
#include <vector>
using namespace std;
typedef struct
{
u32 ToUID;
char *szTo;
bool bDelivered;
Quote:
} Receivers;
typedef vector<Receivers*> RECEIVERS_VECTOR;
class MessageException: public PacketException
{
public:
//Constructors/Destructors
MessageException(): PacketException() {};
// *** Same here as above
// MessageException(MessageException &s): PacketException(s)
{};
virtual ~MessageException() {};
Quote:
};
//Client2Server/Server2Client Packet
class Message: public Packet
{
protected:
//Constructors
Message(u32 pVersion);
//Data Members
RECEIVERS_VECTOR vReceivers; //STL vector of Receivers
u32 FromUID;
char *szFrom;
char *szSubject;
u32 lTime;
char
...
read more »