2013-12-11 21:45:43 +01:00
|
|
|
#include "Translator.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "CustomNetProtocol.h"
|
|
|
|
|
|
|
|
#include "../NetworkDependencies/Messages/MessageHeader.h"
|
|
|
|
#include "../NetworkDependencies/OysterByte.h"
|
|
|
|
|
2013-12-12 10:32:43 +01:00
|
|
|
|
2013-12-11 21:45:43 +01:00
|
|
|
using namespace Oyster::Network;
|
|
|
|
using namespace ::Messages;
|
|
|
|
using namespace Utility::DynamicMemory;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
struct MyCastingStruct
|
|
|
|
{
|
|
|
|
std::map<int, NetAttributeContainer> attributes;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Check if the package has been packed correctly.
|
|
|
|
struct Translator::PrivateData
|
|
|
|
{
|
|
|
|
PrivateData()
|
|
|
|
{
|
|
|
|
numberOfUnknownTypes = 0;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Packages a header with a size(int) and a string of characters(char)
|
2013-12-13 23:47:16 +01:00
|
|
|
void PackHeader(OysterByte &bytes, CustomNetProtocol& protocol)
|
2013-12-11 21:45:43 +01:00
|
|
|
{
|
|
|
|
auto it = ((MyCastingStruct*)protocol.privateData)->attributes.begin();
|
|
|
|
auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end();
|
|
|
|
|
2013-12-12 14:37:35 +01:00
|
|
|
size = 4; //size(int)
|
2013-12-14 22:54:24 +01:00
|
|
|
message.PackInt(size, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
|
|
|
|
//Find all the data types
|
|
|
|
for(; it != end; it++)
|
|
|
|
{
|
|
|
|
headerString.push_back(it->second.type);
|
|
|
|
}
|
|
|
|
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackShort(size, bytes);
|
2013-12-12 14:14:48 +01:00
|
|
|
size += 2;
|
2013-12-11 21:45:43 +01:00
|
|
|
|
|
|
|
for(int i = 0; i < (int)headerString.size(); i++)
|
|
|
|
{
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackChar(headerString.at(i), bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.SetSize(bytes);
|
|
|
|
}
|
|
|
|
|
2013-12-13 23:47:16 +01:00
|
|
|
void PackMessage(OysterByte &bytes, CustomNetProtocol& protocol)
|
2013-12-11 21:45:43 +01:00
|
|
|
{
|
|
|
|
auto it = ((MyCastingStruct*)protocol.privateData)->attributes.begin();
|
|
|
|
auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end();
|
|
|
|
|
2013-12-12 14:37:35 +01:00
|
|
|
for(int i = 0; i < (int)headerString.size(); i++, it++)
|
2013-12-11 21:45:43 +01:00
|
|
|
{
|
|
|
|
switch((int)headerString.at(i))
|
|
|
|
{
|
|
|
|
case NetAttributeType_Bool:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackBool(it->second.value.netBool, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Char:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackChar(it->second.value.netChar, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_UnsignedChar:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackUnsignedChar(it->second.value.netUChar, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Short:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackShort(it->second.value.netShort, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_UnsignedShort:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackUnsignedShort(it->second.value.netUShort, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Int:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackInt(it->second.value.netInt, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_UnsignedInt:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackUnsignedInt(it->second.value.netUInt, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Int64:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackInt64(it->second.value.netInt64, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_UnsignedInt64:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackUnsignedInt64(it->second.value.netUInt64, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Float:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackFloat(it->second.value.netFloat, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Double:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackDouble(it->second.value.netDouble, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_CharArray:
|
2013-12-13 23:47:16 +01:00
|
|
|
message.PackStr(it->second.value.netCharPtr, bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
numberOfUnknownTypes++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
message.SetSize(bytes);
|
|
|
|
}
|
|
|
|
|
2013-12-13 23:47:16 +01:00
|
|
|
bool UnpackHeader(CustomNetProtocol& protocol, OysterByte &bytes)
|
2013-12-11 21:45:43 +01:00
|
|
|
{
|
|
|
|
message.SetSize(0);
|
2013-12-13 23:47:16 +01:00
|
|
|
int packageSize = message.UnpackInt(bytes);
|
|
|
|
if(packageSize != bytes.GetSize())
|
2013-12-11 21:45:43 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-13 23:47:16 +01:00
|
|
|
short numberOfTypes = message.UnpackShort(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
|
|
|
|
for(int i = 0; i < numberOfTypes; i++)
|
|
|
|
{
|
2013-12-13 23:47:16 +01:00
|
|
|
char temp = message.UnpackChar(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
headerString.push_back(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-13 23:47:16 +01:00
|
|
|
void UnpackMessage(CustomNetProtocol& protocol, OysterByte &bytes)
|
2013-12-11 21:45:43 +01:00
|
|
|
{
|
|
|
|
for(int i = 0; i < (int)headerString.size(); i++)
|
|
|
|
{
|
|
|
|
protocol[i].type = (NetAttributeType)headerString.at(i);
|
|
|
|
switch(protocol[i].type)
|
|
|
|
{
|
|
|
|
case NetAttributeType_Bool:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netBool = message.UnpackBool(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Char:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netChar = message.UnpackChar(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_UnsignedChar:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netUChar = message.UnpackUnsignedChar(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Short:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netShort = message.UnpackShort(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_UnsignedShort:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netUShort = message.UnpackUnsignedShort(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Int:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netInt = message.UnpackInt(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_UnsignedInt:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netUInt = message.UnpackUnsignedInt(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Int64:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netInt64 = message.UnpackInt64(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_UnsignedInt64:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netUInt64 = message.UnpackUnsignedInt64(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Float:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netFloat = message.UnpackFloat(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_Double:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netDouble = message.UnpackDouble(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
case NetAttributeType_CharArray:
|
2013-12-13 23:47:16 +01:00
|
|
|
protocol[i].value.netCharPtr = message.UnpackCStr(bytes);
|
2013-12-11 21:45:43 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
numberOfUnknownTypes++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageHeader message;
|
|
|
|
string headerString;
|
|
|
|
unsigned int size;
|
|
|
|
|
|
|
|
int numberOfUnknownTypes;
|
|
|
|
};
|
|
|
|
|
|
|
|
Translator::Translator()
|
|
|
|
{
|
|
|
|
privateData = new PrivateData();
|
|
|
|
}
|
|
|
|
|
|
|
|
Translator::~Translator()
|
|
|
|
{
|
|
|
|
if(privateData)
|
|
|
|
{
|
|
|
|
delete privateData;
|
|
|
|
privateData = NULL;
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 12:17:39 +01:00
|
|
|
Translator::Translator(const Translator& obj)
|
|
|
|
{
|
|
|
|
this->privateData = new PrivateData(*obj.privateData);
|
|
|
|
}
|
|
|
|
const Translator& Translator::operator=(const Translator& obj)
|
|
|
|
{
|
|
|
|
delete this->privateData;
|
|
|
|
this->privateData = new PrivateData(*obj.privateData);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-12-11 21:45:43 +01:00
|
|
|
|
2013-12-13 23:47:16 +01:00
|
|
|
void Translator::Pack(OysterByte &bytes, CustomNetProtocol& protocol)
|
2013-12-11 21:45:43 +01:00
|
|
|
{
|
|
|
|
privateData->headerString.clear();
|
|
|
|
|
|
|
|
privateData->PackHeader(bytes, protocol);
|
|
|
|
privateData->PackMessage(bytes, protocol);
|
|
|
|
}
|
|
|
|
|
2013-12-13 23:47:16 +01:00
|
|
|
bool Translator::Unpack(CustomNetProtocol& protocol, OysterByte &bytes)
|
2013-12-11 21:45:43 +01:00
|
|
|
{
|
|
|
|
if(!privateData->UnpackHeader(protocol, bytes))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
privateData->UnpackMessage(protocol, bytes);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|