Danbias/Code/Network/NetworkAPI/Translator.cpp

232 lines
5.7 KiB
C++
Raw Normal View History

#include "Translator.h"
#include <string>
#include <map>
#include "CustomNetProtocol.h"
#include "../NetworkDependencies/Messages/MessageHeader.h"
#include "../NetworkDependencies/OysterByte.h"
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)
{
auto it = ((MyCastingStruct*)protocol.privateData)->attributes.begin();
auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end();
size = 4; //size(int)
2013-12-13 23:47:16 +01:00
bytes.AddSize(4);
message.SetSize(size);
//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);
for(int i = 0; i < (int)headerString.size(); i++)
{
2013-12-13 23:47:16 +01:00
message.PackChar(headerString.at(i), bytes);
size++;
}
message.SetSize(bytes);
}
2013-12-13 23:47:16 +01:00
void PackMessage(OysterByte &bytes, CustomNetProtocol& protocol)
{
auto it = ((MyCastingStruct*)protocol.privateData)->attributes.begin();
auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end();
for(int i = 0; i < (int)headerString.size(); i++, it++)
{
switch((int)headerString.at(i))
{
case NetAttributeType_Bool:
2013-12-13 23:47:16 +01:00
message.PackBool(it->second.value.netBool, bytes);
break;
case NetAttributeType_Char:
2013-12-13 23:47:16 +01:00
message.PackChar(it->second.value.netChar, bytes);
break;
case NetAttributeType_UnsignedChar:
2013-12-13 23:47:16 +01:00
message.PackUnsignedChar(it->second.value.netUChar, bytes);
break;
case NetAttributeType_Short:
2013-12-13 23:47:16 +01:00
message.PackShort(it->second.value.netShort, bytes);
break;
case NetAttributeType_UnsignedShort:
2013-12-13 23:47:16 +01:00
message.PackUnsignedShort(it->second.value.netUShort, bytes);
break;
case NetAttributeType_Int:
2013-12-13 23:47:16 +01:00
message.PackInt(it->second.value.netInt, bytes);
break;
case NetAttributeType_UnsignedInt:
2013-12-13 23:47:16 +01:00
message.PackUnsignedInt(it->second.value.netUInt, bytes);
break;
case NetAttributeType_Int64:
2013-12-13 23:47:16 +01:00
message.PackInt64(it->second.value.netInt64, bytes);
break;
case NetAttributeType_UnsignedInt64:
2013-12-13 23:47:16 +01:00
message.PackUnsignedInt64(it->second.value.netUInt64, bytes);
break;
case NetAttributeType_Float:
2013-12-13 23:47:16 +01:00
message.PackFloat(it->second.value.netFloat, bytes);
break;
case NetAttributeType_Double:
2013-12-13 23:47:16 +01:00
message.PackDouble(it->second.value.netDouble, bytes);
break;
case NetAttributeType_CharArray:
2013-12-13 23:47:16 +01:00
message.PackStr(it->second.value.netCharPtr, bytes);
break;
default:
numberOfUnknownTypes++;
break;
}
}
message.SetSize(bytes);
}
2013-12-13 23:47:16 +01:00
bool UnpackHeader(CustomNetProtocol& protocol, OysterByte &bytes)
{
message.SetSize(0);
2013-12-13 23:47:16 +01:00
int packageSize = message.UnpackInt(bytes);
if(packageSize != bytes.GetSize())
{
return false;
}
2013-12-13 23:47:16 +01:00
short numberOfTypes = message.UnpackShort(bytes);
for(int i = 0; i < numberOfTypes; i++)
{
2013-12-13 23:47:16 +01:00
char temp = message.UnpackChar(bytes);
headerString.push_back(temp);
}
return true;
}
2013-12-13 23:47:16 +01:00
void UnpackMessage(CustomNetProtocol& protocol, OysterByte &bytes)
{
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);
break;
case NetAttributeType_Char:
2013-12-13 23:47:16 +01:00
protocol[i].value.netChar = message.UnpackChar(bytes);
break;
case NetAttributeType_UnsignedChar:
2013-12-13 23:47:16 +01:00
protocol[i].value.netUChar = message.UnpackUnsignedChar(bytes);
break;
case NetAttributeType_Short:
2013-12-13 23:47:16 +01:00
protocol[i].value.netShort = message.UnpackShort(bytes);
break;
case NetAttributeType_UnsignedShort:
2013-12-13 23:47:16 +01:00
protocol[i].value.netUShort = message.UnpackUnsignedShort(bytes);
break;
case NetAttributeType_Int:
2013-12-13 23:47:16 +01:00
protocol[i].value.netInt = message.UnpackInt(bytes);
break;
case NetAttributeType_UnsignedInt:
2013-12-13 23:47:16 +01:00
protocol[i].value.netUInt = message.UnpackUnsignedInt(bytes);
break;
case NetAttributeType_Int64:
2013-12-13 23:47:16 +01:00
protocol[i].value.netInt64 = message.UnpackInt64(bytes);
break;
case NetAttributeType_UnsignedInt64:
2013-12-13 23:47:16 +01:00
protocol[i].value.netUInt64 = message.UnpackUnsignedInt64(bytes);
break;
case NetAttributeType_Float:
2013-12-13 23:47:16 +01:00
protocol[i].value.netFloat = message.UnpackFloat(bytes);
break;
case NetAttributeType_Double:
2013-12-13 23:47:16 +01:00
protocol[i].value.netDouble = message.UnpackDouble(bytes);
break;
case NetAttributeType_CharArray:
2013-12-13 23:47:16 +01:00
protocol[i].value.netCharPtr = message.UnpackCStr(bytes);
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-13 23:47:16 +01:00
void Translator::Pack(OysterByte &bytes, CustomNetProtocol& protocol)
{
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)
{
if(!privateData->UnpackHeader(protocol, bytes))
{
return false;
}
privateData->UnpackMessage(protocol, bytes);
return true;
}