Danbias/Code/Network/OysterNetworkServer/IServer.cpp

154 lines
2.1 KiB
C++
Raw Normal View History

#include "IServer.h"
2013-12-08 23:56:17 +01:00
#include "../NetworkDependencies/Listener.h"
#include "IClient.h"
#include "../NetworkDependencies/PostBox.h"
#include "../../Misc/Utilities.h"
2013-12-08 23:56:17 +01:00
using namespace Oyster::Network;
using namespace ::Server;
using namespace Utility::DynamicMemory;
/*************************************
PrivateData
*************************************/
struct IServer::PrivateData
{
2013-12-08 23:56:17 +01:00
PrivateData();
~PrivateData();
bool Init(INIT_DESC& initDesc);
bool Start();
bool Stop();
bool Shutdown();
//
IListener* listener;
INIT_DESC initDesc;
bool started;
IPostBox<SmartPointer<int>> *postBox;
};
IServer::PrivateData::PrivateData()
{
listener = 0;
started = false;
postBox = new PostBox<SmartPointer<int>>();
}
2013-12-08 23:56:17 +01:00
IServer::PrivateData::~PrivateData()
{
Shutdown();
}
2013-12-08 23:56:17 +01:00
bool IServer::PrivateData::Init(INIT_DESC& initDesc)
{
//Check if it's a valid port
if(initDesc.port == 0)
{
return false;
}
this->initDesc = initDesc;
//Initiate listener
listener = new Listener();
((Listener*)listener)->Init(this->initDesc.port, false);
return true;
}
bool IServer::PrivateData::Start()
{
2013-12-08 23:56:17 +01:00
//Start listener
((Listener*)listener)->Start();
started = true;
2013-12-08 23:56:17 +01:00
return true;
}
bool IServer::PrivateData::Stop()
{
if(listener)
{
((Listener*)listener)->Stop();
}
started = false;
return true;
}
bool IServer::PrivateData::Shutdown()
{
if(listener)
{
delete listener;
listener = NULL;
}
started = false;
return true;
}
/*************************************
IServer
*************************************/
IServer::IServer()
{
privateData = new PrivateData();
}
IServer::~IServer()
{
if(privateData)
{
delete privateData;
}
}
bool IServer::Init(INIT_DESC& initDesc)
{
2013-12-08 23:56:17 +01:00
privateData->Init(initDesc);
return true;
}
bool IServer::Start()
{
2013-12-08 23:56:17 +01:00
privateData->Start();
return true;
}
bool IServer::Stop()
{
2013-12-08 23:56:17 +01:00
privateData->Stop();
return true;
}
bool IServer::Shutdown()
{
2013-12-08 23:56:17 +01:00
privateData->Shutdown();
return true;
}
void IServer::AddSession(ISession* session)
{
}
void IServer::RemoveSession(ISession* session)
{
2013-12-08 23:56:17 +01:00
}
bool IServer::IsStarted() const
{
return privateData->started;
}