93int main(
int argc,
char** argv)
103 std::string winServiceAction;
106 if (vm.count(
"help") || vm.count(
"version"))
111 GOOGLE_PROTOBUF_VERIFY_VERSION;
115#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
117 if (winServiceAction ==
"install")
119 if (winServiceAction ==
"uninstall")
121 if (winServiceAction ==
"run")
125 std::string configError;
126 if (!
sConfigMgr->LoadInitial(configFile.generic_string(),
127 std::vector<std::string>(argv, argv + argc),
130 printf(
"Error in config file: %s\n", configError.c_str());
134 std::vector<std::string> loadedConfigFiles;
135 std::vector<std::string> configDirErrors;
136 bool additionalConfigFileLoadSuccess =
sConfigMgr->LoadAdditionalDir(configDir.generic_string(),
true, loadedConfigFiles, configDirErrors);
137 for (std::string
const& loadedConfigFile : loadedConfigFiles)
138 printf(
"Loaded additional config file %s\n", loadedConfigFile.c_str());
140 if (!additionalConfigFileLoadSuccess)
142 for (std::string
const& configDirError : configDirErrors)
143 printf(
"Error in additional config files: %s\n", configDirError.c_str());
148 std::vector<std::string> overriddenKeys =
sConfigMgr->OverrideWithEnvVariablesIfAny();
151 sLog->Initialize(
nullptr);
161 TC_LOG_INFO(
"server.bnetserver",
"Using SSL version: {} (library: {})", OPENSSL_VERSION_TEXT, OpenSSL_version(OPENSSL_VERSION));
162 TC_LOG_INFO(
"server.bnetserver",
"Using Boost version: {}.{}.{}", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
166 for (std::string
const& key : overriddenKeys)
167 TC_LOG_INFO(
"server.authserver",
"Configuration field '{}' was overridden with environment variable.", key);
174 std::string pidFile =
sConfigMgr->GetStringDefault(
"PidFile",
"");
175 if (!pidFile.empty())
178 TC_LOG_INFO(
"server.bnetserver",
"Daemon PID: {}\n", pid);
181 TC_LOG_ERROR(
"server.bnetserver",
"Cannot create PID file {}.\n", pidFile);
188 TC_LOG_ERROR(
"server.bnetserver",
"Failed to initialize SSL context");
198 if (vm.count(
"update-databases-only"))
206 std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>();
208 auto ioContextWork = boost::asio::make_work_guard(ioContext->get_executor());
218 std::string httpBindIp =
sConfigMgr->GetStringDefault(
"BindIP",
"0.0.0.0");
220 if (httpPort <= 0 || httpPort > 0xFFFF)
222 TC_LOG_ERROR(
"server.bnetserver",
"Specified login service port ({}) out of allowed range (1-65535)", httpPort);
226 if (!
sLoginService.StartNetwork(*ioContext, httpBindIp, httpPort))
228 TC_LOG_ERROR(
"server.bnetserver",
"Failed to initialize login service");
232 auto sLoginServiceHandle = Trinity::make_unique_ptr_with_deleter<&Battlenet::LoginRESTService::StopNetwork>(&
sLoginService);
236 if (bnport <= 0 || bnport > 0xFFFF)
238 TC_LOG_ERROR(
"server.bnetserver",
"Specified battle.net port ({}) out of allowed range (1-65535)", bnport);
245 auto sRealmListHandle = Trinity::make_unique_ptr_with_deleter<&RealmList::Close>(
sRealmList);
247 std::string bindIp =
sConfigMgr->GetStringDefault(
"BindIP",
"0.0.0.0");
249 if (!
sSessionMgr.StartNetwork(*ioContext, bindIp, bnport, 1))
251 TC_LOG_ERROR(
"server.bnetserver",
"Failed to initialize network");
255 auto sSessionMgrHandle = Trinity::make_unique_ptr_with_deleter<&Battlenet::SessionManager::StopNetwork>(&
sSessionMgr);
258 boost::asio::basic_signal_set<Trinity::Asio::IoContext::Executor> signals(*ioContext, SIGINT, SIGTERM);
259#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
260 signals.add(SIGBREAK);
269 std::shared_ptr<Trinity::Asio::DeadlineTimer> dbPingTimer = std::make_shared<Trinity::Asio::DeadlineTimer>(*ioContext);
270 dbPingTimer->expires_after(std::chrono::minutes(dbPingInterval));
271 dbPingTimer->async_wait([timerRef = std::weak_ptr(dbPingTimer), dbPingInterval](boost::system::error_code
const& error)
mutable
276 int32 banExpiryCheckInterval =
sConfigMgr->GetIntDefault(
"BanExpiryCheckInterval", 60);
277 std::shared_ptr<Trinity::Asio::DeadlineTimer> banExpiryCheckTimer = std::make_shared<Trinity::Asio::DeadlineTimer>(*ioContext);
278 banExpiryCheckTimer->expires_after(std::chrono::seconds(banExpiryCheckInterval));
279 banExpiryCheckTimer->async_wait([timerRef = std::weak_ptr(banExpiryCheckTimer), banExpiryCheckInterval](boost::system::error_code
const& error)
mutable
284#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
285 std::shared_ptr<Trinity::Asio::DeadlineTimer> serviceStatusWatchTimer;
288 serviceStatusWatchTimer = std::make_shared<Trinity::Asio::DeadlineTimer>(*ioContext);
289 serviceStatusWatchTimer->expires_after(1s);
290 serviceStatusWatchTimer->async_wait([timerRef = std::weak_ptr(serviceStatusWatchTimer), ioContextRef = std::weak_ptr(ioContext)](boost::system::error_code
const& error)
mutable
298 std::this_thread::sleep_for(50ms);
300 TC_LOG_INFO(
"server.bnetserver",
"Halting process...");
374void ServiceStatusWatcher(std::weak_ptr<Trinity::Asio::DeadlineTimer> serviceStatusWatchTimerRef, std::weak_ptr<Trinity::Asio::IoContext> ioContextRef, boost::system::error_code
const& error)
378 if (std::shared_ptr<Trinity::Asio::IoContext> ioContext = ioContextRef.lock())
384 else if (std::shared_ptr<Trinity::Asio::DeadlineTimer> serviceStatusWatchTimer = serviceStatusWatchTimerRef.lock())
386 serviceStatusWatchTimer->expires_after(1s);
387 serviceStatusWatchTimer->async_wait([timerRef = std::move(serviceStatusWatchTimerRef), ioContextRef = std::move(ioContextRef)](boost::system::error_code
const& error)
mutable
397variables_map
GetConsoleArguments(
int argc,
char** argv, fs::path& configFile, fs::path& configDir, [[maybe_unused]] std::string& winServiceAction)
399 options_description all(
"Allowed options");
401 (
"help,h",
"print usage message")
402 (
"version,v",
"print version build info")
404 "use <arg> as configuration file")
406 "use <arg> as directory with additional config files")
407 (
"update-databases-only,u",
"updates databases only")
409#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
410 options_description win(
"Windows platform specific options");
412 (
"service,s", value<std::string>(&winServiceAction)->default_value(
""),
"Windows service options: [install | uninstall]")
417 variables_map variablesMap;
420 store(command_line_parser(argc, argv).options(all).allow_unregistered().run(), variablesMap);
421 notify(variablesMap);
423 catch (std::exception& e)
425 std::cerr << e.what() <<
"\n";
428 if (variablesMap.count(
"help"))
430 std::cout << all <<
"\n";
432 else if (variablesMap.count(
"version"))
std::atomic< bool > Stopped
bool StartDB()
Initialize connection to the database.
void ServiceStatusWatcher(std::weak_ptr< Trinity::Asio::DeadlineTimer > serviceStatusWatchTimerRef, std::weak_ptr< Trinity::Asio::IoContext > ioContextRef, boost::system::error_code const &error)
void BanExpiryHandler(std::weak_ptr< Trinity::Asio::DeadlineTimer > banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const &error)
int main(int argc, char **argv)
variables_map GetConsoleArguments(int argc, char **argv, fs::path &configFile, fs::path &configDir, std::string &winServiceAction)
void KeepDatabaseAliveHandler(std::weak_ptr< Trinity::Asio::DeadlineTimer > dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const &error)
#define _TRINITY_BNET_CONFIG_DIR
void StopDB()
Close the connection to the database.
#define _TRINITY_BNET_CONFIG
void SignalHandler(boost::system::error_code const &error, int signalNumber)
TCHAR serviceDescription[]