Danbias/Code/Network/NetworkDependencies/Translator.cpp

74 lines
1.5 KiB
C++
Raw Normal View History

2013-11-21 14:49:30 +01:00
#include "Translator.h"
using namespace Oyster::Network;
using namespace ::Protocols;
using namespace ::Messages;
void Translator::Pack( ProtocolHeader &header, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes )
2013-11-21 14:49:30 +01:00
{
MessageHeader *message = NULL;
switch(header.packageType)
{
2013-11-28 08:51:21 +01:00
case PackageType_header:
2013-11-21 14:49:30 +01:00
message = new MessageHeader();
break;
2013-11-28 08:51:21 +01:00
case PackageType_test:
2013-11-21 14:49:30 +01:00
message = new MessageTest();
break;
case PackageType_player_pos:
message = new MessagePlayerPos();
break;
2013-11-21 14:49:30 +01:00
}
2013-11-22 14:23:08 +01:00
if(message != NULL)
{
message->Pack(header, *bytes);
2013-11-22 14:31:07 +01:00
2013-11-22 14:23:08 +01:00
delete message;
message = NULL;
}
2013-11-21 14:49:30 +01:00
}
void Translator::Unpack(ProtocolSet* set, Utility::DynamicMemory::SmartPointer<OysterByte> &bytes)
2013-11-21 14:49:30 +01:00
{
2013-11-22 08:56:00 +01:00
ProtocolHeader *header = new ProtocolHeader();
2013-11-21 14:49:30 +01:00
MessageHeader *message = new MessageHeader();
message->Unpack(*bytes, *header);
delete message;
message = NULL;
2013-11-21 14:49:30 +01:00
//Switch to the correct package.
set->type = (PackageType)header->packageType;
switch(set->type)
2013-11-21 14:49:30 +01:00
{
2013-11-28 08:51:21 +01:00
case PackageType_header:
2013-11-21 14:49:30 +01:00
message = new MessageHeader();
set->Protocol.pHeader = new ProtocolHeader;
message->Unpack(*bytes, *set->Protocol.pHeader);
2013-11-21 14:49:30 +01:00
break;
2013-11-28 08:51:21 +01:00
case PackageType_test:
2013-11-21 14:49:30 +01:00
message = new MessageTest();
set->Protocol.pTest = new ProtocolTest;
message->Unpack(*bytes, *set->Protocol.pTest);
2013-11-21 14:49:30 +01:00
break;
case PackageType_player_pos:
message = new MessagePlayerPos();
set->Protocol.pPlayerPos = new ProtocolPlayerPos;
message->Unpack(*bytes, *set->Protocol.pPlayerPos);
break;
2013-11-21 14:49:30 +01:00
}
if(message)
{
delete message;
}
delete header;
//return set;
2013-11-22 08:56:00 +01:00
}