diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 535256f9..64fc0bc4 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -5,6 +5,9 @@ #include "GameClientState/GameClientState.h" #include "GameClientState\GameState.h" #include "GameClientState\LobbyState.h" + + +#include "L_inputClass.h" #include "vld.h" namespace DanBias @@ -12,7 +15,6 @@ namespace DanBias __int64 DanBiasGame::cntsPerSec = 0; __int64 DanBiasGame::prevTimeStamp = 0; float DanBiasGame::secsPerCnt = 0; - InputClass* DanBiasGame::inputObj = NULL; HINSTANCE DanBiasGame::g_hInst = NULL; HWND DanBiasGame::g_hWnd = NULL; @@ -32,6 +34,7 @@ namespace DanBias public: Client::GameClientState* gameClientState; + InputClass* inputObj; // gameClient; } data; @@ -163,8 +166,8 @@ namespace DanBias //------------------------------------------------------------------------------------- HRESULT DanBiasGame::InitInput() { - inputObj = new InputClass; - if(!inputObj->Initialize(g_hInst, g_hWnd, 1024, 768)) + m_data->inputObj = new InputClass; + if(!m_data->inputObj->Initialize(g_hInst, g_hWnd, 1024, 768)) { MessageBox(0, L"Could not initialize the input object.", L"Error", MB_OK); return E_FAIL; @@ -174,10 +177,10 @@ namespace DanBias HRESULT DanBiasGame::Update(float deltaTime) { - inputObj->Update(); + m_data->inputObj->Update(); DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same; - state = m_data->gameClientState->Update(deltaTime, inputObj); + state = m_data->gameClientState->Update(deltaTime, m_data->inputObj); if(state != Client::GameClientState::ClientState_Same) { @@ -206,7 +209,7 @@ namespace DanBias HRESULT DanBiasGame::Render(float deltaTime) { int isPressed = 0; - if(inputObj->IsKeyPressed(DIK_A)) + if(m_data->inputObj->IsKeyPressed(DIK_A)) { isPressed = 1; } @@ -224,8 +227,9 @@ namespace DanBias { m_data->gameClientState->Release(); delete m_data->gameClientState; + delete m_data->inputObj; delete m_data; - delete inputObj; + Oyster::Graphics::API::Clean(); return S_OK; diff --git a/Code/Game/DanBiasGame/Include/DanBiasGame.h b/Code/Game/DanBiasGame/Include/DanBiasGame.h index 0f9b83eb..924caac7 100644 --- a/Code/Game/DanBiasGame/Include/DanBiasGame.h +++ b/Code/Game/DanBiasGame/Include/DanBiasGame.h @@ -11,8 +11,6 @@ #include -#include "L_inputClass.h" - namespace DanBias { @@ -60,7 +58,7 @@ namespace DanBias static __int64 cntsPerSec; static __int64 prevTimeStamp; static float secsPerCnt; - static InputClass* inputObj; + static HINSTANCE g_hInst; static HWND g_hWnd; static DanBiasGamePrivateData* m_data; diff --git a/Code/Game/DanBiasLauncher/DanBiasLauncher.vcxproj b/Code/Game/DanBiasLauncher/DanBiasLauncher.vcxproj index 6e5f099c..e99b1754 100644 --- a/Code/Game/DanBiasLauncher/DanBiasLauncher.vcxproj +++ b/Code/Game/DanBiasLauncher/DanBiasLauncher.vcxproj @@ -71,32 +71,32 @@ $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath) - $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL; + $(SolutionDir)..\External\Include\;$(IncludePath) + $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL; true $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath) - $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL; + $(SolutionDir)..\External\Include\;$(IncludePath) + $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL; false $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) - $(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath) - $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL; + $(SolutionDir)..\External\Include\;$(IncludePath) + $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL; false $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) - $(SolutionDir)..\External\Include\;$(IncludePath);$(IncludePath) - $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\External\Lib\Input;$(SolutionDir)..\Bin\DLL; + $(SolutionDir)..\External\Include\;$(IncludePath) + $(OutDir)..\DLL\;$(LibraryPath);$(SolutionDir)..\Bin\DLL; @@ -105,13 +105,13 @@ Level3 Disabled WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) - $(SolutionDir)Input;$(SolutionDir)Include;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterGraphics\;$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include + $(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include Windows true DanBiasServer_$(PlatformShortName)D.dll;DanBiasGame_$(PlatformShortName)D.dll;%(DelayLoadDLLs) - Input_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies) + DanBiasServer_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies) @@ -121,13 +121,13 @@ Level3 Disabled WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) - $(SolutionDir)Input;$(SolutionDir)Include;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterGraphics\;$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include + $(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include Windows true DanBiasServer_$(PlatformShortName)D.dll;DanBiasGame_$(PlatformShortName)D.dll;%(DelayLoadDLLs) - Input_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies) + DanBiasServer_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies) @@ -139,7 +139,7 @@ true true WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) - $(SolutionDir)Input;$(SolutionDir)Include;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterGraphics\;$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include + $(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include Windows @@ -147,7 +147,7 @@ true true DanBiasServer_$(PlatformShortName).dll;DanBiasGame_$(PlatformShortName).dll;%(DelayLoadDLLs) - Input_$(PlatformShortName).lib;DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) + DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) @@ -159,7 +159,7 @@ true true WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) - $(SolutionDir)Input;$(SolutionDir)Include;$(SolutionDir)Misc;$(SolutionDir)OysterMath;$(SolutionDir)OysterGraphics\;$(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include + $(SolutionDir)Game\DanBiasServer\Include;$(SolutionDir)Game\DanBiasGame\Include Windows @@ -167,28 +167,13 @@ true true DanBiasServer_$(PlatformShortName).dll;DanBiasGame_$(PlatformShortName).dll;%(DelayLoadDLLs) - Input_$(PlatformShortName).lib;DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) + DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) - - {104fa3e9-94d9-4e1d-a941-28a03bc8a095} - - - {7e3990d2-3d94-465c-b58d-64a74b3ecf9b} - - - {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} - - - {0ec83e64-230e-48ef-b08c-6ac9651b4f82} - - - {f10cbc03-9809-4cba-95d8-327c287b18ee} - {2a1bc987-af42-4500-802d-89cd32fc1309} diff --git a/Code/Game/DanBiasLauncher/Launcher.cpp b/Code/Game/DanBiasLauncher/Launcher.cpp index 0440d973..28b89485 100644 --- a/Code/Game/DanBiasLauncher/Launcher.cpp +++ b/Code/Game/DanBiasLauncher/Launcher.cpp @@ -4,8 +4,8 @@ #define NOMINMAX #include -//#define DANBIAS_SERVER -#define DANBIAS_CLIENT +#define DANBIAS_SERVER +//#define DANBIAS_CLIENT #if defined(DANBIAS_SERVER) diff --git a/Code/Misc/ThreadSafeQueue.h b/Code/Misc/ThreadSafeQueue.h index d0125f71..eedd9fd9 100644 --- a/Code/Misc/ThreadSafeQueue.h +++ b/Code/Misc/ThreadSafeQueue.h @@ -46,7 +46,7 @@ namespace Oyster Node *front; Node *back; int nrOfNodes; - OysterMutex mutex; + std::mutex stdMutex; }; @@ -68,7 +68,7 @@ namespace Oyster template < typename Type > ThreadSafeQueue::~ThreadSafeQueue() { - this->mutex.LockMutex(); + stdMutex.lock(); if(this->front != NULL) { @@ -87,16 +87,16 @@ namespace Oyster this->back = NULL; } - this->mutex.UnlockMutex(); + stdMutex.unlock(); } template < typename Type > void ThreadSafeQueue::Push(Type item) { + stdMutex.lock(); Node *e = new Node(item); - mutex.LockMutex(); if(this->front != NULL) { this->back->next = e; @@ -111,91 +111,86 @@ namespace Oyster this->nrOfNodes++; - mutex.UnlockMutex(); + stdMutex.unlock(); } template < typename Type > Type ThreadSafeQueue::Pop() { - mutex.LockMutex(); - if(this->front != NULL) - { - Type item = this->front->item; - Node *destroyer = this->front; - this->front = front->next; + stdMutex.lock(); - delete destroyer; - this->nrOfNodes--; + Type item = this->front->item; + Node *destroyer = this->front; + this->front = front->next; + + delete destroyer; + this->nrOfNodes--; - if(nrOfNodes == 0) - { - this->front = NULL; - this->back = NULL; - } - return item; + if(nrOfNodes == 0) + { + this->front = NULL; + this->back = NULL; } - mutex.UnlockMutex(); - - return NULL; + stdMutex.unlock(); + return item; } template < typename Type > Type ThreadSafeQueue::Front() { - mutex.LockMutex(); - if(front != NULL) - { - return this->front->item; - } - mutex.UnlockMutex(); - - return NULL; + stdMutex.lock(); + Type temp = this->front->item; + stdMutex.unlock(); + + return temp; + } template < typename Type > Type ThreadSafeQueue::Back() { - mutex.LockMutex(); - if(back != NULL) - { - return this->back->item; - } - mutex.UnlockMutex(); + stdMutex.lock(); + Type temp = this->back->item; + stdMutex.unlock(); - return NULL; + return temp; + } template < typename Type > int ThreadSafeQueue::Size() { - //? behövs denna låsas? - mutex.LockMutex(); - return this->nrOfNodes; - mutex.UnlockMutex(); + stdMutex.lock(); + int size = this->nrOfNodes; + stdMutex.unlock(); + + return size; + } template < typename Type > bool ThreadSafeQueue::IsEmpty() { - mutex.LockMutex(); - if(nrOfNodes == 0 && this->front == NULL) + stdMutex.lock(); + if(nrOfNodes == 0 || this->front == NULL) { - mutex.UnlockMutex(); + stdMutex.unlock(); return true; } else { - mutex.UnlockMutex(); + stdMutex.unlock(); } + return false; } template < typename Type > void ThreadSafeQueue::Swap(IQueue &queue ) { - mutex.LockMutex(); + stdMutex.lock(); int prevNrOfNodes = this->nrOfNodes; int size = queue.Size(); @@ -208,7 +203,7 @@ namespace Oyster { queue.Push(this->Pop()); } - mutex.UnlockMutex(); + stdMutex.unlock(); } diff --git a/Code/Misc/Utilities.h b/Code/Misc/Utilities.h index ec8b0229..6112bea1 100644 --- a/Code/Misc/Utilities.h +++ b/Code/Misc/Utilities.h @@ -216,7 +216,7 @@ namespace Utility T* operator-> (); operator T* (); operator bool(); - + /** * Returns the connected pointer */ diff --git a/Code/Network/NetworkAPI/CustomNetProtocol.cpp b/Code/Network/NetworkAPI/CustomNetProtocol.cpp new file mode 100644 index 00000000..b636a475 --- /dev/null +++ b/Code/Network/NetworkAPI/CustomNetProtocol.cpp @@ -0,0 +1,58 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "CustomNetProtocol.h" +#include + +using namespace Network; + + +struct CustomNetProtocol::PrivateData +{ + std::map attributes; + + PrivateData() + { } + ~PrivateData() + { + for (auto i = attributes.begin(); i != attributes.end(); i++) + { + RemoveAttribute(i->first); + } + } + void RemoveAttribute(int ID) + { + auto i = attributes.find(ID); + if(i == attributes.end()) return; + + switch (i->second.type) + { + case NetAttributeType_CharArray: + delete [] i->second.value.netCharPtr; + break; + } + } + + //Do network stuff +}; + + +CustomNetProtocol::CustomNetProtocol() +{ + this->privateData = new PrivateData(); +} +CustomNetProtocol::~CustomNetProtocol() +{ + delete this->privateData; +} +NetAttributeContainer& CustomNetProtocol::operator[](int ID) +{ + if(this->privateData->attributes.find(ID) == this->privateData->attributes.end()) + { + this->privateData->attributes[ID]; + this->privateData->attributes[ID].type = NetAttributeType_UNKNOWN; + memset(&this->privateData->attributes[ID].value, 0, sizeof(NetAttributeValue)); + } + + return this->privateData->attributes[ID]; +} diff --git a/Code/Network/NetworkAPI/CustomNetProtocol.h b/Code/Network/NetworkAPI/CustomNetProtocol.h new file mode 100644 index 00000000..9e464f9f --- /dev/null +++ b/Code/Network/NetworkAPI/CustomNetProtocol.h @@ -0,0 +1,103 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef NETWORK_CUSTOM_NETWORK_PROTOCOL_H +#define NETWORK_CUSTOM_NETWORK_PROTOCOL_H + +#include + +#ifdef CUSTOM_NET_PROTOCOL_EXPORT + #define NET_PROTOCOL_EXPORT __declspec(dllexport) +#else + #define NET_PROTOCOL_EXPORT __declspec(dllimport) +#endif + +namespace Network +{ + extern "C" + { + enum NetAttributeType + { + NetAttributeType_Bool, + NetAttributeType_Char, + NetAttributeType_UnsignedChar, + NetAttributeType_Short, + NetAttributeType_UnsignedShort, + NetAttributeType_Int, + NetAttributeType_UnsignedInt, + NetAttributeType_Int64, + NetAttributeType_UnsignedInt64, + NetAttributeType_Float, + NetAttributeType_Double, + NetAttributeType_CharArray, + NetAttributeType_UNKNOWN, + }; + union NetAttributeValue + { + bool netBool; + char netChar; + unsigned char netUChar; + short netShort; + unsigned short netUShort; + int netInt; + unsigned int netUInt; + __int64 netInt64; + unsigned __int64 netUInt64; + float netFloat; + double netDouble; + char* netCharPtr; + + NetAttributeValue(){ memset(this, 0, sizeof(NetAttributeValue)); } + NetAttributeValue(bool v) : netBool (v) {} + NetAttributeValue(char v) : netChar (v) {} + NetAttributeValue(unsigned char v) : netUChar (v) {} + NetAttributeValue(short v) : netShort (v) {} + NetAttributeValue(unsigned short v) : netUShort (v) {} + NetAttributeValue(int v) : netInt (v) {} + NetAttributeValue(unsigned int v) : netUInt (v) {} + NetAttributeValue(__int64 v) : netInt64 (v) {} + NetAttributeValue(unsigned __int64 v) : netUInt64 (v) {} + NetAttributeValue(float v) : netFloat (v) {} + NetAttributeValue(double v) : netDouble (v) {} + NetAttributeValue(char* v) : netCharPtr(v) {} + }; + struct NetAttributeContainer + { + NetAttributeType type; + NetAttributeValue value; + NetAttributeContainer() { type = NetAttributeType_UNKNOWN; } + }; + class CustomNetProtocol; + struct CustomProtocolObject + { + virtual CustomNetProtocol* GetProtocol() = 0; + }; + + class NET_PROTOCOL_EXPORT CustomNetProtocol + { + public: + CustomNetProtocol(); + ~CustomNetProtocol(); + + NetAttributeContainer& operator[](int ID); + + private: + struct PrivateData; + PrivateData* privateData; + }; + + }//End extern "C" +} //End namespace Network +#endif // !NETWORK_CUSTOM_NETWORK_PROTOCOL_H + + + + + + + + + + + + diff --git a/Code/Network/NetworkAPI/NetworkAPI.vcxproj b/Code/Network/NetworkAPI/NetworkAPI.vcxproj new file mode 100644 index 00000000..8b0847b6 --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkAPI.vcxproj @@ -0,0 +1,180 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {460D625F-2AC9-4559-B809-0BA89CEAEDF4} + Win32Proj + NetworkAPI + NetworkAPI + + + + DynamicLibrary + true + v110 + Unicode + + + DynamicLibrary + true + v110 + Unicode + + + DynamicLibrary + false + v110 + true + Unicode + + + DynamicLibrary + false + v110 + true + Unicode + + + + + + + + + + + + + + + + + + + true + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)..\Bin\DLL\ + $(ProjectName)_$(PlatformShortName)D + + + true + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)..\Bin\DLL\ + $(ProjectName)_$(PlatformShortName)D + + + false + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)..\Bin\DLL\ + $(ProjectName)_$(PlatformShortName) + + + false + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)..\Bin\DLL\ + $(ProjectName)_$(PlatformShortName) + + + + + + Level3 + Disabled + CUSTOM_NET_PROTOCOL_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + CUSTOM_NET_PROTOCOL_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + CUSTOM_NET_PROTOCOL_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + CUSTOM_NET_PROTOCOL_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + {c5aa09d0-6594-4cd3-bd92-1d380c7b3b50} + + + + + + \ No newline at end of file diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp new file mode 100644 index 00000000..062817c0 --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -0,0 +1,41 @@ +#include "NetworkClient.h" + +using namespace Oyster::Network; + +/************************************* + PrivateData +*************************************/ + +struct PrivateData +{ + +}; + +/************************************* + NetworkClient +*************************************/ + +NetworkClient::NetworkClient() +{ + +} + +NetworkClient::~NetworkClient() +{ + +} + +void NetworkClient::Disconnect() +{ + +} + +bool NetworkClient::IsConnected() +{ + return false; +} + +void NetworkClient::Send() +{ + +} \ No newline at end of file diff --git a/Code/Network/NetworkAPI/NetworkClient.h b/Code/Network/NetworkAPI/NetworkClient.h new file mode 100644 index 00000000..108cf83f --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkClient.h @@ -0,0 +1,42 @@ +#ifndef NETWORK_API_NETWORK_CLIENT_H +#define NETWORK_API_NETWORK_CLIENT_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#ifdef CUSTOM_NET_PROTOCOL_EXPORT + #define NET_PROTOCOL_EXPORT __declspec(dllexport) +#else + #define NET_PROTOCOL_EXPORT __declspec(dllimport) +#endif + +class RecieverObject; + +namespace Oyster +{ + namespace Network + { + extern "C" + { + class NET_PROTOCOL_EXPORT NetworkClient + { + public: + NetworkClient(); + virtual ~NetworkClient(); + + virtual void Disconnect(); + virtual bool IsConnected(); + + virtual void Send(); + + private: + struct PrivateData; + PrivateData* privateData; + + }; + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkAPI/NetworkServer.cpp b/Code/Network/NetworkAPI/NetworkServer.cpp new file mode 100644 index 00000000..05e72679 --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkServer.cpp @@ -0,0 +1,202 @@ +#include "NetworkServer.h" +#include "NetworkClient.h" + +#include "../NetworkDependencies/Listener.h" +#include "../NetworkDependencies/PostBox.h" + +#include "../../Misc/Utilities.h" +#include "../../Misc/Thread/OysterThread.h" + +using namespace Oyster::Network; +using namespace ::Server; +using namespace Utility::DynamicMemory; +using namespace Oyster::Thread; + +/************************************* + PrivateData +*************************************/ + +struct NetworkServer::PrivateData : public IThreadObject +{ + PrivateData(); + ~PrivateData(); + + bool Init(INIT_DESC& initDesc); + bool Start(); + bool Stop(); + bool Shutdown(); + + void CheckForNewClient(); + + virtual bool DoWork(); + + // + IListener* listener; + INIT_DESC initDesc; + bool started; + + //Postbox for new clients + IPostBox *postBox; + + //Server thread + OysterThread thread; + +}; + +NetworkServer::PrivateData::PrivateData() +{ + listener = 0; + started = false; + postBox = new PostBox; +} + +NetworkServer::PrivateData::~PrivateData() +{ + Shutdown(); +} + +bool NetworkServer::PrivateData::Init(INIT_DESC& initDesc) +{ + //Check if it's a valid port + if(initDesc.port == 0) + { + return false; + } + + this->initDesc = initDesc; + + //Initiate listener + listener = new Listener(postBox); + ((Listener*)listener)->Init(this->initDesc.port, false); + + thread.Create(this, false); + + return true; +} + +bool NetworkServer::PrivateData::Start() +{ + //Start listener + ((Listener*)listener)->Start(); + started = true; + + thread.Start(); + + return true; +} + +bool NetworkServer::PrivateData::Stop() +{ + if(listener) + { + ((Listener*)listener)->Stop(); + } + + started = false; + + thread.Stop(); + + return true; +} + +bool NetworkServer::PrivateData::Shutdown() +{ + //Stop server main thread + thread.Stop(); + + if(listener) + { + delete listener; + listener = NULL; + } + + if(postBox) + { + delete postBox; + postBox = NULL; + } + + started = false; + + return true; +} + +//Checks for new clients and sends them to the proc function. +void NetworkServer::PrivateData::CheckForNewClient() +{ + if(postBox->IsFull()) + { + int clientSocketNum; + postBox->FetchMessage(clientSocketNum); + + //Safety check that is probably not needed. + if(clientSocketNum == -1) + { + return; + } + + //Create client and Proc function if the pointer is not NULL + if(initDesc.proc) + { + Oyster::Network::NetworkClient* client = new Oyster::Network::NetworkClient(); + initDesc.proc((NetworkClient*)client); + } + } +} + +bool NetworkServer::PrivateData::DoWork() +{ + CheckForNewClient(); + + return true; +} + +/************************************* + NetworkServer +*************************************/ + +NetworkServer::NetworkServer() +{ + privateData = new PrivateData(); +} + +NetworkServer::~NetworkServer() +{ + if(privateData) + { + delete privateData; + } +} + +bool NetworkServer::Init(INIT_DESC& initDesc) +{ + privateData->Init(initDesc); + + return true; +} + +bool NetworkServer::Start() +{ + privateData->Start(); + + return true; +} + +bool NetworkServer::Stop() +{ + privateData->Stop(); + + return true; +} + +bool NetworkServer::Shutdown() +{ + privateData->Shutdown(); + + return true; +} + +bool NetworkServer::IsStarted() const +{ + return privateData->started; +} \ No newline at end of file diff --git a/Code/Network/NetworkAPI/NetworkServer.h b/Code/Network/NetworkAPI/NetworkServer.h new file mode 100644 index 00000000..427edab7 --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkServer.h @@ -0,0 +1,56 @@ +#ifndef NETWORK_API_NETWORK_SERVER_H +#define NETWORK_API_NETWORK_SERVER_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#ifdef CUSTOM_NET_PROTOCOL_EXPORT + #define NET_PROTOCOL_EXPORT __declspec(dllexport) +#else + #define NET_PROTOCOL_EXPORT __declspec(dllimport) +#endif + +#pragma comment(lib, "ws2_32.lib") + +//#include "NetworkClient.h" + +namespace Oyster +{ + namespace Network + { + namespace Server + { + extern "C" + { + class NET_PROTOCOL_EXPORT NetworkServer + { + public: + class NetworkClient; + struct INIT_DESC + { + unsigned short port; //Port the server should be accepting clients on. + void (*proc)(NetworkClient*); + }; + + NetworkServer(); + virtual ~NetworkServer(); + + virtual bool Init(INIT_DESC& initDesc); + virtual bool Start(); + virtual bool Stop(); + virtual bool Shutdown(); + + virtual bool IsStarted() const; + + private: + struct PrivateData; + PrivateData* privateData; + + }; + } + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 3f303542..5929ea7f 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -7,6 +7,20 @@ using namespace Oyster::Network; +Connection::Connection() +{ + this->socket = 0; + bool stillSending = false; + bool closed = true; +} + +Connection::Connection(int socket) +{ + this->socket = socket; + bool stillSending = false; + bool closed = true; +} + Connection::~Connection() { closesocket( this->socket ); @@ -30,6 +44,9 @@ int Connection::Connect(unsigned short port , const char serverName[]) return WSAGetLastError(); } + closed = false; + stillSending = true; + //connection succesfull! return 0; } @@ -63,6 +80,9 @@ int Connection::InitiateServer(unsigned short port) return errorCode; } + closed = false; + stillSending = true; + //Server started! return 0; } @@ -79,11 +99,11 @@ int Connection::Disconnect() return WSAGetLastError(); } -int Connection::Send(OysterByte& bytes) +int Connection::Send(Utility::DynamicMemory::SmartPointer &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(); @@ -92,34 +112,32 @@ int Connection::Send(OysterByte& bytes) return 0; } -int Connection::Recieve(OysterByte& bytes) +int Connection::Recieve(Utility::DynamicMemory::SmartPointer &bytes) { int nBytes; - bytes.Clear(1000); - nBytes = recv(this->socket, bytes, 500, 0); + bytes->Resize(1000); + nBytes = recv(this->socket, *bytes , 500, 0); if(nBytes == SOCKET_ERROR) { return WSAGetLastError(); } else { - bytes.SetSize(nBytes); + bytes->SetSize(nBytes); } std::cout << "Size of the recieved data: " << nBytes << " bytes" << std::endl; - //bytes.byteArray[nBytes] = '\0'; - return 0; } int Connection::Listen() { int clientSocket; - if((clientSocket = accept(this->socket, NULL, NULL)) == INVALID_SOCKET) + if((clientSocket = (int)accept(this->socket, NULL, NULL)) == INVALID_SOCKET) { - return WSAGetLastError(); + return (int)INVALID_SOCKET;//WSAGetLastError(); } return clientSocket; @@ -130,7 +148,7 @@ int Connection::Listen() /////////////////////////////////////// int Connection::InitiateSocket() { - this->socket = ::socket(AF_INET, SOCK_STREAM, 0); + this->socket = (int)::socket(AF_INET, SOCK_STREAM, 0); if(this->socket == SOCKET_ERROR) { return WSAGetLastError(); @@ -139,15 +157,24 @@ int Connection::InitiateSocket() return 0; } -void Connection::SetBlockingMode(bool blocking) +int Connection::SetBlockingMode(bool blocking) { - //TODO: Implement this function. Setting the socket to blocking or non-blocking. + DWORD nonBlocking; + if(blocking) { - //fcntl(this->socket, F_SETFL, O_NONBLOCK); + nonBlocking = 0; } else { - + nonBlocking = 1; } + + int result = ioctlsocket(this->socket, FIONBIO, &nonBlocking); + if(result != 0) + { + return WSAGetLastError(); + } + + return 0; } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index b46ccb02..d5f7d412 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -16,28 +16,32 @@ namespace Oyster { public: - Connection() { this->socket = 0; }; - Connection( int socket ) { this->socket = socket; }; + Connection(); + Connection( int socket ); virtual ~Connection(); - virtual int InitiateServer( unsigned short port ); virtual int InitiateClient(); - virtual int Send( OysterByte& bytes ); - virtual int Recieve( OysterByte& bytes ); + virtual int Send( Utility::DynamicMemory::SmartPointer &bytes ); + virtual int Recieve( Utility::DynamicMemory::SmartPointer &bytes ); virtual int Disconnect(); virtual int Connect( unsigned short port , const char serverName[] ); virtual int Listen(); + //Setting the socket to blocking/non-blocking mode. + int SetBlockingMode( bool blocking ); + private: int InitiateSocket(); - void SetBlockingMode( bool blocking ); int socket; + bool stillSending; + bool closed; + }; } } diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index 5f88932b..ecfe3869 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -5,6 +5,8 @@ // Created by Sam Svensson 2013 // ////////////////////////////////// +#include "../../Misc/Utilities.h" + namespace Oyster { namespace Network @@ -17,8 +19,8 @@ namespace Oyster //sends and recieve functions with bytearrays, //will send to the users connection via socket - virtual int Send( OysterByte& bytes ) = 0; - virtual int Recieve( OysterByte& bytes) = 0; + virtual int Send( Utility::DynamicMemory::SmartPointer &bytes ) = 0; + virtual int Recieve( Utility::DynamicMemory::SmartPointer &bytes) = 0; //initiates sockets and address for server and client virtual int InitiateServer( unsigned short port ) { return false; }; diff --git a/Code/Network/NetworkDependencies/IListener.h b/Code/Network/NetworkDependencies/IListener.h index f08cfb27..f88f23ea 100644 --- a/Code/Network/NetworkDependencies/IListener.h +++ b/Code/Network/NetworkDependencies/IListener.h @@ -14,6 +14,7 @@ namespace Oyster class IListener { public: + virtual ~IListener() {} virtual bool Init(unsigned int port) = 0; virtual int Accept() = 0; diff --git a/Code/Network/NetworkDependencies/IPostBox.h b/Code/Network/NetworkDependencies/IPostBox.h index 31c1bef7..70dc7558 100644 --- a/Code/Network/NetworkDependencies/IPostBox.h +++ b/Code/Network/NetworkDependencies/IPostBox.h @@ -15,7 +15,7 @@ namespace Oyster public: virtual ~IPostBox() {} virtual void PostMessage(T& message) = 0; - virtual void FetchMessage(T& message) = 0; + virtual bool FetchMessage(T& message) = 0; virtual bool IsFull() = 0; }; diff --git a/Code/Network/NetworkDependencies/ITranslate.h b/Code/Network/NetworkDependencies/ITranslate.h index 80edc8b1..77ceaa0f 100644 --- a/Code/Network/NetworkDependencies/ITranslate.h +++ b/Code/Network/NetworkDependencies/ITranslate.h @@ -5,6 +5,8 @@ // Created by Sam Svensson 2013 // ////////////////////////////////// +#include "../../Misc/Utilities.h" + namespace Oyster { namespace Network @@ -15,8 +17,8 @@ namespace Oyster public: //packs and unpacks packages for sending or recieving over the connection - virtual void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes) = 0; - virtual void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0; + virtual void Pack (Protocols::ProtocolHeader &header, Utility::DynamicMemory::SmartPointer &bytes) = 0; + virtual void Unpack (Protocols::ProtocolSet* set, Utility::DynamicMemory::SmartPointer &bytes ) = 0; }; } diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index c441d045..1aec11da 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -1,12 +1,19 @@ #include "Listener.h" using namespace Oyster::Network::Server; +using namespace Utility::DynamicMemory; Listener::Listener() { connection = NULL; } +Listener::Listener(Oyster::Network::IPostBox* postBox) +{ + connection = NULL; + this->postBox = postBox; +} + Listener::~Listener() { if(connection) @@ -15,10 +22,10 @@ Listener::~Listener() } } +//Starts the thread immediate bool Listener::Init(unsigned int port) { connection = new Connection(); - connection->InitiateServer(port); thread.Create(this, true); @@ -26,26 +33,53 @@ bool Listener::Init(unsigned int port) return true; } +bool Listener::Init(unsigned int port, bool start) +{ + connection = new Connection(); + connection->InitiateServer(port); + + thread.Create(this, start); + + return true; +} + +void Listener::Start() +{ + thread.Start(); +} + +void Listener::Stop() +{ + thread.Stop(); +} + void Listener::Shutdown() { - thread.Terminate(); + thread.Stop(); } void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) { - mutex.LockMutex(); + stdMutex.lock(); + //mutex.LockMutex(); this->postBox = postBox; - mutex.UnlockMutex(); + //mutex.UnlockMutex(); + stdMutex.unlock(); } int Listener::Accept() { - int clientSocket = 0; + int clientSocket = -1; clientSocket = connection->Listen(); - mutex.LockMutex(); - postBox->PostMessage(clientSocket); - mutex.UnlockMutex(); + if(clientSocket != -1) + { + stdMutex.lock(); + //mutex.LockMutex(); + postBox->PostMessage(clientSocket); + //mutex.UnlockMutex(); + stdMutex.unlock(); + } return clientSocket; } @@ -57,13 +91,10 @@ bool Listener::DoWork() return true; } -#include void Listener::ThreadEntry() { - std::cout << "Thread started" << std::endl; } void Listener::ThreadExit() { - std::cout << "Thread stopped" << std::endl; } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index 8bb16a0f..80b7543f 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -6,10 +6,11 @@ ///////////////////////////////////// #include "IListener.h" -#include "../NetworkDependencies/Connection.h" +#include "Connection.h" +#include "IPostBox.h" #include "../../Misc/Thread/OysterThread.h" #include "../../Misc/Thread/OysterMutex.h" -#include "IPostBox.h" +#include "../../Misc/Utilities.h" namespace Oyster { @@ -17,32 +18,37 @@ namespace Oyster { namespace Server { - class Listener : public ::Oyster::Thread::IThreadObject + class Listener : public IListener, public ::Oyster::Thread::IThreadObject { public: Listener(); + Listener(Oyster::Network::IPostBox* postBox); ~Listener(); bool Init(unsigned int port); + bool Init(unsigned int port, bool start); + void Start(); + void Stop(); + void Shutdown(); void SetPostBox(IPostBox* postBox); + private: //Thread functions bool DoWork(); void ThreadEntry(); void ThreadExit(); - private: //Function that runs in the thread. int Accept(); - private: ::Oyster::Network::Connection* connection; ::Oyster::Thread::OysterThread thread; OysterMutex mutex; + std::mutex stdMutex; IPostBox* postBox; diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp index 97acf526..a8443deb 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -111,7 +111,6 @@ void MessageHeader::PackFloat(float i, OysterByte& bytes) void MessageHeader::PackFloat(float i[], unsigned int elementCount, OysterByte& bytes) { - bytes.AddSize(4); //Pack number of elements PackUnsignedInt(elementCount, bytes); @@ -131,7 +130,7 @@ void MessageHeader::PackDouble(double i, OysterByte& bytes) void MessageHeader::PackStr(char str[], OysterByte& bytes) { - int totalSize = 2 + strlen(str); + int totalSize = 2 + (int)strlen(str); bytes.AddSize(totalSize); Packing::Pack(&bytes.GetByteArray()[size], str); size += totalSize; @@ -139,7 +138,7 @@ void MessageHeader::PackStr(char str[], OysterByte& bytes) void MessageHeader::PackStr(std::string str, OysterByte& bytes) { - int totalSize = 2 + str.length(); + int totalSize = 2 + (int)str.length(); bytes.AddSize(totalSize); Packing::Pack(&bytes.GetByteArray()[size], str); size += totalSize; @@ -244,7 +243,7 @@ double MessageHeader::UnpackDouble(OysterByte& bytes) std::string MessageHeader::UnpackStr(OysterByte& bytes) { std::string str = Packing::UnpackStr(&bytes.GetByteArray()[size]); - size += 2 + str.length(); + size += 2 + (int)str.length(); return str; } diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.h b/Code/Network/NetworkDependencies/Messages/MessageHeader.h index 095ebc1e..61dfedab 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.h +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.h @@ -47,8 +47,9 @@ namespace Oyster void PackStr(char str[], OysterByte& bytes); void PackStr(std::string str, OysterByte& bytes); + //Maybe //TODO: Add Pack functions for Vec2, 3, 4 and maybe Matrix. Etc. - + //Unpack variables from message bool UnpackBool(OysterByte& bytes); @@ -71,6 +72,7 @@ namespace Oyster std::string UnpackStr(OysterByte& bytes); + //Maybe //TODO: Add Unpack functions for Vec2, 3, 4 and maybe Matrix. Etc. diff --git a/Code/Network/NetworkDependencies/Messages/MessagePlayerPos.cpp b/Code/Network/NetworkDependencies/Messages/MessagePlayerPos.cpp new file mode 100644 index 00000000..673b586f --- /dev/null +++ b/Code/Network/NetworkDependencies/Messages/MessagePlayerPos.cpp @@ -0,0 +1,30 @@ +#include "MessagePlayerPos.h" + +using namespace Oyster::Network; +using namespace Oyster::Network::Messages; +using namespace Oyster::Network::Protocols; + +MessagePlayerPos::MessagePlayerPos() +{ +} + +MessagePlayerPos::~MessagePlayerPos() +{ +} + +void MessagePlayerPos::Pack(Protocols::ProtocolHeader& header, OysterByte& bytes) +{ + MessageHeader::Pack(header, bytes); + + PackInt(static_cast(&header)->ID, bytes); + PackFloat(static_cast(&header)->matrix, static_cast(&header)->nrOfFloats, bytes); + SetSize(bytes); +} + +void MessagePlayerPos::Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header) +{ + MessageHeader::Unpack(bytes, header); + + static_cast(&header)->ID = UnpackInt(bytes); + static_cast(&header)->matrix = UnpackFloat(static_cast(&header)->nrOfFloats, bytes); +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessagePlayerPos.h b/Code/Network/NetworkDependencies/Messages/MessagePlayerPos.h new file mode 100644 index 00000000..247e200e --- /dev/null +++ b/Code/Network/NetworkDependencies/Messages/MessagePlayerPos.h @@ -0,0 +1,34 @@ +#ifndef NETWORK_DEPENDENCIES_MESSAGE_PLAYER_POS_H +#define NETWORK_DEPENDENCIES_MESSAGE_PLAYER_POS_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#include "MessageHeader.h" + +namespace Oyster +{ + namespace Network + { + namespace Messages + { + class MessagePlayerPos : public MessageHeader + { + public: + MessagePlayerPos(); + virtual ~MessagePlayerPos(); + + virtual void Pack(Protocols::ProtocolHeader& header, OysterByte& bytes); + virtual void Unpack(OysterByte& bytes, Protocols::ProtocolHeader& header); + + private: + + + + }; + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessagesInclude.h b/Code/Network/NetworkDependencies/Messages/MessagesInclude.h index 8f4d41e3..8bdb5c8a 100644 --- a/Code/Network/NetworkDependencies/Messages/MessagesInclude.h +++ b/Code/Network/NetworkDependencies/Messages/MessagesInclude.h @@ -7,5 +7,6 @@ #include "MessageHeader.h" #include "MessageTest.h" +#include "MessagePlayerPos.h" #endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index dfabbcba..d33e34d3 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -153,11 +153,12 @@ - + + @@ -168,6 +169,7 @@ + @@ -175,6 +177,7 @@ + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index 74cb9a56..3189aba4 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -2,30 +2,33 @@ - - - - - - + + + + + + + + + + + + + - - - - - - - - + + + + \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/OysterByte.cpp b/Code/Network/NetworkDependencies/OysterByte.cpp index ef7aa434..db343e29 100644 --- a/Code/Network/NetworkDependencies/OysterByte.cpp +++ b/Code/Network/NetworkDependencies/OysterByte.cpp @@ -16,18 +16,38 @@ OysterByte::OysterByte(int cap) byteArray = new unsigned char[capacity]; } +OysterByte::OysterByte(const OysterByte& obj) +{ + //delete[] this->byteArray; + this->byteArray = new unsigned char[obj.capacity]; + + for(int i = 0; i < (int)obj.size; i++) + { + this->byteArray[i] = obj.byteArray[i]; + } + this->size = obj.size; + this->capacity = obj.capacity; +} + OysterByte::~OysterByte() { delete[] byteArray; } -void OysterByte::Clear(unsigned int cap) +void OysterByte::Clear() { - delete[] byteArray; - byteArray = new unsigned char[cap]; size = 0; } +void OysterByte::Resize(unsigned int cap) +{ + if(capacity < cap) + { + delete[] byteArray; + byteArray = new unsigned char[cap]; + } +} + int OysterByte::GetSize() { return size; @@ -60,6 +80,21 @@ void OysterByte::SetSize(unsigned int size) this->size = size; } +OysterByte& OysterByte::operator =(const OysterByte& obj) +{ + delete[] this->byteArray; + this->byteArray = new unsigned char[obj.capacity]; + + for(int i = 0; i < (int)obj.size; i++) + { + this->byteArray[i] = obj.byteArray[i]; + } + this->size = obj.size; + this->capacity = obj.capacity; + + return *this; +} + OysterByte::operator char*() { return (char*)byteArray; diff --git a/Code/Network/NetworkDependencies/OysterByte.h b/Code/Network/NetworkDependencies/OysterByte.h index 87a0103c..99629bfc 100644 --- a/Code/Network/NetworkDependencies/OysterByte.h +++ b/Code/Network/NetworkDependencies/OysterByte.h @@ -16,9 +16,11 @@ namespace Oyster public: OysterByte(); OysterByte(int cap); + OysterByte(const OysterByte& obj); virtual ~OysterByte(); - void Clear(unsigned int cap); + void Clear(); //Resets size to 0 + void Resize(unsigned int cap); //Resizes the array with, it does not keep anything in it. int GetSize(); unsigned char* GetByteArray(); @@ -27,6 +29,8 @@ namespace Oyster void SetBytes(unsigned char* bytes); void SetSize(unsigned int size); //Only sets the private variable 'size' + OysterByte& operator =(const OysterByte& obj); + operator char*(); operator const char*(); operator unsigned char*(); diff --git a/Code/Network/NetworkDependencies/Packing.cpp b/Code/Network/NetworkDependencies/Packing.cpp index 7adc395c..405cd5e6 100644 --- a/Code/Network/NetworkDependencies/Packing.cpp +++ b/Code/Network/NetworkDependencies/Packing.cpp @@ -87,7 +87,7 @@ namespace Oyster //floating point (32, 64-bit) void Pack(unsigned char buffer[], float i) { - int tempFloat = Pack754(i, 32, 8); + int tempFloat = (int)Pack754(i, 32, 8); Pack(buffer, tempFloat); } @@ -100,7 +100,7 @@ namespace Oyster //string void Pack(unsigned char buffer[], char str[]) { - short len = strlen(str); + short len = (short)strlen(str); Pack(buffer, len); buffer += 2; memcpy(buffer, str, len); @@ -108,7 +108,7 @@ namespace Oyster void Pack(unsigned char buffer[], std::string& str) { - short len = str.length(); + short len = (short)str.length(); Pack(buffer, len); buffer += 2; memcpy(buffer, str.c_str(), len); @@ -153,7 +153,7 @@ namespace Oyster fnorm = fnorm - 1.0; // calculate the binary form (non-float) of the significand data - significand = fnorm * ((1LL << significandbits) + 0.5f); + significand = (long long)(fnorm * ((1LL << significandbits) + 0.5f)); // get the biased exponent exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias @@ -169,7 +169,7 @@ namespace Oyster //bool (1-bit) bool Unpackb(unsigned char buffer[]) { - return (bool)buffer; + return buffer; } //char (8-bit) @@ -305,7 +305,7 @@ namespace Oyster return 0.0; // pull the significand - result = (i&((1LL << significandbits) - 1)); // mask + result = (long double)(i&((1LL << significandbits) - 1)); // mask result /= (1LL << significandbits); // convert back to float result += 1.0f; // add the one back on diff --git a/Code/Network/NetworkDependencies/PostBox.h b/Code/Network/NetworkDependencies/PostBox.h index b2e553f8..f6db2c44 100644 --- a/Code/Network/NetworkDependencies/PostBox.h +++ b/Code/Network/NetworkDependencies/PostBox.h @@ -23,7 +23,7 @@ namespace Oyster virtual ~PostBox(); virtual void PostMessage(T& message); - virtual void FetchMessage(T& message); + virtual bool FetchMessage(T& message); virtual bool IsFull(); private: @@ -49,13 +49,14 @@ namespace Oyster } template - void PostBox::FetchMessage(T& message) + bool PostBox::FetchMessage(T& message) { if(IsFull()) { - message = messages.Front(); - messages.Pop(); + message = messages.Pop(); + return true; } + return false; } template diff --git a/Code/Network/NetworkDependencies/Protocols.h b/Code/Network/NetworkDependencies/Protocols.h index 8defcfb3..adf7154e 100644 --- a/Code/Network/NetworkDependencies/Protocols.h +++ b/Code/Network/NetworkDependencies/Protocols.h @@ -22,7 +22,7 @@ namespace Oyster PackageType_header, PackageType_test, PackageType_input, - PackageType_update_position + PackageType_player_pos, }; struct ProtocolHeader @@ -45,6 +45,16 @@ namespace Oyster virtual ~ProtocolTest() { delete[] f; } }; + struct ProtocolPlayerPos : public ProtocolHeader + { + int ID; + unsigned int nrOfFloats; + float *matrix; + + ProtocolPlayerPos() { this->packageType = PackageType_player_pos; } + virtual ~ProtocolPlayerPos() { delete[] matrix; } + }; + //Holding every protocol in an union. //Used because we now don't have to type case our protocol when we recieve them. @@ -56,6 +66,7 @@ namespace Oyster { ProtocolHeader* pHeader; ProtocolTest *pTest; + ProtocolPlayerPos *pPlayerPos; }Protocol; @@ -75,6 +86,12 @@ namespace Oyster delete Protocol.pTest; } break; + case PackageType_player_pos: + if(Protocol.pPlayerPos) + { + delete Protocol.pPlayerPos; + } + break; } } }; diff --git a/Code/Network/NetworkDependencies/ThreadedClient.cpp b/Code/Network/NetworkDependencies/ThreadedClient.cpp new file mode 100644 index 00000000..a49caabd --- /dev/null +++ b/Code/Network/NetworkDependencies/ThreadedClient.cpp @@ -0,0 +1,143 @@ +#include "ThreadedClient.h" +#include "OysterByte.h" + +#include +using namespace Oyster::Network; +using namespace Oyster::Thread; +using namespace Utility::DynamicMemory; + +ThreadedClient::ThreadedClient() +{ + this->connection = new Connection(); + this->sendPostBox = new PostBox>(); + this->recvPostBox = NULL; + + connection->SetBlockingMode(false); +} + +ThreadedClient::ThreadedClient(unsigned int socket) +{ + this->connection = new Connection(socket); + this->sendPostBox = new PostBox>(); + this->recvPostBox = NULL; + + connection->SetBlockingMode(false); + + thread.Create(this, true); +} + +ThreadedClient::ThreadedClient(IPostBox>* postBox, unsigned int socket) +{ + this->connection = new Connection(socket); + this->sendPostBox = new PostBox>; + this->recvPostBox = postBox; + + connection->SetBlockingMode(false); + + thread.Create(this, true); +} + +ThreadedClient::~ThreadedClient() +{ + thread.Terminate(); + delete this->connection; + this->connection = NULL; + this->recvPostBox = NULL; + + if(sendPostBox != NULL) + { + delete sendPostBox; + this->sendPostBox = NULL; + } +} + +void ThreadedClient::Send(SmartPointer& byte) +{ + this->sendPostBox->PostMessage(byte); +} + +int ThreadedClient::Send() +{ + int errorCode = 0; + + if(sendPostBox->IsFull()) + { + SmartPointer temp = new OysterByte; + sendPostBox->FetchMessage(temp); + errorCode = this->connection->Send(temp); + } + + return errorCode; +} + +int ThreadedClient::Recv() +{ + int errorCode = -1; + + SmartPointer temp = new OysterByte; + errorCode = this->connection->Recieve(temp); + + if(errorCode == 0) + { + stdMutex.lock(); + recvPostBox->PostMessage(temp); + stdMutex.unlock(); + } + + return errorCode; +} + +void ThreadedClient::ThreadEntry() +{ + std::cout<< "Client Thread started" << std::endl; +} + +void ThreadedClient::ThreadExit() +{ + std::cout << "Client Thread exit" << std::endl; +} + +#include + +bool ThreadedClient::DoWork() +{ + int errorCode; + errorCode = Send(); + /*if(errorCode != 0) + { + return false; + }*/ + + errorCode = Recv(); + /*if(errorCode != 0) + { + return false; + }*/ + Sleep(1); + return true; +} + +int ThreadedClient::Connect(unsigned short port, const char serverName[]) +{ + int errorCode; + + if((errorCode = connection->InitiateClient()) != 0) + { + return errorCode; + } + + else if((errorCode = connection->Connect(port, serverName)) != 0) + { + return errorCode; + } + + thread.Create(this, true); + return 0; +} + +void ThreadedClient::setRecvPostBox(IPostBox> *postBox) +{ + stdMutex.lock(); + this->recvPostBox = postBox; + stdMutex.unlock(); +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/ThreadedClient.h b/Code/Network/NetworkDependencies/ThreadedClient.h new file mode 100644 index 00000000..ae026a61 --- /dev/null +++ b/Code/Network/NetworkDependencies/ThreadedClient.h @@ -0,0 +1,55 @@ +#ifndef NETWORK_DEPENDENCIES_THREADED_CLIENT_H +#define NETWORK_DEPENDENCIES_THREADED_CLIENT_H + +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#include "../../Misc/Thread/IThreadObject.h" +#include "PostBox.h" +#include "Connection.h" +#include "../../Misc/Thread/OysterThread.h" +#include "../../Misc/Thread/OysterMutex.h" +#include "../../Misc/Utilities.h" + +#include + +namespace Oyster +{ + namespace Network + { + class OysterByte; + class ThreadedClient : public Thread::IThreadObject + { + public: + ThreadedClient(); + ThreadedClient(unsigned int socket); + ThreadedClient(IPostBox> *postBox, unsigned int socket); + virtual ~ThreadedClient(); + + void Send(Utility::DynamicMemory::SmartPointer& byte); + + int Connect(unsigned short port, const char serverName[]); + + void setRecvPostBox(IPostBox> *postBox); + + private: + virtual int Send(); + virtual int Recv(); + + virtual void ThreadEntry(); + virtual void ThreadExit(); + virtual bool DoWork(); + + private: + Connection* connection; + IPostBox> *sendPostBox; + IPostBox> *recvPostBox; + Oyster::Thread::OysterThread thread; + std::mutex stdMutex; + + }; + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Translator.cpp b/Code/Network/NetworkDependencies/Translator.cpp index 4bb739ca..94f66d62 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; -void Translator::Pack( ProtocolHeader &header, OysterByte& bytes ) +void Translator::Pack( ProtocolHeader &header, Utility::DynamicMemory::SmartPointer &bytes ) { MessageHeader *message = NULL; @@ -17,23 +17,27 @@ void Translator::Pack( ProtocolHeader &header, OysterByte& bytes ) case PackageType_test: message = new MessageTest(); break; + + case PackageType_player_pos: + message = new MessagePlayerPos(); + break; } if(message != NULL) { - message->Pack(header, bytes); + message->Pack(header, *bytes); delete message; message = NULL; } } -void Translator::Unpack(ProtocolSet* set, OysterByte& bytes ) +void Translator::Unpack(ProtocolSet* set, Utility::DynamicMemory::SmartPointer &bytes) { ProtocolHeader *header = new ProtocolHeader(); MessageHeader *message = new MessageHeader(); - message->Unpack(bytes, *header); + message->Unpack(*bytes, *header); delete message; message = NULL; @@ -44,13 +48,19 @@ void Translator::Unpack(ProtocolSet* set, OysterByte& bytes ) case PackageType_header: message = new MessageHeader(); set->Protocol.pHeader = new ProtocolHeader; - message->Unpack(bytes, *set->Protocol.pHeader); + message->Unpack(*bytes, *set->Protocol.pHeader); break; case PackageType_test: message = new MessageTest(); set->Protocol.pTest = new ProtocolTest; - message->Unpack(bytes, *set->Protocol.pTest); + message->Unpack(*bytes, *set->Protocol.pTest); + break; + + case PackageType_player_pos: + message = new MessagePlayerPos(); + set->Protocol.pPlayerPos = new ProtocolPlayerPos; + message->Unpack(*bytes, *set->Protocol.pPlayerPos); break; } diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 56459f72..94c88457 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -20,8 +20,8 @@ namespace Oyster Translator () { }; ~Translator() { }; - void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes ); - void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ); + void Pack (Protocols::ProtocolHeader &header, Utility::DynamicMemory::SmartPointer &bytes ); + void Unpack (Protocols::ProtocolSet* set, Utility::DynamicMemory::SmartPointer &bytes ); private: diff --git a/Code/Network/NetworkDependencies/WinsockFunctions.cpp b/Code/Network/NetworkDependencies/WinsockFunctions.cpp index 3e2e32f3..772fc6ec 100644 --- a/Code/Network/NetworkDependencies/WinsockFunctions.cpp +++ b/Code/Network/NetworkDependencies/WinsockFunctions.cpp @@ -24,7 +24,7 @@ std::wstring GetErrorMessage(int errorCode) (LPWSTR)&lpMessage, 0 , NULL ); - + if(bufLen) { retVal = lpMessage; diff --git a/Code/Network/NetworkDependencies/main.cpp b/Code/Network/NetworkDependencies/main.cpp deleted file mode 100644 index e69de29b..00000000 diff --git a/Code/Network/OysterNetworkClient/Client.cpp b/Code/Network/OysterNetworkClient/Client.cpp deleted file mode 100644 index daffe9b6..00000000 --- a/Code/Network/OysterNetworkClient/Client.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "Client.h" - -using namespace Oyster::Network::Client; - -Client::Client() -{ - connection = new Connection(); -} - -Client::~Client() -{ - delete this->connection; - connection = 0; -} - -int Client::Connect(unsigned int port, char filename[]) -{ - int errorCode; - - if((errorCode = connection->InitiateClient()) != 0) - { - return errorCode; - } - - if((errorCode = connection->Connect(port, filename)) != 0) - { - return errorCode; - } - - return 0; -} - -void Client::Send(Oyster::Network::OysterByte& bytes) -{ - connection->Send(bytes); -} - -void Client::Recv(Oyster::Network::OysterByte& bytes) -{ - connection->Recieve(bytes); -} \ No newline at end of file diff --git a/Code/Network/OysterNetworkClient/Client.h b/Code/Network/OysterNetworkClient/Client.h deleted file mode 100644 index 6e69e657..00000000 --- a/Code/Network/OysterNetworkClient/Client.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef NETWORK_CLIENT_CLIENT_H -#define NETWORK_CLIENT_CLIENT_H - -///////////////////////////////////// -// Created by Pontus Fransson 2013 // -///////////////////////////////////// - -#include "../NetworkDependencies/Connection.h" -#include "../NetworkDependencies/OysterByte.h" - -namespace Oyster -{ - namespace Network - { - namespace Client - { - class Client - { - public: - Client(); - ~Client(); - - int Connect(unsigned int port, char filename[]); - - void Send(OysterByte& bytes); - void Recv(OysterByte& bytes); - - private: - ::Oyster::Network::Connection* connection; - }; - } - } -} - -#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index b8f1057f..72c011a2 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -6,15 +6,20 @@ #include "..\NetworkDependencies\Protocols.h" #include "../NetworkDependencies/OysterByte.h" #include "../../Misc/ThreadSafeQueue.h" -#include "Client.h" +#include "../NetworkDependencies/ThreadedClient.h" +#include "../../Misc/WinTimer.h" +#include "../../Misc/Utilities.h" #pragma comment(lib, "ws2_32.lib") using namespace std; using namespace Oyster::Network::Protocols; -using namespace Oyster::Network::Client; +using namespace Oyster::Network; +using namespace Utility; +using namespace Utility::DynamicMemory; -void chat(Client &client); +void chat(ThreadedClient &client); +void PrintOutMessage(ProtocolSet* set); int main() { @@ -27,10 +32,10 @@ int main() cout << "Client" << endl; //Create Client - Client client; + ThreadedClient* client = new ThreadedClient; //Connect to server - errorCode = client.Connect(9876, "localhost"); + errorCode = client->Connect(9876, "localhost"); if(errorCode != 0) { @@ -38,7 +43,8 @@ int main() wcout << "errorMessage: " << errorTest << endl; } - chat(client); + chat(*client); + delete client; ShutdownWinSock(); @@ -46,76 +52,79 @@ int main() return 0; } -void chat(Client &client) +void chat(ThreadedClient &client) { Oyster::Network::Translator *t = new Oyster::Network::Translator(); + IPostBox< SmartPointer> *postBox = new PostBox< SmartPointer>; - Oyster::Network::OysterByte msgRecv; - string msgSend = ""; + client.setRecvPostBox(postBox); + + SmartPointer msgRecv = new OysterByte(); + SmartPointer msgSend = new OysterByte(); ProtocolSet* set = new ProtocolSet; - ProtocolTest test; - test.numOfFloats = 5; - test.f = new float[test.numOfFloats]; - float temp = 12345.5654f; - for(int i = 0; i < 5; i++) + 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.f[i] = temp; + test.matrix[i] = temp; temp++; } + t->Pack(test, msgSend); - bool chatDone = false; + WinTimer timer; - while(!chatDone) + while(1) { - client.Recv(msgRecv); - - t->Unpack(set, msgRecv); - - switch(set->type) + //Fetch new messages from the postbox + if(postBox->FetchMessage(msgRecv)) { - 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; - } - - set->Release(); - msgRecv.Clear(1000); + t->Unpack(set, msgRecv); - /*std::getline(std::cin, msgSend); - - - - if( msgSend != "exit") - { - if(msgSend.length() < 1) - { - msgSend = "ERROR!"; - } - - test.textMessage = msgSend; - - t->Pack(test, msgRecv); - - client.Send(msgRecv); + //PrintOutMessage(set); + set->Release(); } - else + //Send message to server each second + if(timer.getElapsedSeconds() > 1) { - chatDone = true; + cout << "Sending to server." << endl; + timer.reset(); + client.Send(msgSend); } - - cin.clear();*/ - + 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 f4e13099..bb63d4cf 100644 --- a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj +++ b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj @@ -154,12 +154,8 @@ - - - - diff --git a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj.filters b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj.filters index 2e5e9ef6..cd4a498b 100644 --- a/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj.filters +++ b/Code/Network/OysterNetworkClient/OysterNetworkClient.vcxproj.filters @@ -18,13 +18,5 @@ Source Files - - Source Files - - - - - Header Files - \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/Client.cpp b/Code/Network/OysterNetworkServer/Client.cpp deleted file mode 100644 index 5cc15eec..00000000 --- a/Code/Network/OysterNetworkServer/Client.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Client.h" - -using namespace Oyster::Network; -using namespace Oyster::Network::Server; - -Client::Client(unsigned int socket) -{ - connection = new Connection(socket); -} - -Client::~Client() -{ - delete connection; -} - -void Client::Send(OysterByte& bytes) -{ - connection->Send(bytes); -} - -void Client::Recv(OysterByte& bytes) -{ - connection->Recieve(bytes); -} \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/Client.h b/Code/Network/OysterNetworkServer/Client.h deleted file mode 100644 index 2c5ba35f..00000000 --- a/Code/Network/OysterNetworkServer/Client.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef NETWORK_SERVER_CLIENT_H -#define NETWORK_SERVER_CLIENT_H - -///////////////////////////////////// -// Created by Pontus Fransson 2013 // -///////////////////////////////////// - -#include "../NetworkDependencies/Connection.h" -#include "../NetworkDependencies/OysterByte.h" - -namespace Oyster -{ - namespace Network - { - namespace Server - { - class Client - { - public: - Client(unsigned int socket); - ~Client(); - - void Send(OysterByte& bytes); - void Recv(OysterByte& bytes); - - 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 65136729..4452ee37 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -149,16 +149,20 @@ {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} + + {460d625f-2ac9-4559-b809-0ba89ceaedf4} + {c5aa09d0-6594-4cd3-bd92-1d380c7b3b50} - + - + + diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters index 3cb5827c..6073a0c5 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters @@ -18,12 +18,15 @@ Source Files - + Source Files - + + Header Files + + Header Files diff --git a/Code/Network/OysterNetworkServer/RecieverObject.h b/Code/Network/OysterNetworkServer/RecieverObject.h new file mode 100644 index 00000000..681c7204 --- /dev/null +++ b/Code/Network/OysterNetworkServer/RecieverObject.h @@ -0,0 +1,19 @@ +#ifndef OYSTER_NETWORK_SERVER_RECIEVER_OBJECT_H +#define OYSTER_NETWORK_SERVER_RECIEVER_OBJECT_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#include "../NetworkDependencies/Protocols.h" +#include "../NetworkDependencies/OysterByte.h" +#include "../../Misc/Utilities.h" + +class RecieverObject +{ +public: + virtual void ProcFunc(Utility::DynamicMemory::SmartPointer msg) = 0; + +}; + +#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 9dd0a0c1..f97aeb98 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -3,126 +3,53 @@ #include #include #include "../NetworkDependencies/WinsockFunctions.h" -#include "../NetworkDependencies/Listener.h" -#include "../NetworkDependencies/Translator.h" -#include "Client.h" -#include "../NetworkDependencies/OysterByte.h" -#include "../NetworkDependencies/PostBox.h" -#include "../../Misc/WinTimer.h" - -#pragma comment(lib, "ws2_32.lib") +#include "TestClass.h" using namespace std; -using namespace Oyster::Network::Server; -using namespace Oyster::Network; -using namespace ::Protocols; -using namespace Utility; + +void clientProc(Oyster::Network::NetworkClient* client); int main() { - OysterByte recvBuffer; - IPostBox* postBox = new PostBox(); - - cout << "Server" << endl; - Translator t; - int errorCode; - if(!InitWinSock()) { cout << "errorMessage: unable to start winsock" << endl; } - //Create socket - Listener listener; - listener.Init(9876); - listener.SetPostBox(postBox); - Sleep(1000); + Test test; - //Start listening - //Accept a client - ProtocolTest test; - test.clientID = 0; - test.size = 2; - test.textMessage = "hej"; - test.numOfFloats = 0; - test.f = new float[test.numOfFloats]; - float temp = 395.456f; - for(int i = 0; i < (int)test.numOfFloats; i++) - { - test.f[i] = temp; - temp--; - } + cout << "Server" << endl; - t.Pack(test, recvBuffer); - - WinTimer timer; + test.mainLoop(); - vector clients; - int client = -1; while(1) { - client = -1; - postBox->FetchMessage(client); - if(client != -1) - { - cout << "Client connected: " << client << endl; - clients.push_back(new Client(client)); - - clients.at(clients.size()-1)->Send(recvBuffer); - } - - //Send a message every 1 secounds to all clients. + //Fetch new clients from the postbox + /* + //Send a message every 1 seconds to all clients. if(timer.getElapsedSeconds() > 1) { cout << "Sending to " << clients.size() << " clients." << endl; timer.reset(); for(int i = 0; i < (int)clients.size(); i++) { - clients.at(i)->Send(recvBuffer); + clients.at(i)->Send(sendBuffer); } - } - Sleep(100); - } - listener.Shutdown(); + }*/ - /* - ProtocolSet* set = new ProtocolSet; - - client1.Send(recvBuffer); - - while(1) - { - client1.Recv(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 < (int)set->Protocol.pTest->numOfFloats; i++) + /*//Fetch messages + if(recvPostBox->FetchMessage(recvBuffer)) { - cout << set->Protocol.pTest->f[i] << ' '; - } - cout << endl; - set->Release(); - client2.Send(recvBuffer); + t.Unpack(set, recvBuffer); - client2.Recv(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 < (int)set->Protocol.pTest->numOfFloats; i++) - { - cout << set->Protocol.pTest->f[i] << ' '; - } - cout << endl; - set->Release(); - client1.Send(recvBuffer); + //PrintOutMessage(set); + set->Release(); + }*/ } + Sleep(1000); - ShutdownWinSock(); - delete set; - */ system("pause"); + return 0; -} +} \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/TestClass.cpp b/Code/Network/OysterNetworkServer/TestClass.cpp new file mode 100644 index 00000000..1580b644 --- /dev/null +++ b/Code/Network/OysterNetworkServer/TestClass.cpp @@ -0,0 +1,86 @@ +#include "TestClass.h" +#include "../../Misc/WinTimer.h" +#include + +using namespace Oyster::Network; +using namespace ::Server; +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; + initDesc.proc = NULL; + 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::ProcFunc(Utility::DynamicMemory::SmartPointer msg) +{ + + 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 new file mode 100644 index 00000000..439e5b1f --- /dev/null +++ b/Code/Network/OysterNetworkServer/TestClass.h @@ -0,0 +1,37 @@ +#ifndef TEST_CLASS_H +#define TEST_CLASS_H + +#include "RecieverObject.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 + +class Test : public RecieverObject +{ +public: + Test(); + ~Test(); + + void mainLoop(); + + virtual void ProcFunc(Utility::DynamicMemory::SmartPointer msg); + 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::Server::NetworkServer server; + +}; + +#endif \ No newline at end of file