2013-11-21 13:42:38 +01:00
|
|
|
#include "MessageHeader.h"
|
|
|
|
#include "../Packing.h"
|
|
|
|
|
|
|
|
using namespace Oyster::Network::Messages;
|
2013-11-21 14:49:30 +01:00
|
|
|
using namespace Oyster::Network::Protocols;
|
2013-11-21 13:42:38 +01:00
|
|
|
|
|
|
|
MessageHeader::MessageHeader()
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageHeader::~MessageHeader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
void MessageHeader::Pack(ProtocolHeader& header, unsigned char msg[] )
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
|
|
|
size = 0;
|
2013-11-22 14:23:08 +01:00
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
PackInt(header.size, msg);
|
|
|
|
PackInt(header.packageType, msg);
|
|
|
|
PackInt(header.clientID, msg);
|
2013-11-22 15:48:49 +01:00
|
|
|
SetSize(msg);
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
void MessageHeader::Unpack(unsigned char msg[], ProtocolHeader& header)
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
header.clientID = UnpackInt(msg);
|
|
|
|
header.packageType = UnpackInt(msg);
|
|
|
|
header.size = UnpackInt(msg);
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
/**************************
|
|
|
|
Pack
|
|
|
|
**************************/
|
|
|
|
|
|
|
|
void MessageHeader::PackBool(bool i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackChar(char i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackUnsignedChar(unsigned char i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackShort(short i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackUnsignedShort(unsigned short i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackInt(int i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackUnsignedInt(unsigned int i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackInt64(__int64 i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackUnsignedInt64(unsigned __int64 i, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackFloat(float i, unsigned char msg[])
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
2013-11-25 19:00:33 +01:00
|
|
|
Packing::Pack(&msg[size], i);
|
2013-11-21 13:42:38 +01:00
|
|
|
size += 4;
|
|
|
|
}
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[])
|
|
|
|
{
|
|
|
|
//Pack number of elements
|
2013-11-25 20:27:23 +01:00
|
|
|
PackUnsignedInt(elementCount, msg);
|
2013-11-25 19:00:33 +01:00
|
|
|
|
|
|
|
//Pack all elements
|
|
|
|
for(int j = 0; j < elementCount; j++)
|
|
|
|
{
|
2013-11-25 20:27:23 +01:00
|
|
|
PackFloat(i[j], msg);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackDouble(double i, unsigned char msg[])
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
2013-11-25 19:00:33 +01:00
|
|
|
Packing::Pack(&msg[size], i);
|
|
|
|
size += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackStr(char str[], unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], str);
|
|
|
|
size += 2 + strlen(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::PackStr(std::string str, unsigned char msg[])
|
|
|
|
{
|
|
|
|
Packing::Pack(&msg[size], str);
|
2013-11-21 13:42:38 +01:00
|
|
|
size += 2 + str.length();
|
|
|
|
}
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
/**************************
|
|
|
|
Unpack
|
|
|
|
**************************/
|
|
|
|
|
|
|
|
bool MessageHeader::UnpackBool(unsigned char msg[])
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
2013-11-25 19:00:33 +01:00
|
|
|
bool i = Packing::Unpackb(&msg[size]);
|
|
|
|
size += 1;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
char MessageHeader::UnpackChar(unsigned char msg[])
|
|
|
|
{
|
|
|
|
char i = Packing::Unpackc(&msg[size]);
|
|
|
|
size += 1;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char MessageHeader::UnpackUnsignedChar(unsigned char msg[])
|
|
|
|
{
|
|
|
|
unsigned char i = Packing::UnpackC(&msg[size]);
|
|
|
|
size += 1;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
short MessageHeader::UnpackShort(unsigned char msg[])
|
|
|
|
{
|
|
|
|
short i = Packing::Unpacks(&msg[size]);
|
|
|
|
size += 2;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short MessageHeader::UnpackUnsignedShort(unsigned char msg[])
|
|
|
|
{
|
|
|
|
unsigned short i = Packing::UnpackS(&msg[size]);
|
|
|
|
size += 2;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MessageHeader::UnpackInt(unsigned char msg[])
|
|
|
|
{
|
|
|
|
int i = Packing::Unpacki(&msg[size]);
|
2013-11-21 13:42:38 +01:00
|
|
|
size += 4;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
unsigned int MessageHeader::UnpackUnsignedInt(unsigned char msg[])
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
2013-11-25 19:00:33 +01:00
|
|
|
unsigned int i = Packing::UnpackI(&msg[size]);
|
|
|
|
size += 4;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
__int64 MessageHeader::UnpackInt64(unsigned char msg[])
|
|
|
|
{
|
|
|
|
__int64 i = Packing::Unpacki64(&msg[size]);
|
|
|
|
size += 8;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned __int64 MessageHeader::UnpackUnsignedInt64(unsigned char msg[])
|
|
|
|
{
|
|
|
|
unsigned __int64 i = Packing::UnpackI64(&msg[size]);
|
|
|
|
size += 8;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
float MessageHeader::UnpackFloat(unsigned char msg[])
|
|
|
|
{
|
|
|
|
float i = Packing::Unpackf(&msg[size]);
|
|
|
|
size += 4;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
float* MessageHeader::UnpackFloat(unsigned int& elementCount, unsigned char msg[])
|
|
|
|
{
|
|
|
|
float* i;
|
|
|
|
|
2013-11-25 20:27:23 +01:00
|
|
|
elementCount = UnpackUnsignedInt(msg);
|
2013-11-25 19:00:33 +01:00
|
|
|
|
|
|
|
i = new float[elementCount];
|
|
|
|
for(int j = 0; j < elementCount; j++)
|
|
|
|
{
|
2013-11-25 20:27:23 +01:00
|
|
|
i[j] = UnpackFloat(msg);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
double MessageHeader::UnpackDouble(unsigned char msg[])
|
|
|
|
{
|
|
|
|
double i = Packing::Unpackd(&msg[size]);
|
|
|
|
size += 8;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MessageHeader::UnpackStr(unsigned char msg[])
|
|
|
|
{
|
|
|
|
std::string str = Packing::UnpackStr(&msg[size]);
|
|
|
|
size += 2 + str.length();
|
|
|
|
return str;
|
2013-11-22 15:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::SetSize(unsigned char msg[])
|
|
|
|
{
|
2013-11-25 19:00:33 +01:00
|
|
|
Packing::Pack(&msg[0], size);
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|