diff --git a/Code/Misc/ThreadSafeQueue.h b/Code/Misc/ThreadSafeQueue.h index 2e10b74c..b26d35ff 100644 --- a/Code/Misc/ThreadSafeQueue.h +++ b/Code/Misc/ThreadSafeQueue.h @@ -94,9 +94,9 @@ namespace Oyster template < typename Type > void ThreadSafeQueue::Push(Type item) { + mutex.LockMutex(); Node *e = new Node(item); - mutex.LockMutex(); if(this->front != NULL) { this->back->next = e; @@ -140,32 +140,33 @@ namespace Oyster Type ThreadSafeQueue::Front() { mutex.LockMutex(); - - return this->front->item; - + Type temp = this->front->item; mutex.UnlockMutex(); - + + return temp; + } template < typename Type > Type ThreadSafeQueue::Back() { mutex.LockMutex(); - - return this->back->item; - + Type temp = this->back->item; mutex.UnlockMutex(); + return temp; + } template < typename Type > int ThreadSafeQueue::Size() { mutex.LockMutex(); - - return this->nrOfNodes; - + int size = this->nrOfNodes; mutex.UnlockMutex(); + + return size; + } template < typename Type > diff --git a/Code/Network/NetworkDependencies/IClient.h b/Code/Network/NetworkDependencies/IClient.h deleted file mode 100644 index bf044bae..00000000 --- a/Code/Network/NetworkDependencies/IClient.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef NETWORK_DEPENDENCIES_I_CLIENT_H -#define NETWORK_DEPENDENCIES_I_CLIENT_H - -////////////////////////////////// -// Created by Sam Svensson 2013 // -////////////////////////////////// - -#include "../NetworkDependencies/Connection.h" -#include "../NetworkDependencies/OysterByte.h" - -namespace Oyster -{ - namespace Network - { - class IClient - { - - public: - virtual ~IClient() {}; - virtual int Send() = 0; - virtual int Recv() = 0; - - private: - - }; - } -} -#endif \ No newline at end of file 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/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index f00c282a..6a9fbeb5 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -8,6 +8,12 @@ Listener::Listener() connection = NULL; } +Listener::Listener(Oyster::Network::IPostBox>* postBox) +{ + connection = NULL; + this->postBox = postBox; +} + Listener::~Listener() { if(connection) @@ -16,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); @@ -27,6 +33,26 @@ 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.Stop(); @@ -61,13 +87,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 b9fdab21..16b884dd 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -6,11 +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 "../../Misc/Utilities.h" -#include "IPostBox.h" namespace Oyster { @@ -18,13 +18,18 @@ 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); diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index d320872f..ca48c730 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -165,7 +165,6 @@ - diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index 083f7a1d..9d10d4dc 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -2,38 +2,34 @@ + - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - + + + \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/ThreadedClient.cpp b/Code/Network/NetworkDependencies/ThreadedClient.cpp index 4330e9f6..ba6b2c3b 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.cpp +++ b/Code/Network/NetworkDependencies/ThreadedClient.cpp @@ -1,4 +1,5 @@ #include "ThreadedClient.h" +#include "OysterByte.h" #include using namespace Oyster::Network; @@ -25,7 +26,7 @@ ThreadedClient::ThreadedClient(unsigned int socket) thread.Create(this, true); } -ThreadedClient::ThreadedClient(IPostBox>* postBox, unsigned int socket) +ThreadedClient::ThreadedClient(IPostBox>* postBox, unsigned int socket) { this->connection = new Connection(socket); this->sendPostBox = new PostBox>; @@ -57,29 +58,32 @@ int ThreadedClient::Send(SmartPointer& byte) mutex.LockMutex(); this->sendPostBox->PostMessage(temp); mutex.UnlockMutex(); + return 0; } int ThreadedClient::Send() { int errorCode = 0; + mutex.LockMutex(); if(sendPostBox->IsFull()) { - //SmartPointer temp = NULL; - //sendPostBox->FetchMessage(temp); - //errorCode = this->connection->Send(temp); + SmartPointer temp = new OysterByte; + sendPostBox->FetchMessage(temp); + errorCode = this->connection->Send(temp); } mutex.UnlockMutex(); + return errorCode; } int ThreadedClient::Recv() { int errorCode = 0; - - /*SmartPointer temp = new OysterByte(); + + SmartPointer temp = new OysterByte; errorCode = this->connection->Recieve(temp); if(errorCode == 0) @@ -88,7 +92,7 @@ int ThreadedClient::Recv() recvPostBox->PostMessage(temp); mutex.UnlockMutex(); } - */ + return errorCode; } diff --git a/Code/Network/NetworkDependencies/ThreadedClient.h b/Code/Network/NetworkDependencies/ThreadedClient.h index 007f9338..3d12ab6f 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.h +++ b/Code/Network/NetworkDependencies/ThreadedClient.h @@ -5,9 +5,9 @@ // Created by Sam Svensson 2013 // ////////////////////////////////// -#include "../NetworkDependencies/IClient.h" #include "../../Misc/Thread/IThreadObject.h" -#include "../NetworkDependencies/PostBox.h" +#include "PostBox.h" +#include "Connection.h" #include "../../Misc/Thread/OysterThread.h" #include "../../Misc/Thread/OysterMutex.h" #include "../../Misc/Utilities.h" @@ -16,19 +16,20 @@ namespace Oyster { namespace Network { - class ThreadedClient : public IClient, public Thread::IThreadObject + class OysterByte; + class ThreadedClient : public Thread::IThreadObject { public: ThreadedClient(); ThreadedClient(unsigned int socket); - ThreadedClient(IPostBox> *postBox, unsigned int socket); + ThreadedClient(IPostBox> *postBox, unsigned int socket); virtual ~ThreadedClient(); - int Send(Utility::DynamicMemory::SmartPointer< OysterByte >& byte); + int Send(Utility::DynamicMemory::SmartPointer& byte); int Connect(unsigned short port, const char serverName[]); - void setRecvPostBox(IPostBox< Utility::DynamicMemory::SmartPointer< OysterByte >> *postBox); + void setRecvPostBox(IPostBox> *postBox); private: virtual int Send(); diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index d436759e..95f966b8 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -84,7 +84,7 @@ void chat(ThreadedClient &client) { t->Unpack(set, msgRecv); - PrintOutMessage(set); + //PrintOutMessage(set); set->Release(); } diff --git a/Code/Network/OysterNetworkServer/IClient.h b/Code/Network/OysterNetworkServer/IClient.h index f7a115c3..5b7fafe9 100644 --- a/Code/Network/OysterNetworkServer/IClient.h +++ b/Code/Network/OysterNetworkServer/IClient.h @@ -8,12 +8,13 @@ class IClient { public: + IClient() {} virtual ~IClient() {} - virtual void Disconnect() {}; - virtual bool IsConnected() {}; + virtual void Disconnect() {} + virtual bool IsConnected() {return true;} - virtual void Send() {}; + virtual void Send() {} private: diff --git a/Code/Network/OysterNetworkServer/IServer.cpp b/Code/Network/OysterNetworkServer/IServer.cpp index 17c50571..82d5a155 100644 --- a/Code/Network/OysterNetworkServer/IServer.cpp +++ b/Code/Network/OysterNetworkServer/IServer.cpp @@ -1,35 +1,139 @@ #include "IServer.h" +#include "../NetworkDependencies/Listener.h" +#include "IClient.h" +#include "../NetworkDependencies/PostBox.h" +#include "../../Misc/Utilities.h" + +using namespace Oyster::Network; +using namespace ::Server; +using namespace Utility::DynamicMemory; + +/************************************* + PrivateData +*************************************/ + +struct IServer::PrivateData +{ + PrivateData(); + ~PrivateData(); + + bool Init(INIT_DESC& initDesc); + bool Start(); + bool Stop(); + bool Shutdown(); + + // + IListener* listener; + INIT_DESC initDesc; + bool started; + + IPostBox> *postBox; +}; + +IServer::PrivateData::PrivateData() +{ + listener = 0; + started = false; + postBox = new PostBox>(); +} + +IServer::PrivateData::~PrivateData() +{ + Shutdown(); +} + +bool IServer::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(); + ((Listener*)listener)->Init(this->initDesc.port, false); + + return true; +} + +bool IServer::PrivateData::Start() +{ + //Start listener + ((Listener*)listener)->Start(); + started = true; + + return true; +} + +bool IServer::PrivateData::Stop() +{ + if(listener) + { + ((Listener*)listener)->Stop(); + } + + started = false; + + return true; +} + +bool IServer::PrivateData::Shutdown() +{ + if(listener) + { + delete listener; + listener = NULL; + } + + started = false; + + return true; +} + +/************************************* + IServer +*************************************/ IServer::IServer() { - + privateData = new PrivateData(); } IServer::~IServer() { - + if(privateData) + { + delete privateData; + } } bool IServer::Init(INIT_DESC& initDesc) { + privateData->Init(initDesc); return true; } bool IServer::Start() { + privateData->Start(); return true; } bool IServer::Stop() { + privateData->Stop(); return true; } bool IServer::Shutdown() { + privateData->Shutdown(); return true; } @@ -42,4 +146,9 @@ void IServer::AddSession(ISession* session) void IServer::RemoveSession(ISession* session) { +} + +bool IServer::IsStarted() const +{ + return privateData->started; } \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/IServer.h b/Code/Network/OysterNetworkServer/IServer.h index cdb1131a..e7382192 100644 --- a/Code/Network/OysterNetworkServer/IServer.h +++ b/Code/Network/OysterNetworkServer/IServer.h @@ -5,30 +5,44 @@ // Created by Pontus Fransson 2013 // ///////////////////////////////////// -class IServer +#include "IClient.h" + +namespace Oyster { - class ISession; - -public: - struct INIT_DESC + namespace Network { + namespace Server + { + class IServer + { + class ISession; + public: + struct INIT_DESC + { + unsigned short port; //Port the server should be accepting clients on. + void (*proc)(IClient*); + }; - }; + IServer(); + virtual ~IServer(); - IServer(); - virtual ~IServer(); + virtual bool Init(INIT_DESC& initDesc); + virtual bool Start(); + virtual bool Stop(); + virtual bool Shutdown(); - virtual bool Init(INIT_DESC& initDesc); - virtual bool Start(); - virtual bool Stop(); - virtual bool Shutdown(); + virtual void AddSession(ISession* session); + virtual void RemoveSession(ISession* session); - virtual void AddSession(ISession* session); - virtual void RemoveSession(ISession* session); + virtual bool IsStarted() const; -private: + private: + struct PrivateData; + PrivateData* privateData; - -}; + }; + } + } +} #endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index f8c262de..ced434ff 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -13,6 +13,7 @@ #include "../../Misc/Utilities-Impl.h" #include "IServer.h" +#include "IClient.h" #include "ISession.h" #pragma comment(lib, "ws2_32.lib") @@ -25,18 +26,15 @@ using namespace Utility; using namespace ::Utility::DynamicMemory; void PrintOutMessage(ProtocolSet* set); +void clientProc(IClient* client); +vector clients; int main() { - IServer server; - IServer::INIT_DESC initDesc; - server.Init(initDesc); - - - //Old program SmartPointer sendBuffer = new OysterByte; - SmartPointer recvBuffer = new OysterByte(); + SmartPointer recvBuffer = new OysterByte; ProtocolSet* set = new ProtocolSet; + IPostBox> *postBox = new PostBox>(); IPostBox> *recvPostBox = new PostBox>(); @@ -48,11 +46,18 @@ 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(); Sleep(1000); //Start listening @@ -72,7 +77,6 @@ int main() WinTimer timer; - vector clients; SmartPointer client = int(); while(1) { @@ -101,21 +105,22 @@ int main() { t.Unpack(set, recvBuffer); - PrintOutMessage(set); + //PrintOutMessage(set); set->Release(); } Sleep(1); } + //server.Stop(); + //server.Shutdown(); listener.Shutdown(); Sleep(1000); system("pause"); - for(int i = 0; i < clients.size(); i++) + for(int i = 0; i < (int)clients.size(); i++) delete clients.at(i); - delete postBox; return 0; } @@ -143,4 +148,10 @@ void PrintOutMessage(ProtocolSet* set) cout << endl; break; } +} + +void clientProc(IClient* client) +{ + cout << "Proc" << endl; + //clients.push_back(client); } \ No newline at end of file