From b2b4f9d1e4562ec246efb5136c1c674ec78f50b7 Mon Sep 17 00:00:00 2001 From: Shauren Date: Sat, 6 Jan 2018 12:28:38 +0100 Subject: Core/Misc: Added compatibility layer for boost 1.66 and future std:: networking stuff * Based on work done by @dimiandre in PR #21173 Closes #21171 Closes #21173 (cherry picked from commit dfd2660a85e4f0891c63009ee8425b2796586409) --- src/server/worldserver/Main.cpp | 71 +++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 38 deletions(-) (limited to 'src/server/worldserver/Main.cpp') diff --git a/src/server/worldserver/Main.cpp b/src/server/worldserver/Main.cpp index 7c9708d6146..c6461775066 100644 --- a/src/server/worldserver/Main.cpp +++ b/src/server/worldserver/Main.cpp @@ -32,6 +32,7 @@ #include "DatabaseLoader.h" #include "GitRevision.h" #include "InstanceSaveMgr.h" +#include "IoContext.h" #include "MapManager.h" #include "Metric.h" #include "MySQLThreading.h" @@ -41,6 +42,7 @@ #include "ProcessPriority.h" #include "RASession.h" #include "RealmList.h" +#include "Resolver.h" #include "ScriptLoader.h" #include "ScriptMgr.h" #include "ScriptReloadMgr.h" @@ -50,7 +52,6 @@ #include "WorldSocketMgr.h" #include #include -#include #include #include #include @@ -84,8 +85,8 @@ int m_ServiceStatus = -1; class FreezeDetector { public: - FreezeDetector(boost::asio::io_service& ioService, uint32 maxCoreStuckTime) - : _timer(ioService), _worldLoopCounter(0), _lastChangeMsTime(0), _maxCoreStuckTimeInMs(maxCoreStuckTime) { } + FreezeDetector(Trinity::Asio::IoContext& ioContext, uint32 maxCoreStuckTime) + : _timer(ioContext), _worldLoopCounter(0), _lastChangeMsTime(0), _maxCoreStuckTimeInMs(maxCoreStuckTime) { } static void Start(std::shared_ptr const& freezeDetector) { @@ -103,13 +104,13 @@ class FreezeDetector }; void SignalHandler(boost::system::error_code const& error, int signalNumber); -AsyncAcceptor* StartRaSocketAcceptor(boost::asio::io_service& ioService); +AsyncAcceptor* StartRaSocketAcceptor(Trinity::Asio::IoContext& ioContext); bool StartDB(); void StopDB(); void WorldUpdateLoop(); void ClearOnlineAccounts(); void ShutdownCLIThread(std::thread* cliThread); -bool LoadRealmInfo(boost::asio::io_service& ioService); +bool LoadRealmInfo(Trinity::Asio::IoContext& ioContext); variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& cfg_service); /// Launch the Trinity server @@ -143,11 +144,11 @@ extern int main(int argc, char** argv) return 1; } - std::shared_ptr ioService = std::make_shared(); + std::shared_ptr ioContext = std::make_shared(); sLog->RegisterAppender(); - // If logs are supposed to be handled async then we need to pass the io_service into the Log singleton - sLog->Initialize(sConfigMgr->GetBoolDefault("Log.Async.Enable", false) ? ioService.get() : nullptr); + // If logs are supposed to be handled async then we need to pass the IoContext into the Log singleton + sLog->Initialize(sConfigMgr->GetBoolDefault("Log.Async.Enable", false) ? ioContext.get() : nullptr); Trinity::Banner::Show("worldserver-daemon", [](char const* text) @@ -184,8 +185,8 @@ extern int main(int argc, char** argv) } } - // Set signal handlers (this must be done before starting io_service threads, because otherwise they would unblock and exit) - boost::asio::signal_set signals(*ioService, SIGINT, SIGTERM); + // Set signal handlers (this must be done before starting IoContext threads, because otherwise they would unblock and exit) + boost::asio::signal_set signals(*ioContext, SIGINT, SIGTERM); #if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS signals.add(SIGBREAK); #endif @@ -193,9 +194,9 @@ extern int main(int argc, char** argv) // Start the Boost based thread pool int numThreads = sConfigMgr->GetIntDefault("ThreadPool", 1); - std::shared_ptr> threadPool(new std::vector(), [ioService](std::vector* del) + std::shared_ptr> threadPool(new std::vector(), [ioContext](std::vector* del) { - ioService->stop(); + ioContext->stop(); for (std::thread& thr : *del) thr.join(); @@ -206,7 +207,7 @@ extern int main(int argc, char** argv) numThreads = 1; for (int i = 0; i < numThreads; ++i) - threadPool->push_back(std::thread([ioService]() { ioService->run(); })); + threadPool->push_back(std::thread([ioContext]() { ioContext->run(); })); // Set process priority according to configuration settings SetProcessPriority("server.worldserver", sConfigMgr->GetIntDefault(CONFIG_PROCESSOR_AFFINITY, 0), sConfigMgr->GetBoolDefault(CONFIG_HIGH_PRIORITY, false)); @@ -220,9 +221,9 @@ extern int main(int argc, char** argv) // Set server offline (not connectable) LoginDatabase.DirectPExecute("UPDATE realmlist SET flag = flag | %u WHERE id = '%d'", REALM_FLAG_OFFLINE, realm.Id.Realm); - LoadRealmInfo(*ioService); + LoadRealmInfo(*ioContext); - sMetric->Initialize(realm.Name, *ioService, []() + sMetric->Initialize(realm.Name, *ioContext, []() { TC_METRIC_VALUE("online_players", sWorld->GetPlayerCount()); }); @@ -258,7 +259,7 @@ extern int main(int argc, char** argv) // Start the Remote Access port (acceptor) if enabled std::unique_ptr raAcceptor; if (sConfigMgr->GetBoolDefault("Ra.Enable", false)) - raAcceptor.reset(StartRaSocketAcceptor(*ioService)); + raAcceptor.reset(StartRaSocketAcceptor(*ioContext)); // Start soap serving thread if enabled std::shared_ptr soapThread; @@ -284,7 +285,7 @@ extern int main(int argc, char** argv) return 1; } - if (!sWorldSocketMgr.StartNetwork(*ioService, worldListener, worldPort, networkThreads)) + if (!sWorldSocketMgr.StartWorldNetwork(*ioContext, worldListener, worldPort, networkThreads)) { TC_LOG_ERROR("server.worldserver", "Failed to initialize network"); return 1; @@ -321,7 +322,7 @@ extern int main(int argc, char** argv) std::shared_ptr freezeDetector; if (int coreStuckTime = sConfigMgr->GetIntDefault("MaxCoreStuckTime", 0)) { - freezeDetector = std::make_shared(*ioService, coreStuckTime * 1000); + freezeDetector = std::make_shared(*ioContext, coreStuckTime * 1000); FreezeDetector::Start(freezeDetector); TC_LOG_INFO("server.worldserver", "Starting up anti-freeze thread (%u seconds max stuck time)...", coreStuckTime); } @@ -477,12 +478,12 @@ void FreezeDetector::Handler(std::weak_ptr freezeDetectorRef, bo } } -AsyncAcceptor* StartRaSocketAcceptor(boost::asio::io_service& ioService) +AsyncAcceptor* StartRaSocketAcceptor(Trinity::Asio::IoContext& ioContext) { uint16 raPort = uint16(sConfigMgr->GetIntDefault("Ra.Port", 3443)); std::string raListener = sConfigMgr->GetStringDefault("Ra.IP", "0.0.0.0"); - AsyncAcceptor* acceptor = new AsyncAcceptor(ioService, raListener, raPort); + AsyncAcceptor* acceptor = new AsyncAcceptor(ioContext, raListener, raPort); if (!acceptor->Bind()) { TC_LOG_ERROR("server.worldserver", "Failed to bind RA socket acceptor"); @@ -494,48 +495,42 @@ AsyncAcceptor* StartRaSocketAcceptor(boost::asio::io_service& ioService) return acceptor; } -bool LoadRealmInfo(boost::asio::io_service& ioService) +bool LoadRealmInfo(Trinity::Asio::IoContext& ioContext) { - boost::asio::ip::tcp::resolver resolver(ioService); - boost::asio::ip::tcp::resolver::iterator end; - QueryResult result = LoginDatabase.PQuery("SELECT id, name, address, localAddress, localSubnetMask, port, icon, flag, timezone, allowedSecurityLevel, population, gamebuild FROM realmlist WHERE id = %u", realm.Id.Realm); if (!result) return false; + boost::asio::ip::tcp::resolver resolver(ioContext); + Field* fields = result->Fetch(); realm.Name = fields[1].GetString(); - boost::asio::ip::tcp::resolver::query externalAddressQuery(boost::asio::ip::tcp::v4(), fields[2].GetString(), ""); - - boost::system::error_code ec; - boost::asio::ip::tcp::resolver::iterator endPoint = resolver.resolve(externalAddressQuery, ec); - if (endPoint == end || ec) + Optional externalAddress = Trinity::Net::Resolve(resolver, boost::asio::ip::tcp::v4(), fields[2].GetString(), ""); + if (!externalAddress) { TC_LOG_ERROR("server.worldserver", "Could not resolve address %s", fields[2].GetString().c_str()); return false; } - realm.ExternalAddress = Trinity::make_unique((*endPoint).endpoint().address()); + realm.ExternalAddress = Trinity::make_unique(externalAddress->address()); - boost::asio::ip::tcp::resolver::query localAddressQuery(boost::asio::ip::tcp::v4(), fields[3].GetString(), ""); - endPoint = resolver.resolve(localAddressQuery, ec); - if (endPoint == end || ec) + Optional localAddress = Trinity::Net::Resolve(resolver, boost::asio::ip::tcp::v4(), fields[3].GetString(), ""); + if (!localAddress) { TC_LOG_ERROR("server.worldserver", "Could not resolve address %s", fields[3].GetString().c_str()); return false; } - realm.LocalAddress = Trinity::make_unique((*endPoint).endpoint().address()); + realm.LocalAddress = Trinity::make_unique(localAddress->address()); - boost::asio::ip::tcp::resolver::query localSubmaskQuery(boost::asio::ip::tcp::v4(), fields[4].GetString(), ""); - endPoint = resolver.resolve(localSubmaskQuery, ec); - if (endPoint == end || ec) + Optional localSubmask = Trinity::Net::Resolve(resolver, boost::asio::ip::tcp::v4(), fields[4].GetString(), ""); + if (!localSubmask) { TC_LOG_ERROR("server.worldserver", "Could not resolve address %s", fields[4].GetString().c_str()); return false; } - realm.LocalSubnetMask = Trinity::make_unique((*endPoint).endpoint().address()); + realm.LocalSubnetMask = Trinity::make_unique(localSubmask->address()); realm.Port = fields[5].GetUInt16(); realm.Type = fields[6].GetUInt8(); -- cgit v1.2.3