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

189 lines
4.1 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>
2014-01-30 00:19:00 +01:00
#include <thread>
2014-01-29 10:18:01 +01:00
using namespace DanBias;
using namespace Oyster::Network;
using namespace Oyster::Thread;
using namespace Utility;
void DefaultClientConnectedNotify(int ID, wchar_t clientAlias[255], wchar_t clientIp[255])
{
}
void DefaultClientDisconnectedNotify(int ID)
{
}
2014-01-29 10:18:01 +01:00
namespace
2014-01-28 09:00:02 +01:00
{
GameLobby lobby;
NetworkServer server;
WinTimer timer;
GameServerAPI::ClientConnectedNotify clientConnectedCallback = DefaultClientConnectedNotify;
GameServerAPI::ClientDisconnectedNotify clientDisconnectedCallback = DefaultClientDisconnectedNotify;
2014-01-29 10:18:01 +01:00
}
2014-01-28 09:00:02 +01:00
2014-01-30 00:19:00 +01:00
DanBiasServerReturn GameServerAPI::ServerInitiate(const ServerInitDesc& desc)
2014-01-29 10:18:01 +01:00
{
ServerOptions opt;
opt.mainOptions.listenPort = desc.listenPort;
opt.mainOptions.ownerSession = &lobby;
if(server.Init(opt) == 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
}
2014-01-30 00:19:00 +01:00
GameSession::GameDescription d;
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;
}
2014-01-30 00:19:00 +01:00
void GameServerAPI::ServerStart()
2014-01-29 10:18:01 +01:00
{
timer.reset();
2014-01-30 00:19:00 +01:00
server.Start();
}
void GameServerAPI::ServerStop()
{
if(!server.IsRunning()) return;
2014-01-30 00:19:00 +01:00
lobby.Release();
server.Shutdown();
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
hour = time / 3600;
time = time % 3600;
min = time / 60;
time = time % 60;
2014-01-29 10:18:01 +01:00
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 < 3; i++)
//{
// printf( "%i ", 3-i );
// Sleep(1000);
//}
2014-01-30 00:19:00 +01:00
printf( "\nServer terminated!" );
2014-01-29 10:18:01 +01:00
}
2014-01-30 00:19:00 +01:00
void GameServerAPI::ServerUpdate()
2014-01-29 10:18:01 +01:00
{
2014-01-30 00:19:00 +01:00
server.Update();
lobby.Update();
2014-01-28 09:00:02 +01:00
2014-01-30 00:19:00 +01:00
}
GameServerAPI::GameServerInfo GameServerAPI::ServerGetInfo()
{
GameServerAPI::GameServerInfo i;
std::wstring temp;
Utility::String::StringToWstring(server.GetLanAddress().c_str(), temp) ;
memcpy(&i.serverIp[0], temp.c_str(), sizeof(wchar_t) * (temp.length() + 1));
2014-01-30 00:19:00 +01:00
i.listenPort = server.GetPort();
return i;
2014-01-29 10:18:01 +01:00
}
bool GameServerAPI::ServerIsRunning()
{
return server.IsRunning();
}
void GameServerAPI::NotifyWhenClientConnect(ClientConnectedNotify func)
{
if(!func) clientConnectedCallback = DefaultClientConnectedNotify;
else clientConnectedCallback = func;
}
void GameServerAPI::NotifyWhenClientDisconnect(ClientDisconnectedNotify func)
{
if(!func) clientDisconnectedCallback = DefaultClientDisconnectedNotify;
else clientDisconnectedCallback = func;
}
void GameServerAPI::GameSetMapName(const wchar_t* val)
2014-01-30 00:19:00 +01:00
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
//d.mapNumber = val; //TODO: implement
2014-01-30 00:19:00 +01:00
lobby.SetGameDesc(d);
}
void GameServerAPI::GameSetMaxClients(const int& val)
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
d.maxClients = val;
lobby.SetGameDesc(d);
}
void GameServerAPI::GameSetGameMode(const wchar_t* val)
2014-01-30 00:19:00 +01:00
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
//d.gameMode = val; //TODO: implement
2014-01-30 00:19:00 +01:00
lobby.SetGameDesc(d);
}
void GameServerAPI::GameSetGameTime(const int& val)
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
d.gameTime = val;
lobby.SetGameDesc(d);
}
int GameServerAPI::GameGetMapId()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.mapNumber;
}
int GameServerAPI::GameGetMaxClients()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.maxClients;
}
int GameServerAPI::GameGetGameMode()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.gameMode;
}
int GameServerAPI::GameGetGameTime()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.gameTime;
}
const char* GameServerAPI::GameGetGameName()
{
LobbyLevelData d;
2014-01-30 00:19:00 +01:00
lobby.GetGameDesc(d);
return d.gameName.c_str();
}
bool GameServerAPI::GameStart()
{
if(lobby.StartGameSession())
{
return true;
}
return false;
2014-01-30 00:19:00 +01:00
}