Danbias/Code/Game/DanBiasServer/GameServer.cpp

162 lines
3.8 KiB
C++
Raw Normal View History

2013-12-12 09:33:59 +01:00
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#define NOMINMAX
2013-12-12 09:33:59 +01:00
#include <Windows.h>
#include <WindowShell.h>
#include <iostream>
2013-12-12 09:33:59 +01:00
#include "GameServer.h"
2014-01-07 10:26:09 +01:00
#include "Helpers\ServerDataReader.h"
#include "GameSession\GameSessionManager.h"
#include "LobbySessions\LobbyClient.h"
#include "GameSession\GameSession.h"
#include "AdminInterface\AdminInterface.h"
#include <Thread\OysterThread.h>
2014-01-07 10:26:09 +01:00
#include <Utilities.h>
2013-12-19 12:32:23 +01:00
#include <CollisionManager.h>
2013-12-12 09:33:59 +01:00
namespace DanBias
{
using namespace Oyster::Network;
GameServer* GameServer::instance = 0;
2013-12-19 12:32:23 +01:00
2014-01-07 10:26:09 +01:00
void GameServer::NetworkCallback(NetworkClient* client)
2013-12-12 09:33:59 +01:00
{
static bool myTest = false;
static int sessionId = -1;
2014-01-07 10:26:09 +01:00
printf("Client with ID [%i] connected.\n", client->GetID());
2013-12-20 09:42:02 +01:00
2013-12-19 12:32:23 +01:00
if(!myTest)
{
2014-01-07 10:26:09 +01:00
Utility::DynamicMemory::SmartPointer<LobbyClient> c = new LobbyClient(client);
2013-12-19 12:32:23 +01:00
2014-01-07 10:26:09 +01:00
GameSessionDescription desc;
desc.mapName = L"test";
desc.clients.Push(c);
desc.exitDestionation = this->mainLobby;
if((sessionId = GameSessionManager::AddSession(desc, true)) == 0)
printf("Failed to create a game session\n");
myTest = true;
2014-01-07 10:26:09 +01:00
//myTest = new GameSession();
//
//DanBias::GameSession::GameSessionDescription desc;
//desc.owner = 0;
//desc.clients.Push(c);
//
//if(!myTest->Create(desc)) return;
//myTest->Run();
}
else
{
Utility::DynamicMemory::SmartPointer<LobbyClient> c = new LobbyClient(client);
GameSessionManager::JoinSession(sessionId, c);
2013-12-19 12:32:23 +01:00
}
2014-01-07 10:26:09 +01:00
//Utility::DynamicMemory::SmartPointer<LobbyClient> c = new LobbyClient(client);
//this->mainLobby->Attach(c, this->mainLobby->GetPostbox());
2013-12-12 09:33:59 +01:00
}
2014-01-07 10:26:09 +01:00
2013-12-12 09:33:59 +01:00
GameServer::GameServer()
: initiated(0)
, running(0)
, released(0)
, maxClients(0)
, mainLobby(0)
, server(0)
{ this->instance = this; }
2013-12-12 09:33:59 +01:00
GameServer::~GameServer()
{
}
DanBiasServerReturn GameServer::Create()
{
this->server = new NetworkServer();
this->mainLobby = new MainLobby();
InitData data;
if(!LoadIniFile(data)) return DanBiasServerReturn_Error;
NetworkServer::INIT_DESC serverDesc;
this->maxClients = data.clients;
2013-12-12 10:36:55 +01:00
serverDesc.port = data.port;
serverDesc.recvObj = this;
serverDesc.callbackType = Oyster::Network::NetworkClientCallbackType_Object;
2013-12-12 09:33:59 +01:00
if(!this->server->Init(serverDesc)) return DanBiasServerReturn_Error;
if(!WindowShell::CreateConsoleWindow()) return DanBiasServerReturn_Error;
this->initiated = true;
return DanBiasServerReturn_Sucess;
}
DanBiasServerReturn GameServer::Run()
{
2013-12-12 10:36:55 +01:00
if(this->running) return DanBiasServerReturn_Error;
if(this->released) return DanBiasServerReturn_Error;
if(!this->initiated) return DanBiasServerReturn_Error;
if(!this->server->Start()) return DanBiasServerReturn_Error;
2014-01-07 10:26:09 +01:00
//Oyster::Thread::OysterThread ioThread;
//ioThread.Create(this, true,
2013-12-12 09:33:59 +01:00
2013-12-13 23:47:16 +01:00
while (true)
2013-12-12 09:33:59 +01:00
{
2013-12-13 23:47:16 +01:00
if(!WindowShell::Frame()) break;
this->mainLobby->Frame();
2014-01-07 10:26:09 +01:00
if(GetAsyncKeyState(0x51)) //Q for exit
2014-01-07 10:26:09 +01:00
break;
2013-12-12 09:33:59 +01:00
}
return DanBiasServerReturn_Sucess;
}
DanBiasServerReturn GameServer::Release()
{
GameSessionManager::CloseSession();
this->mainLobby->Release();
2013-12-19 12:32:23 +01:00
delete this->mainLobby;
2013-12-12 10:36:55 +01:00
this->server->Shutdown();
delete this->server;
2013-12-12 09:33:59 +01:00
this->released = true;
return DanBiasServerReturn_Sucess;
}
NetworkSession* GameServer::MainLobbyInstance()
{
return GameServer::instance->mainLobby;
}
2013-12-12 09:33:59 +01:00
bool GameServer::LoadIniFile(InitData& ini)
{
std::ifstream in;
std::string f = GetInitPath(InitPath_ServerIni);
in.open(f, std::ios::in);
if(!in.is_open()) return false;
std::string buffer;
while (!in.eof())
{
in >> buffer;
if(buffer == "port")
{
in >> ini.port;
}
else if(buffer == "clients")
{
in >> ini.clients;
}
}
in.close();
return true;
}
}//End namespace DanBias