2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <map>
|
|
|
|
|
2018-03-06 22:22:19 +01:00
|
|
|
#include <Network/Hive.h>
|
|
|
|
#include <Network/Acceptor.h>
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-03-06 22:22:19 +01:00
|
|
|
#include <Version.h>
|
|
|
|
#include <Logging/Logger.h>
|
2018-06-10 16:34:26 +00:00
|
|
|
#include <Config/ConfigMgr.h>
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
#include "Framework.h"
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
#include "ServerLobby.h"
|
|
|
|
|
|
|
|
#include "GameConnection.h"
|
|
|
|
#include "RestConnector.h"
|
2018-03-06 23:20:07 +01:00
|
|
|
#include "Forwards.h"
|
|
|
|
#include <Forwards.h>
|
2017-08-08 13:53:47 +02:00
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
2019-06-03 00:21:58 +10:00
|
|
|
using namespace Sapphire;
|
|
|
|
|
2019-06-02 23:28:19 +10:00
|
|
|
Sapphire::Lobby::Network::RestConnector g_restConnector;
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2019-06-02 23:28:19 +10:00
|
|
|
namespace Sapphire::Lobby
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
ServerLobby::ServerLobby( const std::string& configPath ) :
|
|
|
|
m_configPath( configPath ),
|
|
|
|
m_numConnections( 0 )
|
|
|
|
{
|
2019-06-02 23:28:19 +10:00
|
|
|
m_pConfig = std::make_shared< Sapphire::Common::ConfigMgr >();
|
2018-12-23 03:53:08 +01:00
|
|
|
}
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
LobbySessionPtr ServerLobby::getSession( char* sessionId )
|
|
|
|
{
|
|
|
|
return g_restConnector.getSession( sessionId );
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2019-01-07 23:00:09 +11:00
|
|
|
Sapphire::Common::Config::LobbyConfig& ServerLobby::getConfig()
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2019-01-07 23:00:09 +11:00
|
|
|
return m_config;
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
void ServerLobby::run( int32_t argc, char* argv[] )
|
|
|
|
{
|
2018-12-30 22:36:44 +11:00
|
|
|
Logger::init( "log/lobby" );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::info( "===========================================================" );
|
|
|
|
Logger::info( "Sapphire Server Project " );
|
|
|
|
Logger::info( "Version: " + Version::VERSION );
|
|
|
|
Logger::info( "Git Hash: " + Version::GIT_HASH );
|
|
|
|
Logger::info( "Compiled: " __DATE__ " " __TIME__ );
|
|
|
|
Logger::info( "===========================================================" );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
if( !loadSettings( argc, argv ) )
|
|
|
|
{
|
|
|
|
Logger::fatal( "Error loading settings! " );
|
|
|
|
return;
|
|
|
|
}
|
2018-10-25 12:17:40 +02:00
|
|
|
|
2019-01-25 19:00:21 +11:00
|
|
|
Logger::setLogLevel( m_config.global.general.logLevel );
|
|
|
|
|
2019-06-03 00:21:58 +10:00
|
|
|
auto pFw = std::make_shared< Framework >();
|
|
|
|
auto hive = Sapphire::Network::make_Hive();
|
|
|
|
Sapphire::Network::addServerToHive< Network::GameConnection >( m_ip, m_port, hive, pFw );
|
2018-10-25 12:17:40 +02:00
|
|
|
|
2019-01-07 00:03:48 +11:00
|
|
|
Logger::info( "Lobby server running on {0}:{1}", m_ip, m_port );
|
2017-08-08 13:53:47 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
std::vector< std::thread > threadGroup;
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2019-06-03 00:21:58 +10:00
|
|
|
threadGroup.emplace_back( std::bind( &Sapphire::Network::Hive::run, hive.get() ) );
|
2018-12-23 03:53:08 +01:00
|
|
|
|
|
|
|
for( auto& thread : threadGroup )
|
|
|
|
if( thread.joinable() )
|
|
|
|
thread.join();
|
2018-08-29 21:40:59 +02:00
|
|
|
|
|
|
|
}
|
2018-12-23 03:53:08 +01:00
|
|
|
|
|
|
|
bool ServerLobby::loadSettings( int32_t argc, char* argv[] )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2019-01-04 22:37:01 +11:00
|
|
|
Logger::info( "Loading config {0}", m_configPath );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2019-01-07 23:00:09 +11:00
|
|
|
bool failedLoad = false;
|
|
|
|
if( !m_pConfig->loadGlobalConfig( m_config.global, "global.ini" ) )
|
|
|
|
{
|
|
|
|
Logger::fatal( "Error loading config global.ini, copying default..." );
|
|
|
|
failedLoad = true;
|
|
|
|
}
|
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
if( !m_pConfig->loadConfig( m_configPath ) )
|
2018-08-29 21:40:59 +02:00
|
|
|
{
|
2019-01-04 22:37:01 +11:00
|
|
|
Logger::fatal( "Error loading config {0}", m_configPath );
|
2019-01-07 23:00:09 +11:00
|
|
|
failedLoad = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( failedLoad )
|
|
|
|
{
|
|
|
|
Logger::fatal( "If this is the first time starting the server, "
|
|
|
|
"we've copied the default configs for your editing pleasure." );
|
2018-12-23 03:53:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-01-07 23:00:09 +11:00
|
|
|
|
|
|
|
// load lobby config
|
|
|
|
m_config.allowNoSessionConnect = m_pConfig->getValue< bool >( "Lobby", "AllowNoSessionConnect", false );
|
|
|
|
m_config.worldName = m_pConfig->getValue< std::string >( "Lobby", "WorldName", "Sapphire" );
|
|
|
|
|
|
|
|
m_config.network.listenIp = m_pConfig->getValue< std::string >( "Network", "ListenIp", "0.0.0.0" );
|
|
|
|
m_config.network.listenPort = m_pConfig->getValue< uint16_t >( "Network", "ListenPort", 54994 );
|
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
std::vector< std::string > args( argv + 1, argv + argc );
|
|
|
|
for( size_t i = 0; i + 1 < args.size(); i += 2 )
|
|
|
|
{
|
|
|
|
std::string arg( "" );
|
|
|
|
std::string val( "" );
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
try
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2018-12-23 03:53:08 +01:00
|
|
|
std::transform( arg.begin(), arg.end(), arg.begin(), [](unsigned char c){ return std::tolower( c ); } );
|
|
|
|
val = std::string( args[ i + 1 ] );
|
|
|
|
|
|
|
|
// trim '-' from start of arg
|
|
|
|
arg = arg.erase( 0, arg.find_first_not_of( '-' ) );
|
|
|
|
|
|
|
|
if( arg == "ip" )
|
|
|
|
{
|
|
|
|
// todo: ip addr in config
|
2019-01-07 23:00:09 +11:00
|
|
|
m_config.network.listenIp = val;
|
2018-12-23 03:53:08 +01:00
|
|
|
}
|
|
|
|
else if( arg == "p" || arg == "port" )
|
|
|
|
{
|
2019-01-07 23:00:09 +11:00
|
|
|
m_config.network.listenPort = std::stoi( val );
|
2018-12-23 03:53:08 +01:00
|
|
|
}
|
|
|
|
else if( arg == "worldip" || arg == "worldip" )
|
|
|
|
{
|
2019-01-07 23:00:09 +11:00
|
|
|
m_config.global.network.zoneHost = val;
|
2018-12-23 03:53:08 +01:00
|
|
|
}
|
|
|
|
else if( arg == "worldport" )
|
|
|
|
{
|
2019-01-07 23:00:09 +11:00
|
|
|
m_config.global.network.zonePort = std::stoi( val );
|
2018-12-23 03:53:08 +01:00
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|
2018-12-23 03:53:08 +01:00
|
|
|
catch( ... )
|
2017-08-08 13:53:47 +02:00
|
|
|
{
|
2019-01-04 22:37:01 +11:00
|
|
|
Logger::error( "Error parsing argument: {0} value: {1}\n", arg, val );
|
2018-12-23 03:53:08 +01:00
|
|
|
Logger::error( "Usage: <arg> <val> \n" );
|
2018-08-29 21:40:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:00:09 +11:00
|
|
|
m_port = m_config.network.listenPort;
|
|
|
|
m_ip = m_config.network.listenIp;
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2019-01-07 00:03:48 +11:00
|
|
|
|
2019-01-07 23:00:09 +11:00
|
|
|
g_restConnector.restHost = m_config.global.network.restHost + ":" +
|
|
|
|
std::to_string( m_config.global.network.restPort );
|
2019-01-07 23:26:34 +11:00
|
|
|
g_restConnector.serverSecret = m_config.global.general.serverSecret;
|
2018-08-29 21:40:59 +02:00
|
|
|
|
2018-12-23 03:53:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-08 13:53:47 +02:00
|
|
|
}
|