TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Main.cpp File Reference
#include "SessionManager.h"
#include "AppenderDB.h"
#include "ProcessPriority.h"
#include "RealmList.h"
#include "GitRevision.h"
#include "SslContext.h"
#include "DatabaseLoader.h"
#include "LoginRESTService.h"
#include <iostream>
#include <boost/program_options.hpp>
#include <google/protobuf/stubs/common.h>
#include "ServiceWin32.h"
+ Include dependency graph for Main.cpp:

Macros

#define _TRINITY_BNET_CONFIG   "bnetserver.conf"
 

Functions

void ServiceStatusWatcher (boost::system::error_code const &error)
 
bool StartDB ()
 Initialize connection to the database. More...
 
void StopDB ()
 Close the connection to the database. More...
 
void SignalHandler (boost::system::error_code const &error, int signalNumber)
 
void KeepDatabaseAliveHandler (boost::system::error_code const &error)
 
void BanExpiryHandler (boost::system::error_code const &error)
 
variables_map GetConsoleArguments (int argc, char **argv, std::string &configFile, std::string &configService)
 
int main (int argc, char **argv)
 

Variables

char serviceName [] = "bnetserver"
 
char serviceLongName [] = "TrinityCore bnet service"
 
char serviceDescription [] = "TrinityCore Battle.net emulator authentication service"
 
int m_ServiceStatus = -1
 
static
boost::asio::deadline_timer * 
_serviceStatusWatchTimer
 
static boost::asio::io_service * _ioService
 
static
boost::asio::deadline_timer * 
_dbPingTimer
 
static uint32 _dbPingInterval
 
static
boost::asio::deadline_timer * 
_banExpiryCheckTimer
 
static uint32 _banExpiryCheckInterval
 

Macro Definition Documentation

#define _TRINITY_BNET_CONFIG   "bnetserver.conf"

Function Documentation

void BanExpiryHandler ( boost::system::error_code const error)
268 {
269  if (!error)
270  {
273 
276  }
277 }
Definition: LoginDatabase.h:34
void Execute(const char *sql)
Definition: DatabaseWorkerPool.h:87
Definition: LoginDatabase.h:33
void BanExpiryHandler(boost::system::error_code const &error)
Definition: Main.cpp:267
float seconds()
Definition: units.h:97
LoginDatabaseWorkerPool LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22
PreparedStatement * GetPreparedStatement(PreparedStatementIndex index)
Definition: DatabaseWorkerPool.h:263
static boost::asio::deadline_timer * _banExpiryCheckTimer
Definition: Main.cpp:72
static uint32 _banExpiryCheckInterval
Definition: Main.cpp:73

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

variables_map GetConsoleArguments ( int  argc,
char **  argv,
std::string &  configFile,
std::string &  configService 
)
299 {
300  (void)configService;
301 
302  options_description all("Allowed options");
303  all.add_options()
304  ("help,h", "print usage message")
305  ("version,v", "print version build info")
306  ("config,c", value<std::string>(&configFile)->default_value(_TRINITY_BNET_CONFIG), "use <arg> as configuration file")
307  ;
308 #if PLATFORM == PLATFORM_WINDOWS
309  options_description win("Windows platform specific options");
310  win.add_options()
311  ("service,s", value<std::string>(&configService)->default_value(""), "Windows service options: [install | uninstall]")
312  ;
313 
314  all.add(win);
315 #endif
316  variables_map variablesMap;
317  try
318  {
319  store(command_line_parser(argc, argv).options(all).allow_unregistered().run(), variablesMap);
320  notify(variablesMap);
321  }
322  catch (std::exception& e)
323  {
324  std::cerr << e.what() << "\n";
325  }
326 
327  if (variablesMap.count("help"))
328  {
329  std::cout << all << "\n";
330  }
331  else if (variablesMap.count("version"))
332  {
333  std::cout << GitRevision::GetFullVersion() << "\n";
334  }
335 
336  return variablesMap;
337 }
TC_COMMON_API char const * GetFullVersion()
Definition: GitRevision.cpp:51
#define _TRINITY_BNET_CONFIG
Definition: Main.cpp:42
bool all(float x)
Definition: g3dmath.h:431

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void KeepDatabaseAliveHandler ( boost::system::error_code const error)
256 {
257  if (!error)
258  {
259  TC_LOG_INFO("server.bnetserver", "Ping MySQL to keep connection alive");
261 
264  }
265 }
void KeepAlive()
Keeps all our MySQL connections alive, prevent the server from disconnecting us.
Definition: DatabaseWorkerPool.cpp:243
void KeepDatabaseAliveHandler(boost::system::error_code const &error)
Definition: Main.cpp:255
LoginDatabaseWorkerPool LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22
float minutes()
Definition: units.h:102
static boost::asio::deadline_timer * _dbPingTimer
Definition: Main.cpp:70
static uint32 _dbPingInterval
Definition: Main.cpp:71
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)
76 {
77  signal(SIGABRT, &Trinity::AbortHandler);
78 
79  std::string configFile = _TRINITY_BNET_CONFIG;
80  std::string configService;
81  auto vm = GetConsoleArguments(argc, argv, configFile, configService);
82  // exit if help or version is enabled
83  if (vm.count("help") || vm.count("version"))
84  return 0;
85 
87 
88 #if PLATFORM == PLATFORM_WINDOWS
89  if (configService.compare("install") == 0)
90  return WinServiceInstall() ? 0 : 1;
91  else if (configService.compare("uninstall") == 0)
92  return WinServiceUninstall() ? 0 : 1;
93  else if (configService.compare("run") == 0)
94  return WinServiceRun() ? 0 : 1;
95 #endif
96 
97  std::string configError;
98  if (!sConfigMgr->LoadInitial(configFile, configError))
99  {
100  printf("Error in config file: %s\n", configError.c_str());
101  return 1;
102  }
103 
104  sLog->RegisterAppender<AppenderDB>();
105  sLog->Initialize(nullptr);
106 
107  TC_LOG_INFO("server.bnetserver", "%s (bnetserver)", GitRevision::GetFullVersion());
108  TC_LOG_INFO("server.bnetserver", "<Ctrl-C> to stop.\n");
109  TC_LOG_INFO("server.bnetserver", "Using configuration file %s.", configFile.c_str());
110  TC_LOG_INFO("server.bnetserver", "Using SSL version: %s (library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
111  TC_LOG_INFO("server.bnetserver", "Using Boost version: %i.%i.%i", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
112 
113  // Seed the OpenSSL's PRNG here.
114  // That way it won't auto-seed when calling BigNumber::SetRand and slow down the first world login
115  BigNumber seed;
116  seed.SetRand(16 * 8);
117 
118  // bnetserver PID file creation
119  std::string pidFile = sConfigMgr->GetStringDefault("PidFile", "");
120  if (!pidFile.empty())
121  {
122  if (uint32 pid = CreatePIDFile(pidFile))
123  TC_LOG_INFO("server.bnetserver", "Daemon PID: %u\n", pid);
124  else
125  {
126  TC_LOG_ERROR("server.bnetserver", "Cannot create PID file %s.\n", pidFile.c_str());
127  return 1;
128  }
129  }
130 
132  {
133  TC_LOG_ERROR("server.bnetserver", "Failed to initialize SSL context");
134  return 1;
135  }
136 
137  // Initialize the database connection
138  if (!StartDB())
139  return 1;
140 
141  _ioService = new boost::asio::io_service();
142 
143  // Start the listening port (acceptor) for auth connections
144  int32 bnport = sConfigMgr->GetIntDefault("BattlenetPort", 1119);
145  if (bnport < 0 || bnport > 0xFFFF)
146  {
147  TC_LOG_ERROR("server.bnetserver", "Specified battle.net port (%d) out of allowed range (1-65535)", bnport);
148  StopDB();
149  delete _ioService;
150  return 1;
151  }
152 
153  if (!sLoginService.Start(*_ioService))
154  {
155  StopDB();
156  delete _ioService;
157  TC_LOG_ERROR("server.bnetserver", "Failed to initialize login service");
158  return 1;
159  }
160 
161  // Get the list of realms for the server
162  sRealmList->Initialize(*_ioService, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 10));
163 
164  std::string bindIp = sConfigMgr->GetStringDefault("BindIP", "0.0.0.0");
165 
166  sSessionMgr.StartNetwork(*_ioService, bindIp, bnport);
167 
168  // Set signal handlers
169  boost::asio::signal_set signals(*_ioService, SIGINT, SIGTERM);
170 #if PLATFORM == PLATFORM_WINDOWS
171  signals.add(SIGBREAK);
172 #endif
173  signals.async_wait(SignalHandler);
174 
175  // Set process priority according to configuration settings
176  SetProcessPriority("server.bnetserver");
177 
178  // Enabled a timed callback for handling the database keep alive ping
179  _dbPingInterval = sConfigMgr->GetIntDefault("MaxPingTime", 30);
180  _dbPingTimer = new boost::asio::deadline_timer(*_ioService);
183 
184  _banExpiryCheckInterval = sConfigMgr->GetIntDefault("BanExpiryCheckInterval", 60);
185  _banExpiryCheckTimer = new boost::asio::deadline_timer(*_ioService);
188 
189 #if PLATFORM == PLATFORM_WINDOWS
190  if (m_ServiceStatus != -1)
191  {
192  _serviceStatusWatchTimer = new boost::asio::deadline_timer(*_ioService);
195  }
196 #endif
197 
198  // Start the io service worker loop
199  _ioService->run();
200 
201  _banExpiryCheckTimer->cancel();
202  _dbPingTimer->cancel();
203 
204  sLoginService.Stop();
205 
206  sSessionMgr.StopNetwork();
207 
208  sRealmList->Close();
209 
210  // Close the Database Pool and library
211  StopDB();
212 
213  TC_LOG_INFO("server.bnetserver", "Halting process...");
214 
215  signals.cancel();
216 
217  delete _banExpiryCheckTimer;
218  delete _dbPingTimer;
219  delete _ioService;
221  return 0;
222 }
Definition: BigNumber.h:28
#define sSessionMgr
Definition: SessionManager.h:43
static boost::asio::io_service * _ioService
Definition: Main.cpp:69
static bool Initialize()
Definition: SslContext.cpp:21
uint32 CreatePIDFile(std::string const &filename)
create PID file
Definition: Util.cpp:222
void StopDB()
Close the connection to the database.
Definition: Main.cpp:243
variables_map GetConsoleArguments(int argc, char **argv, std::string &configFile, std::string &configService)
Definition: Main.cpp:298
#define sConfigMgr
Definition: Config.h:61
void BanExpiryHandler(boost::system::error_code const &error)
Definition: Main.cpp:267
TC_COMMON_API char const * GetFullVersion()
Definition: GitRevision.cpp:51
#define sLog
Definition: Log.h:154
int m_ServiceStatus
Definition: Main.cpp:56
#define _TRINITY_BNET_CONFIG
Definition: Main.cpp:42
void KeepDatabaseAliveHandler(boost::system::error_code const &error)
Definition: Main.cpp:255
float seconds()
Definition: units.h:97
static boost::asio::deadline_timer * _serviceStatusWatchTimer
Definition: Main.cpp:58
#define GOOGLE_PROTOBUF_VERIFY_VERSION
Definition: common.h:149
void SignalHandler(boost::system::error_code const &error, int signalNumber)
Definition: Main.cpp:249
float minutes()
Definition: units.h:102
LIBPROTOBUF_EXPORT void ShutdownProtobufLibrary()
static boost::asio::deadline_timer * _dbPingTimer
Definition: Main.cpp:70
int32_t int32
Definition: Define.h:146
void AbortHandler(int)
Definition: Errors.cpp:99
uint32_t uint32
Definition: Define.h:150
static uint32 _dbPingInterval
Definition: Main.cpp:71
void SetRand(int32 numbits)
Definition: BigNumber.cpp:74
void ServiceStatusWatcher(boost::system::error_code const &error)
Definition: Main.cpp:280
bool StartDB()
Initialize connection to the database.
Definition: Main.cpp:225
static boost::asio::deadline_timer * _banExpiryCheckTimer
Definition: Main.cpp:72
#define sRealmList
Definition: RealmList.h:101
Definition: AppenderDB.h:23
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201
#define sLoginService
Definition: LoginRESTService.h:108
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:207
void SetProcessPriority(const std::string &logChannel)
Definition: ProcessPriority.h:29
static uint32 _banExpiryCheckInterval
Definition: Main.cpp:73
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)
Definition: format.h:3083

+ Here is the call graph for this function:

void ServiceStatusWatcher ( boost::system::error_code const error)
281 {
282  if (!error)
283  {
284  if (m_ServiceStatus == 0)
285  {
286  _ioService->stop();
288  }
289  else
290  {
293  }
294  }
295 }
static boost::asio::io_service * _ioService
Definition: Main.cpp:69
int m_ServiceStatus
Definition: Main.cpp:56
float seconds()
Definition: units.h:97
static boost::asio::deadline_timer * _serviceStatusWatchTimer
Definition: Main.cpp:58
void ServiceStatusWatcher(boost::system::error_code const &error)
Definition: Main.cpp:280

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void SignalHandler ( boost::system::error_code const error,
int  signalNumber 
)
250 {
251  if (!error)
252  _ioService->stop();
253 }
static boost::asio::io_service * _ioService
Definition: Main.cpp:69

+ Here is the caller graph for this function:

bool StartDB ( )

Initialize connection to the database.

Initialize connection to the databases.

  • Get the realm Id from the configuration file
  • Clean the database before starting
  • Insert version info into DB
226 {
228 
229  // Load databases
230  DatabaseLoader loader("server.bnetserver", DatabaseLoader::DATABASE_NONE);
231  loader
232  .AddDatabase(LoginDatabase, "Login");
233 
234  if (!loader.Load())
235  return false;
236 
237  TC_LOG_INFO("server.bnetserver", "Started auth database connection pool.");
238  sLog->SetRealmId(0); // Enables DB appenders when realm is set.
239  return true;
240 }
Definition: DatabaseLoader.h:44
#define sLog
Definition: Log.h:154
LoginDatabaseWorkerPool LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22
Definition: DatabaseLoader.h:30
static void Library_Init()
Definition: MySQLThreading.h:26
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:201

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void StopDB ( )

Close the connection to the database.

244 {
247 }
static void Library_End()
Definition: MySQLThreading.h:31
void Close()
Definition: DatabaseWorkerPool.cpp:72
LoginDatabaseWorkerPool LoginDatabase
Accessor to the realm/login database.
Definition: DatabaseEnv.cpp:22

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

uint32 _banExpiryCheckInterval
static
boost::asio::deadline_timer* _banExpiryCheckTimer
static
uint32 _dbPingInterval
static
boost::asio::deadline_timer* _dbPingTimer
static
boost::asio::io_service* _ioService
static
boost::asio::deadline_timer* _serviceStatusWatchTimer
static
int m_ServiceStatus = -1
char serviceDescription[] = "TrinityCore Battle.net emulator authentication service"
char serviceLongName[] = "TrinityCore bnet service"
char serviceName[] = "bnetserver"