From aa4cf634d3989915b8fa77a0e59a7841d673e886 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Tue, 3 Dec 2013 13:04:53 +0100 Subject: [PATCH] Client interface and class for both server and client. now using postbox system --- Code/Network/NetworkDependencies/IClient.h | 28 ++++ .../NetworkDependencies.vcxproj | 3 + .../NetworkDependencies.vcxproj.filters | 3 + .../NetworkDependencies/ThreadedClient.cpp | 123 ++++++++++++++++++ .../NetworkDependencies/ThreadedClient.h | 52 ++++++++ Code/Network/OysterNetworkClient/Client.cpp | 41 ------ Code/Network/OysterNetworkClient/Client.h | 35 ----- .../OysterNetworkClient/ClientMain.cpp | 17 +-- .../OysterNetworkClient.vcxproj | 4 - .../OysterNetworkClient.vcxproj.filters | 8 -- Code/Network/OysterNetworkServer/Client.cpp | 24 ---- Code/Network/OysterNetworkServer/Client.h | 34 ----- .../OysterNetworkServer.vcxproj | 4 - .../OysterNetworkServer.vcxproj.filters | 8 -- .../OysterNetworkServer/ServerMain.cpp | 10 +- 15 files changed, 223 insertions(+), 171 deletions(-) create mode 100644 Code/Network/NetworkDependencies/IClient.h create mode 100644 Code/Network/NetworkDependencies/ThreadedClient.cpp create mode 100644 Code/Network/NetworkDependencies/ThreadedClient.h delete mode 100644 Code/Network/OysterNetworkClient/Client.cpp delete mode 100644 Code/Network/OysterNetworkClient/Client.h delete mode 100644 Code/Network/OysterNetworkServer/Client.cpp delete mode 100644 Code/Network/OysterNetworkServer/Client.h diff --git a/Code/Network/NetworkDependencies/IClient.h b/Code/Network/NetworkDependencies/IClient.h new file mode 100644 index 00000000..bf044bae --- /dev/null +++ b/Code/Network/NetworkDependencies/IClient.h @@ -0,0 +1,28 @@ +#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/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index dfabbcba..c0a1a8a9 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -158,11 +158,13 @@ + + @@ -175,6 +177,7 @@ + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index 74cb9a56..3474a3b6 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -10,6 +10,7 @@ + @@ -27,5 +28,7 @@ + + \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/ThreadedClient.cpp b/Code/Network/NetworkDependencies/ThreadedClient.cpp new file mode 100644 index 00000000..73a5ebb2 --- /dev/null +++ b/Code/Network/NetworkDependencies/ThreadedClient.cpp @@ -0,0 +1,123 @@ +#include "ThreadedClient.h" + +#include +using namespace Oyster::Network; +using namespace Oyster::Thread; + + +ThreadedClient::ThreadedClient() +{ + this->connection = new Connection(); + this->sendPostBox = new PostBox(); + this->recvPostBox = NULL; +} + +ThreadedClient::ThreadedClient(unsigned int socket) +{ + this->connection = new Connection(socket); + this->sendPostBox = new PostBox(); + this->recvPostBox = NULL; +} + +ThreadedClient::~ThreadedClient() +{ + thread.Terminate(); + delete this->connection; + this->connection = NULL; + this->recvPostBox = NULL; + + if(sendPostBox != NULL) + { + delete sendPostBox; + this->sendPostBox = NULL; + } +} + +int ThreadedClient::Send(OysterByte* byte) +{ + this->sendPostBox->PostMessage(byte); + return 0; +} + +int ThreadedClient::Send() +{ + int errorCode = 0; + mutex.LockMutex(); + if(!sendPostBox->IsFull()) + { + OysterByte *temp = NULL; + sendPostBox->FetchMessage(temp); + errorCode = this->connection->Send(*temp); + mutex.UnlockMutex(); + } + + return errorCode; +} + +int ThreadedClient::Recv() +{ + int errorCode = 0; + mutex.LockMutex(); + if(!recvPostBox->IsFull()) + { + OysterByte *temp = NULL; + errorCode = this->connection->Recieve(*temp); + recvPostBox->PostMessage(temp); + mutex.UnlockMutex(); + } + return errorCode; +} + +void ThreadedClient::ThreadEntry() +{ + std::cout<< "Thread started" << std::endl; +} + +void ThreadedClient::ThreadExit() +{ + std::cout << "Thread exit" << std::endl; +} + +bool ThreadedClient::DoWork() +{ + int errorCode; + errorCode = Send(); + + if(errorCode != 0) + { + return false; + } + + errorCode = Recv(); + if(errorCode != 0) + { + return false; + } + + 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) +{ + this->mutex.LockMutex(); + this->recvPostBox = postBox; + this->mutex.UnlockMutex(); +} \ 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..384fe96c --- /dev/null +++ b/Code/Network/NetworkDependencies/ThreadedClient.h @@ -0,0 +1,52 @@ +#ifndef NETWORK_DEPENDENCIES_THREADED_CLIENT_H +#define NETWORK_DEPENDENCIES_THREADED_CLIENT_H + +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#include "../NetworkDependencies/IClient.h" +#include "../../Misc/Thread/IThreadObject.h" +#include "../NetworkDependencies/PostBox.h" +#include "../../Misc/Thread/OysterThread.h" +#include "../../Misc/Thread/OysterMutex.h" + +namespace Oyster +{ + namespace Network + { + class ThreadedClient : public IClient, public Thread::IThreadObject + { + public: + ThreadedClient(); + ThreadedClient(unsigned int socket); + virtual ~ThreadedClient(); + + int Send(OysterByte* 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(); + + + + + Connection* connection; + IPostBox* sendPostBox; + IPostBox* recvPostBox; + Oyster::Thread::OysterThread thread; + OysterMutex mutex; + + }; + } +} + +#endif \ No newline at end of file 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..60c9453f 100644 --- a/Code/Network/OysterNetworkClient/ClientMain.cpp +++ b/Code/Network/OysterNetworkClient/ClientMain.cpp @@ -6,15 +6,15 @@ #include "..\NetworkDependencies\Protocols.h" #include "../NetworkDependencies/OysterByte.h" #include "../../Misc/ThreadSafeQueue.h" -#include "Client.h" +#include "../NetworkDependencies/ThreadedClient.h" #pragma comment(lib, "ws2_32.lib") using namespace std; using namespace Oyster::Network::Protocols; -using namespace Oyster::Network::Client; +using namespace Oyster::Network; -void chat(Client &client); +void chat(ThreadedClient &client); int main() { @@ -27,7 +27,7 @@ int main() cout << "Client" << endl; //Create Client - Client client; + ThreadedClient client; //Connect to server errorCode = client.Connect(9876, "localhost"); @@ -38,7 +38,7 @@ int main() wcout << "errorMessage: " << errorTest << endl; } - chat(client); + //chat(client); ShutdownWinSock(); @@ -46,7 +46,7 @@ int main() return 0; } -void chat(Client &client) +void chat(ThreadedClient &client) { Oyster::Network::Translator *t = new Oyster::Network::Translator(); @@ -65,7 +65,7 @@ void chat(Client &client) } bool chatDone = false; - + /* while(!chatDone) { client.Recv(msgRecv); @@ -113,9 +113,10 @@ void chat(Client &client) } cin.clear();*/ - + /* } delete t; delete set; + */ } \ 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..d2268387 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj @@ -154,12 +154,8 @@ - - - - diff --git a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters index 3cb5827c..f8025a15 100644 --- a/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters +++ b/Code/Network/OysterNetworkServer/OysterNetworkServer.vcxproj.filters @@ -18,13 +18,5 @@ Source Files - - Source Files - - - - - Header Files - \ No newline at end of file diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index 9dd0a0c1..cc80b165 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -5,7 +5,7 @@ #include "../NetworkDependencies/WinsockFunctions.h" #include "../NetworkDependencies/Listener.h" #include "../NetworkDependencies/Translator.h" -#include "Client.h" +#include "../NetworkDependencies/ThreadedClient.h" #include "../NetworkDependencies/OysterByte.h" #include "../NetworkDependencies/PostBox.h" #include "../../Misc/WinTimer.h" @@ -57,7 +57,7 @@ int main() WinTimer timer; - vector clients; + vector clients; int client = -1; while(1) { @@ -66,9 +66,9 @@ int main() if(client != -1) { cout << "Client connected: " << client << endl; - clients.push_back(new Client(client)); + clients.push_back(new ThreadedClient(client)); - clients.at(clients.size()-1)->Send(recvBuffer); + clients.at(clients.size()-1)->Send(&recvBuffer); } //Send a message every 1 secounds to all clients. @@ -78,7 +78,7 @@ int main() timer.reset(); for(int i = 0; i < (int)clients.size(); i++) { - clients.at(i)->Send(recvBuffer); + clients.at(i)->Send(&recvBuffer); } } Sleep(100);