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.
This commit is contained in:
Pontus Fransson 2013-11-27 14:33:08 +01:00
parent 43b111f0d3
commit 6e24acea2b
6 changed files with 121 additions and 15 deletions

View File

@ -3,6 +3,7 @@
#include <winsock2.h> #include <winsock2.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fcntl.h>
using namespace Oyster::Network; using namespace Oyster::Network;
@ -137,3 +138,16 @@ int Connection::InitiateSocket()
return 0; 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
{
}
}

View File

@ -32,6 +32,7 @@ namespace Oyster
private: private:
int InitiateSocket(); int InitiateSocket();
void SetBlockingMode(bool blocking);
int socket; int socket;

View File

@ -4,7 +4,8 @@ using namespace Oyster::Network::Server;
Listener::Listener() Listener::Listener()
{ {
newSocket = false;
tempSocket = 0;
} }
Listener::~Listener() Listener::~Listener()
@ -21,14 +22,53 @@ bool Listener::Init(unsigned int port)
connection->InitiateServer(port); connection->InitiateServer(port);
thread.Create(this, true);
return 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 Listener::Accept()
{ {
int clientSocket = 0; int clientSocket = 0;
clientSocket = connection->Listen(); clientSocket = connection->Listen();
mutex.LockMutex();
tempSocket = clientSocket;
newSocket = true;
mutex.UnlockMutex();
return clientSocket; return clientSocket;
} }
bool Listener::DoWork()
{
Accept();
return true;
}
#include <iostream>
void Listener::ThreadEntry()
{
std::cout << "Thread started" << std::endl;
}
void Listener::ThreadExit()
{
std::cout << "Thread stopped" << std::endl;
}

View File

@ -7,6 +7,9 @@
#include "IListener.h" #include "IListener.h"
#include "../NetworkDependencies/Connection.h" #include "../NetworkDependencies/Connection.h"
#include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Thread/OysterMutex.h"
namespace Oyster namespace Oyster
{ {
@ -14,7 +17,7 @@ namespace Oyster
{ {
namespace Server namespace Server
{ {
class Listener class Listener : public ::Oyster::Thread::IThreadObject
{ {
public: public:
Listener(); Listener();
@ -22,9 +25,23 @@ namespace Oyster
bool Init(unsigned int port); bool Init(unsigned int port);
int Accept(); int Accept();
int GetNewClient();
//Thread functions
bool DoWork();
void ThreadEntry();
void ThreadExit();
private:
private: private:
::Oyster::Network::Connection* connection; ::Oyster::Network::Connection* connection;
int tempSocket;
bool newSocket;
::Oyster::Thread::OysterThread thread;
OysterMutex mutex;
}; };
} }

View File

@ -87,7 +87,7 @@ namespace Oyster
//floating point (32, 64-bit) //floating point (32, 64-bit)
void Pack(unsigned char buffer[], float i) void Pack(unsigned char buffer[], float i)
{ {
__int64 tempFloat = Pack754(i, 32, 8); int tempFloat = Pack754(i, 32, 8);
Pack(buffer, tempFloat); Pack(buffer, tempFloat);
} }

View File

@ -30,19 +30,9 @@ int main()
//Create socket //Create socket
Listener listener; Listener listener;
listener.Init(9876); listener.Init(9876);
Sleep(1000);
//Start listening //Start listening
//Accept a client //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; ProtocolTest test;
test.clientID = 0; test.clientID = 0;
test.size = 2; test.size = 2;
@ -58,6 +48,50 @@ int main()
t.Pack(test, recvBuffer); 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); client1.Send(recvBuffer);
while(1) while(1)
@ -92,7 +126,7 @@ int main()
ShutdownWinSock(); ShutdownWinSock();
delete set; delete set;
*/
system("pause"); system("pause");
return 0; return 0;
} }