From caabb6702e93776ade210541b2b7675fed3a6271 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Mon, 25 Nov 2013 19:00:33 +0100 Subject: [PATCH] Fixed code standard, Added packing functions --- .../NetworkDependencies/Connection.cpp | 7 +- Code/Network/NetworkDependencies/IListener.h | 25 +++ Code/Network/NetworkDependencies/ITranslate.h | 4 +- .../Listener.cpp | 2 +- Code/Network/NetworkDependencies/Listener.h | 34 +++ .../Messages/MessageHeader.cpp | 210 ++++++++++++++++-- .../Messages/MessageHeader.h | 65 +++++- .../Messages/MessageTest.cpp | 14 +- .../Messages/MessageTest.h | 11 +- .../Messages/MessagesInclude.h | 4 + .../NetworkDependencies.vcxproj | 3 + .../NetworkDependencies.vcxproj.filters | 3 + Code/Network/NetworkDependencies/Packing.cpp | 36 +-- Code/Network/NetworkDependencies/Packing.h | 6 +- Code/Network/NetworkDependencies/Protocols.h | 11 +- .../NetworkDependencies/Translator.cpp | 18 +- Code/Network/NetworkDependencies/Translator.h | 4 +- Code/Network/OysterNetworkClient/Client.h | 6 +- .../OysterNetworkClient/ClientMain.cpp | 25 ++- Code/Network/OysterNetworkServer/Client.h | 7 +- Code/Network/OysterNetworkServer/IListener.h | 16 -- Code/Network/OysterNetworkServer/Listener.h | 25 --- .../OysterNetworkServer.vcxproj | 3 - .../OysterNetworkServer.vcxproj.filters | 9 - .../OysterNetworkServer/ServerMain.cpp | 29 ++- 25 files changed, 423 insertions(+), 154 deletions(-) create mode 100644 Code/Network/NetworkDependencies/IListener.h rename Code/Network/{OysterNetworkServer => NetworkDependencies}/Listener.cpp (89%) create mode 100644 Code/Network/NetworkDependencies/Listener.h delete mode 100644 Code/Network/OysterNetworkServer/IListener.h delete mode 100644 Code/Network/OysterNetworkServer/Listener.h diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index cac38e71..6d567aca 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -96,8 +96,11 @@ bool Connection::Send(const unsigned char message[]) { int nBytes; unsigned long messageSize = strlen((char*)message); + int optlen = sizeof(int); + int optval = 0; + getsockopt(mySocket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *)&optval, &optlen); - messageSize = 255; + messageSize = 1000; nBytes = send(mySocket, (char*)message , messageSize, 0); if(nBytes == SOCKET_ERROR) { @@ -111,7 +114,7 @@ bool Connection::Send(const unsigned char message[]) int Connection::Recieve(unsigned char message[]) { int nBytes; - nBytes = recv(mySocket, (char*)message , 255, 0); + nBytes = recv(mySocket, (char*)message , 10000, 0); if(nBytes == SOCKET_ERROR) { //Recv failed diff --git a/Code/Network/NetworkDependencies/IListener.h b/Code/Network/NetworkDependencies/IListener.h new file mode 100644 index 00000000..f08cfb27 --- /dev/null +++ b/Code/Network/NetworkDependencies/IListener.h @@ -0,0 +1,25 @@ +#ifndef NETWORK_SERVER_ILISTENER_H +#define NETWORK_SERVER_ILISTENER_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +namespace Oyster +{ + namespace Network + { + namespace Server + { + class IListener + { + public: + virtual bool Init(unsigned int port) = 0; + virtual int Accept() = 0; + + }; + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/ITranslate.h b/Code/Network/NetworkDependencies/ITranslate.h index 63f18e68..30360dcc 100644 --- a/Code/Network/NetworkDependencies/ITranslate.h +++ b/Code/Network/NetworkDependencies/ITranslate.h @@ -9,8 +9,8 @@ namespace Oyster { public: - virtual unsigned char* Translate (Protocols::ProtocolHeader &header ) = 0; - virtual Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char message[] ) = 0; + virtual unsigned char* Pack (Protocols::ProtocolHeader &header ) = 0; + virtual Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char message[] ) = 0; }; } diff --git a/Code/Network/OysterNetworkServer/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp similarity index 89% rename from Code/Network/OysterNetworkServer/Listener.cpp rename to Code/Network/NetworkDependencies/Listener.cpp index 5bda043f..4e0d149f 100644 --- a/Code/Network/OysterNetworkServer/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -1,6 +1,6 @@ #include "Listener.h" -using namespace Oyster::Network; +using namespace Oyster::Network::Server; Listener::Listener() { diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h new file mode 100644 index 00000000..f72491c1 --- /dev/null +++ b/Code/Network/NetworkDependencies/Listener.h @@ -0,0 +1,34 @@ +#ifndef NETWORK_SERVER_LISTENER_H +#define NETWORK_SERVER_LISTENER_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#include "IListener.h" +#include "../NetworkDependencies/Connection.h" + +namespace Oyster +{ + namespace Network + { + namespace Server + { + class Listener + { + public: + Listener(); + ~Listener(); + + bool Init(unsigned int port); + int Accept(); + + private: + ::Oyster::Network::Connection* connection; + + }; + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp index fc26dd34..8418da77 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -2,7 +2,6 @@ #include "../Packing.h" using namespace Oyster::Network::Messages; -using namespace Oyster::Network::Packing; using namespace Oyster::Network::Protocols; MessageHeader::MessageHeader() @@ -14,52 +13,223 @@ MessageHeader::~MessageHeader() { } -void MessageHeader::Translate(ProtocolHeader& header, unsigned char msg[] ) +void MessageHeader::Pack(ProtocolHeader& header, unsigned char msg[] ) { size = 0; - AddInt(header.size, msg); - AddInt(header.packageType, msg); - AddInt(header.clientID, msg); + PackInt(header.size, msg); + PackInt(header.packageType, msg); + PackInt(header.clientID, msg); SetSize(msg); } -void MessageHeader::Translate(unsigned char message[], ProtocolHeader& header) +void MessageHeader::Unpack(unsigned char msg[], ProtocolHeader& header) { size = 0; - header.clientID = GetInt(message); - header.packageType = GetInt(message); - header.size = GetInt(message); + header.clientID = UnpackInt(msg); + header.packageType = UnpackInt(msg); + header.size = UnpackInt(msg); } -void MessageHeader::AddInt(int i, unsigned char msg[]) +/************************** + Pack +**************************/ + +void MessageHeader::PackBool(bool i, unsigned char msg[]) { - Pack(&msg[size], i); + Packing::Pack(&msg[size], i); + size += 1; +} + +void MessageHeader::PackChar(char i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); + size += 1; +} + +void MessageHeader::PackUnsignedChar(unsigned char i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); + size += 1; +} + +void MessageHeader::PackShort(short i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); + size += 2; +} + +void MessageHeader::PackUnsignedShort(unsigned short i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); + size += 2; +} + +void MessageHeader::PackInt(int i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); size += 4; } -void MessageHeader::AddStr(std::string str, unsigned char msg[]) +void MessageHeader::PackUnsignedInt(unsigned int i, unsigned char msg[]) { - Pack(&msg[size], str); + Packing::Pack(&msg[size], i); + size += 4; +} + +void MessageHeader::PackInt64(__int64 i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); + size += 8; +} + +void MessageHeader::PackUnsignedInt64(unsigned __int64 i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); + size += 8; +} + +void MessageHeader::PackFloat(float i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); + size += 4; +} + +void MessageHeader::PackFloat(float i[], unsigned int elementCount, unsigned char msg[]) +{ + //Pack number of elements + PackUnsignedInt(elementCount, &msg[size]); + + //Pack all elements + for(int j = 0; j < elementCount; j++) + { + PackFloat(i[j], &msg[size]); + } +} + +void MessageHeader::PackDouble(double i, unsigned char msg[]) +{ + Packing::Pack(&msg[size], i); + size += 8; +} + +void MessageHeader::PackStr(char str[], unsigned char msg[]) +{ + Packing::Pack(&msg[size], str); + size += 2 + strlen(str); +} + +void MessageHeader::PackStr(std::string str, unsigned char msg[]) +{ + Packing::Pack(&msg[size], str); size += 2 + str.length(); } -int MessageHeader::GetInt(unsigned char message[]) +/************************** + Unpack +**************************/ + +bool MessageHeader::UnpackBool(unsigned char msg[]) { - int i = Unpacki(&message[size]); + bool i = Packing::Unpackb(&msg[size]); + size += 1; + return i; +} + +char MessageHeader::UnpackChar(unsigned char msg[]) +{ + char i = Packing::Unpackc(&msg[size]); + size += 1; + return i; +} + +unsigned char MessageHeader::UnpackUnsignedChar(unsigned char msg[]) +{ + unsigned char i = Packing::UnpackC(&msg[size]); + size += 1; + return i; +} + +short MessageHeader::UnpackShort(unsigned char msg[]) +{ + short i = Packing::Unpacks(&msg[size]); + size += 2; + return i; +} + +unsigned short MessageHeader::UnpackUnsignedShort(unsigned char msg[]) +{ + unsigned short i = Packing::UnpackS(&msg[size]); + size += 2; + return i; +} + +int MessageHeader::UnpackInt(unsigned char msg[]) +{ + int i = Packing::Unpacki(&msg[size]); size += 4; return i; } -std::string MessageHeader::GetStr(unsigned char message[]) +unsigned int MessageHeader::UnpackUnsignedInt(unsigned char msg[]) { - std::string std = UnpackStr(&message[size]); - size += 2 + std.length(); - return std; + unsigned int i = Packing::UnpackI(&msg[size]); + size += 4; + return i; +} + +__int64 MessageHeader::UnpackInt64(unsigned char msg[]) +{ + __int64 i = Packing::Unpacki64(&msg[size]); + size += 8; + return i; +} + +unsigned __int64 MessageHeader::UnpackUnsignedInt64(unsigned char msg[]) +{ + unsigned __int64 i = Packing::UnpackI64(&msg[size]); + size += 8; + return i; +} + +float MessageHeader::UnpackFloat(unsigned char msg[]) +{ + float i = Packing::Unpackf(&msg[size]); + size += 4; + return i; +} + +float* MessageHeader::UnpackFloat(unsigned int& elementCount, unsigned char msg[]) +{ + float* i; + + elementCount = UnpackUnsignedInt(&msg[size]); + + i = new float[elementCount]; + for(int j = 0; j < elementCount; j++) + { + i[j] = UnpackFloat(&msg[size]); + } + + return i; +} + +double MessageHeader::UnpackDouble(unsigned char msg[]) +{ + double i = Packing::Unpackd(&msg[size]); + size += 8; + return i; +} + +std::string MessageHeader::UnpackStr(unsigned char msg[]) +{ + std::string str = Packing::UnpackStr(&msg[size]); + size += 2 + str.length(); + return str; } void MessageHeader::SetSize(unsigned char msg[]) { - Pack(&msg[0], size); + Packing::Pack(&msg[0], size); } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.h b/Code/Network/NetworkDependencies/Messages/MessageHeader.h index af9be77f..5852e239 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.h +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.h @@ -1,9 +1,9 @@ #ifndef NETWORK_DEPENDENCIES_MESSAGE_HEADER_H #define NETWORK_DEPENDENCIES_MESSAGE_HEADER_H -///////////////////////////////////////////////////////////////////// -// Created by Pontus Fransson 2013 -///////////////////////////////////////////////////////////////////// +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// #include #include "../Protocols.h" @@ -20,19 +20,60 @@ namespace Oyster MessageHeader(); virtual ~MessageHeader(); - virtual void Translate(Protocols::ProtocolHeader& header, unsigned char msg[] ); - virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header); - + virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[] ); + virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header); protected: - //Add variables to messages - void AddInt(int i, unsigned char msg[]); - void AddStr(std::string str, unsigned char msg[]); + //Pack variables to messages + void PackBool(bool i, unsigned char msg[]); - //Get variables from message - int GetInt(unsigned char message[]); - std::string GetStr(unsigned char message[]); + void PackChar(char i, unsigned char msg[]); + void PackUnsignedChar(unsigned char i, unsigned char msg[]); + void PackShort(short i, unsigned char msg[]); + void PackUnsignedShort(unsigned short i, unsigned char msg[]); + + void PackInt(int i, unsigned char msg[]); + void PackUnsignedInt(unsigned int i, unsigned char msg[]); + + void PackInt64(__int64 i, unsigned char msg[]); + void PackUnsignedInt64(unsigned __int64 i, unsigned char msg[]); + + void PackFloat(float i, unsigned char msg[]); + void PackFloat(float i[], unsigned int elementCount, unsigned char msg[]); + void PackDouble(double i, unsigned char msg[]); + + void PackStr(char str[], unsigned char msg[]); + void PackStr(std::string str, unsigned char msg[]); + + //TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc. + + + //Unpack variables from message + bool UnpackBool(unsigned char msg[]); + + char UnpackChar(unsigned char msg[]); + unsigned char UnpackUnsignedChar(unsigned char msg[]); + + short UnpackShort(unsigned char msg[]); + unsigned short UnpackUnsignedShort(unsigned char msg[]); + + int UnpackInt(unsigned char msg[]); + unsigned int UnpackUnsignedInt(unsigned char msg[]); + + __int64 UnpackInt64(unsigned char msg[]); + unsigned __int64 UnpackUnsignedInt64(unsigned char msg[]); + + float UnpackFloat(unsigned char msg[]); + float* UnpackFloat(unsigned int& elementCount, unsigned char msg[]); + double UnpackDouble(unsigned char msg[]); + + std::string UnpackStr(unsigned char msg[]); + + //TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc. + + + //Sets the this->size to first position in msg void SetSize(unsigned char msg[]); private: diff --git a/Code/Network/NetworkDependencies/Messages/MessageTest.cpp b/Code/Network/NetworkDependencies/Messages/MessageTest.cpp index 2d3bbe10..ce5ca474 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageTest.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageTest.cpp @@ -11,17 +11,19 @@ MessageTest::~MessageTest() { } -void MessageTest::Translate(ProtocolHeader& header, unsigned char msg[]) +void MessageTest::Pack(ProtocolHeader& header, unsigned char msg[]) { - MessageHeader::Translate(header, msg); + MessageHeader::Pack(header, msg); - AddStr(static_cast(&header)->textMessage, msg); + PackStr(static_cast(&header)->textMessage, msg); + PackFloat(static_cast(&header)->f, static_cast(&header)->numOfFloats, msg); SetSize(msg); } -void MessageTest::Translate(unsigned char message[], ProtocolHeader& header) +void MessageTest::Unpack(unsigned char msg[], ProtocolHeader& header) { - MessageHeader::Translate(message, header); + MessageHeader::Unpack(msg, header); - static_cast(&header)->textMessage = GetStr(message); + static_cast(&header)->textMessage = UnpackStr(msg); + static_cast(&header)->f = UnpackFloat(static_cast(&header)->numOfFloats, msg); } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageTest.h b/Code/Network/NetworkDependencies/Messages/MessageTest.h index 62a55f24..203fad23 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageTest.h +++ b/Code/Network/NetworkDependencies/Messages/MessageTest.h @@ -1,9 +1,9 @@ #ifndef NETWORK_DEPENDENCIES_MESSAGE_TEST_H #define NETWORK_DEPENDENCIES_MESSAGE_TEST_H -///////////////////////////////////////////////////////////////////// -// Created by Pontus Fransson 2013 -///////////////////////////////////////////////////////////////////// +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// #include "MessageHeader.h" @@ -19,11 +19,10 @@ namespace Oyster MessageTest(); virtual ~MessageTest(); - virtual void Translate(Protocols::ProtocolHeader& header, unsigned char msg[]); - virtual void Translate(unsigned char message[], Protocols::ProtocolHeader& header); + virtual void Pack(Protocols::ProtocolHeader& header, unsigned char msg[]); + virtual void Unpack(unsigned char msg[], Protocols::ProtocolHeader& header); private: - }; } } diff --git a/Code/Network/NetworkDependencies/Messages/MessagesInclude.h b/Code/Network/NetworkDependencies/Messages/MessagesInclude.h index 4e1980ba..8f4d41e3 100644 --- a/Code/Network/NetworkDependencies/Messages/MessagesInclude.h +++ b/Code/Network/NetworkDependencies/Messages/MessagesInclude.h @@ -1,6 +1,10 @@ #ifndef NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H #define NETWORK_DEPENDENCIES_MESSAGES_INCLUDE_H +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + #include "MessageHeader.h" #include "MessageTest.h" diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index c395d509..d302efe7 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -160,6 +160,7 @@ + @@ -169,6 +170,8 @@ + + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index 0bb7c6e2..f3cfc718 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -7,6 +7,7 @@ + @@ -18,5 +19,7 @@ + + \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Packing.cpp b/Code/Network/NetworkDependencies/Packing.cpp index 6c6edfda..bd827cfb 100644 --- a/Code/Network/NetworkDependencies/Packing.cpp +++ b/Code/Network/NetworkDependencies/Packing.cpp @@ -33,13 +33,13 @@ namespace Oyster void Pack(unsigned char buffer[], short i) { *buffer++ = i >> 8; - *buffer++ = i; + *buffer++ = (char)i; } void Pack(unsigned char buffer[], unsigned short i) { *buffer++ = i >> 8; - *buffer++ = i; + *buffer++ = (char)i; } //int (32-bit) @@ -62,26 +62,26 @@ namespace Oyster //__int64 (64-bit) void Pack(unsigned char buffer[], __int64 i) { - *buffer++ = i >> 56; - *buffer++ = i >> 48; - *buffer++ = i >> 40; - *buffer++ = i >> 32; - *buffer++ = i >> 24; - *buffer++ = i >> 16; - *buffer++ = i >> 8; - *buffer++ = i; + *buffer++ = (char)(i >> 56); + *buffer++ = (char)(i >> 48); + *buffer++ = (char)(i >> 40); + *buffer++ = (char)(i >> 32); + *buffer++ = (char)(i >> 24); + *buffer++ = (char)(i >> 16); + *buffer++ = (char)(i >> 8); + *buffer++ = (char)i; } void Pack(unsigned char buffer[], unsigned __int64 i) { - *buffer++ = i >> 56; - *buffer++ = i >> 48; - *buffer++ = i >> 40; - *buffer++ = i >> 32; - *buffer++ = i >> 24; - *buffer++ = i >> 16; - *buffer++ = i >> 8; - *buffer++ = i; + *buffer++ = (char)(i >> 56); + *buffer++ = (char)(i >> 48); + *buffer++ = (char)(i >> 40); + *buffer++ = (char)(i >> 32); + *buffer++ = (char)(i >> 24); + *buffer++ = (char)(i >> 16); + *buffer++ = (char)(i >> 8); + *buffer++ = (char)i; } //floating point (32, 64-bit) diff --git a/Code/Network/NetworkDependencies/Packing.h b/Code/Network/NetworkDependencies/Packing.h index 8339ba68..7a52c644 100644 --- a/Code/Network/NetworkDependencies/Packing.h +++ b/Code/Network/NetworkDependencies/Packing.h @@ -1,9 +1,9 @@ #ifndef PACKING_H #define PACKING_H -///////////////////////////////////////////////////////////////////// -// Created by Pontus Fransson 2013 -///////////////////////////////////////////////////////////////////// +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// #include diff --git a/Code/Network/NetworkDependencies/Protocols.h b/Code/Network/NetworkDependencies/Protocols.h index dcddf797..d11cd682 100644 --- a/Code/Network/NetworkDependencies/Protocols.h +++ b/Code/Network/NetworkDependencies/Protocols.h @@ -23,19 +23,24 @@ namespace Oyster int packageType; int clientID; - ProtocolHeader() { this->packageType = package_type_header; } + ProtocolHeader() { this->packageType = package_type_header; } + virtual ~ProtocolHeader() { } }; struct ProtocolTest : public ProtocolHeader { std::string textMessage; + unsigned int numOfFloats; + float *f; + ProtocolTest() { this->packageType = package_type_test; } + virtual ~ProtocolTest() { delete[] f; } }; class ProtocolSet { public: - PackageType t; + PackageType type; union { ProtocolHeader* pHeader; @@ -45,7 +50,7 @@ namespace Oyster void Release() { - switch(t) + switch(type) { case package_type_header: if(Protocol.pHeader) diff --git a/Code/Network/NetworkDependencies/Translator.cpp b/Code/Network/NetworkDependencies/Translator.cpp index 71525172..8e187c36 100644 --- a/Code/Network/NetworkDependencies/Translator.cpp +++ b/Code/Network/NetworkDependencies/Translator.cpp @@ -4,7 +4,7 @@ using namespace Oyster::Network; using namespace ::Protocols; using namespace ::Messages; -unsigned char* Translator::Translate( ProtocolHeader &header ) +unsigned char* Translator::Pack( ProtocolHeader &header ) { MessageHeader *message = NULL; @@ -21,7 +21,7 @@ unsigned char* Translator::Translate( ProtocolHeader &header ) if(message != NULL) { - message->Translate(header, this->msg); + message->Pack(header, this->msg); delete message; message = NULL; @@ -30,34 +30,36 @@ unsigned char* Translator::Translate( ProtocolHeader &header ) return msg; } -ProtocolSet* Translator::Translate(ProtocolSet* set, unsigned char msg[] ) +ProtocolSet* Translator::Unpack(ProtocolSet* set, unsigned char msg[] ) { ProtocolHeader *header = new ProtocolHeader(); MessageHeader *message = new MessageHeader(); - message->Translate(msg, *header); + message->Unpack(msg, *header); delete message; message = NULL; //Switch to the correct package. - set->t = (PackageType)header->packageType; - switch(set->t) + set->type = (PackageType)header->packageType; + switch(set->type) { case package_type_header: message = new MessageHeader(); set->Protocol.pHeader = new ProtocolHeader; - message->Translate(msg, *set->Protocol.pHeader); + message->Unpack(msg, *set->Protocol.pHeader); break; case package_type_test: message = new MessageTest(); set->Protocol.pTest = new ProtocolTest; - message->Translate(msg, *set->Protocol.pTest); + message->Unpack(msg, *set->Protocol.pTest); break; } if(message) + { delete message; + } delete header; return set; diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 26b5a56c..ef74bc34 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -15,8 +15,8 @@ namespace Oyster Translator () { msg = new unsigned char[256]; }; ~Translator() { if(msg != NULL) { delete [] this->msg; }}; - unsigned char* Translate (Protocols::ProtocolHeader &header ); - Protocols::ProtocolSet* Translate (Protocols::ProtocolSet* set, unsigned char msg[] ); + unsigned char* Pack (Protocols::ProtocolHeader &header ); + Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, unsigned char msg[] ); private: unsigned char* msg; diff --git a/Code/Network/OysterNetworkClient/Client.h b/Code/Network/OysterNetworkClient/Client.h index a48e37d9..0929d721 100644 --- a/Code/Network/OysterNetworkClient/Client.h +++ b/Code/Network/OysterNetworkClient/Client.h @@ -1,9 +1,9 @@ #ifndef NETWORK_CLIENT_CLIENT_H #define NETWORK_CLIENT_CLIENT_H -///////////////////////////////////////////////////////////////////// -// Created by Pontus Fransson 2013 -///////////////////////////////////////////////////////////////////// +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// #include "../NetworkDependencies/Connection.h" diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 47106f74..1976d1fe 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -27,7 +27,7 @@ int main() Client client; //Connect to server - client.Connect(9876, "10.0.0.3"); + client.Connect(9876, "localhost"); chat(client); @@ -52,12 +52,19 @@ void chat(Client &client) { Oyster::Network::Translator *t = new Oyster::Network::Translator(); - unsigned char msgRecv[256] = "\0"; + unsigned char msgRecv[2560] = "\0"; string msgSend = ""; ProtocolSet* set = new ProtocolSet; - ProtocolHeader* header; ProtocolTest test; + test.numOfFloats = 5; + test.f = new float[test.numOfFloats]; + float temp = 12345.5654f; + for(int i = 0; i < 5; i++) + { + test.f[i] = temp; + temp++; + } bool chatDone = false; @@ -65,20 +72,26 @@ void chat(Client &client) { client.Recv(msgRecv); - t->Translate(set, msgRecv); + t->Unpack(set, msgRecv); - switch(set->t) + switch(set->type) { case package_type_header: break; case package_type_test: cout <<"Client 2: " << set->Protocol.pTest->textMessage <Protocol.pTest->numOfFloats; i++) + { + cout << set->Protocol.pTest->f[i] << ' ' ; + } break; } set->Release(); std::getline(std::cin, msgSend); + + if( msgSend != "exit") { @@ -91,7 +104,7 @@ void chat(Client &client) test.size = msgSend.length(); test.textMessage = msgSend; - unsigned char *message = t->Translate(test); + unsigned char *message = t->Pack(test); client.Send(message); } diff --git a/Code/Network/OysterNetworkServer/Client.h b/Code/Network/OysterNetworkServer/Client.h index 05215b31..0e3a6c29 100644 --- a/Code/Network/OysterNetworkServer/Client.h +++ b/Code/Network/OysterNetworkServer/Client.h @@ -1,9 +1,9 @@ #ifndef NETWORK_SERVER_CLIENT_H #define NETWORK_SERVER_CLIENT_H -///////////////////////////////////////////////////////////////////// -// Created by Pontus Fransson 2013 -///////////////////////////////////////////////////////////////////// +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// #include "../NetworkDependencies/Connection.h" @@ -15,7 +15,6 @@ namespace Oyster { class Client { - public: Client(unsigned int socket); ~Client(); diff --git a/Code/Network/OysterNetworkServer/IListener.h b/Code/Network/OysterNetworkServer/IListener.h deleted file mode 100644 index 93b7a935..00000000 --- a/Code/Network/OysterNetworkServer/IListener.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef NETWORK_SERVER_ILISTENER_H -#define NETWORK_SERVER_ILISTENER_H - -///////////////////////////////////////////////////////////////////// -// Created by Pontus Fransson 2013 -///////////////////////////////////////////////////////////////////// - -class IListener -{ -public: - virtual bool Init(unsigned int port) = 0; - virtual int Accept() = 0; - -}; - -#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/Listener.h b/Code/Network/OysterNetworkServer/Listener.h deleted file mode 100644 index e621a0bd..00000000 --- a/Code/Network/OysterNetworkServer/Listener.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef NETWORK_SERVER_LISTENER_H -#define NETWORK_SERVER_LISTENER_H - -///////////////////////////////////////////////////////////////////// -// Created by Pontus Fransson 2013 -///////////////////////////////////////////////////////////////////// - -#include "IListener.h" -#include "../NetworkDependencies/Connection.h" - -class Listener -{ -public: - Listener(); - ~Listener(); - - bool Init(unsigned int port); - int Accept(); - -private: - ::Oyster::Network::Connection* connection; - -}; - -#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj index 855b323e..260586da 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -158,13 +158,10 @@ - - - diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters index 1e38f641..3cb5827c 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters @@ -21,19 +21,10 @@ Source Files - - Source Files - Header Files - - Header Files - - - Header Files - \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 2f954b69..2dfbc875 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -1,8 +1,9 @@ #include #include #include -#include "Listener.h" +#include "../NetworkDependencies/Listener.h" #include "Client.h" +#include "../NetworkDependencies/Packing.h" using namespace std; using namespace Oyster::Network::Server; @@ -17,7 +18,7 @@ using namespace ::Protocols; int main() { - unsigned char* recvBuffer = new unsigned char[256]; + unsigned char* recvBuffer = new unsigned char[5000]; cout << "Server" << endl; Translator t; @@ -45,7 +46,15 @@ int main() ProtocolTest test; test.clientID = 0; test.textMessage = "hej"; - recvBuffer = t.Translate(test); + test.numOfFloats = 500; + test.f = new float[test.numOfFloats]; + float temp = 500.456f; + for(int i = 0; i < test.numOfFloats; i++) + { + test.f[i] = temp; + temp--; + } + recvBuffer = t.Pack(test); client1.Send(recvBuffer); @@ -53,17 +62,27 @@ int main() { client1.Recv(recvBuffer); - t.Translate(set, recvBuffer); + t.Unpack(set, recvBuffer); cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; cout << "Client1: " << set->Protocol.pTest->textMessage << endl; + for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++) + { + cout << set->Protocol.pTest->f[i] << ' '; + } + cout << endl; set->Release(); client2.Send(recvBuffer); client2.Recv(recvBuffer); - t.Translate(set, recvBuffer); + t.Unpack(set, recvBuffer); cout << set->Protocol.pTest->clientID << ' ' << set->Protocol.pTest->packageType << ' ' << set->Protocol.pTest->size << endl; cout << "Client2: " << set->Protocol.pTest->textMessage << endl; + for(int i = 0; i < set->Protocol.pTest->numOfFloats; i++) + { + cout << set->Protocol.pTest->f[i] << ' '; + } + cout << endl; set->Release(); client1.Send(recvBuffer); }