Core/Auth: Fixed authserver not shutting down correctly when started as windows service

This commit is contained in:
Shauren
2015-06-10 00:22:34 +02:00
parent abda6840e1
commit 9ecef15057

View File

@@ -46,7 +46,7 @@ using namespace boost::program_options;
# define _TRINITY_REALM_CONFIG "authserver.conf"
#endif
#ifdef _WIN32
#if PLATFORM == PLATFORM_WINDOWS
#include "ServiceWin32.h"
char serviceName[] = "authserver";
char serviceLongName[] = "TrinityCore auth service";
@@ -58,6 +58,9 @@ char serviceDescription[] = "TrinityCore World of Warcraft emulator auth service
* 2 - paused
*/
int m_ServiceStatus = -1;
boost::asio::deadline_timer* _serviceStatusWatchTimer;
void ServiceStatusWatcher(boost::system::error_code const& error);
#endif
bool StartDB();
@@ -66,8 +69,8 @@ void SignalHandler(const boost::system::error_code& error, int signalNumber);
void KeepDatabaseAliveHandler(const boost::system::error_code& error);
variables_map GetConsoleArguments(int argc, char** argv, std::string& configFile, std::string& configService);
boost::asio::io_service _ioService;
boost::asio::deadline_timer _dbPingTimer(_ioService);
boost::asio::io_service* _ioService;
boost::asio::deadline_timer* _dbPingTimer;
uint32 _dbPingInterval;
LoginDatabaseWorkerPool LoginDatabase;
@@ -80,13 +83,13 @@ int main(int argc, char** argv)
if (vm.count("help"))
return 0;
#ifdef _WIN32
#if PLATFORM == PLATFORM_WINDOWS
if (configService.compare("install") == 0)
return WinServiceInstall() == true ? 0 : 1;
else if (configService.compare("uninstall") == 0)
return WinServiceUninstall() == true ? 0 : 1;
else if (configService.compare("run") == 0)
WinServiceRun();
return WinServiceRun() ? 0 : 1;
#endif
std::string configError;
@@ -119,13 +122,16 @@ int main(int argc, char** argv)
if (!StartDB())
return 1;
_ioService = new boost::asio::io_service();
// Get the list of realms for the server
sRealmList->Initialize(_ioService, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 20));
sRealmList->Initialize(*_ioService, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 20));
if (sRealmList->size() == 0)
{
TC_LOG_ERROR("server.authserver", "No valid realms specified.");
StopDB();
delete _ioService;
return 1;
}
@@ -135,15 +141,16 @@ int main(int argc, char** argv)
{
TC_LOG_ERROR("server.authserver", "Specified port out of allowed range (1-65535)");
StopDB();
delete _ioService;
return 1;
}
std::string bindIp = sConfigMgr->GetStringDefault("BindIP", "0.0.0.0");
sAuthSocketMgr.StartNetwork(_ioService, bindIp, port);
sAuthSocketMgr.StartNetwork(*_ioService, bindIp, port);
// Set signal handlers
boost::asio::signal_set signals(_ioService, SIGINT, SIGTERM);
boost::asio::signal_set signals(*_ioService, SIGINT, SIGTERM);
#if PLATFORM == PLATFORM_WINDOWS
signals.add(SIGBREAK);
#endif
@@ -154,20 +161,38 @@ int main(int argc, char** argv)
// Enabled a timed callback for handling the database keep alive ping
_dbPingInterval = sConfigMgr->GetIntDefault("MaxPingTime", 30);
_dbPingTimer.expires_from_now(boost::posix_time::minutes(_dbPingInterval));
_dbPingTimer.async_wait(KeepDatabaseAliveHandler);
_dbPingTimer = new boost::asio::deadline_timer(*_ioService);
_dbPingTimer->expires_from_now(boost::posix_time::minutes(_dbPingInterval));
_dbPingTimer->async_wait(KeepDatabaseAliveHandler);
#if PLATFORM == PLATFORM_WINDOWS
if (m_ServiceStatus != -1)
{
_serviceStatusWatchTimer = new boost::asio::deadline_timer(*_ioService);
_serviceStatusWatchTimer->expires_from_now(boost::posix_time::seconds(1));
_serviceStatusWatchTimer->async_wait(ServiceStatusWatcher);
}
#endif
// Start the io service worker loop
_ioService.run();
_ioService->run();
_dbPingTimer->cancel();
sAuthSocketMgr.StopNetwork();
// Close the Database Pool and library
StopDB();
TC_LOG_INFO("server.authserver", "Halting process...");
signals.cancel();
delete _dbPingTimer;
delete _ioService;
return 0;
}
/// Initialize connection to the database
bool StartDB()
{
@@ -198,7 +223,7 @@ void StopDB()
void SignalHandler(const boost::system::error_code& error, int /*signalNumber*/)
{
if (!error)
_ioService.stop();
_ioService->stop();
}
void KeepDatabaseAliveHandler(const boost::system::error_code& error)
@@ -208,11 +233,30 @@ void KeepDatabaseAliveHandler(const boost::system::error_code& error)
TC_LOG_INFO("server.authserver", "Ping MySQL to keep connection alive");
LoginDatabase.KeepAlive();
_dbPingTimer.expires_from_now(boost::posix_time::minutes(_dbPingInterval));
_dbPingTimer.async_wait(KeepDatabaseAliveHandler);
_dbPingTimer->expires_from_now(boost::posix_time::minutes(_dbPingInterval));
_dbPingTimer->async_wait(KeepDatabaseAliveHandler);
}
}
#if PLATFORM == PLATFORM_WINDOWS
void ServiceStatusWatcher(boost::system::error_code const& error)
{
if (!error)
{
if (m_ServiceStatus == 0)
{
_ioService->stop();
delete _serviceStatusWatchTimer;
}
else
{
_serviceStatusWatchTimer->expires_from_now(boost::posix_time::seconds(1));
_serviceStatusWatchTimer->async_wait(ServiceStatusWatcher);
}
}
}
#endif
variables_map GetConsoleArguments(int argc, char** argv, std::string& configFile, std::string& configService)
{
options_description all("Allowed options");
@@ -220,7 +264,7 @@ variables_map GetConsoleArguments(int argc, char** argv, std::string& configFile
("help,h", "print usage message")
("config,c", value<std::string>(&configFile)->default_value(_TRINITY_REALM_CONFIG), "use <arg> as configuration file")
;
#ifdef _WIN32
#if PLATFORM == PLATFORM_WINDOWS
options_description win("Windows platform specific options");
win.add_options()
("service,s", value<std::string>(&configService)->default_value(""), "Windows service options: [install | uninstall]")