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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,16 +3,18 @@
#include "IClient.h" #include "IClient.h"
#include "../NetworkDependencies/PostBox.h" #include "../NetworkDependencies/PostBox.h"
#include "../../Misc/Utilities.h" #include "../../Misc/Utilities.h"
#include "../../Misc/Thread/OysterThread.h"
using namespace Oyster::Network; using namespace Oyster::Network;
using namespace ::Server; using namespace ::Server;
using namespace Utility::DynamicMemory; using namespace Utility::DynamicMemory;
using namespace Oyster::Thread;
/************************************* /*************************************
PrivateData PrivateData
*************************************/ *************************************/
struct IServer::PrivateData struct IServer::PrivateData : public IThreadObject
{ {
PrivateData(); PrivateData();
~PrivateData(); ~PrivateData();
@ -22,19 +24,28 @@ struct IServer::PrivateData
bool Stop(); bool Stop();
bool Shutdown(); bool Shutdown();
void CheckForNewClient();
virtual bool DoWork();
// //
IListener* listener; IListener* listener;
INIT_DESC initDesc; INIT_DESC initDesc;
bool started; bool started;
IPostBox<SmartPointer<int>> *postBox; //Postbox for new clients
IPostBox<int> *postBox;
//Server thread
OysterThread thread;
}; };
IServer::PrivateData::PrivateData() IServer::PrivateData::PrivateData()
{ {
listener = 0; listener = 0;
started = false; started = false;
postBox = new PostBox<SmartPointer<int>>(); postBox = new PostBox<int>;
} }
IServer::PrivateData::~PrivateData() IServer::PrivateData::~PrivateData()
@ -53,9 +64,11 @@ bool IServer::PrivateData::Init(INIT_DESC& initDesc)
this->initDesc = initDesc; this->initDesc = initDesc;
//Initiate listener //Initiate listener
listener = new Listener(); listener = new Listener(postBox);
((Listener*)listener)->Init(this->initDesc.port, false); ((Listener*)listener)->Init(this->initDesc.port, false);
thread.Create(this, false);
return true; return true;
} }
@ -65,6 +78,8 @@ bool IServer::PrivateData::Start()
((Listener*)listener)->Start(); ((Listener*)listener)->Start();
started = true; started = true;
thread.Start();
return true; return true;
} }
@ -77,22 +92,60 @@ bool IServer::PrivateData::Stop()
started = false; started = false;
thread.Stop();
return true; return true;
} }
bool IServer::PrivateData::Shutdown() bool IServer::PrivateData::Shutdown()
{ {
//Stop server main thread
thread.Stop();
if(listener) if(listener)
{ {
delete listener; delete listener;
listener = NULL; listener = NULL;
} }
if(postBox)
{
delete postBox;
postBox = NULL;
}
started = false; started = false;
return true; 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 IServer
*************************************/ *************************************/
@ -138,16 +191,6 @@ bool IServer::Shutdown()
return true; return true;
} }
void IServer::AddSession(ISession* session)
{
}
void IServer::RemoveSession(ISession* session)
{
}
bool IServer::IsStarted() const bool IServer::IsStarted() const
{ {
return privateData->started; return privateData->started;

View File

@ -15,7 +15,6 @@ namespace Oyster
{ {
class IServer class IServer
{ {
class ISession;
public: public:
struct INIT_DESC struct INIT_DESC
{ {
@ -31,9 +30,6 @@ namespace Oyster
virtual bool Stop(); virtual bool Stop();
virtual bool Shutdown(); virtual bool Shutdown();
virtual void AddSession(ISession* session);
virtual void RemoveSession(ISession* session);
virtual bool IsStarted() const; virtual bool IsStarted() const;
private: 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> <ItemGroup>
<ClCompile Include="IServer.cpp" /> <ClCompile Include="IServer.cpp" />
<ClCompile Include="ServerMain.cpp" /> <ClCompile Include="ServerMain.cpp" />
<ClCompile Include="TestClass.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="IClient.h" /> <ClInclude Include="IClient.h" />
<ClInclude Include="IServer.h" /> <ClInclude Include="IServer.h" />
<ClInclude Include="ISession.h" /> <ClInclude Include="RecieverObject.h" />
<ClInclude Include="TestClass.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -21,15 +21,21 @@
<ClCompile Include="IServer.cpp"> <ClCompile Include="IServer.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="TestClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="IServer.h"> <ClInclude Include="IServer.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="ISession.h"> <ClInclude Include="IClient.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="IClient.h"> <ClInclude Include="RecieverObject.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="TestClass.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </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.h"
#include "../../Misc/Utilities-Impl.h" #include "../../Misc/Utilities-Impl.h"
#include "TestClass.h"
#include "IServer.h" #include "IServer.h"
#include "IClient.h" #include "IClient.h"
#include "ISession.h"
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")
@ -25,19 +25,16 @@ using namespace ::Protocols;
using namespace Utility; using namespace Utility;
using namespace ::Utility::DynamicMemory; using namespace ::Utility::DynamicMemory;
void PrintOutMessage(ProtocolSet* set);
void clientProc(IClient* client); void clientProc(IClient* client);
vector<ThreadedClient*> clients;
int main() int main()
{ {
Test tests;
SmartPointer<OysterByte> sendBuffer = new OysterByte; SmartPointer<OysterByte> sendBuffer = new OysterByte;
SmartPointer<OysterByte> recvBuffer = new OysterByte; SmartPointer<OysterByte> recvBuffer = new OysterByte;
ProtocolSet* set = new ProtocolSet; ProtocolSet* set = new ProtocolSet;
IPostBox<SmartPointer<int>> *postBox = new PostBox<SmartPointer<int>>();
IPostBox<SmartPointer<OysterByte>> *recvPostBox = new PostBox<SmartPointer<OysterByte>>();
cout << "Server" << endl; cout << "Server" << endl;
Translator t; Translator t;
int errorCode = 0; int errorCode = 0;
@ -46,22 +43,17 @@ int main()
{ {
cout << "errorMessage: unable to start winsock" << endl; cout << "errorMessage: unable to start winsock" << endl;
} }
/*
IServer server; IServer server;
IServer::INIT_DESC initDesc; IServer::INIT_DESC initDesc;
initDesc.port = 9876; initDesc.port = 9876;
initDesc.proc = clientProc; initDesc.proc = clientProc;
server.Init(initDesc); server.Init(initDesc);
*/ server.Start();
//Create socket
Listener listener;
listener.Init(9876);
listener.SetPostBox(postBox);
listener.Start();
Sleep(1000); Sleep(1000);
//Start listening //Create a test protocol
//Accept a client
ProtocolPlayerPos test; ProtocolPlayerPos test;
test.clientID = 0; test.clientID = 0;
test.ID = 5; test.ID = 5;
@ -77,25 +69,11 @@ int main()
WinTimer timer; WinTimer timer;
/* DEBUGGING: Connect 25 clients
for(int i = 0; i < 25; i++)
{
clients.push_back(new ThreadedClient(recvPostBox, 1));
}*/
SmartPointer<int> client = int();
while(1) while(1)
{ {
//Fetch new clients from the postbox //Fetch new clients from the postbox
if(postBox->FetchMessage(client)) /*
{ //Send a message every 1 seconds to all clients.
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.
if(timer.getElapsedSeconds() > 1) if(timer.getElapsedSeconds() > 1)
{ {
cout << "Sending to " << clients.size() << " clients." << endl; cout << "Sending to " << clients.size() << " clients." << endl;
@ -104,60 +82,31 @@ int main()
{ {
clients.at(i)->Send(sendBuffer); clients.at(i)->Send(sendBuffer);
} }
} }*/
//Fetch messages /*//Fetch messages
if(recvPostBox->FetchMessage(recvBuffer)) if(recvPostBox->FetchMessage(recvBuffer))
{ {
t.Unpack(set, recvBuffer); t.Unpack(set, recvBuffer);
//PrintOutMessage(set); //PrintOutMessage(set);
set->Release(); set->Release();
} }*/
Sleep(1); Sleep(1);
} }
//server.Stop(); server.Stop();
//server.Shutdown(); server.Shutdown();
listener.Shutdown(); //listener.Shutdown();
Sleep(1000); Sleep(1000);
system("pause"); system("pause");
for(int i = 0; i < (int)clients.size(); i++)
delete clients.at(i);
return 0; 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) void clientProc(IClient* client)
{ {
cout << "Proc" << endl; 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