Danbias/Code/Game/GameServer/Implementation/GameServer.cpp

95 lines
1.8 KiB
C++
Raw Normal View History

2014-01-28 09:00:02 +01:00
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#define NOMINMAX
#include <Windows.h>
#include <iostream>
2014-01-29 10:18:01 +01:00
#include <vld.h>
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
#include "..\GameServerAPI.h"
#include "..\GameLobby.h"
2014-01-28 09:00:02 +01:00
#include "..\GameSession.h"
2014-01-29 10:18:01 +01:00
#include <NetworkServer.h>
2014-01-28 09:00:02 +01:00
#include <NetworkClient.h>
2014-01-29 10:18:01 +01:00
#include <WindowShell.h>
#include <Utilities.h>
#include <WinTimer.h>
#include <Thread\OysterThread.h>
using namespace DanBias;
using namespace Oyster::Network;
using namespace Oyster::Thread;
using namespace Utility;
namespace
2014-01-28 09:00:02 +01:00
{
2014-01-29 10:18:01 +01:00
GameLobby lobby;
NetworkServer server;
WinTimer timer;
GameServerAPI instance;
//typedef void(*WorkerThreadFnc)(GameServerAPI*);
}
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
DanBiasServerReturn GameServerAPI::Create(const GameInitDesc& desc)
{
if(server.Init(desc.listenPort, &lobby) == NetworkServer::ServerReturnCode_Error)
2014-01-28 09:00:02 +01:00
{
2014-01-29 10:18:01 +01:00
return DanBiasServerReturn_Error;
2014-01-28 09:00:02 +01:00
}
std::printf("Server created!\t-\t%s: [%i]\n\n", server.GetLanAddress().c_str(), desc.listenPort);
2014-01-29 10:18:01 +01:00
return DanBiasServerReturn_Sucess;
}
void GameServerAPI::Start()
{
server.Start();
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
timer.reset();
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
while (true)
{
server.ProcessConnectedClients();
2014-01-29 10:18:01 +01:00
lobby.Update();
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
if(GetAsyncKeyState(0x51)) //Q for exit
break;
}
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
double total = timer.getElapsedSeconds();
int time = (int)total;
int hour, min, sec;
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
hour=time / 3600;
time=time % 3600;
min=time / 60;
time=time % 60;
sec = time;
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
printf( "Server has been running for: %i:%i:%i - [hh:mm:ss] \n\n", hour, min, sec );
printf( "Terminating in : ");
for (int i = 0; i < 4; i++)
2014-01-28 09:00:02 +01:00
{
2014-01-29 10:18:01 +01:00
printf( "%i ", 3-i );
Sleep(1000);
2014-01-28 09:00:02 +01:00
}
2014-01-29 10:18:01 +01:00
}
void GameServerAPI::Stop()
{
server.Stop();
lobby.ProcessClients();
}
void GameServerAPI::Terminate()
{
lobby.Release();
server.Shutdown();
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
printf( "Server terminated!" );
2014-01-28 09:00:02 +01:00
2014-01-29 10:18:01 +01:00
}