Danbias/Code/Network/NetworkAPI/NetworkServer.cpp

202 lines
3.0 KiB
C++
Raw Normal View History

2013-12-10 08:32:08 +01:00
#include "NetworkServer.h"
#include "NetworkClient.h"
2013-12-08 23:56:17 +01:00
#include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/PostBox.h"
2013-12-10 08:32:08 +01:00
2013-12-08 23:56:17 +01:00
#include "../../Misc/Utilities.h"
#include "../../Misc/Thread/OysterThread.h"
2013-12-08 23:56:17 +01:00
using namespace Oyster::Network;
using namespace ::Server;
using namespace Utility::DynamicMemory;
using namespace Oyster::Thread;
2013-12-08 23:56:17 +01:00
/*************************************
PrivateData
*************************************/
2013-12-10 08:32:08 +01:00
struct NetworkServer::PrivateData : public IThreadObject
{
2013-12-08 23:56:17 +01:00
PrivateData();
~PrivateData();
bool Init(INIT_DESC& initDesc);
bool Start();
bool Stop();
bool Shutdown();
void CheckForNewClient();
virtual bool DoWork();
2013-12-08 23:56:17 +01:00
//
IListener* listener;
INIT_DESC initDesc;
bool started;
//Postbox for new clients
IPostBox<int> *postBox;
//Server thread
OysterThread thread;
2013-12-08 23:56:17 +01:00
};
2013-12-10 08:32:08 +01:00
NetworkServer::PrivateData::PrivateData()
2013-12-08 23:56:17 +01:00
{
listener = 0;
started = false;
postBox = new PostBox<int>;
2013-12-08 23:56:17 +01:00
}
2013-12-10 08:32:08 +01:00
NetworkServer::PrivateData::~PrivateData()
2013-12-08 23:56:17 +01:00
{
Shutdown();
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::PrivateData::Init(INIT_DESC& initDesc)
2013-12-08 23:56:17 +01:00
{
//Check if it's a valid port
if(initDesc.port == 0)
{
return false;
}
this->initDesc = initDesc;
//Initiate listener
listener = new Listener(postBox);
2013-12-08 23:56:17 +01:00
((Listener*)listener)->Init(this->initDesc.port, false);
thread.Create(this, false);
2013-12-08 23:56:17 +01:00
return true;
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::PrivateData::Start()
{
2013-12-08 23:56:17 +01:00
//Start listener
((Listener*)listener)->Start();
started = true;
thread.Start();
2013-12-08 23:56:17 +01:00
return true;
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::PrivateData::Stop()
2013-12-08 23:56:17 +01:00
{
if(listener)
{
((Listener*)listener)->Stop();
}
started = false;
thread.Stop();
2013-12-08 23:56:17 +01:00
return true;
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::PrivateData::Shutdown()
2013-12-08 23:56:17 +01:00
{
//Stop server main thread
thread.Stop();
2013-12-08 23:56:17 +01:00
if(listener)
{
delete listener;
listener = NULL;
}
if(postBox)
{
delete postBox;
postBox = NULL;
}
2013-12-08 23:56:17 +01:00
started = false;
return true;
}
//Checks for new clients and sends them to the proc function.
2013-12-10 08:32:08 +01:00
void NetworkServer::PrivateData::CheckForNewClient()
{
if(postBox->IsFull())
{
int clientSocketNum;
postBox->FetchMessage(clientSocketNum);
//Safety check that is probably not needed.
if(clientSocketNum == -1)
{
return;
}
2013-12-10 08:32:08 +01:00
//Create client and Proc function if the pointer is not NULL
if(initDesc.proc)
{
Oyster::Network::NetworkClient* client = new Oyster::Network::NetworkClient();
initDesc.proc((NetworkClient*)client);
}
}
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::PrivateData::DoWork()
{
CheckForNewClient();
return true;
}
2013-12-08 23:56:17 +01:00
/*************************************
2013-12-10 08:32:08 +01:00
NetworkServer
2013-12-08 23:56:17 +01:00
*************************************/
2013-12-10 08:32:08 +01:00
NetworkServer::NetworkServer()
2013-12-08 23:56:17 +01:00
{
privateData = new PrivateData();
}
2013-12-10 08:32:08 +01:00
NetworkServer::~NetworkServer()
2013-12-08 23:56:17 +01:00
{
if(privateData)
{
delete privateData;
}
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::Init(INIT_DESC& initDesc)
{
2013-12-08 23:56:17 +01:00
privateData->Init(initDesc);
return true;
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::Start()
{
2013-12-08 23:56:17 +01:00
privateData->Start();
return true;
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::Stop()
{
2013-12-08 23:56:17 +01:00
privateData->Stop();
return true;
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::Shutdown()
{
2013-12-08 23:56:17 +01:00
privateData->Shutdown();
return true;
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::IsStarted() const
2013-12-08 23:56:17 +01:00
{
return privateData->started;
}