Network - IServer running on separate thread.

IServer accepting clients.
This commit is contained in:
Pontus Fransson 2013-12-09 22:22:05 +01:00
parent b72fb21b07
commit 2cb8bbd8a9
15 changed files with 211 additions and 136 deletions

View File

@ -201,6 +201,7 @@ namespace Utility
T *_ptr;
/** Destroys the pointer and returns the memory allocated. */
void Destroy();
public:
SmartPointer();
@ -216,7 +217,6 @@ namespace Utility
operator T* ();
operator bool();
void Destroy();
/**
* Returns the connected pointer
*/

View File

@ -8,7 +8,7 @@ Listener::Listener()
connection = NULL;
}
Listener::Listener(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
Listener::Listener(Oyster::Network::IPostBox<int>* postBox)
{
connection = NULL;
this->postBox = postBox;
@ -58,7 +58,7 @@ void Listener::Shutdown()
thread.Stop();
}
void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
void Listener::SetPostBox(Oyster::Network::IPostBox<int>* postBox)
{
stdMutex.lock();
//mutex.LockMutex();
@ -69,10 +69,10 @@ void Listener::SetPostBox(Oyster::Network::IPostBox<SmartPointer<int>>* postBox)
int Listener::Accept()
{
SmartPointer<int> clientSocket = SmartPointer<int>(new int());
*clientSocket = connection->Listen();
int clientSocket = -1;
clientSocket = connection->Listen();
if(*clientSocket != -1)
if(clientSocket != -1)
{
stdMutex.lock();
//mutex.LockMutex();

View File

@ -22,7 +22,7 @@ namespace Oyster
{
public:
Listener();
Listener(Oyster::Network::IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox);
Listener(Oyster::Network::IPostBox<int>* postBox);
~Listener();
bool Init(unsigned int port);
@ -32,7 +32,7 @@ namespace Oyster
void Shutdown();
void SetPostBox(IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox);
void SetPostBox(IPostBox<int>* postBox);
private:
//Thread functions
@ -50,7 +50,7 @@ namespace Oyster
OysterMutex mutex;
std::mutex stdMutex;
IPostBox<Utility::DynamicMemory::SmartPointer<int>>* postBox;
IPostBox<int>* postBox;
};
}

View File

@ -153,7 +153,6 @@
<ItemGroup>
<ClCompile Include="Connection.cpp" />
<ClCompile Include="Listener.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Messages\MessageHeader.cpp" />
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
<ClCompile Include="Messages\MessageTest.cpp" />

View File

@ -3,7 +3,6 @@
<ItemGroup>
<ClCompile Include="Connection.cpp" />
<ClCompile Include="Listener.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Messages\MessageHeader.cpp" />
<ClCompile Include="Messages\MessagePlayerPos.cpp" />
<ClCompile Include="Messages\MessageTest.cpp" />

View File

@ -3,16 +3,18 @@
#include "IClient.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/Utilities.h"
#include "../../Misc/Thread/OysterThread.h"
using namespace Oyster::Network;
using namespace ::Server;
using namespace Utility::DynamicMemory;
using namespace Oyster::Thread;
/*************************************
PrivateData
*************************************/
struct IServer::PrivateData
struct IServer::PrivateData : public IThreadObject
{
PrivateData();
~PrivateData();
@ -21,20 +23,29 @@ struct IServer::PrivateData
bool Start();
bool Stop();
bool Shutdown();
void CheckForNewClient();
virtual bool DoWork();
//
IListener* listener;
INIT_DESC initDesc;
bool started;
IPostBox<SmartPointer<int>> *postBox;
//Postbox for new clients
IPostBox<int> *postBox;
//Server thread
OysterThread thread;
};
IServer::PrivateData::PrivateData()
{
listener = 0;
started = false;
postBox = new PostBox<SmartPointer<int>>();
postBox = new PostBox<int>;
}
IServer::PrivateData::~PrivateData()
@ -53,9 +64,11 @@ bool IServer::PrivateData::Init(INIT_DESC& initDesc)
this->initDesc = initDesc;
//Initiate listener
listener = new Listener();
listener = new Listener(postBox);
((Listener*)listener)->Init(this->initDesc.port, false);
thread.Create(this, false);
return true;
}
@ -65,6 +78,8 @@ bool IServer::PrivateData::Start()
((Listener*)listener)->Start();
started = true;
thread.Start();
return true;
}
@ -77,22 +92,60 @@ bool IServer::PrivateData::Stop()
started = false;
thread.Stop();
return true;
}
bool IServer::PrivateData::Shutdown()
{
//Stop server main thread
thread.Stop();
if(listener)
{
delete listener;
listener = NULL;
}
if(postBox)
{
delete postBox;
postBox = NULL;
}
started = false;
return true;
}
//Checks for new clients and sends them to the proc function.
void IServer::PrivateData::CheckForNewClient()
{
if(postBox->IsFull())
{
int clientSocketNum;
postBox->FetchMessage(clientSocketNum);
//Safety check that is probably not needed.
if(clientSocketNum == -1)
{
return;
}
//Create the new client
IClient* client = new IClient();
initDesc.proc(client);
}
}
bool IServer::PrivateData::DoWork()
{
CheckForNewClient();
return true;
}
/*************************************
IServer
*************************************/
@ -138,16 +191,6 @@ bool IServer::Shutdown()
return true;
}
void IServer::AddSession(ISession* session)
{
}
void IServer::RemoveSession(ISession* session)
{
}
bool IServer::IsStarted() const
{
return privateData->started;

View File

@ -15,7 +15,6 @@ namespace Oyster
{
class IServer
{
class ISession;
public:
struct INIT_DESC
{
@ -31,9 +30,6 @@ namespace Oyster
virtual bool Stop();
virtual bool Shutdown();
virtual void AddSession(ISession* session);
virtual void RemoveSession(ISession* session);
virtual bool IsStarted() const;
private:

View File

@ -1,34 +0,0 @@
#ifndef OYSTER_NETWORK_SERVER_I_SESSION_H
#define OYSTER_NETWORK_SERVER_I_SESSION_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
class ISession
{
class IClient;
public:
struct INIT_DESC
{
};
ISession();
virtual ~ISession();
virtual bool Init();
virtual bool Start();
virtual bool Stop();
virtual bool Shutdown();
virtual void SendToAll();
virtual void AddClient(IClient* client);
virtual void RemoveClient(IClient* client);
private:
};
#endif

View File

@ -156,11 +156,13 @@
<ItemGroup>
<ClCompile Include="IServer.cpp" />
<ClCompile Include="ServerMain.cpp" />
<ClCompile Include="TestClass.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="IClient.h" />
<ClInclude Include="IServer.h" />
<ClInclude Include="ISession.h" />
<ClInclude Include="RecieverObject.h" />
<ClInclude Include="TestClass.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -21,15 +21,21 @@
<ClCompile Include="IServer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="TestClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="IServer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ISession.h">
<ClInclude Include="IClient.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="IClient.h">
<ClInclude Include="RecieverObject.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="TestClass.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>

View File

@ -0,0 +1,19 @@
#ifndef OYSTER_NETWORK_SERVER_RECIEVER_OBJECT_H
#define OYSTER_NETWORK_SERVER_RECIEVER_OBJECT_H
/////////////////////////////////////
// Created by Pontus Fransson 2013 //
/////////////////////////////////////
#include "../NetworkDependencies/Protocols.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../../Misc/Utilities.h"
class RecieverObject
{
public:
virtual void ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg) = 0;
};
#endif

View File

@ -12,9 +12,9 @@
#include "../../Misc/Utilities.h"
#include "../../Misc/Utilities-Impl.h"
#include "TestClass.h"
#include "IServer.h"
#include "IClient.h"
#include "ISession.h"
#pragma comment(lib, "ws2_32.lib")
@ -25,18 +25,15 @@ using namespace ::Protocols;
using namespace Utility;
using namespace ::Utility::DynamicMemory;
void PrintOutMessage(ProtocolSet* set);
void clientProc(IClient* client);
vector<ThreadedClient*> clients;
int main()
{
Test tests;
SmartPointer<OysterByte> sendBuffer = new OysterByte;
SmartPointer<OysterByte> recvBuffer = new OysterByte;
ProtocolSet* set = new ProtocolSet;
IPostBox<SmartPointer<int>> *postBox = new PostBox<SmartPointer<int>>();
IPostBox<SmartPointer<OysterByte>> *recvPostBox = new PostBox<SmartPointer<OysterByte>>();
cout << "Server" << endl;
Translator t;
@ -46,22 +43,17 @@ int main()
{
cout << "errorMessage: unable to start winsock" << endl;
}
/*
IServer server;
IServer::INIT_DESC initDesc;
initDesc.port = 9876;
initDesc.proc = clientProc;
server.Init(initDesc);
*/
//Create socket
Listener listener;
listener.Init(9876);
listener.SetPostBox(postBox);
listener.Start();
server.Start();
Sleep(1000);
//Start listening
//Accept a client
//Create a test protocol
ProtocolPlayerPos test;
test.clientID = 0;
test.ID = 5;
@ -74,28 +66,14 @@ int main()
}
t.Pack(test, sendBuffer);
WinTimer timer;
/* DEBUGGING: Connect 25 clients
for(int i = 0; i < 25; i++)
{
clients.push_back(new ThreadedClient(recvPostBox, 1));
}*/
SmartPointer<int> client = int();
WinTimer timer;
while(1)
{
//Fetch new clients from the postbox
if(postBox->FetchMessage(client))
{
cout << "Client connected: " << *client << endl;
clients.push_back(new ThreadedClient(recvPostBox, *client));
clients.at(clients.size()-1)->Send(sendBuffer);
}
//Send a message every 1 secounds to all clients.
/*
//Send a message every 1 seconds to all clients.
if(timer.getElapsedSeconds() > 1)
{
cout << "Sending to " << clients.size() << " clients." << endl;
@ -104,60 +82,31 @@ int main()
{
clients.at(i)->Send(sendBuffer);
}
}
}*/
//Fetch messages
/*//Fetch messages
if(recvPostBox->FetchMessage(recvBuffer))
{
t.Unpack(set, recvBuffer);
//PrintOutMessage(set);
set->Release();
}
}*/
Sleep(1);
}
//server.Stop();
//server.Shutdown();
listener.Shutdown();
server.Stop();
server.Shutdown();
//listener.Shutdown();
Sleep(1000);
system("pause");
for(int i = 0; i < (int)clients.size(); i++)
delete clients.at(i);
return 0;
}
void PrintOutMessage(ProtocolSet* set)
{
switch(set->type)
{
case PackageType_header:
break;
case PackageType_test:
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < (int)set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
}
cout << endl;
break;
case PackageType_player_pos:
//cout << "ID " << set->Protocol.pPlayerPos->ID << endl;
for(int i = 0; i < (int)set->Protocol.pPlayerPos->nrOfFloats; i++)
{
cout << set->Protocol.pPlayerPos->matrix[i] << ' ';
}
cout << endl;
break;
}
}
void clientProc(IClient* client)
{
cout << "Proc" << endl;
//clients.push_back(client);
clients.push_back(client);
}

View File

@ -0,0 +1,62 @@
#include "TestClass.h"
#include "../../Misc/WinTimer.h"
#include <iostream>
using namespace Oyster::Network;
using namespace ::Protocols;
using namespace Utility;
using namespace ::DynamicMemory;
using namespace std;
Test::Test()
{
recvPostBox = new PostBox<SmartPointer<OysterByte>>;
}
Test::~Test()
{
for(int i = 0; i < (int)clients.size(); i++)
delete clients.at(i);
}
void Test::ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg)
{
return;
}
void Test::mainLoop()
{
WinTimer timer;
while(1)
{
}
}
void Test::PrintOutMessage(ProtocolSet* set)
{
switch(set->type)
{
case PackageType_header:
break;
case PackageType_test:
cout <<"Client 2: " << set->Protocol.pTest->textMessage <<endl;
for(int i = 0; i < (int)set->Protocol.pTest->numOfFloats; i++)
{
cout << set->Protocol.pTest->f[i] << ' ' ;
}
cout << endl;
break;
case PackageType_player_pos:
//cout << "ID " << set->Protocol.pPlayerPos->ID << endl;
for(int i = 0; i < (int)set->Protocol.pPlayerPos->nrOfFloats; i++)
{
cout << set->Protocol.pPlayerPos->matrix[i] << ' ';
}
cout << endl;
break;
}
}

View File

@ -0,0 +1,34 @@
#ifndef TEST_CLASS_H
#define TEST_CLASS_H
#include "RecieverObject.h"
#include "../../Misc/Utilities.h"
#include "../NetworkDependencies/OysterByte.h"
#include "../NetworkDependencies/PostBox.h"
#include "IClient.h"
#include "../NetworkDependencies/Translator.h"
#include <vector>
class Test : public RecieverObject
{
public:
Test();
~Test();
void mainLoop();
virtual void ProcFunc(Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte> msg);
void PrintOutMessage(Oyster::Network::Protocols::ProtocolSet* set);
private:
std::vector<IClient*> clients;
Oyster::Network::IPostBox<Utility::DynamicMemory::SmartPointer<Oyster::Network::OysterByte>> *recvPostBox;
Oyster::Network::Translator t;
Oyster::Network::Protocols::ProtocolPlayerPos test;
};
#endif