2013-12-09 14:23:30 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Created by [Dennis Andersen] [2013]
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
#include "CustomNetProtocol.h"
|
|
|
|
#include <map>
|
2013-12-11 21:45:43 +01:00
|
|
|
#include "Translator.h"
|
|
|
|
using namespace Oyster::Network;
|
2013-12-09 14:23:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct CustomNetProtocol::PrivateData
|
|
|
|
{
|
|
|
|
std::map<int, NetAttributeContainer> attributes;
|
|
|
|
|
|
|
|
PrivateData()
|
|
|
|
{ }
|
2013-12-12 12:17:39 +01:00
|
|
|
PrivateData( const CustomNetProtocol::PrivateData& o)
|
|
|
|
{
|
|
|
|
for (auto i = o.attributes.begin(); i != o.attributes.end(); i++)
|
|
|
|
{
|
|
|
|
if(i->second.type == NetAttributeType_CharArray)
|
|
|
|
{
|
|
|
|
size_t size = strlen(i->second.value.netCharPtr);
|
|
|
|
if(size == 0) continue;
|
|
|
|
|
|
|
|
attributes[i->first];
|
|
|
|
attributes[i->first].value.netCharPtr = new char[size + 1];
|
|
|
|
//strcpy_s(attributes[i->first].value.netCharPtr, size + 1, i->second.value.netCharPtr);
|
|
|
|
memcpy(&attributes[i->first].value.netCharPtr[0], &i->second.value.netCharPtr[0], size + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
attributes[i->first] = i->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-09 14:23:30 +01:00
|
|
|
~PrivateData()
|
|
|
|
{
|
|
|
|
for (auto i = attributes.begin(); i != attributes.end(); i++)
|
|
|
|
{
|
|
|
|
RemoveAttribute(i->first);
|
|
|
|
}
|
2013-12-12 12:17:39 +01:00
|
|
|
attributes.clear();
|
2013-12-09 14:23:30 +01:00
|
|
|
}
|
|
|
|
void RemoveAttribute(int ID)
|
|
|
|
{
|
|
|
|
auto i = attributes.find(ID);
|
|
|
|
if(i == attributes.end()) return;
|
|
|
|
|
|
|
|
switch (i->second.type)
|
|
|
|
{
|
|
|
|
case NetAttributeType_CharArray:
|
2013-12-12 12:17:39 +01:00
|
|
|
//delete [] i->second.value.netCharPtr;
|
|
|
|
i->second.value.netCharPtr = 0;
|
2013-12-09 14:23:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Do network stuff
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CustomNetProtocol::CustomNetProtocol()
|
|
|
|
{
|
|
|
|
this->privateData = new PrivateData();
|
|
|
|
}
|
2013-12-12 12:17:39 +01:00
|
|
|
CustomNetProtocol::CustomNetProtocol(const CustomNetProtocol& o)
|
|
|
|
{
|
|
|
|
this->privateData = new PrivateData(*o.privateData);
|
|
|
|
}
|
|
|
|
const CustomNetProtocol& CustomNetProtocol::operator=(const CustomNetProtocol& o)
|
|
|
|
{
|
|
|
|
delete this->privateData;
|
|
|
|
this->privateData = new PrivateData(*o.privateData);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-12-09 14:23:30 +01:00
|
|
|
CustomNetProtocol::~CustomNetProtocol()
|
|
|
|
{
|
|
|
|
delete this->privateData;
|
|
|
|
}
|
|
|
|
NetAttributeContainer& CustomNetProtocol::operator[](int ID)
|
|
|
|
{
|
|
|
|
if(this->privateData->attributes.find(ID) == this->privateData->attributes.end())
|
|
|
|
{
|
|
|
|
this->privateData->attributes[ID];
|
|
|
|
this->privateData->attributes[ID].type = NetAttributeType_UNKNOWN;
|
|
|
|
memset(&this->privateData->attributes[ID].value, 0, sizeof(NetAttributeValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->privateData->attributes[ID];
|
2013-12-11 21:45:43 +01:00
|
|
|
}
|