Danbias/Code/Network/NetworkAPI/CustomNetProtocol.cpp

137 lines
3.5 KiB
C++
Raw Normal View History

/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#include "CustomNetProtocol.h"
#include <map>
#include "Translator.h"
2014-01-29 10:18:01 +01:00
#include "Utilities.h"
using namespace Oyster::Network;
2014-01-29 10:18:01 +01:00
using namespace Utility::DynamicMemory;
struct CustomNetProtocol::PrivateData
{
2014-01-29 10:18:01 +01:00
std::map<int, NetAttributeContainer> attributes; //...Im an idiot
Utility::DynamicMemory::ReferenceCount *c;
PrivateData()
2014-01-29 10:18:01 +01:00
{
//this->attributes = new std::map<int, NetAttributeContainer>();
this->c = new ReferenceCount();
c->Incref();
2013-12-12 12:17:39 +01:00
}
2014-01-29 10:18:01 +01:00
~PrivateData()
{
2014-01-29 10:18:01 +01:00
delete c;
c = 0;
for (auto i = attributes.begin(); i != attributes.end(); i++)
{
RemoveAttribute(i->first);
}
2013-12-12 12:17:39 +01:00
attributes.clear();
}
void RemoveAttribute(int ID)
{
auto i = attributes.find(ID);
if(i == attributes.end()) return;
switch (i->second.type)
{
case NetAttributeType_CharArray:
delete [] i->second.value.netCharPtr;
break;
}
}
//Do network stuff
};
CustomNetProtocol::CustomNetProtocol()
{
this->privateData = new PrivateData();
}
2013-12-12 12:17:39 +01:00
CustomNetProtocol::CustomNetProtocol(const CustomNetProtocol& o)
{
2014-01-29 10:18:01 +01:00
this->privateData = o.privateData;
if(this->privateData)
{
this->privateData->c = o.privateData->c;
this->privateData->c->Incref();
}
2013-12-12 12:17:39 +01:00
}
const CustomNetProtocol& CustomNetProtocol::operator=(const CustomNetProtocol& o)
{
2014-01-29 10:18:01 +01:00
if(this->privateData && this->privateData->c)
{
if(this->privateData->c->Decref() == 0)
{
delete this->privateData;
}
}
this->privateData = o.privateData;
if(this->privateData)
{
this->privateData->c = o.privateData->c;
this->privateData->c->Incref();
}
2013-12-12 12:17:39 +01:00
return *this;
}
CustomNetProtocol::~CustomNetProtocol()
{
2014-01-29 10:18:01 +01:00
if(this->privateData && this->privateData->c)
{
if(this->privateData->c->Decref() == 0)
{
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];
}
void CustomNetProtocol::Set(int ID, Oyster::Network::NetAttributeValue val, Oyster::Network::NetAttributeType type)
{
this->privateData->attributes[ID].type = type;
switch (type)
{
case Oyster::Network::NetAttributeType_Bool:
case Oyster::Network::NetAttributeType_Char:
case Oyster::Network::NetAttributeType_UnsignedChar:
case Oyster::Network::NetAttributeType_Short:
case Oyster::Network::NetAttributeType_UnsignedShort:
case Oyster::Network::NetAttributeType_Int:
case Oyster::Network::NetAttributeType_UnsignedInt:
case Oyster::Network::NetAttributeType_Int64:
case Oyster::Network::NetAttributeType_UnsignedInt64:
case Oyster::Network::NetAttributeType_Float:
case Oyster::Network::NetAttributeType_Double:
this->privateData->attributes[ID].value = val;
break;
}
}
void CustomNetProtocol::Set(int ID, std::string s)
{
if(s.size() == 0) return;
this->privateData->attributes[ID].type = Oyster::Network::NetAttributeType_CharArray;
this->privateData->attributes[ID].value.netCharPtr = new char[s.size() + 1];
memcpy(&this->privateData->attributes[ID].value.netCharPtr[0], &s[0], s.size() + 1);
}
const NetAttributeContainer& CustomNetProtocol::Get(int id)
{
return this->privateData->attributes[id];
}