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:
parent
43b111f0d3
commit
6e24acea2b
|
@ -3,6 +3,7 @@
|
|||
#include <winsock2.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fcntl.h>
|
||||
|
||||
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
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -32,6 +32,7 @@ namespace Oyster
|
|||
|
||||
private:
|
||||
int InitiateSocket();
|
||||
void SetBlockingMode(bool blocking);
|
||||
|
||||
int socket;
|
||||
|
||||
|
|
|
@ -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 <iostream>
|
||||
void Listener::ThreadEntry()
|
||||
{
|
||||
std::cout << "Thread started" << std::endl;
|
||||
}
|
||||
|
||||
void Listener::ThreadExit()
|
||||
{
|
||||
std::cout << "Thread stopped" << std::endl;
|
||||
}
|
|
@ -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;
|
||||
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue