From 2cb8bbd8a955fe3f08549295b7b062eb5db5f379 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Mon, 9 Dec 2013 22:22:05 +0100 Subject: [PATCH] Network - IServer running on separate thread. IServer accepting clients. --- Code/Misc/Utilities.h | 2 +- Code/Network/NetworkDependencies/Listener.cpp | 10 +-- Code/Network/NetworkDependencies/Listener.h | 6 +- .../NetworkDependencies.vcxproj | 1 - .../NetworkDependencies.vcxproj.filters | 1 - Code/Network/NetworkDependencies/main.cpp | 0 Code/Network/OysterNetworkServer/IServer.cpp | 73 ++++++++++++---- Code/Network/OysterNetworkServer/IServer.h | 4 - Code/Network/OysterNetworkServer/ISession.h | 34 -------- .../OysterNetworkServer.vcxproj | 4 +- .../OysterNetworkServer.vcxproj.filters | 10 ++- .../OysterNetworkServer/RecieverObject.h | 19 ++++ .../OysterNetworkServer/ServerMain.cpp | 87 ++++--------------- .../Network/OysterNetworkServer/TestClass.cpp | 62 +++++++++++++ Code/Network/OysterNetworkServer/TestClass.h | 34 ++++++++ 15 files changed, 211 insertions(+), 136 deletions(-) delete mode 100644 Code/Network/NetworkDependencies/main.cpp delete mode 100644 Code/Network/OysterNetworkServer/ISession.h create mode 100644 Code/Network/OysterNetworkServer/RecieverObject.h create mode 100644 Code/Network/OysterNetworkServer/TestClass.cpp create mode 100644 Code/Network/OysterNetworkServer/TestClass.h diff --git a/Code/Misc/Utilities.h b/Code/Misc/Utilities.h index a8e4b406..6112bea1 100644 --- a/Code/Misc/Utilities.h +++ b/Code/Misc/Utilities.h @@ -201,6 +201,7 @@ namespace Utility T *_ptr; /** Destroys the pointer and returns the memory allocated. */ + void Destroy(); public: SmartPointer(); @@ -216,7 +217,6 @@ namespace Utility operator T* (); operator bool(); - void Destroy(); /** * Returns the connected pointer */ diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index d606df10..1aec11da 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -8,7 +8,7 @@ Listener::Listener() connection = NULL; } -Listener::Listener(Oyster::Network::IPostBox>* postBox) +Listener::Listener(Oyster::Network::IPostBox* postBox) { connection = NULL; this->postBox = postBox; @@ -58,7 +58,7 @@ void Listener::Shutdown() thread.Stop(); } -void Listener::SetPostBox(Oyster::Network::IPostBox>* postBox) +void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) { stdMutex.lock(); //mutex.LockMutex(); @@ -69,10 +69,10 @@ void Listener::SetPostBox(Oyster::Network::IPostBox>* postBox) int Listener::Accept() { - SmartPointer clientSocket = SmartPointer(new int()); - *clientSocket = connection->Listen(); + int clientSocket = -1; + clientSocket = connection->Listen(); - if(*clientSocket != -1) + if(clientSocket != -1) { stdMutex.lock(); //mutex.LockMutex(); diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index abf57ba9..80b7543f 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -22,7 +22,7 @@ namespace Oyster { public: Listener(); - Listener(Oyster::Network::IPostBox>* postBox); + Listener(Oyster::Network::IPostBox* postBox); ~Listener(); bool Init(unsigned int port); @@ -32,7 +32,7 @@ namespace Oyster void Shutdown(); - void SetPostBox(IPostBox>* postBox); + void SetPostBox(IPostBox* postBox); private: //Thread functions @@ -50,7 +50,7 @@ namespace Oyster OysterMutex mutex; std::mutex stdMutex; - IPostBox>* postBox; + IPostBox* postBox; }; } diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index ca48c730..d33e34d3 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -153,7 +153,6 @@ - diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index 9d10d4dc..3189aba4 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -3,7 +3,6 @@ - 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/OysterNetworkServer/IServer.cpp b/Code/Network/OysterNetworkServer/IServer.cpp index 82d5a155..84da54ba 100644 --- a/Code/Network/OysterNetworkServer/IServer.cpp +++ b/Code/Network/OysterNetworkServer/IServer.cpp @@ -3,16 +3,18 @@ #include "IClient.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 IServer::PrivateData +struct IServer::PrivateData : public IThreadObject { PrivateData(); ~PrivateData(); @@ -21,20 +23,29 @@ struct IServer::PrivateData bool Start(); bool Stop(); bool Shutdown(); - + + void CheckForNewClient(); + + virtual bool DoWork(); + // IListener* listener; INIT_DESC initDesc; bool started; - IPostBox> *postBox; + //Postbox for new clients + IPostBox *postBox; + + //Server thread + OysterThread thread; + }; IServer::PrivateData::PrivateData() { listener = 0; started = false; - postBox = new PostBox>(); + postBox = new PostBox; } IServer::PrivateData::~PrivateData() @@ -53,9 +64,11 @@ bool IServer::PrivateData::Init(INIT_DESC& initDesc) this->initDesc = initDesc; //Initiate listener - listener = new Listener(); + listener = new Listener(postBox); ((Listener*)listener)->Init(this->initDesc.port, false); + thread.Create(this, false); + return true; } @@ -65,6 +78,8 @@ bool IServer::PrivateData::Start() ((Listener*)listener)->Start(); started = true; + thread.Start(); + return true; } @@ -77,22 +92,60 @@ bool IServer::PrivateData::Stop() started = false; + thread.Stop(); + return true; } bool IServer::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 IServer::PrivateData::CheckForNewClient() +{ + if(postBox->IsFull()) + { + int clientSocketNum; + postBox->FetchMessage(clientSocketNum); + + //Safety check that is probably not needed. + if(clientSocketNum == -1) + { + return; + } + + //Create the new client + IClient* client = new IClient(); + initDesc.proc(client); + } +} + +bool IServer::PrivateData::DoWork() +{ + CheckForNewClient(); + + return true; +} + /************************************* IServer *************************************/ @@ -138,16 +191,6 @@ bool IServer::Shutdown() return true; } -void IServer::AddSession(ISession* session) -{ - -} - -void IServer::RemoveSession(ISession* session) -{ - -} - bool IServer::IsStarted() const { return privateData->started; diff --git a/Code/Network/OysterNetworkServer/IServer.h b/Code/Network/OysterNetworkServer/IServer.h index e7382192..0f34fdc3 100644 --- a/Code/Network/OysterNetworkServer/IServer.h +++ b/Code/Network/OysterNetworkServer/IServer.h @@ -15,7 +15,6 @@ namespace Oyster { class IServer { - class ISession; public: struct INIT_DESC { @@ -31,9 +30,6 @@ namespace Oyster virtual bool Stop(); virtual bool Shutdown(); - virtual void AddSession(ISession* session); - virtual void RemoveSession(ISession* session); - virtual bool IsStarted() const; private: diff --git a/Code/Network/OysterNetworkServer/ISession.h b/Code/Network/OysterNetworkServer/ISession.h deleted file mode 100644 index 84ca05e0..00000000 --- a/Code/Network/OysterNetworkServer/ISession.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef OYSTER_NETWORK_SERVER_I_SESSION_H -#define OYSTER_NETWORK_SERVER_I_SESSION_H - -///////////////////////////////////// -// Created by Pontus Fransson 2013 // -///////////////////////////////////// - -class ISession -{ - class IClient; -public: - struct INIT_DESC - { - - }; - - ISession(); - virtual ~ISession(); - - virtual bool Init(); - virtual bool Start(); - virtual bool Stop(); - virtual bool Shutdown(); - - virtual void SendToAll(); - - virtual void AddClient(IClient* client); - virtual void RemoveClient(IClient* client); - -private: -}; - - -#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj index 196da525..82cec50e 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -156,11 +156,13 @@ + - + + diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters index fc74d185..23947d8a 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters @@ -21,15 +21,21 @@ Source Files + + Source Files + Header Files - + Header 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 9564a024..ce8e2742 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -12,9 +12,9 @@ #include "../../Misc/Utilities.h" #include "../../Misc/Utilities-Impl.h" +#include "TestClass.h" #include "IServer.h" #include "IClient.h" -#include "ISession.h" #pragma comment(lib, "ws2_32.lib") @@ -25,18 +25,15 @@ using namespace ::Protocols; using namespace Utility; using namespace ::Utility::DynamicMemory; -void PrintOutMessage(ProtocolSet* set); void clientProc(IClient* client); -vector clients; int main() { + Test tests; + SmartPointer sendBuffer = new OysterByte; SmartPointer recvBuffer = new OysterByte; ProtocolSet* set = new ProtocolSet; - - IPostBox> *postBox = new PostBox>(); - IPostBox> *recvPostBox = new PostBox>(); cout << "Server" << endl; Translator t; @@ -46,22 +43,17 @@ int main() { cout << "errorMessage: unable to start winsock" << endl; } - /* + IServer server; IServer::INIT_DESC initDesc; initDesc.port = 9876; initDesc.proc = clientProc; server.Init(initDesc); - */ - //Create socket - Listener listener; - listener.Init(9876); - listener.SetPostBox(postBox); - listener.Start(); + server.Start(); + Sleep(1000); - //Start listening - //Accept a client + //Create a test protocol ProtocolPlayerPos test; test.clientID = 0; test.ID = 5; @@ -74,28 +66,14 @@ int main() } t.Pack(test, sendBuffer); - - WinTimer timer; - - /* DEBUGGING: Connect 25 clients - for(int i = 0; i < 25; i++) - { - clients.push_back(new ThreadedClient(recvPostBox, 1)); - }*/ - SmartPointer client = int(); + WinTimer timer; + while(1) { //Fetch new clients from the postbox - if(postBox->FetchMessage(client)) - { - cout << "Client connected: " << *client << endl; - clients.push_back(new ThreadedClient(recvPostBox, *client)); - - clients.at(clients.size()-1)->Send(sendBuffer); - } - - //Send a message every 1 secounds to all clients. + /* + //Send a message every 1 seconds to all clients. if(timer.getElapsedSeconds() > 1) { cout << "Sending to " << clients.size() << " clients." << endl; @@ -104,60 +82,31 @@ int main() { clients.at(i)->Send(sendBuffer); } - } + }*/ - //Fetch messages + /*//Fetch messages if(recvPostBox->FetchMessage(recvBuffer)) { t.Unpack(set, recvBuffer); //PrintOutMessage(set); set->Release(); - } + }*/ Sleep(1); } - //server.Stop(); - //server.Shutdown(); - listener.Shutdown(); + server.Stop(); + server.Shutdown(); + //listener.Shutdown(); Sleep(1000); system("pause"); - for(int i = 0; i < (int)clients.size(); i++) - delete clients.at(i); - return 0; } -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; - } -} - void clientProc(IClient* client) { cout << "Proc" << endl; - //clients.push_back(client); + clients.push_back(client); } \ 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..ec50bf9e --- /dev/null +++ b/Code/Network/OysterNetworkServer/TestClass.cpp @@ -0,0 +1,62 @@ +#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>; +} + +Test::~Test() +{ + for(int i = 0; i < (int)clients.size(); i++) + delete clients.at(i); +} + +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..e3edf31f --- /dev/null +++ b/Code/Network/OysterNetworkServer/TestClass.h @@ -0,0 +1,34 @@ +#ifndef TEST_CLASS_H +#define TEST_CLASS_H + +#include "RecieverObject.h" +#include "../../Misc/Utilities.h" +#include "../NetworkDependencies/OysterByte.h" +#include "../NetworkDependencies/PostBox.h" +#include "IClient.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; + + +}; + +#endif \ No newline at end of file