Danbias/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp

235 lines
4.4 KiB
C++
Raw Normal View History

#include "MessageHeader.h"
#include "../Packing.h"
using namespace Oyster::Network::Messages;
2013-11-21 14:49:30 +01:00
using namespace Oyster::Network::Protocols;
MessageHeader::MessageHeader()
{
size = 0;
}
MessageHeader::~MessageHeader()
{
}
void MessageHeader::Pack(ProtocolHeader& header, unsigned char msg[] )
{
size = 0;
2013-11-22 14:23:08 +01:00
PackInt(header.size, msg);
PackInt(header.packageType, msg);
PackInt(header.clientID, msg);
2013-11-22 15:48:49 +01:00
SetSize(msg);
}
void MessageHeader::Unpack(unsigned char msg[], ProtocolHeader& header)
{
size = 0;
header.clientID = UnpackInt(msg);
header.packageType = UnpackInt(msg);
header.size = UnpackInt(msg);
}
/**************************
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[])
{
Packing::Pack(&msg[size], i);
size += 4;
}
void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[])
{
//Pack number of elements
PackUnsignedInt(elementCount, &msg[size]);
//Pack all elements
for(int j = 0; j < elementCount; j++)
{
PackFloat(i[j], &msg[size]);
}
}
void MessageHeader::PackDouble(double i, unsigned char msg[])
{
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);
size += 2 + str.length();
}
/**************************
Unpack
**************************/
bool MessageHeader::UnpackBool(unsigned char msg[])
{
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]);
size += 4;
return i;
}
unsigned int MessageHeader::UnpackUnsignedInt(unsigned char msg[])
{
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;
elementCount = UnpackUnsignedInt(&msg[size]);
i = new float[elementCount];
for(int j = 0; j < elementCount; j++)
{
i[j] = UnpackFloat(&msg[size]);
}
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[])
{
Packing::Pack(&msg[0], size);
}