From 10c1dc57f3c9235ca6c6d4e69fed43465daa7752 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Tue, 3 Dec 2013 13:08:04 +0100 Subject: [PATCH] merge fix --- Code/Network/NetworkDependencies/IClient.h | 28 ++++ .../NetworkDependencies.vcxproj | 3 + .../NetworkDependencies.vcxproj.filters | 7 + .../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, 227 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 129568cc..d320872f 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -159,11 +159,13 @@ + + @@ -177,6 +179,7 @@ + diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index e26441c6..de894c8b 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -11,6 +11,8 @@ + + @@ -28,6 +30,11 @@ +<<<<<<< HEAD +======= + + +>>>>>>> Client interface and class for both server and client. now using postbox system \ 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 63b1b6e5..bbf2ec6f 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); @@ -121,9 +121,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 abbf9167..f1014f59 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" @@ -55,7 +55,7 @@ int main() WinTimer timer; - vector clients; + vector clients; int client = -1; while(1) { @@ -64,9 +64,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. @@ -76,7 +76,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);