2013-11-21 13:42:38 +01:00
|
|
|
#include "MessageHeader.h"
|
|
|
|
#include "../Packing.h"
|
|
|
|
|
|
|
|
using namespace Oyster::Network::Messages;
|
|
|
|
using namespace Oyster::Network::Packing;
|
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;
|
|
|
|
msg = new unsigned char[1024];
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageHeader::~MessageHeader()
|
|
|
|
{
|
|
|
|
delete[] msg;
|
|
|
|
}
|
|
|
|
|
2013-11-21 14:49:30 +01:00
|
|
|
void MessageHeader::Translate(ProtocolHeader& header)
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
|
2013-11-21 14:49:30 +01:00
|
|
|
AddInt(header.clientID);
|
|
|
|
AddInt(header.packageType);
|
|
|
|
AddInt(header.size);
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-21 14:49:30 +01:00
|
|
|
void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header)
|
2013-11-21 13:42:38 +01:00
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
|
2013-11-21 14:49:30 +01:00
|
|
|
header.clientID = GetInt(message);
|
|
|
|
header.packageType = GetInt(message);
|
|
|
|
header.size = GetInt(message);
|
2013-11-21 13:42:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* MessageHeader::GetMsg()
|
|
|
|
{
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::AddInt(int i)
|
|
|
|
{
|
|
|
|
Pack(&msg[size], i);
|
|
|
|
size += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageHeader::AddStr(std::string str)
|
|
|
|
{
|
|
|
|
Pack(&msg[size], str);
|
|
|
|
size += 2 + str.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
int MessageHeader::GetInt(unsigned char message[])
|
|
|
|
{
|
|
|
|
int i = Unpacki(&message[size]);
|
|
|
|
size += 4;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MessageHeader::GetStr(unsigned char message[])
|
|
|
|
{
|
|
|
|
std::string std = UnpackStr(&message[size]);
|
|
|
|
size += 2 + std.length();
|
|
|
|
return std;
|
|
|
|
}
|