From 6e24acea2b7a2cf964dcdf3eb8cd8fe28aa282c4 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Wed, 27 Nov 2013 14:33:08 +0100 Subject: [PATCH] Listener's Accept() is now threaded. Added function private to Connection SetBlockingMode(bool blocking); But it doesn't do anything yet. Listener is now accepting clients in a thread. GetNewClient() returns the socket that has connected. -1 if there is no new client connected. (Can only connect 1 client at a time.) The simple chat program is not working anymore because of changes in the server. --- .../NetworkDependencies/Connection.cpp | 14 +++++ Code/Network/NetworkDependencies/Connection.h | 1 + Code/Network/NetworkDependencies/Listener.cpp | 42 +++++++++++++- Code/Network/NetworkDependencies/Listener.h | 19 +++++- Code/Network/NetworkDependencies/Packing.cpp | 2 +- .../OysterNetworkServer/ServerMain.cpp | 58 +++++++++++++++---- 6 files changed, 121 insertions(+), 15 deletions(-) diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 1cf5efb1..3f303542 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace Oyster::Network; @@ -137,3 +138,16 @@ int Connection::InitiateSocket() return 0; } + +void Connection::SetBlockingMode(bool blocking) +{ + //TODO: Implement this function. Setting the socket to blocking or non-blocking. + if(blocking) + { + //fcntl(this->socket, F_SETFL, O_NONBLOCK); + } + else + { + + } +} \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index 34569807..b6b17c60 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -32,6 +32,7 @@ namespace Oyster private: int InitiateSocket(); + void SetBlockingMode(bool blocking); int socket; diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index 4e0d149f..1af94613 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -4,7 +4,8 @@ using namespace Oyster::Network::Server; Listener::Listener() { - + newSocket = false; + tempSocket = 0; } Listener::~Listener() @@ -21,14 +22,53 @@ bool Listener::Init(unsigned int port) connection->InitiateServer(port); + thread.Create(this, true); return true; } +int Listener::GetNewClient() +{ + mutex.LockMutex(); + int temp = -1; + + if(newSocket) + { + temp = tempSocket; + newSocket = false; + } + mutex.UnlockMutex(); + + return temp; +} + int Listener::Accept() { int clientSocket = 0; clientSocket = connection->Listen(); + mutex.LockMutex(); + tempSocket = clientSocket; + newSocket = true; + mutex.UnlockMutex(); + return clientSocket; +} + +bool Listener::DoWork() +{ + Accept(); + + 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 f72491c1..aa3817cf 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -7,6 +7,9 @@ #include "IListener.h" #include "../NetworkDependencies/Connection.h" +#include "../../Misc/Thread/OysterThread.h" +#include "../../Misc/Thread/OysterMutex.h" + namespace Oyster { @@ -14,7 +17,7 @@ namespace Oyster { namespace Server { - class Listener + class Listener : public ::Oyster::Thread::IThreadObject { public: Listener(); @@ -22,9 +25,23 @@ namespace Oyster bool Init(unsigned int port); int Accept(); + int GetNewClient(); + + //Thread functions + bool DoWork(); + void ThreadEntry(); + void ThreadExit(); + + private: + private: ::Oyster::Network::Connection* connection; + int tempSocket; + bool newSocket; + + ::Oyster::Thread::OysterThread thread; + OysterMutex mutex; }; } diff --git a/Code/Network/NetworkDependencies/Packing.cpp b/Code/Network/NetworkDependencies/Packing.cpp index f636f891..7adc395c 100644 --- a/Code/Network/NetworkDependencies/Packing.cpp +++ b/Code/Network/NetworkDependencies/Packing.cpp @@ -87,7 +87,7 @@ namespace Oyster //floating point (32, 64-bit) void Pack(unsigned char buffer[], float i) { - __int64 tempFloat = Pack754(i, 32, 8); + int tempFloat = Pack754(i, 32, 8); Pack(buffer, tempFloat); } diff --git a/Code/Network/OysterNetworkServer/ServerMain.cpp b/Code/Network/OysterNetworkServer/ServerMain.cpp index ad8e06b9..6d958e2d 100644 --- a/Code/Network/OysterNetworkServer/ServerMain.cpp +++ b/Code/Network/OysterNetworkServer/ServerMain.cpp @@ -30,19 +30,9 @@ int main() //Create socket Listener listener; listener.Init(9876); - + Sleep(1000); //Start listening //Accept a client - int clientSocket = listener.Accept(); - Client client1(clientSocket); - cout << "First client connected." << endl; - - //Accept a client - clientSocket = listener.Accept(); - Client client2(clientSocket); - cout << "Second client connected." << endl; - - ProtocolSet* set = new ProtocolSet; ProtocolTest test; test.clientID = 0; test.size = 2; @@ -57,7 +47,51 @@ int main() } t.Pack(test, recvBuffer); + + while(1) + { + int client = listener.GetNewClient(); + if(client != -1) + { + cout << "Client connected: " << client << endl; + Client client1(client); + client1.Send(recvBuffer); + } + //Sleep(100); + } + + +/* int clientSocket = listener.Accept(); + Client client1(clientSocket); + cout << "First client connected." << endl; + + //Accept a client + clientSocket = listener.Accept(); + Client client2(clientSocket); + cout << "Second client connected." << endl; +*/ + + + /* + ProtocolSet* set = new ProtocolSet; + ProtocolTest test; + test.clientID = 0; + test.size = 2; + test.textMessage = "hej"; + test.numOfFloats = 35; + test.f = new float[test.numOfFloats]; + float temp = 395.456f; + for(int i = 0; i < (int)test.numOfFloats; i++) + { + test.f[i] = temp; + temp--; + } + + t.Pack(test, recvBuffer);*/ + + +/* client1.Send(recvBuffer); while(1) @@ -92,7 +126,7 @@ int main() ShutdownWinSock(); delete set; - + */ system("pause"); return 0; }