88int main(
int argc,
char** argv)
99 std::string winServiceAction;
102 if (vm.count(
"help") || vm.count(
"version"))
105#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
107 if (winServiceAction ==
"install")
109 if (winServiceAction ==
"uninstall")
111 if (winServiceAction ==
"run")
115 std::string configError;
116 if (!
sConfigMgr->LoadInitial(configFile.generic_string(),
117 std::vector<std::string>(argv, argv + argc),
120 printf(
"Error in config file: %s\n", configError.c_str());
124 std::vector<std::string> loadedConfigFiles;
125 std::vector<std::string> configDirErrors;
126 bool additionalConfigFileLoadSuccess =
sConfigMgr->LoadAdditionalDir(configDir.generic_string(),
true, loadedConfigFiles, configDirErrors);
127 for (std::string
const& loadedConfigFile : loadedConfigFiles)
128 printf(
"Loaded additional config file %s\n", loadedConfigFile.c_str());
130 if (!additionalConfigFileLoadSuccess)
132 for (std::string
const& configDirError : configDirErrors)
133 printf(
"Error in additional config files: %s\n", configDirError.c_str());
138 std::vector<std::string> overriddenKeys =
sConfigMgr->OverrideWithEnvVariablesIfAny();
141 sLog->Initialize(
nullptr);
151 TC_LOG_INFO(
"server.authserver",
"Using SSL version: {} (library: {})", OPENSSL_VERSION_TEXT, OpenSSL_version(OPENSSL_VERSION));
152 TC_LOG_INFO(
"server.authserver",
"Using Boost version: {}.{}.{}", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
156 for (std::string
const& key : overriddenKeys)
157 TC_LOG_INFO(
"server.authserver",
"Configuration field '{}' was overridden with environment variable.", key);
164 std::string pidFile =
sConfigMgr->GetStringDefault(
"PidFile",
"");
165 if (!pidFile.empty())
168 TC_LOG_INFO(
"server.authserver",
"Daemon PID: {}\n", pid);
171 TC_LOG_ERROR(
"server.authserver",
"Cannot create PID file {}.\n", pidFile);
180 std::shared_ptr<void> dbHandle(
nullptr, [](
void*) {
StopDB(); });
182 if (vm.count(
"update-databases-only"))
190 std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>();
195 std::shared_ptr<void> sRealmListHandle(
nullptr, [](
void*) {
sRealmList->Close(); });
199 if (port < 0 || port > 0xFFFF)
201 TC_LOG_ERROR(
"server.authserver",
"Specified port out of allowed range (1-65535)");
205 std::string bindIp =
sConfigMgr->GetStringDefault(
"BindIP",
"0.0.0.0");
209 TC_LOG_ERROR(
"server.authserver",
"Failed to initialize network");
213 std::shared_ptr<void> sAuthSocketMgrHandle(
nullptr, [](
void*) {
sAuthSocketMgr.StopNetwork(); });
216 boost::asio::signal_set signals(*ioContext, SIGINT, SIGTERM);
217#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
218 signals.add(SIGBREAK);
220 signals.async_wait(std::bind(&
SignalHandler, std::weak_ptr<Trinity::Asio::IoContext>(ioContext), std::placeholders::_1, std::placeholders::_2));
227 std::shared_ptr<Trinity::Asio::DeadlineTimer> dbPingTimer = std::make_shared<Trinity::Asio::DeadlineTimer>(*ioContext);
228 dbPingTimer->expires_after(std::chrono::minutes(dbPingInterval));
229 dbPingTimer->async_wait(std::bind(&
KeepDatabaseAliveHandler, std::weak_ptr<Trinity::Asio::DeadlineTimer>(dbPingTimer), dbPingInterval, std::placeholders::_1));
231 int32 banExpiryCheckInterval =
sConfigMgr->GetIntDefault(
"BanExpiryCheckInterval", 60);
232 std::shared_ptr<Trinity::Asio::DeadlineTimer> banExpiryCheckTimer = std::make_shared<Trinity::Asio::DeadlineTimer>(*ioContext);
233 banExpiryCheckTimer->expires_after(std::chrono::seconds(banExpiryCheckInterval));
234 banExpiryCheckTimer->async_wait(std::bind(&
BanExpiryHandler, std::weak_ptr<Trinity::Asio::DeadlineTimer>(banExpiryCheckTimer), banExpiryCheckInterval, std::placeholders::_1));
236#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
237 std::shared_ptr<Trinity::Asio::DeadlineTimer> serviceStatusWatchTimer;
240 serviceStatusWatchTimer = std::make_shared<Trinity::Asio::DeadlineTimer>(*ioContext);
241 serviceStatusWatchTimer->expires_after(1s);
243 std::weak_ptr<Trinity::Asio::DeadlineTimer>(serviceStatusWatchTimer),
244 std::weak_ptr<Trinity::Asio::IoContext>(ioContext),
245 std::placeholders::_1));
252 banExpiryCheckTimer->cancel();
253 dbPingTimer->cancel();
255 TC_LOG_INFO(
"server.authserver",
"Halting process...");
327void ServiceStatusWatcher(std::weak_ptr<Trinity::Asio::DeadlineTimer> serviceStatusWatchTimerRef, std::weak_ptr<Trinity::Asio::IoContext> ioContextRef, boost::system::error_code
const& error)
331 if (std::shared_ptr<Trinity::Asio::IoContext> ioContext = ioContextRef.lock())
335 else if (std::shared_ptr<Trinity::Asio::DeadlineTimer> serviceStatusWatchTimer = serviceStatusWatchTimerRef.lock())
337 serviceStatusWatchTimer->expires_after(1s);
338 serviceStatusWatchTimer->async_wait(std::bind(&
ServiceStatusWatcher, serviceStatusWatchTimerRef, ioContextRef, std::placeholders::_1));
345variables_map
GetConsoleArguments(
int argc,
char** argv, fs::path& configFile, fs::path& configDir, [[maybe_unused]] std::string& winServiceAction)
347 options_description all(
"Allowed options");
349 (
"help,h",
"print usage message")
350 (
"version,v",
"print version build info")
352 "use <arg> as configuration file")
354 "use <arg> as directory with additional config files")
355 (
"update-databases-only,u",
"updates databases only")
357#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
358 options_description win(
"Windows platform specific options");
360 (
"service,s", value<std::string>(&winServiceAction)->default_value(
""),
"Windows service options: [install | uninstall]")
365 variables_map variablesMap;
368 store(command_line_parser(argc, argv).options(all).allow_unregistered().run(), variablesMap);
369 notify(variablesMap);
371 catch (std::exception& e)
373 std::cerr << e.what() <<
"\n";
376 if (variablesMap.count(
"help"))
377 std::cout << all <<
"\n";
378 else if (variablesMap.count(
"version"))
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)
#define _TRINITY_REALM_CONFIG_DIR
void SignalHandler(std::weak_ptr< Trinity::Asio::IoContext > ioContextRef, boost::system::error_code const &error, int signalNumber)
int main(int argc, char **argv)
#define _TRINITY_REALM_CONFIG
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)
void StopDB()
Close the connection to the database.
TCHAR serviceDescription[]