From 161760614518153aee7bffceea1c5611c9463f33 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Thu, 28 Nov 2013 16:15:28 +0100 Subject: [PATCH] PostBox Created PostBox and IPostBox. Listener post new clients to PostBox. --- Code/Misc/Thread/OysterThread_Impl.cpp | 2 +- Code/Network/NetworkDependencies/IPostBox.h | 25 +++++++ Code/Network/NetworkDependencies/ITranslate.h | 2 +- Code/Network/NetworkDependencies/Listener.cpp | 22 +++--- Code/Network/NetworkDependencies/Listener.h | 8 ++- .../NetworkDependencies.vcxproj | 10 +-- .../NetworkDependencies.vcxproj.filters | 2 + Code/Network/NetworkDependencies/PostBox.h | 72 +++++++++++++++++++ .../NetworkDependencies/Translator.cpp | 4 +- Code/Network/NetworkDependencies/Translator.h | 8 +-- .../OysterNetworkClient/ClientMain.cpp | 4 +- .../OysterNetworkServer.vcxproj | 3 - .../OysterNetworkServer/ServerMain.cpp | 33 +++++++-- 13 files changed, 151 insertions(+), 44 deletions(-) create mode 100644 Code/Network/NetworkDependencies/IPostBox.h create mode 100644 Code/Network/NetworkDependencies/PostBox.h diff --git a/Code/Misc/Thread/OysterThread_Impl.cpp b/Code/Misc/Thread/OysterThread_Impl.cpp index 9b01e6d5..46b889fe 100644 --- a/Code/Misc/Thread/OysterThread_Impl.cpp +++ b/Code/Misc/Thread/OysterThread_Impl.cpp @@ -62,7 +62,7 @@ using namespace Utility::DynamicMemory::SmartPointer; ~PrivateData() { //@todo TODO: Make detatch avalible. - //this->threadData->workerThread->detach(); + this->threadData->workerThread->detach(); this->threadData->owner = 0; diff --git a/Code/Network/NetworkDependencies/IPostBox.h b/Code/Network/NetworkDependencies/IPostBox.h new file mode 100644 index 00000000..31c1bef7 --- /dev/null +++ b/Code/Network/NetworkDependencies/IPostBox.h @@ -0,0 +1,25 @@ +#ifndef NETWORK_DEPENDENCIES_I_POST_BOX_H +#define NETWORK_DEPENDENCIES_I_POST_BOX_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +namespace Oyster +{ + namespace Network + { + template + class IPostBox + { + public: + virtual ~IPostBox() {} + virtual void PostMessage(T& message) = 0; + virtual void FetchMessage(T& message) = 0; + virtual bool IsFull() = 0; + + }; + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/ITranslate.h b/Code/Network/NetworkDependencies/ITranslate.h index 1143c716..90eb1431 100644 --- a/Code/Network/NetworkDependencies/ITranslate.h +++ b/Code/Network/NetworkDependencies/ITranslate.h @@ -15,7 +15,7 @@ namespace Oyster public: virtual void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes) = 0; - virtual Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0; + virtual void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ) = 0; }; } diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index 1af94613..e1b8fa08 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -4,8 +4,6 @@ using namespace Oyster::Network::Server; Listener::Listener() { - newSocket = false; - tempSocket = 0; } Listener::~Listener() @@ -27,19 +25,16 @@ bool Listener::Init(unsigned int port) return true; } -int Listener::GetNewClient() +void Listener::Shutdown() +{ + thread.Terminate(); +} + +void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) { mutex.LockMutex(); - int temp = -1; - - if(newSocket) - { - temp = tempSocket; - newSocket = false; - } + this->postBox = postBox; mutex.UnlockMutex(); - - return temp; } int Listener::Accept() @@ -48,8 +43,7 @@ int Listener::Accept() clientSocket = connection->Listen(); mutex.LockMutex(); - tempSocket = clientSocket; - newSocket = true; + postBox->PostMessage(clientSocket); mutex.UnlockMutex(); return clientSocket; diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index aa3817cf..5888aed9 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -9,7 +9,7 @@ #include "../NetworkDependencies/Connection.h" #include "../../Misc/Thread/OysterThread.h" #include "../../Misc/Thread/OysterMutex.h" - +#include "IPostBox.h" namespace Oyster { @@ -24,8 +24,10 @@ namespace Oyster ~Listener(); bool Init(unsigned int port); + void Shutdown(); int Accept(); int GetNewClient(); + void SetPostBox(IPostBox* postBox); //Thread functions bool DoWork(); @@ -37,12 +39,12 @@ namespace Oyster private: ::Oyster::Network::Connection* connection; - int tempSocket; - bool newSocket; ::Oyster::Thread::OysterThread thread; OysterMutex mutex; + IPostBox* postBox; + }; } } diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index 5b660b13..dfabbcba 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -149,14 +149,6 @@ {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} - - {f10cbc03-9809-4cba-95d8-327c287b18ee} - true - true - true - false - false - @@ -173,6 +165,7 @@ + @@ -180,6 +173,7 @@ + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index dc3225ff..74cb9a56 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -25,5 +25,7 @@ + + \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/PostBox.h b/Code/Network/NetworkDependencies/PostBox.h new file mode 100644 index 00000000..a24cea5e --- /dev/null +++ b/Code/Network/NetworkDependencies/PostBox.h @@ -0,0 +1,72 @@ +#ifndef NETWORK_DEPENDENCIES_POST_BOX_H +#define NETWORK_DEPENDENCIES_POST_BOX_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#include "IPostBox.h" +#include +#include "../../Misc/Thread/OysterMutex.h" + +namespace Oyster +{ + namespace Network + { + template + class PostBox : public IPostBox + { + public: + PostBox(); + virtual ~PostBox(); + + virtual void PostMessage(T& message); + virtual void FetchMessage(T& message); + virtual bool IsFull(); + + private: + std::queue messages; + OysterMutex mutex; + + }; + + //Implementation of PostBox + template + PostBox::PostBox() + { + } + + template + PostBox::~PostBox() + { + } + + template + void PostBox::PostMessage(T& message) + { + mutex.LockMutex(); + messages.push(message); + mutex.UnlockMutex(); + } + + template + void PostBox::FetchMessage(T& message) + { + mutex.LockMutex(); + if(IsFull()) + { + message = messages.front(); + messages.pop(); + } + mutex.UnlockMutex(); + } + + template + bool PostBox::IsFull() + { + return !messages.empty(); + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Translator.cpp b/Code/Network/NetworkDependencies/Translator.cpp index b0b6d587..4bb739ca 100644 --- a/Code/Network/NetworkDependencies/Translator.cpp +++ b/Code/Network/NetworkDependencies/Translator.cpp @@ -28,7 +28,7 @@ void Translator::Pack( ProtocolHeader &header, OysterByte& bytes ) } } -ProtocolSet* Translator::Unpack(ProtocolSet* set, OysterByte& bytes ) +void Translator::Unpack(ProtocolSet* set, OysterByte& bytes ) { ProtocolHeader *header = new ProtocolHeader(); MessageHeader *message = new MessageHeader(); @@ -60,5 +60,5 @@ ProtocolSet* Translator::Unpack(ProtocolSet* set, OysterByte& bytes ) } delete header; - return set; + //return set; } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Translator.h b/Code/Network/NetworkDependencies/Translator.h index 44ce39d2..56459f72 100644 --- a/Code/Network/NetworkDependencies/Translator.h +++ b/Code/Network/NetworkDependencies/Translator.h @@ -17,15 +17,13 @@ namespace Oyster class Translator : public ITranslate { public: - Translator () { /*msg = new unsigned char[1601];*/ }; - ~Translator() { /*if(msg != NULL) { delete [] this->msg; }*/ }; + Translator () { }; + ~Translator() { }; void Pack (Protocols::ProtocolHeader &header, OysterByte& bytes ); - Protocols::ProtocolSet* Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ); + void Unpack (Protocols::ProtocolSet* set, OysterByte& bytes ); private: - //unsigned char* msg; - //OysterByte bytes; }; } diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index ce66446f..6ebc8dfc 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -88,7 +88,7 @@ void chat(Client &client) set->Release(); msgRecv.Clear(1000); - std::getline(std::cin, msgSend); + /*std::getline(std::cin, msgSend); @@ -111,7 +111,7 @@ void chat(Client &client) chatDone = true; } - cin.clear(); + cin.clear();*/ } diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj index 260586da..65136729 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -149,9 +149,6 @@ {2ec4dded-8f75-4c86-a10b-e1e8eb29f3ee} - - {f10cbc03-9809-4cba-95d8-327c287b18ee} - {c5aa09d0-6594-4cd3-bd92-1d380c7b3b50} diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 6d958e2d..cc2e398b 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -1,11 +1,14 @@ #include #include +#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") @@ -13,10 +16,12 @@ using namespace std; using namespace Oyster::Network::Server; using namespace Oyster::Network; using namespace ::Protocols; +using namespace Utility; int main() { OysterByte recvBuffer; + IPostBox* postBox = new PostBox(); cout << "Server" << endl; Translator t; @@ -30,6 +35,7 @@ int main() //Create socket Listener listener; listener.Init(9876); + listener.SetPostBox(postBox); Sleep(1000); //Start listening //Accept a client @@ -37,7 +43,7 @@ int main() test.clientID = 0; test.size = 2; test.textMessage = "hej"; - test.numOfFloats = 35; + test.numOfFloats = 0; test.f = new float[test.numOfFloats]; float temp = 395.456f; for(int i = 0; i < (int)test.numOfFloats; i++) @@ -48,18 +54,35 @@ int main() t.Pack(test, recvBuffer); + WinTimer timer; + + vector clients; + int client = -1; while(1) { - int client = listener.GetNewClient(); + client = -1; + postBox->FetchMessage(client); if(client != -1) { cout << "Client connected: " << client << endl; - Client client1(client); + clients.push_back(new Client(client)); - client1.Send(recvBuffer); + clients.at(clients.size()-1)->Send(recvBuffer); } - //Sleep(100); + + //Send a message every 1 secounds 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); + } + } + Sleep(100); } + listener.Shutdown(); /* int clientSocket = listener.Accept();