Danbias/Code/Network/NetworkAPI/NetworkServer.cpp

220 lines
3.6 KiB
C++
Raw Normal View History

#ifndef INCLUDE_WINSOCK_LIB
#define INCLUDE_WINSOCK_LIB
#pragma comment(lib, "ws2_32.lib")
#endif
2013-12-10 08:32:08 +01:00
#include "NetworkServer.h"
2013-12-08 23:56:17 +01:00
#include "../NetworkDependencies/Listener.h"
#include "../NetworkDependencies/PostBox.h"
#include "../NetworkDependencies/WinsockFunctions.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();
void Stop();
void Shutdown();
void CheckForNewClient();
bool DoWork();
2013-12-08 23:56:17 +01:00
//
IListener* listener;
INIT_DESC initDesc;
bool started;
//Postbox for new clients
2013-12-19 12:32:23 +01:00
PostBox<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;
2013-12-19 12:32:23 +01:00
//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;
}
if(!InitWinSock())
return false;
2013-12-08 23:56:17 +01:00
this->initDesc = initDesc;
//Initiate listener
2013-12-19 12:32:23 +01:00
listener = new Listener(&postBox);
if(!((Listener*)listener)->Init(this->initDesc.port, false))
{
return false;
}
2013-12-08 23:56:17 +01:00
if(thread.Create(this, false) == OYSTER_THREAD_ERROR_FAILED)
{
return 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
if(!((Listener*)listener)->Start())
{
return false;
}
2013-12-08 23:56:17 +01:00
started = true;
if(thread.Start() == OYSTER_THREAD_ERROR_FAILED)
{
return false;
}
2013-12-08 23:56:17 +01:00
return true;
}
void NetworkServer::PrivateData::Stop()
2013-12-08 23:56:17 +01:00
{
if(listener)
{
((Listener*)listener)->Stop();
}
started = false;
2014-01-07 10:26:09 +01:00
thread.Stop(true);
2013-12-08 23:56:17 +01:00
}
void NetworkServer::PrivateData::Shutdown()
2013-12-08 23:56:17 +01:00
{
if(listener)
{
2014-01-07 10:26:09 +01:00
listener->Shutdown();
2013-12-08 23:56:17 +01:00
delete listener;
listener = NULL;
}
started = false;
2014-01-07 10:26:09 +01:00
thread.Terminate();
2013-12-08 23:56:17 +01:00
ShutdownWinSock();
2013-12-08 23:56:17 +01:00
}
//Checks for new clients and sends them to the proc function.
2013-12-10 08:32:08 +01:00
void NetworkServer::PrivateData::CheckForNewClient()
{
2013-12-19 12:32:23 +01:00
if(postBox.IsFull())
{
2013-12-19 12:32:23 +01:00
int clientSocketNum = postBox.FetchMessage();
//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.callbackType == NetworkClientCallbackType_Function)
{
2013-12-17 10:25:34 +01:00
Oyster::Network::NetworkClient *client = new Oyster::Network::NetworkClient(clientSocketNum);
initDesc.recvObj.clientConnectFnc(client);
}
else if(initDesc.callbackType == NetworkClientCallbackType_Object)
2013-12-10 08:32:08 +01:00
{
2013-12-17 10:25:34 +01:00
Oyster::Network::NetworkClient *client = new Oyster::Network::NetworkClient(clientSocketNum);
2014-01-07 10:26:09 +01:00
initDesc.recvObj.clientConnectObject->NetworkCallback(client);
2013-12-10 08:32:08 +01:00
}
}
}
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)
{
if(!privateData->Init(initDesc))
{
return false;
}
return true;
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::Start()
{
if(!privateData->Start())
{
return false;
}
return true;
}
void NetworkServer::Stop()
{
2013-12-08 23:56:17 +01:00
privateData->Stop();
}
void NetworkServer::Shutdown()
{
2013-12-08 23:56:17 +01:00
privateData->Shutdown();
}
2013-12-10 08:32:08 +01:00
bool NetworkServer::IsStarted() const
2013-12-08 23:56:17 +01:00
{
return privateData->started;
}