2013-11-21 13:42:38 +01:00
|
|
|
#include "MessageHeader.h"
|
|
|
|
#include "../Packing.h"
|
2013-11-27 11:01:22 +01:00
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
2013-11-21 13:42:38 +01:00
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
using namespace Oyster::Network;
|
2013-11-21 13:42:38 +01:00
|
|
|
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-27 11:01:22 +01:00
|
|
|
void MessageHeader::Pack(ProtocolHeader& header, OysterByte& bytes)
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
|
|
|
size = 0;
|
2013-11-27 11:01:22 +01:00
|
|
|
|
|
|
|
PackInt(header.size, bytes);
|
|
|
|
PackInt(header.packageType, bytes);
|
|
|
|
PackInt(header.clientID, bytes);
|
|
|
|
SetSize(bytes);
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::Unpack(OysterByte& bytes, ProtocolHeader& header)
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
|
|
|
size = 0;
|
2013-11-27 11:01:22 +01:00
|
|
|
|
|
|
|
header.size = UnpackInt(bytes);
|
|
|
|
header.packageType = UnpackInt(bytes);
|
|
|
|
header.clientID = UnpackInt(bytes);
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
/**************************
|
|
|
|
Pack
|
|
|
|
**************************/
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackBool(bool i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackChar(char i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackUnsignedChar(unsigned char i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackShort(short i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackUnsignedShort(unsigned short i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackInt(int i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackUnsignedInt(unsigned int i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackInt64(__int64 i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackUnsignedInt64(unsigned __int64 i, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackFloat(float i, OysterByte& bytes)
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
|
|
|
//Pack number of elements
|
2013-11-27 11:01:22 +01:00
|
|
|
PackUnsignedInt(elementCount, bytes);
|
2013-11-25 19:00:33 +01:00
|
|
|
|
|
|
|
//Pack all elements
|
2013-11-27 11:28:11 +01:00
|
|
|
for(int j = 0; j < (int)elementCount; j++)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
PackFloat(i[j], bytes);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackDouble(double i, OysterByte& bytes)
|
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
bytes.AddSize(sizeof(i));
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], i);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackStr(char str[], OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
int totalSize = sizeof(short) + (int)strlen(str);
|
2013-11-27 11:01:22 +01:00
|
|
|
bytes.AddSize(totalSize);
|
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], str);
|
|
|
|
size += totalSize;
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::PackStr(std::string str, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-12-13 08:17:05 +01:00
|
|
|
int totalSize = sizeof(short) + (int)str.length();
|
2013-11-27 11:01:22 +01:00
|
|
|
bytes.AddSize(totalSize);
|
|
|
|
Packing::Pack(&bytes.GetByteArray()[size], str);
|
|
|
|
size += totalSize;
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 19:00:33 +01:00
|
|
|
/**************************
|
|
|
|
Unpack
|
|
|
|
**************************/
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
bool MessageHeader::UnpackBool(OysterByte& bytes)
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
bool i = Packing::Unpackb(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
char MessageHeader::UnpackChar(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
char i = Packing::Unpackc(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned char MessageHeader::UnpackUnsignedChar(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned char i = Packing::UnpackC(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
short MessageHeader::UnpackShort(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
short i = Packing::Unpacks(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned short MessageHeader::UnpackUnsignedShort(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned short i = Packing::UnpackS(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
int MessageHeader::UnpackInt(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
int i = Packing::Unpacki(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-21 13:42:38 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned int MessageHeader::UnpackUnsignedInt(OysterByte& bytes)
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned int i = Packing::UnpackI(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
__int64 MessageHeader::UnpackInt64(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
__int64 i = Packing::Unpacki64(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned __int64 MessageHeader::UnpackUnsignedInt64(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
unsigned __int64 i = Packing::UnpackI64(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
float MessageHeader::UnpackFloat(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
float i = Packing::Unpackf(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
float* MessageHeader::UnpackFloat(unsigned int& elementCount, OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
|
|
|
float* i;
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
elementCount = UnpackUnsignedInt(bytes);
|
2013-11-25 19:00:33 +01:00
|
|
|
|
|
|
|
i = new float[elementCount];
|
2013-11-27 11:28:11 +01:00
|
|
|
for(int j = 0; j < (int)elementCount; j++)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
i[j] = UnpackFloat(bytes);
|
2013-11-25 19:00:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
double MessageHeader::UnpackDouble(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
double i = Packing::Unpackd(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(i);
|
2013-11-25 19:00:33 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-12-12 12:14:53 +01:00
|
|
|
char* MessageHeader::UnpackCStr(OysterByte& bytes)
|
|
|
|
{
|
|
|
|
char* str = Packing::UnpackCStr(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(short) + (int)strlen(str);
|
2013-12-12 12:14:53 +01:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
std::string MessageHeader::UnpackStr(OysterByte& bytes)
|
2013-11-25 19:00:33 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]);
|
2013-12-13 08:17:05 +01:00
|
|
|
size += sizeof(short) + (int)str.length();
|
2013-11-25 19:00:33 +01:00
|
|
|
return str;
|
2013-11-22 15:48:49 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 11:01:22 +01:00
|
|
|
void MessageHeader::SetSize(OysterByte& bytes)
|
2013-11-22 15:48:49 +01:00
|
|
|
{
|
2013-11-27 11:01:22 +01:00
|
|
|
Packing::Pack(bytes, size);
|
2013-12-11 21:45:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::SetSize(unsigned int size)
|
|
|
|
{
|
|
|
|
this->size = size;
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|