From 2ddb984a2b4a1f5bf926b9cb27e3a0e9a751f9ec Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Fri, 13 Dec 2013 08:17:05 +0100 Subject: [PATCH] Network - Fixed bugg, and small fixed Using sizeof in MessageHeader class. Fixed bugg in Translator. --- Code/Network/NetworkAPI/Translator.cpp | 3 +- Code/Network/NetworkDependencies/Listener.cpp | 1 - .../Messages/MessageHeader.cpp | 74 +++++++-------- .../NetworkDependencies/OysterByte.cpp | 1 - Code/Network/NetworkDependencies/OysterByte.h | 16 +++- .../OysterNetworkClient/ClientMain.cpp | 92 ++----------------- .../OysterNetworkClient.vcxproj | 6 +- .../OysterNetworkServer.vcxproj | 4 - .../OysterNetworkServer.vcxproj.filters | 8 -- .../OysterNetworkServer/ServerMain.cpp | 1 - .../Network/OysterNetworkServer/TestClass.cpp | 84 ----------------- Code/Network/OysterNetworkServer/TestClass.h | 38 -------- 12 files changed, 61 insertions(+), 267 deletions(-) delete mode 100644 Code/Network/OysterNetworkServer/TestClass.cpp delete mode 100644 Code/Network/OysterNetworkServer/TestClass.h diff --git a/Code/Network/NetworkAPI/Translator.cpp b/Code/Network/NetworkAPI/Translator.cpp index d5e8d0df..e331f1aa 100644 --- a/Code/Network/NetworkAPI/Translator.cpp +++ b/Code/Network/NetworkAPI/Translator.cpp @@ -57,8 +57,7 @@ struct Translator::PrivateData auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end(); size = 4; //size(int) - bytes->AddSize(4); - message.SetSize(size); + message.PackInt(size, *bytes); //Find all the data types for(; it != end; it++) diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index 4bb22609..b82694d3 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -17,7 +17,6 @@ Listener::Listener(Oyster::Network::IPostBox* postBox) Listener::~Listener() { - Stop(); if(connection) { delete connection; diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp index 7f7f1771..d2823b62 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -41,72 +41,72 @@ void MessageHeader::Unpack(OysterByte& bytes, ProtocolHeader& header) void MessageHeader::PackBool(bool i, OysterByte& bytes) { - bytes.AddSize(1); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 1; + size += sizeof(i); } void MessageHeader::PackChar(char i, OysterByte& bytes) { - bytes.AddSize(1); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 1; + size += sizeof(i); } void MessageHeader::PackUnsignedChar(unsigned char i, OysterByte& bytes) { - bytes.AddSize(1); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 1; + size += sizeof(i); } void MessageHeader::PackShort(short i, OysterByte& bytes) { - bytes.AddSize(2); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 2; + size += sizeof(i); } void MessageHeader::PackUnsignedShort(unsigned short i, OysterByte& bytes) { - bytes.AddSize(2); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 2; + size += sizeof(i); } void MessageHeader::PackInt(int i, OysterByte& bytes) { - bytes.AddSize(4); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 4; + size += sizeof(i); } void MessageHeader::PackUnsignedInt(unsigned int i, OysterByte& bytes) { - bytes.AddSize(4); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 4; + size += sizeof(i); } void MessageHeader::PackInt64(__int64 i, OysterByte& bytes) { - bytes.AddSize(8); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 8; + size += sizeof(i); } void MessageHeader::PackUnsignedInt64(unsigned __int64 i, OysterByte& bytes) { - bytes.AddSize(8); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 8; + size += sizeof(i); } void MessageHeader::PackFloat(float i, OysterByte& bytes) { - bytes.AddSize(4); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 4; + size += sizeof(i); } void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes) @@ -123,14 +123,14 @@ void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& void MessageHeader::PackDouble(double i, OysterByte& bytes) { - bytes.AddSize(8); + bytes.AddSize(sizeof(i)); Packing::Pack(&bytes.GetByteArray()[size], i); - size += 8; + size += sizeof(i); } void MessageHeader::PackStr(char str[], OysterByte& bytes) { - int totalSize = 2 + (int)strlen(str); + int totalSize = sizeof(short) + (int)strlen(str); bytes.AddSize(totalSize); Packing::Pack(&bytes.GetByteArray()[size], str); size += totalSize; @@ -138,7 +138,7 @@ void MessageHeader::PackStr(char str[], OysterByte& bytes) void MessageHeader::PackStr(std::string str, OysterByte& bytes) { - int totalSize = 2 + (int)str.length(); + int totalSize = sizeof(short) + (int)str.length(); bytes.AddSize(totalSize); Packing::Pack(&bytes.GetByteArray()[size], str); size += totalSize; @@ -151,70 +151,70 @@ void MessageHeader::PackStr(std::string str, OysterByte& bytes) bool MessageHeader::UnpackBool(OysterByte& bytes) { bool i = Packing::Unpackb(&bytes.GetByteArray()[size]); - size += 1; + size += sizeof(i); return i; } char MessageHeader::UnpackChar(OysterByte& bytes) { char i = Packing::Unpackc(&bytes.GetByteArray()[size]); - size += 1; + size += sizeof(i); return i; } unsigned char MessageHeader::UnpackUnsignedChar(OysterByte& bytes) { unsigned char i = Packing::UnpackC(&bytes.GetByteArray()[size]); - size += 1; + size += sizeof(i); return i; } short MessageHeader::UnpackShort(OysterByte& bytes) { short i = Packing::Unpacks(&bytes.GetByteArray()[size]); - size += 2; + size += sizeof(i); return i; } unsigned short MessageHeader::UnpackUnsignedShort(OysterByte& bytes) { unsigned short i = Packing::UnpackS(&bytes.GetByteArray()[size]); - size += 2; + size += sizeof(i); return i; } int MessageHeader::UnpackInt(OysterByte& bytes) { int i = Packing::Unpacki(&bytes.GetByteArray()[size]); - size += 4; + size += sizeof(i); return i; } unsigned int MessageHeader::UnpackUnsignedInt(OysterByte& bytes) { unsigned int i = Packing::UnpackI(&bytes.GetByteArray()[size]); - size += 4; + size += sizeof(i); return i; } __int64 MessageHeader::UnpackInt64(OysterByte& bytes) { __int64 i = Packing::Unpacki64(&bytes.GetByteArray()[size]); - size += 8; + size += sizeof(i); return i; } unsigned __int64 MessageHeader::UnpackUnsignedInt64(OysterByte& bytes) { unsigned __int64 i = Packing::UnpackI64(&bytes.GetByteArray()[size]); - size += 8; + size += sizeof(i); return i; } float MessageHeader::UnpackFloat(OysterByte& bytes) { float i = Packing::Unpackf(&bytes.GetByteArray()[size]); - size += 4; + size += sizeof(i); return i; } @@ -236,21 +236,21 @@ float* MessageHeader::UnpackFloat(unsigned int& elementCount, OysterByte& bytes) double MessageHeader::UnpackDouble(OysterByte& bytes) { double i = Packing::Unpackd(&bytes.GetByteArray()[size]); - size += 8; + size += sizeof(i); return i; } char* MessageHeader::UnpackCStr(OysterByte& bytes) { char* str = Packing::UnpackCStr(&bytes.GetByteArray()[size]); - size += 2 + (int)strlen(str); + size += sizeof(short) + (int)strlen(str); return str; } std::string MessageHeader::UnpackStr(OysterByte& bytes) { std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]); - size += 2 + (int)str.length(); + size += sizeof(short) + (int)str.length(); return str; } diff --git a/Code/Network/NetworkDependencies/OysterByte.cpp b/Code/Network/NetworkDependencies/OysterByte.cpp index db343e29..99c88d44 100644 --- a/Code/Network/NetworkDependencies/OysterByte.cpp +++ b/Code/Network/NetworkDependencies/OysterByte.cpp @@ -18,7 +18,6 @@ OysterByte::OysterByte(int cap) OysterByte::OysterByte(const OysterByte& obj) { - //delete[] this->byteArray; this->byteArray = new unsigned char[obj.capacity]; for(int i = 0; i < (int)obj.size; i++) diff --git a/Code/Network/NetworkDependencies/OysterByte.h b/Code/Network/NetworkDependencies/OysterByte.h index 99629bfc..e525095b 100644 --- a/Code/Network/NetworkDependencies/OysterByte.h +++ b/Code/Network/NetworkDependencies/OysterByte.h @@ -19,24 +19,30 @@ namespace Oyster OysterByte(const OysterByte& obj); virtual ~OysterByte(); - void Clear(); //Resets size to 0 - void Resize(unsigned int cap); //Resizes the array with, it does not keep anything in it. + //Resets size to 0 + void Clear(); + + //Resizes the array with, it does not keep anything in it. + void Resize(unsigned int cap); int GetSize(); unsigned char* GetByteArray(); void AddSize(unsigned int size); void SetBytes(unsigned char* bytes); - void SetSize(unsigned int size); //Only sets the private variable 'size' + + //Only sets the private variable 'size' + void SetSize(unsigned int size); OysterByte& operator =(const OysterByte& obj); operator char*(); operator const char*(); operator unsigned char*(); - + private: - void IncreaseCapacity(unsigned int cap); //Expands the byteArray + //Expands the byteArray + void IncreaseCapacity(unsigned int cap); private: diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index bc70190d..2b75c92d 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -8,6 +8,7 @@ #include "../NetworkDependencies/ThreadedClient.h" #include "../../Misc/WinTimer.h" #include "../../Misc/Utilities.h" +#include "../NetworkAPI/NetworkClient.h" #pragma comment(lib, "ws2_32.lib") @@ -20,6 +21,11 @@ using namespace Utility::DynamicMemory; void chat(ThreadedClient &client); void PrintOutMessage(ProtocolSet* set); +void proc(CustomNetProtocol& protocol) +{ + +} + int main() { int errorCode; @@ -31,10 +37,11 @@ int main() cout << "Client" << endl; //Create Client - ThreadedClient* client = new ThreadedClient; + NetworkClient client; //Connect to server - errorCode = client->Connect(15151, "193.11.186.101"); + errorCode = client.Connect(15151, "193.11.186.101"); + client.SetRecieverObject(proc, NetworkProtocolCallbackType_Function); if(errorCode != 0) { @@ -42,94 +49,13 @@ int main() wcout << "errorMessage: " << errorTest << endl; } - chat(*client); - while(1) { } - delete client; - ShutdownWinSock(); system("pause"); return 0; -} - -void chat(ThreadedClient &client) -{ - /*Oyster::Network::Translator *t = new Oyster::Network::Translator(); - IPostBox< SmartPointer> *postBox = new PostBox< SmartPointer>; - - //client.setRecvPostBox(postBox); - - SmartPointer msgRecv = new OysterByte(); - SmartPointer msgSend = new OysterByte(); - - ProtocolSet* set = new ProtocolSet; - ProtocolPlayerPos test; - test.ID = 5; - test.nrOfFloats = 5; - test.matrix = new float[test.nrOfFloats]; - float temp = 10; - for(int i = 0; i < (int)test.nrOfFloats; i++) - { - test.matrix[i] = temp; - temp++; - } - t->Pack(test, msgSend); - - WinTimer timer; - - while(1) - { - //Fetch new messages from the postbox - //if(postBox->FetchMessage(msgRecv)) - { - t->Unpack(set, msgRecv); - - //PrintOutMessage(set); - set->Release(); - } - - //Send message to server each second - if(timer.getElapsedSeconds() > 1) - { - cout << "Sending to server." << endl; - timer.reset(); - //client.Send(msgSend); - } - Sleep(1); - } - - delete postBox; - delete t; - delete set;*/ -} - -void PrintOutMessage(ProtocolSet* set) -{ - switch(set->type) - { - case PackageType_header: - break; - case PackageType_test: - cout <<"Client 2: " << set->Protocol.pTest->textMessage <Protocol.pTest->numOfFloats; i++) - { - cout << set->Protocol.pTest->f[i] << ' ' ; - } - cout << endl; - break; - - case PackageType_player_pos: - cout << "ID " << set->Protocol.pPlayerPos->ID << endl; - for(int i = 0; i < (int)set->Protocol.pPlayerPos->nrOfFloats; i++) - { - cout << set->Protocol.pPlayerPos->matrix[i] << ' '; - } - cout << endl; - break; - } } \ No newline at end of file diff --git a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj index 040b3ff9..f4a69e17 100644 --- a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj +++ b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj @@ -73,21 +73,21 @@ $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\NetworkAPI;$(SolutionDir)..\Bin\DLL;C:\Program Files (x86)\Visual Leak Detector\lib\Win32 - $(SolutionDir)..\External\Lib\$(ProjectName)\ + $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Lib\$(ProjectName)\ + $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) - $(SolutionDir)..\External\Lib\$(ProjectName)\ + $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj index 6797039c..335b889c 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -160,10 +160,6 @@ - - - - diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters index aa216b6e..f8025a15 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters @@ -18,13 +18,5 @@ Source Files - - Source Files - - - - - Header Files - \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 4a061c37..52624ca6 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -2,7 +2,6 @@ #include #include #include "../NetworkDependencies/WinsockFunctions.h" -#include "../NetworkAPI/Translator.h" using namespace std; diff --git a/Code/Network/OysterNetworkServer/TestClass.cpp b/Code/Network/OysterNetworkServer/TestClass.cpp deleted file mode 100644 index c277d8a6..00000000 --- a/Code/Network/OysterNetworkServer/TestClass.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include "TestClass.h" -#include "../../Misc/WinTimer.h" -#include -/* -using namespace Oyster::Network; -using namespace ::Protocols; -using namespace Utility; -using namespace ::DynamicMemory; -using namespace std; - -Test::Test() -{ - recvPostBox = new PostBox>; - - sendBuffer = new OysterByte; - recvBuffer = new OysterByte; - - NetworkServer::INIT_DESC initDesc; - initDesc.port = 9876; - server.Init(initDesc); - server.Start(); - - test.clientID = 0; - test.ID = 5; - test.nrOfFloats = 10; - test.matrix = new float[test.nrOfFloats]; - - for(int i = 0; i < (int)test.nrOfFloats; i++) - { - test.matrix[i] = (float)i; - } - - //t.Pack(test, sendBuffer); -} - -Test::~Test() -{ - for(int i = 0; i < (int)clients.size(); i++) - delete clients.at(i); - - server.Stop(); -} - -void Test::ProcFunction(CustomNetProtocol& protocol) -{ - - return; -} - -void Test::mainLoop() -{ - WinTimer timer; - - while(1) - { - - } -} - -void Test::PrintOutMessage(ProtocolSet* set) -{ - switch(set->type) - { - case PackageType_header: - break; - case PackageType_test: - cout <<"Client 2: " << set->Protocol.pTest->textMessage <Protocol.pTest->numOfFloats; i++) - { - cout << set->Protocol.pTest->f[i] << ' ' ; - } - cout << endl; - break; - - case PackageType_player_pos: - //cout << "ID " << set->Protocol.pPlayerPos->ID << endl; - for(int i = 0; i < (int)set->Protocol.pPlayerPos->nrOfFloats; i++) - { - cout << set->Protocol.pPlayerPos->matrix[i] << ' '; - } - cout << endl; - break; - } -}*/ \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/TestClass.h b/Code/Network/OysterNetworkServer/TestClass.h deleted file mode 100644 index 56b151dd..00000000 --- a/Code/Network/OysterNetworkServer/TestClass.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef TEST_CLASS_H -#define TEST_CLASS_H -/* -#include "../../Misc/Utilities.h" -#include "../NetworkDependencies/OysterByte.h" -#include "../NetworkDependencies/PostBox.h" -#include "../NetworkAPI/NetworkClient.h" -#include "../NetworkAPI/NetworkServer.h" -//#include "../NetworkDependencies/Translator.h" -#include "../NetworkAPI/CustomNetProtocol.h" -#include "../NetworkDependencies/Protocols.h" -#include - -class Test -{ -public: - Test(); - ~Test(); - - void mainLoop(); - - virtual void ProcFunction(Oyster::Network::CustomNetProtocol& protocol); - void PrintOutMessage(Oyster::Network::Protocols::ProtocolSet* set); - -private: - std::vector clients; - Oyster::Network::IPostBox> *recvPostBox; - - //Oyster::Network::Translator t; - Oyster::Network::Protocols::ProtocolPlayerPos test; - Utility::DynamicMemory::SmartPointer sendBuffer; - Utility::DynamicMemory::SmartPointer recvBuffer; - - Oyster::Network::NetworkServer server; - -}; -*/ -#endif \ No newline at end of file