diff --git a/Code/Misc/ThreadSafeQueue.h b/Code/Misc/ThreadSafeQueue.h index 2e10b74c..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,13 +111,13 @@ namespace Oyster this->nrOfNodes++; - mutex.UnlockMutex(); + stdMutex.unlock(); } template < typename Type > Type ThreadSafeQueue::Pop() { - mutex.LockMutex(); + stdMutex.lock(); Type item = this->front->item; Node *destroyer = this->front; @@ -132,55 +132,56 @@ namespace Oyster this->back = NULL; } - mutex.UnlockMutex(); + stdMutex.unlock(); return item; } template < typename Type > Type ThreadSafeQueue::Front() { - mutex.LockMutex(); + stdMutex.lock(); + Type temp = this->front->item; + stdMutex.unlock(); - return this->front->item; + return temp; - mutex.UnlockMutex(); - } template < typename Type > Type ThreadSafeQueue::Back() { - mutex.LockMutex(); + stdMutex.lock(); + Type temp = this->back->item; + stdMutex.unlock(); - return this->back->item; + return temp; - mutex.UnlockMutex(); - } template < typename Type > int ThreadSafeQueue::Size() { - mutex.LockMutex(); + stdMutex.lock(); + int size = this->nrOfNodes; + stdMutex.unlock(); - return this->nrOfNodes; + return size; - mutex.UnlockMutex(); } template < typename Type > bool ThreadSafeQueue::IsEmpty() { - mutex.LockMutex(); + stdMutex.lock(); if(nrOfNodes == 0 || this->front == NULL) { - mutex.UnlockMutex(); + stdMutex.unlock(); return true; } else { - mutex.UnlockMutex(); + stdMutex.unlock(); } return false; @@ -189,7 +190,7 @@ namespace Oyster template < typename Type > void ThreadSafeQueue::Swap(IQueue &queue ) { - mutex.LockMutex(); + stdMutex.lock(); int prevNrOfNodes = this->nrOfNodes; int size = queue.Size(); @@ -202,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..a8e4b406 100644 --- a/Code/Misc/Utilities.h +++ b/Code/Misc/Utilities.h @@ -201,7 +201,6 @@ namespace Utility T *_ptr; /** Destroys the pointer and returns the memory allocated. */ - void Destroy(); public: SmartPointer(); @@ -216,7 +215,8 @@ namespace Utility T* operator-> (); operator T* (); operator bool(); - + + void Destroy(); /** * Returns the connected pointer */ 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..d606df10 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(); @@ -34,9 +60,11 @@ void Listener::Shutdown() 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() @@ -46,9 +74,11 @@ int Listener::Accept() if(*clientSocket != -1) { - mutex.LockMutex(); + stdMutex.lock(); + //mutex.LockMutex(); postBox->PostMessage(clientSocket); - mutex.UnlockMutex(); + //mutex.UnlockMutex(); + stdMutex.unlock(); } return clientSocket; @@ -61,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 b9fdab21..abf57ba9 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); @@ -43,6 +48,7 @@ namespace Oyster ::Oyster::Thread::OysterThread thread; OysterMutex mutex; + std::mutex stdMutex; 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/OysterByte.cpp b/Code/Network/NetworkDependencies/OysterByte.cpp index 0ae7bf96..a0883604 100644 --- a/Code/Network/NetworkDependencies/OysterByte.cpp +++ b/Code/Network/NetworkDependencies/OysterByte.cpp @@ -18,7 +18,7 @@ OysterByte::OysterByte(int cap) OysterByte::OysterByte(const OysterByte& obj) { - delete[] this->byteArray; + //delete[] this->byteArray; this->byteArray = new unsigned char[obj.capacity]; for(int i = 0; i < obj.size; i++) diff --git a/Code/Network/NetworkDependencies/ThreadedClient.cpp b/Code/Network/NetworkDependencies/ThreadedClient.cpp index 2ad49553..a49caabd 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>; @@ -50,41 +51,37 @@ ThreadedClient::~ThreadedClient() } } -int ThreadedClient::Send(SmartPointer &byte) +void ThreadedClient::Send(SmartPointer& byte) { - mutex.LockMutex(); this->sendPostBox->PostMessage(byte); - mutex.UnlockMutex(); - return 0; } int ThreadedClient::Send() { int errorCode = 0; - mutex.LockMutex(); + if(sendPostBox->IsFull()) { - SmartPointer temp = NULL; + SmartPointer temp = new OysterByte; sendPostBox->FetchMessage(temp); errorCode = this->connection->Send(temp); } - mutex.UnlockMutex(); return errorCode; } int ThreadedClient::Recv() { - int errorCode = 0; + int errorCode = -1; - SmartPointer temp = new OysterByte(); + SmartPointer temp = new OysterByte; errorCode = this->connection->Recieve(temp); if(errorCode == 0) { - mutex.LockMutex(); + stdMutex.lock(); recvPostBox->PostMessage(temp); - mutex.UnlockMutex(); + stdMutex.unlock(); } return errorCode; @@ -100,6 +97,8 @@ void ThreadedClient::ThreadExit() std::cout << "Client Thread exit" << std::endl; } +#include + bool ThreadedClient::DoWork() { int errorCode; @@ -114,7 +113,7 @@ bool ThreadedClient::DoWork() { return false; }*/ - + Sleep(1); return true; } @@ -138,7 +137,7 @@ int ThreadedClient::Connect(unsigned short port, const char serverName[]) void ThreadedClient::setRecvPostBox(IPostBox> *postBox) { - this->mutex.LockMutex(); + stdMutex.lock(); this->recvPostBox = postBox; - this->mutex.UnlockMutex(); + stdMutex.unlock(); } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/ThreadedClient.h b/Code/Network/NetworkDependencies/ThreadedClient.h index 2808f351..ae026a61 100644 --- a/Code/Network/NetworkDependencies/ThreadedClient.h +++ b/Code/Network/NetworkDependencies/ThreadedClient.h @@ -5,30 +5,33 @@ // 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" +#include + 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); + void 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(); @@ -43,7 +46,7 @@ namespace Oyster IPostBox> *sendPostBox; IPostBox> *recvPostBox; Oyster::Thread::OysterThread thread; - OysterMutex mutex; + std::mutex stdMutex; }; } diff --git a/Code/Network/OysterNetworkClient/ClientMain.cpp b/Code/Network/OysterNetworkClient/ClientMain.cpp index 2d2d428f..72c011a2 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -59,8 +59,8 @@ void chat(ThreadedClient &client) client.setRecvPostBox(postBox); - SmartPointer msgRecv = SmartPointer(new OysterByte()); - SmartPointer msgSend = SmartPointer(new OysterByte()); + SmartPointer msgRecv = new OysterByte(); + SmartPointer msgSend = new OysterByte(); ProtocolSet* set = new ProtocolSet; ProtocolPlayerPos test; @@ -84,7 +84,7 @@ void chat(ThreadedClient &client) { t->Unpack(set, msgRecv); - PrintOutMessage(set); + //PrintOutMessage(set); set->Release(); } @@ -95,6 +95,7 @@ void chat(ThreadedClient &client) timer.reset(); client.Send(msgSend); } + Sleep(1); } delete postBox; diff --git a/Code/Network/OysterNetworkServer/IClient.h b/Code/Network/OysterNetworkServer/IClient.h new file mode 100644 index 00000000..5b7fafe9 --- /dev/null +++ b/Code/Network/OysterNetworkServer/IClient.h @@ -0,0 +1,23 @@ +#ifndef OYSTER_NETWORK_SERVER_I_CLIENT_H +#define OYSTER_NETWORK_SERVER_I_CLIENT_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +class IClient +{ +public: + IClient() {} + virtual ~IClient() {} + + virtual void Disconnect() {} + virtual bool IsConnected() {return true;} + + virtual void Send() {} + +private: + +}; + +#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/IServer.cpp b/Code/Network/OysterNetworkServer/IServer.cpp new file mode 100644 index 00000000..82d5a155 --- /dev/null +++ b/Code/Network/OysterNetworkServer/IServer.cpp @@ -0,0 +1,154 @@ +#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; +} + +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 new file mode 100644 index 00000000..e7382192 --- /dev/null +++ b/Code/Network/OysterNetworkServer/IServer.h @@ -0,0 +1,48 @@ +#ifndef OYSTER_NETWORK_SERVER_I_SERVER_H +#define OYSTER_NETWORK_SERVER_I_SERVER_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#include "IClient.h" + +namespace Oyster +{ + 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(); + + 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 bool IsStarted() const; + + private: + struct PrivateData; + PrivateData* privateData; + + }; + } + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/ISession.h b/Code/Network/OysterNetworkServer/ISession.h new file mode 100644 index 00000000..84ca05e0 --- /dev/null +++ b/Code/Network/OysterNetworkServer/ISession.h @@ -0,0 +1,34 @@ +#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 d2268387..196da525 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -154,8 +154,14 @@ + + + + + + diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters index f8025a15..fc74d185 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters @@ -18,5 +18,19 @@ Source Files + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 14c28da5..9564a024 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -12,6 +12,10 @@ #include "../../Misc/Utilities.h" #include "../../Misc/Utilities-Impl.h" +#include "IServer.h" +#include "IClient.h" +#include "ISession.h" + #pragma comment(lib, "ws2_32.lib") using namespace std; @@ -22,12 +26,15 @@ using namespace Utility; using namespace ::Utility::DynamicMemory; void PrintOutMessage(ProtocolSet* set); +void clientProc(IClient* client); +vector clients; int main() -{ - SmartPointer sendBuffer = SmartPointer(new OysterByte); - SmartPointer recvBuffer = SmartPointer(new OysterByte()); +{ + SmartPointer sendBuffer = new OysterByte; + SmartPointer recvBuffer = new OysterByte; ProtocolSet* set = new ProtocolSet; + IPostBox> *postBox = new PostBox>(); IPostBox> *recvPostBox = new PostBox>(); @@ -39,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 @@ -62,9 +76,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)); + }*/ - vector clients; - SmartPointer client = SmartPointer(); + SmartPointer client = int(); while(1) { //Fetch new clients from the postbox @@ -92,21 +111,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; } @@ -134,4 +154,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