From 7acb3a3613f734e01ce653f75266956f2f67609f Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Mon, 16 Dec 2013 08:59:38 +0100 Subject: [PATCH] Network - --- Code/Network/NetworkAPI/Translator.cpp | 74 +++++++++---------- Code/Network/NetworkAPI/Translator.h | 4 +- .../NetworkDependencies/Connection.cpp | 15 ++-- Code/Network/NetworkDependencies/Connection.h | 4 +- .../Network/NetworkDependencies/IConnection.h | 4 +- .../NetworkDependencies/ThreadedClient.cpp | 2 +- .../OysterNetworkClient/ClientMain.cpp | 14 ++-- .../OysterNetworkClient.vcxproj | 11 ++- .../OysterNetworkServer.vcxproj | 17 +++-- .../OysterNetworkServer/ServerMain.cpp | 32 +++++++- 10 files changed, 103 insertions(+), 74 deletions(-) diff --git a/Code/Network/NetworkAPI/Translator.cpp b/Code/Network/NetworkAPI/Translator.cpp index e331f1aa..28b62dd8 100644 --- a/Code/Network/NetworkAPI/Translator.cpp +++ b/Code/Network/NetworkAPI/Translator.cpp @@ -51,13 +51,13 @@ struct Translator::PrivateData } //Packages a header with a size(int) and a string of characters(char) - void PackHeader(SmartPointer &bytes, CustomNetProtocol& protocol) + void PackHeader(OysterByte &bytes, CustomNetProtocol& protocol) { auto it = ((MyCastingStruct*)protocol.privateData)->attributes.begin(); auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end(); size = 4; //size(int) - message.PackInt(size, *bytes); + message.PackInt(size, bytes); //Find all the data types for(; it != end; it++) @@ -65,19 +65,19 @@ struct Translator::PrivateData headerString.push_back(it->second.type); } - message.PackShort(size, *bytes); + message.PackShort(size, bytes); size += 2; for(int i = 0; i < (int)headerString.size(); i++) { - message.PackChar(headerString.at(i), *bytes); + message.PackChar(headerString.at(i), bytes); size++; } message.SetSize(bytes); } - void PackMessage(SmartPointer &bytes, CustomNetProtocol& protocol) + void PackMessage(OysterByte &bytes, CustomNetProtocol& protocol) { auto it = ((MyCastingStruct*)protocol.privateData)->attributes.begin(); auto end = ((MyCastingStruct*)protocol.privateData)->attributes.end(); @@ -87,40 +87,40 @@ struct Translator::PrivateData switch((int)headerString.at(i)) { case NetAttributeType_Bool: - message.PackBool(it->second.value.netBool, *bytes); + message.PackBool(it->second.value.netBool, bytes); break; case NetAttributeType_Char: - message.PackChar(it->second.value.netChar, *bytes); + message.PackChar(it->second.value.netChar, bytes); break; case NetAttributeType_UnsignedChar: - message.PackUnsignedChar(it->second.value.netUChar, *bytes); + message.PackUnsignedChar(it->second.value.netUChar, bytes); break; case NetAttributeType_Short: - message.PackShort(it->second.value.netShort, *bytes); + message.PackShort(it->second.value.netShort, bytes); break; case NetAttributeType_UnsignedShort: - message.PackUnsignedShort(it->second.value.netUShort, *bytes); + message.PackUnsignedShort(it->second.value.netUShort, bytes); break; case NetAttributeType_Int: - message.PackInt(it->second.value.netInt, *bytes); + message.PackInt(it->second.value.netInt, bytes); break; case NetAttributeType_UnsignedInt: - message.PackUnsignedInt(it->second.value.netUInt, *bytes); + message.PackUnsignedInt(it->second.value.netUInt, bytes); break; case NetAttributeType_Int64: - message.PackInt64(it->second.value.netInt64, *bytes); + message.PackInt64(it->second.value.netInt64, bytes); break; case NetAttributeType_UnsignedInt64: - message.PackUnsignedInt64(it->second.value.netUInt64, *bytes); + message.PackUnsignedInt64(it->second.value.netUInt64, bytes); break; case NetAttributeType_Float: - message.PackFloat(it->second.value.netFloat, *bytes); + message.PackFloat(it->second.value.netFloat, bytes); break; case NetAttributeType_Double: - message.PackDouble(it->second.value.netDouble, *bytes); + message.PackDouble(it->second.value.netDouble, bytes); break; case NetAttributeType_CharArray: - message.PackStr(it->second.value.netCharPtr, *bytes); + message.PackStr(it->second.value.netCharPtr, bytes); break; default: numberOfUnknownTypes++; @@ -131,27 +131,27 @@ struct Translator::PrivateData message.SetSize(bytes); } - bool UnpackHeader(CustomNetProtocol& protocol, SmartPointer &bytes) + bool UnpackHeader(CustomNetProtocol& protocol, OysterByte &bytes) { message.SetSize(0); - int packageSize = message.UnpackInt(*bytes); - if(packageSize != bytes->GetSize()) + int packageSize = message.UnpackInt(bytes); + if(packageSize != bytes.GetSize()) { return false; } - short numberOfTypes = message.UnpackShort(*bytes); + short numberOfTypes = message.UnpackShort(bytes); for(int i = 0; i < numberOfTypes; i++) { - char temp = message.UnpackChar(*bytes); + char temp = message.UnpackChar(bytes); headerString.push_back(temp); } return true; } - void UnpackMessage(CustomNetProtocol& protocol, SmartPointer &bytes) + void UnpackMessage(CustomNetProtocol& protocol, OysterByte &bytes) { for(int i = 0; i < (int)headerString.size(); i++) { @@ -159,40 +159,40 @@ struct Translator::PrivateData switch(protocol[i].type) { case NetAttributeType_Bool: - protocol[i].value.netBool = message.UnpackBool(*bytes); + protocol[i].value.netBool = message.UnpackBool(bytes); break; case NetAttributeType_Char: - protocol[i].value.netChar = message.UnpackChar(*bytes); + protocol[i].value.netChar = message.UnpackChar(bytes); break; case NetAttributeType_UnsignedChar: - protocol[i].value.netUChar = message.UnpackUnsignedChar(*bytes); + protocol[i].value.netUChar = message.UnpackUnsignedChar(bytes); break; case NetAttributeType_Short: - protocol[i].value.netShort = message.UnpackShort(*bytes); + protocol[i].value.netShort = message.UnpackShort(bytes); break; case NetAttributeType_UnsignedShort: - protocol[i].value.netUShort = message.UnpackUnsignedShort(*bytes); + protocol[i].value.netUShort = message.UnpackUnsignedShort(bytes); break; case NetAttributeType_Int: - protocol[i].value.netInt = message.UnpackInt(*bytes); + protocol[i].value.netInt = message.UnpackInt(bytes); break; case NetAttributeType_UnsignedInt: - protocol[i].value.netUInt = message.UnpackUnsignedInt(*bytes); + protocol[i].value.netUInt = message.UnpackUnsignedInt(bytes); break; case NetAttributeType_Int64: - protocol[i].value.netInt64 = message.UnpackInt64(*bytes); + protocol[i].value.netInt64 = message.UnpackInt64(bytes); break; case NetAttributeType_UnsignedInt64: - protocol[i].value.netUInt64 = message.UnpackUnsignedInt64(*bytes); + protocol[i].value.netUInt64 = message.UnpackUnsignedInt64(bytes); break; case NetAttributeType_Float: - protocol[i].value.netFloat = message.UnpackFloat(*bytes); + protocol[i].value.netFloat = message.UnpackFloat(bytes); break; case NetAttributeType_Double: - protocol[i].value.netDouble = message.UnpackDouble(*bytes); + protocol[i].value.netDouble = message.UnpackDouble(bytes); break; case NetAttributeType_CharArray: - protocol[i].value.netCharPtr = message.UnpackCStr(*bytes); + protocol[i].value.netCharPtr = message.UnpackCStr(bytes); break; default: numberOfUnknownTypes++; @@ -222,7 +222,7 @@ Translator::~Translator() } } -void Translator::Pack(SmartPointer &bytes, CustomNetProtocol& protocol) +void Translator::Pack(OysterByte &bytes, CustomNetProtocol& protocol) { privateData->headerString.clear(); @@ -230,7 +230,7 @@ void Translator::Pack(SmartPointer &bytes, CustomNetProtocol& protoc privateData->PackMessage(bytes, protocol); } -bool Translator::Unpack(CustomNetProtocol& protocol, SmartPointer &bytes) +bool Translator::Unpack(CustomNetProtocol& protocol, OysterByte &bytes) { if(!privateData->UnpackHeader(protocol, bytes)) { diff --git a/Code/Network/NetworkAPI/Translator.h b/Code/Network/NetworkAPI/Translator.h index 3f5fe673..aed765f8 100644 --- a/Code/Network/NetworkAPI/Translator.h +++ b/Code/Network/NetworkAPI/Translator.h @@ -49,10 +49,10 @@ namespace Oyster Translator (); ~Translator(); - void Pack(Utility::DynamicMemory::SmartPointer &bytes, CustomNetProtocol& protocol); + void Pack(OysterByte &bytes, CustomNetProtocol& protocol); //Returns false if it discovers any faulty stuff with the package. - bool Unpack(CustomNetProtocol& protocol, Utility::DynamicMemory::SmartPointer &bytes); + bool Unpack(CustomNetProtocol& protocol, OysterByte &bytes); private: struct PrivateData; diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index b470ebc2..bf0b108e 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -104,11 +104,11 @@ int Connection::Disconnect() return 0; } -int Connection::Send(Utility::DynamicMemory::SmartPointer &bytes) +int Connection::Send(OysterByte &bytes) { int nBytes; - nBytes = send(this->socket, *bytes, bytes->GetSize(), 0); + nBytes = send(this->socket, bytes, bytes.GetSize(), 0); if(nBytes == SOCKET_ERROR) { return WSAGetLastError(); @@ -117,20 +117,20 @@ int Connection::Send(Utility::DynamicMemory::SmartPointer &bytes) return 0; } -int Connection::Recieve(Utility::DynamicMemory::SmartPointer &bytes) +int Connection::Recieve(OysterByte &bytes) { int nBytes; - bytes->Resize(1000); - nBytes = recv(this->socket, *bytes, 1000, 0); + bytes.Resize(1000); + nBytes = recv(this->socket, bytes, 1000, 0); if(nBytes == SOCKET_ERROR) { - bytes->SetSize(0); + bytes.SetSize(0); return WSAGetLastError(); } else { - bytes->SetSize(nBytes); + bytes.SetSize(nBytes); } return 0; @@ -177,6 +177,7 @@ int Connection::SetBlockingMode(bool blocking) return WSAGetLastError(); } + //Success return 0; } diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index 815c1d32..ae76a3f7 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -23,8 +23,8 @@ namespace Oyster virtual int InitiateServer( unsigned short port ); virtual int InitiateClient(); - virtual int Send( Utility::DynamicMemory::SmartPointer &bytes ); - virtual int Recieve( Utility::DynamicMemory::SmartPointer &bytes ); + virtual int Send( OysterByte &bytes ); + virtual int Recieve( OysterByte &bytes ); virtual int Disconnect(); virtual int Connect( unsigned short port , const char serverName[] ); diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index ecfe3869..76736071 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -19,8 +19,8 @@ namespace Oyster //sends and recieve functions with bytearrays, //will send to the users connection via socket - virtual int Send( Utility::DynamicMemory::SmartPointer &bytes ) = 0; - virtual int Recieve( Utility::DynamicMemory::SmartPointer &bytes) = 0; + virtual int Send( OysterByte &bytes ) = 0; + virtual int Recieve( OysterByte &bytes) = 0; //initiates sockets and address for server and client virtual int InitiateServer( unsigned short port ) { return false; }; diff --git a/Code/Network/NetworkDependencies/ThreadedClient.cpp b/Code/Network/NetworkDependencies/ThreadedClient.cpp index 204b26c3..fc652f7f 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.cpp +++ b/Code/Network/NetworkDependencies/ThreadedClient.cpp @@ -84,7 +84,7 @@ int ThreadedClient::Recv() { int errorCode = -1; - SmartPointer temp = new OysterByte; + OysterByte temp; errorCode = this->connection->Recieve(temp); if(errorCode == 0) diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 2b75c92d..b2f4c4bd 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -18,9 +18,6 @@ using namespace Oyster::Network; using namespace Utility; using namespace Utility::DynamicMemory; -void chat(ThreadedClient &client); -void PrintOutMessage(ProtocolSet* set); - void proc(CustomNetProtocol& protocol) { @@ -40,14 +37,13 @@ int main() NetworkClient client; //Connect to server - errorCode = client.Connect(15151, "193.11.186.101"); + if(client.Connect(15151, "localhost")) + { + cout << "FAILED" << endl; + } client.SetRecieverObject(proc, NetworkProtocolCallbackType_Function); - if(errorCode != 0) - { - wstring errorTest = GetErrorMessage(errorCode); - wcout << "errorMessage: " << errorTest << endl; - } + cout << "Done" << endl; while(1) { diff --git a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj index f4a69e17..bc491966 100644 --- a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj +++ b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj @@ -70,28 +70,28 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D $(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 + $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\NetworkAPI;$(SolutionDir)..\Bin\DLL;C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(OutDir)..\DLL\ $(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) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath);$(OutDir)..\DLL\ $(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) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath);$(OutDir)..\DLL\ $(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\Win64;$(LibraryPath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath);$(OutDir)..\DLL\ @@ -116,6 +116,7 @@ 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) @@ -131,6 +132,7 @@ true true 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) @@ -146,6 +148,7 @@ true true 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) diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj index 335b889c..089427f7 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -66,32 +66,32 @@ - $(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) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath);$(OutDir)..\DLL\ - $(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) + C:\Program Files (x86)\Visual Leak Detector\lib\Win32;$(LibraryPath);$(OutDir)..\DLL\ - $(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) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath);$(OutDir)..\DLL\ - $(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\Win64;$(LibraryPath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath);$(OutDir)..\DLL\ @@ -104,6 +104,7 @@ 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) diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 52624ca6..7f78bc78 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -2,16 +2,44 @@ #include #include #include "../NetworkDependencies/WinsockFunctions.h" +#include "../NetworkAPI/NetworkServer.h" +using namespace Oyster::Network; using namespace std; +void proc(NetworkClient client) +{ + cout << "Hej" << endl; +} + int main() { - if(!InitWinSock()) + NetworkServer server; + Oyster::Network::NetworkServer::INIT_DESC desc; + desc.port = 15151; + desc.callbackType = NetworkClientCallbackType_Function; + desc.recvObj = proc; + + if(!server.Init(desc)) { - cout << "errorMessage: unable to start winsock" << endl; + cout << "Init failed" << endl; + return 0; } + if(!server.Start()) + { + cout << "Start failed" << endl; + return 0; + } + + cout << "Server started" << endl; + + while(1) + { + + } + + system("pause"); return 0;