From 623cffced662466520115e20bfbc5ed745b4e20e Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Fri, 22 Nov 2013 11:40:55 +0100 Subject: [PATCH] Added protocols to an enum Now we don't have to type cast --- Code/Network/NetworkDependencies/ITranslate.h | 2 +- Code/Network/NetworkDependencies/Protocols.h | 34 +++++++++++++++---- .../NetworkDependencies/Translator.cpp | 21 ++++++++---- Code/Network/NetworkDependencies/Translator.h | 9 +++-- .../OysterNetworkClient/ClientMain.cpp | 8 +++-- .../OysterNetworkServer/ServerMain.cpp | 18 ++++++---- 6 files changed, 66 insertions(+), 26 deletions(-) diff --git a/Code/Network/NetworkDependencies/ITranslate.h b/Code/Network/NetworkDependencies/ITranslate.h index d17325a0..63f18e68 100644 --- a/Code/Network/NetworkDependencies/ITranslate.h +++ b/Code/Network/NetworkDependencies/ITranslate.h @@ -10,7 +10,7 @@ namespace Oyster public: virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0; - virtual Protocols::ProtocolHeader* Translate ( unsigned char message[] ) = 0; + virtual Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char message[] ) = 0; }; } diff --git a/Code/Network/NetworkDependencies/Protocols.h b/Code/Network/NetworkDependencies/Protocols.h index e0bb75a1..dcddf797 100644 --- a/Code/Network/NetworkDependencies/Protocols.h +++ b/Code/Network/NetworkDependencies/Protocols.h @@ -17,7 +17,6 @@ namespace Oyster package_type_update_position }; - struct ProtocolHeader { int size; @@ -32,14 +31,37 @@ namespace Oyster std::string textMessage; ProtocolTest() { this->packageType = package_type_test; } }; - /*struct Prutt + + class ProtocolSet { + public: PackageType t; - union PRUTT + union { - ProtocolTest *ptest, - }; - };*/ + ProtocolHeader* pHeader; + ProtocolTest *pTest; + + }Protocol; + + void Release() + { + switch(t) + { + case package_type_header: + if(Protocol.pHeader) + { + delete Protocol.pHeader; + } + break; + case package_type_test: + if(Protocol.pTest) + { + delete Protocol.pTest; + } + break; + } + } + }; } } } diff --git a/Code/Network/NetworkDependencies/Translator.cpp b/Code/Network/NetworkDependencies/Translator.cpp index 2915f913..216123cb 100644 --- a/Code/Network/NetworkDependencies/Translator.cpp +++ b/Code/Network/NetworkDependencies/Translator.cpp @@ -25,26 +25,35 @@ unsigned char* Translator::Translate( ProtocolHeader &header ) return message->GetMsg(); } -ProtocolHeader* Translator::Translate(unsigned char msg[] ) +ProtocolSet* Translator::Translate(ProtocolSet* set, unsigned char msg[] ) { ProtocolHeader *header = new ProtocolHeader(); MessageHeader *message = new MessageHeader(); message->Translate(msg, *header); + delete message; + message = NULL; - switch(header->packageType) + //Switch to the correct package. + set->t = (PackageType)header->packageType; + switch(set->t) { case package_type_header: message = new MessageHeader(); - header = new ProtocolHeader(); + set->Protocol.pHeader = new ProtocolHeader; + message->Translate(msg, *set->Protocol.pHeader); break; case package_type_test: message = new MessageTest(); - header = new ProtocolTest(); + set->Protocol.pTest = new ProtocolTest; + message->Translate(msg, *set->Protocol.pTest); break; } - message->Translate(msg, *header); - return header; + if(message) + delete message; + delete header; + + return set; } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 14bf4318..95002711 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -12,11 +12,14 @@ namespace Oyster class Translator : public ITranslate { public: - Translator (){}; - ~Translator(){}; + Translator (){ }; + ~Translator(){ }; unsigned char* Translate (Protocols::ProtocolHeader &header ); - Protocols::ProtocolHeader* Translate (unsigned char msg[] ); + Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char msg[] ); + + private: + }; } } diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index c1ed48da..359ae7bd 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -63,6 +63,7 @@ void chat(Client client) unsigned char msgRecv[255] = "\0"; string msgSend = ""; + ProtocolSet* set = new ProtocolSet; ProtocolHeader* header; ProtocolTest test; @@ -72,14 +73,14 @@ void chat(Client client) { client.Recv(msgRecv); - header = t->Translate(msgRecv); + set = t->Translate(set, msgRecv); - switch(header->packageType) + switch(set->t) { case package_type_header: break; case package_type_test: - cout <<"Client 2: " <<((ProtocolTest*)header)->textMessage <Protocol.pTest->textMessage <clientID << ' ' << header->packageType << ' ' << header->size << endl; - cout << "Client1: " << ((ProtocolTest*)header)->textMessage << endl; + set = t.Translate(set, recvBuffer); + cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; + cout << "Client1: " << set->Protocol.pTest->textMessage << endl; + set->Release(); client2.Send(recvBuffer); + client2.Recv(recvBuffer); - header = t.Translate(recvBuffer); - cout << header->clientID << ' ' << header->packageType << ' ' << header->size << endl; - cout << "Client1: " << ((ProtocolTest*)header)->textMessage << endl; + set = t.Translate(set, recvBuffer); + cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; + cout << "Client2: " << set->Protocol.pTest->textMessage << endl; + set->Release(); client1.Send(recvBuffer); } ShutdownSockets(); delete[] recvBuffer; + delete set; system("pause"); return 0;