diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 01d991ca..e9de4297 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -38,7 +38,12 @@ struct ClientDataContainer Translator translator; + //ID + static unsigned int currID; + const unsigned int ID; + ClientDataContainer(IThreadObject* o) + : ID(currID++) { InitWinSock(); callbackType = NetworkProtocolCallbackType_Unknown; @@ -46,7 +51,7 @@ struct ClientDataContainer connection.SetBlockingMode(false); } ClientDataContainer(IThreadObject* o, unsigned int socket ) - :connection(socket) + :connection(socket), ID(currID++) { InitWinSock(); callbackType = NetworkProtocolCallbackType_Unknown; @@ -64,6 +69,8 @@ struct ClientDataContainer } }; +unsigned int ClientDataContainer::currID = 0; + struct NetworkClient::PrivateData : public IThreadObject { Utility::DynamicMemory::SmartPointer data; @@ -233,4 +240,9 @@ void NetworkClient::SetRecieverObject(RecieverObject recvObj, NetworkProtocolCal privateData->data->recvObj = recvObj; privateData->data->callbackType = type; privateData->data->recvObjMutex.unlock(); +} + +bool NetworkClient::operator ==(const NetworkClient& obj) +{ + return (this->privateData->data->ID == obj.privateData->data->ID); } \ No newline at end of file diff --git a/Code/Network/NetworkAPI/NetworkClient.h b/Code/Network/NetworkAPI/NetworkClient.h index af95aeac..5c08e794 100644 --- a/Code/Network/NetworkAPI/NetworkClient.h +++ b/Code/Network/NetworkAPI/NetworkClient.h @@ -38,10 +38,14 @@ namespace Oyster bool IsConnected(); + //Adds the protocol to the queue of protocols to be sent. void Send(CustomProtocolObject& protocol); void SetRecieverObject(RecieverObject recvObj, NetworkProtocolCallbackType type); + //Compares the internal ID. + bool operator ==(const NetworkClient& obj); + private: struct PrivateData; PrivateData* privateData; diff --git a/Code/Network/NetworkAPI/Translator.cpp b/Code/Network/NetworkAPI/Translator.cpp index 1a2105df..75598b75 100644 --- a/Code/Network/NetworkAPI/Translator.cpp +++ b/Code/Network/NetworkAPI/Translator.cpp @@ -35,9 +35,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++) @@ -46,6 +44,7 @@ struct Translator::PrivateData } message.PackShort(size, bytes); + size += 2; for(int i = 0; i < (int)headerString.size(); i++) { 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 99785b41..3f451315 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,11 +37,12 @@ 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, "127.0.0.1"); + client.SetRecieverObject(proc, NetworkProtocolCallbackType_Function); if(errorCode != 0) { @@ -43,94 +50,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 bb63d4cf..f4a69e17 100644 --- a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj +++ b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj @@ -66,28 +66,28 @@ - $(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\Win32;$(LibraryPath) + $(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(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) @@ -102,6 +102,9 @@ true + + + NetworkAPI_$(PlatformShortName)D.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) @@ -149,6 +152,9 @@ {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} + + {460d625f-2ac9-4559-b809-0ba89ceaedf4} + {c5aa09d0-6594-4cd3-bd92-1d380c7b3b50} 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