diff options
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/authserver/Main.cpp | 39 | ||||
| -rw-r--r-- | src/server/authserver/Server/AuthSocketMgr.h | 4 | ||||
| -rw-r--r-- | src/server/game/Server/Protocol/PacketLog.cpp | 3 | ||||
| -rw-r--r-- | src/server/game/Server/WorldSocketMgr.cpp | 6 | ||||
| -rw-r--r-- | src/server/game/Server/WorldSocketMgr.h | 2 | ||||
| -rw-r--r-- | src/server/scripts/Commands/cs_account.cpp | 4 | ||||
| -rw-r--r-- | src/server/scripts/Commands/cs_misc.cpp | 4 | ||||
| -rw-r--r-- | src/server/shared/Networking/AsyncAcceptor.h | 17 | ||||
| -rw-r--r-- | src/server/shared/Networking/NetworkThread.h | 11 | ||||
| -rw-r--r-- | src/server/shared/Networking/SocketMgr.h | 4 | ||||
| -rw-r--r-- | src/server/shared/Realm/Realm.cpp | 9 | ||||
| -rw-r--r-- | src/server/shared/Realm/RealmList.cpp | 55 | ||||
| -rw-r--r-- | src/server/shared/Realm/RealmList.h | 15 | ||||
| -rw-r--r-- | src/server/worldserver/Main.cpp | 71 |
14 files changed, 121 insertions, 123 deletions
diff --git a/src/server/authserver/Main.cpp b/src/server/authserver/Main.cpp index 244a77d62d6..fa024d324ab 100644 --- a/src/server/authserver/Main.cpp +++ b/src/server/authserver/Main.cpp @@ -30,6 +30,7 @@ #include "Config.h" #include "DatabaseEnv.h" #include "DatabaseLoader.h" +#include "IoContext.h" #include "GitRevision.h" #include "MySQLThreading.h" #include "ProcessPriority.h" @@ -64,12 +65,12 @@ char serviceDescription[] = "TrinityCore World of Warcraft emulator auth service */ int m_ServiceStatus = -1; -void ServiceStatusWatcher(std::weak_ptr<boost::asio::deadline_timer> serviceStatusWatchTimerRef, std::weak_ptr<boost::asio::io_service> ioServiceRef, boost::system::error_code const& error); +void ServiceStatusWatcher(std::weak_ptr<boost::asio::deadline_timer> serviceStatusWatchTimerRef, std::weak_ptr<Trinity::Asio::IoContext> ioContextRef, boost::system::error_code const& error); #endif bool StartDB(); void StopDB(); -void SignalHandler(std::weak_ptr<boost::asio::io_service> ioServiceRef, boost::system::error_code const& error, int signalNumber); +void SignalHandler(std::weak_ptr<Trinity::Asio::IoContext> ioContextRef, boost::system::error_code const& error, int signalNumber); void KeepDatabaseAliveHandler(std::weak_ptr<boost::asio::deadline_timer> dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error); void BanExpiryHandler(std::weak_ptr<boost::asio::deadline_timer> banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error); variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, std::string& configService); @@ -138,10 +139,10 @@ int main(int argc, char** argv) std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); }); - std::shared_ptr<boost::asio::io_service> ioService = std::make_shared<boost::asio::io_service>(); + std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>(); // Get the list of realms for the server - sRealmList->Initialize(*ioService, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 20)); + sRealmList->Initialize(*ioContext, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 20)); std::shared_ptr<void> sRealmListHandle(nullptr, [](void*) { sRealmList->Close(); }); @@ -161,7 +162,7 @@ int main(int argc, char** argv) std::string bindIp = sConfigMgr->GetStringDefault("BindIP", "0.0.0.0"); - if (!sAuthSocketMgr.StartNetwork(*ioService, bindIp, port)) + if (!sAuthSocketMgr.StartNetwork(*ioContext, bindIp, port)) { TC_LOG_ERROR("server.authserver", "Failed to initialize network"); return 1; @@ -170,23 +171,23 @@ int main(int argc, char** argv) std::shared_ptr<void> sAuthSocketMgrHandle(nullptr, [](void*) { sAuthSocketMgr.StopNetwork(); }); // Set signal handlers - boost::asio::signal_set signals(*ioService, SIGINT, SIGTERM); + boost::asio::signal_set signals(*ioContext, SIGINT, SIGTERM); #if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS signals.add(SIGBREAK); #endif - signals.async_wait(std::bind(&SignalHandler, std::weak_ptr<boost::asio::io_service>(ioService), std::placeholders::_1, std::placeholders::_2)); + signals.async_wait(std::bind(&SignalHandler, std::weak_ptr<Trinity::Asio::IoContext>(ioContext), std::placeholders::_1, std::placeholders::_2)); // Set process priority according to configuration settings SetProcessPriority("server.authserver", sConfigMgr->GetIntDefault(CONFIG_PROCESSOR_AFFINITY, 0), sConfigMgr->GetBoolDefault(CONFIG_HIGH_PRIORITY, false)); // Enabled a timed callback for handling the database keep alive ping int32 dbPingInterval = sConfigMgr->GetIntDefault("MaxPingTime", 30); - std::shared_ptr<boost::asio::deadline_timer> dbPingTimer = std::make_shared<boost::asio::deadline_timer>(*ioService); + std::shared_ptr<boost::asio::deadline_timer> dbPingTimer = std::make_shared<boost::asio::deadline_timer>(*ioContext); dbPingTimer->expires_from_now(boost::posix_time::minutes(dbPingInterval)); dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, std::weak_ptr<boost::asio::deadline_timer>(dbPingTimer), dbPingInterval, std::placeholders::_1)); int32 banExpiryCheckInterval = sConfigMgr->GetIntDefault("BanExpiryCheckInterval", 60); - std::shared_ptr<boost::asio::deadline_timer> banExpiryCheckTimer = std::make_shared<boost::asio::deadline_timer>(*ioService); + std::shared_ptr<boost::asio::deadline_timer> banExpiryCheckTimer = std::make_shared<boost::asio::deadline_timer>(*ioContext); banExpiryCheckTimer->expires_from_now(boost::posix_time::seconds(banExpiryCheckInterval)); banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, std::weak_ptr<boost::asio::deadline_timer>(banExpiryCheckTimer), banExpiryCheckInterval, std::placeholders::_1)); @@ -194,17 +195,17 @@ int main(int argc, char** argv) std::shared_ptr<boost::asio::deadline_timer> serviceStatusWatchTimer; if (m_ServiceStatus != -1) { - serviceStatusWatchTimer = std::make_shared<boost::asio::deadline_timer>(*ioService); + serviceStatusWatchTimer = std::make_shared<boost::asio::deadline_timer>(*ioContext); serviceStatusWatchTimer->expires_from_now(boost::posix_time::seconds(1)); serviceStatusWatchTimer->async_wait(std::bind(&ServiceStatusWatcher, std::weak_ptr<boost::asio::deadline_timer>(serviceStatusWatchTimer), - std::weak_ptr<boost::asio::io_service>(ioService), + std::weak_ptr<Trinity::Asio::IoContext>(ioContext), std::placeholders::_1)); } #endif // Start the io service worker loop - ioService->run(); + ioContext->run(); banExpiryCheckTimer->cancel(); dbPingTimer->cancel(); @@ -243,11 +244,11 @@ void StopDB() MySQL::Library_End(); } -void SignalHandler(std::weak_ptr<boost::asio::io_service> ioServiceRef, boost::system::error_code const& error, int /*signalNumber*/) +void SignalHandler(std::weak_ptr<Trinity::Asio::IoContext> ioContextRef, boost::system::error_code const& error, int /*signalNumber*/) { if (!error) - if (std::shared_ptr<boost::asio::io_service> ioService = ioServiceRef.lock()) - ioService->stop(); + if (std::shared_ptr<Trinity::Asio::IoContext> ioContext = ioContextRef.lock()) + ioContext->stop(); } void KeepDatabaseAliveHandler(std::weak_ptr<boost::asio::deadline_timer> dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error) @@ -281,18 +282,18 @@ void BanExpiryHandler(std::weak_ptr<boost::asio::deadline_timer> banExpiryCheckT } #if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS -void ServiceStatusWatcher(std::weak_ptr<boost::asio::deadline_timer> serviceStatusWatchTimerRef, std::weak_ptr<boost::asio::io_service> ioServiceRef, boost::system::error_code const& error) +void ServiceStatusWatcher(std::weak_ptr<boost::asio::deadline_timer> serviceStatusWatchTimerRef, std::weak_ptr<Trinity::Asio::IoContext> ioContextRef, boost::system::error_code const& error) { if (!error) { - if (std::shared_ptr<boost::asio::io_service> ioService = ioServiceRef.lock()) + if (std::shared_ptr<Trinity::Asio::IoContext> ioContext = ioContextRef.lock()) { if (m_ServiceStatus == 0) - ioService->stop(); + ioContext->stop(); else if (std::shared_ptr<boost::asio::deadline_timer> serviceStatusWatchTimer = serviceStatusWatchTimerRef.lock()) { serviceStatusWatchTimer->expires_from_now(boost::posix_time::seconds(1)); - serviceStatusWatchTimer->async_wait(std::bind(&ServiceStatusWatcher, serviceStatusWatchTimerRef, ioServiceRef, std::placeholders::_1)); + serviceStatusWatchTimer->async_wait(std::bind(&ServiceStatusWatcher, serviceStatusWatchTimerRef, ioContextRef, std::placeholders::_1)); } } } diff --git a/src/server/authserver/Server/AuthSocketMgr.h b/src/server/authserver/Server/AuthSocketMgr.h index 781fb09e5c8..04d65d396b0 100644 --- a/src/server/authserver/Server/AuthSocketMgr.h +++ b/src/server/authserver/Server/AuthSocketMgr.h @@ -32,9 +32,9 @@ public: return instance; } - bool StartNetwork(boost::asio::io_service& service, std::string const& bindIp, uint16 port, int threadCount = 1) override + bool StartNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int threadCount = 1) override { - if (!BaseSocketMgr::StartNetwork(service, bindIp, port, threadCount)) + if (!BaseSocketMgr::StartNetwork(ioContext, bindIp, port, threadCount)) return false; _acceptor->AsyncAcceptWithCallback<&AuthSocketMgr::OnSocketAccept>(); diff --git a/src/server/game/Server/Protocol/PacketLog.cpp b/src/server/game/Server/Protocol/PacketLog.cpp index 132f6964c8e..d3dda606c7a 100644 --- a/src/server/game/Server/Protocol/PacketLog.cpp +++ b/src/server/game/Server/Protocol/PacketLog.cpp @@ -17,8 +17,9 @@ #include "PacketLog.h" #include "Config.h" -#include "WorldPacket.h" +#include "IpAddress.h" #include "Timer.h" +#include "WorldPacket.h" #pragma pack(push, 1) diff --git a/src/server/game/Server/WorldSocketMgr.cpp b/src/server/game/Server/WorldSocketMgr.cpp index ca1737b823c..e2c1a19e547 100644 --- a/src/server/game/Server/WorldSocketMgr.cpp +++ b/src/server/game/Server/WorldSocketMgr.cpp @@ -54,11 +54,11 @@ WorldSocketMgr& WorldSocketMgr::Instance() return instance; } -bool WorldSocketMgr::StartNetwork(boost::asio::io_service& service, std::string const& bindIp, uint16 port, int threadCount) +bool WorldSocketMgr::StartWorldNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int threadCount) { _tcpNoDelay = sConfigMgr->GetBoolDefault("Network.TcpNodelay", true); - int const max_connections = boost::asio::socket_base::max_connections; + int const max_connections = TRINITY_MAX_LISTEN_CONNECTIONS; TC_LOG_DEBUG("misc", "Max allowed socket connections %d", max_connections); // -1 means use default @@ -72,7 +72,7 @@ bool WorldSocketMgr::StartNetwork(boost::asio::io_service& service, std::string return false; } - if (!BaseSocketMgr::StartNetwork(service, bindIp, port, threadCount)) + if (!BaseSocketMgr::StartNetwork(ioContext, bindIp, port, threadCount)) return false; _acceptor->SetSocketFactory(std::bind(&BaseSocketMgr::GetSocketForAccept, this)); diff --git a/src/server/game/Server/WorldSocketMgr.h b/src/server/game/Server/WorldSocketMgr.h index da53057b158..209024c88e0 100644 --- a/src/server/game/Server/WorldSocketMgr.h +++ b/src/server/game/Server/WorldSocketMgr.h @@ -38,7 +38,7 @@ public: static WorldSocketMgr& Instance(); /// Start network, listen at address:port . - bool StartNetwork(boost::asio::io_service& service, std::string const& bindIp, uint16 port, int networkThreads) override; + bool StartWorldNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int networkThreads); /// Stops all network threads, It will wait for all running threads . void StopNetwork() override; diff --git a/src/server/scripts/Commands/cs_account.cpp b/src/server/scripts/Commands/cs_account.cpp index 0da1d637191..bd0ae263d68 100644 --- a/src/server/scripts/Commands/cs_account.cpp +++ b/src/server/scripts/Commands/cs_account.cpp @@ -25,13 +25,13 @@ EndScriptData */ #include "AccountMgr.h" #include "Chat.h" #include "DatabaseEnv.h" +#include "IpAddress.h" #include "Language.h" #include "Log.h" #include "Player.h" #include "ScriptMgr.h" #include "World.h" #include "WorldSession.h" -#include <boost/asio/ip/address_v4.hpp> class account_commandscript : public CommandScript { @@ -291,7 +291,7 @@ public: if (param == "on") { PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_LOGON_COUNTRY); - uint32 ip = boost::asio::ip::address_v4::from_string(handler->GetSession()->GetRemoteAddress()).to_ulong(); + uint32 ip = Trinity::Net::address_to_uint(Trinity::Net::make_address_v4(handler->GetSession()->GetRemoteAddress())); EndianConvertReverse(ip); stmt->setUInt32(0, ip); PreparedQueryResult result = LoginDatabase.Query(stmt); diff --git a/src/server/scripts/Commands/cs_misc.cpp b/src/server/scripts/Commands/cs_misc.cpp index cc28b2da8ab..3829ca3afa2 100644 --- a/src/server/scripts/Commands/cs_misc.cpp +++ b/src/server/scripts/Commands/cs_misc.cpp @@ -27,6 +27,7 @@ #include "Group.h" #include "GroupMgr.h" #include "InstanceSaveMgr.h" +#include "IpAddress.h" #include "Item.h" #include "Language.h" #include "LFG.h" @@ -48,7 +49,6 @@ #include "WeatherMgr.h" #include "World.h" #include "WorldSession.h" -#include <boost/asio/ip/address_v4.hpp> // temporary hack until includes are sorted out (don't want to pull in Windows.h) #ifdef GetClassName @@ -1700,7 +1700,7 @@ public: lastIp = fields[4].GetString(); lastLogin = fields[5].GetString(); - uint32 ip = boost::asio::ip::address_v4::from_string(lastIp).to_ulong(); + uint32 ip = Trinity::Net::address_to_uint(Trinity::Net::make_address_v4(lastIp)); EndianConvertReverse(ip); // If ip2nation table is populated, it displays the country diff --git a/src/server/shared/Networking/AsyncAcceptor.h b/src/server/shared/Networking/AsyncAcceptor.h index cea7c8abd99..254d9336dc4 100644 --- a/src/server/shared/Networking/AsyncAcceptor.h +++ b/src/server/shared/Networking/AsyncAcceptor.h @@ -18,22 +18,29 @@ #ifndef __ASYNCACCEPT_H_ #define __ASYNCACCEPT_H_ +#include "IoContext.h" +#include "IpAddress.h" #include "Log.h" #include <boost/asio/ip/tcp.hpp> -#include <boost/asio/ip/address.hpp> #include <functional> #include <atomic> using boost::asio::ip::tcp; +#if BOOST_VERSION >= 106600 +#define TRINITY_MAX_LISTEN_CONNECTIONS boost::asio::socket_base::max_listen_connections +#else +#define TRINITY_MAX_LISTEN_CONNECTIONS boost::asio::socket_base::max_connections +#endif + class AsyncAcceptor { public: typedef void(*AcceptCallback)(tcp::socket&& newSocket, uint32 threadIndex); - AsyncAcceptor(boost::asio::io_service& ioService, std::string const& bindIp, uint16 port) : - _acceptor(ioService), _endpoint(boost::asio::ip::address::from_string(bindIp), port), - _socket(ioService), _closed(false), _socketFactory(std::bind(&AsyncAcceptor::DefeaultSocketFactory, this)) + AsyncAcceptor(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port) : + _acceptor(ioContext), _endpoint(Trinity::Net::make_address(bindIp), port), + _socket(ioContext), _closed(false), _socketFactory(std::bind(&AsyncAcceptor::DefeaultSocketFactory, this)) { } @@ -84,7 +91,7 @@ public: return false; } - _acceptor.listen(boost::asio::socket_base::max_connections, errorCode); + _acceptor.listen(TRINITY_MAX_LISTEN_CONNECTIONS, errorCode); if (errorCode) { TC_LOG_INFO("network", "Failed to start listening on %s:%u %s", _endpoint.address().to_string().c_str(), _endpoint.port(), errorCode.message().c_str()); diff --git a/src/server/shared/Networking/NetworkThread.h b/src/server/shared/Networking/NetworkThread.h index e384e1515ae..2f032a20247 100644 --- a/src/server/shared/Networking/NetworkThread.h +++ b/src/server/shared/Networking/NetworkThread.h @@ -20,6 +20,7 @@ #include "Define.h" #include "Errors.h" +#include "IoContext.h" #include "Log.h" #include "Timer.h" #include <boost/asio/ip/tcp.hpp> @@ -37,8 +38,8 @@ template<class SocketType> class NetworkThread { public: - NetworkThread() : _connections(0), _stopped(false), _thread(nullptr), _io_service(1), - _acceptSocket(_io_service), _updateTimer(_io_service) + NetworkThread() : _connections(0), _stopped(false), _thread(nullptr), _ioContext(1), + _acceptSocket(_ioContext), _updateTimer(_ioContext) { } @@ -55,7 +56,7 @@ public: void Stop() { _stopped = true; - _io_service.stop(); + _ioContext.stop(); } bool Start() @@ -123,7 +124,7 @@ protected: _updateTimer.expires_from_now(boost::posix_time::milliseconds(10)); _updateTimer.async_wait(std::bind(&NetworkThread<SocketType>::Update, this)); - _io_service.run(); + _ioContext.run(); TC_LOG_DEBUG("misc", "Network Thread exits"); _newSockets.clear(); @@ -170,7 +171,7 @@ private: std::mutex _newSocketsLock; SocketContainer _newSockets; - boost::asio::io_service _io_service; + Trinity::Asio::IoContext _ioContext; tcp::socket _acceptSocket; boost::asio::deadline_timer _updateTimer; }; diff --git a/src/server/shared/Networking/SocketMgr.h b/src/server/shared/Networking/SocketMgr.h index 585aad409c0..1fc0ef88e44 100644 --- a/src/server/shared/Networking/SocketMgr.h +++ b/src/server/shared/Networking/SocketMgr.h @@ -35,14 +35,14 @@ public: ASSERT(!_threads && !_acceptor && !_threadCount, "StopNetwork must be called prior to SocketMgr destruction"); } - virtual bool StartNetwork(boost::asio::io_service& service, std::string const& bindIp, uint16 port, int threadCount) + virtual bool StartNetwork(Trinity::Asio::IoContext& ioContext, std::string const& bindIp, uint16 port, int threadCount) { ASSERT(threadCount > 0); AsyncAcceptor* acceptor = nullptr; try { - acceptor = new AsyncAcceptor(service, bindIp, port); + acceptor = new AsyncAcceptor(ioContext, bindIp, port); } catch (boost::system::system_error const& err) { diff --git a/src/server/shared/Realm/Realm.cpp b/src/server/shared/Realm/Realm.cpp index f99feaae96c..79d9891dfa2 100644 --- a/src/server/shared/Realm/Realm.cpp +++ b/src/server/shared/Realm/Realm.cpp @@ -16,7 +16,8 @@ */ #include "Realm.h" -#include <boost/asio/ip/basic_endpoint.hpp> +#include "IpAddress.h" +#include "IpNetwork.h" boost::asio::ip::tcp_endpoint Realm::GetAddressForClient(boost::asio::ip::address const& clientAddr) const { @@ -37,12 +38,8 @@ boost::asio::ip::tcp_endpoint Realm::GetAddressForClient(boost::asio::ip::addres } else { - if (clientAddr.is_v4() && - (clientAddr.to_v4().to_ulong() & LocalSubnetMask->to_v4().to_ulong()) == - (LocalAddress->to_v4().to_ulong() & LocalSubnetMask->to_v4().to_ulong())) - { + if (clientAddr.is_v4() && Trinity::Net::IsInNetwork(LocalAddress->to_v4(), LocalSubnetMask->to_v4(), clientAddr.to_v4())) realmIp = *LocalAddress; - } else realmIp = *ExternalAddress; } diff --git a/src/server/shared/Realm/RealmList.cpp b/src/server/shared/Realm/RealmList.cpp index aa8ab98da56..e0523f04af2 100644 --- a/src/server/shared/Realm/RealmList.cpp +++ b/src/server/shared/Realm/RealmList.cpp @@ -18,7 +18,9 @@ #include "RealmList.h" #include "DatabaseEnv.h" +#include "IoContext.h" #include "Log.h" +#include "Resolver.h" #include "Util.h" #include <boost/asio/deadline_timer.hpp> #include <boost/asio/ip/tcp.hpp> @@ -38,11 +40,11 @@ RealmList* RealmList::Instance() } // Load the realm list from the database -void RealmList::Initialize(boost::asio::io_service& ioService, uint32 updateInterval) +void RealmList::Initialize(Trinity::Asio::IoContext& ioContext, uint32 updateInterval) { _updateInterval = updateInterval; - _updateTimer = Trinity::make_unique<boost::asio::deadline_timer>(ioService); - _resolver = Trinity::make_unique<boost::asio::ip::tcp::resolver>(ioService); + _updateTimer = Trinity::make_unique<boost::asio::deadline_timer>(ioContext); + _resolver = Trinity::make_unique<boost::asio::ip::tcp::resolver>(ioContext); // Get the content of the realmlist table in the database UpdateRealms(boost::system::error_code()); @@ -54,7 +56,7 @@ void RealmList::Close() } void RealmList::UpdateRealm(RealmHandle const& id, uint32 build, std::string const& name, - boost::asio::ip::address const& address, boost::asio::ip::address const& localAddr, boost::asio::ip::address const& localSubmask, + boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr, boost::asio::ip::address&& localSubmask, uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population) { // Create new if not exist or update existed @@ -69,11 +71,11 @@ void RealmList::UpdateRealm(RealmHandle const& id, uint32 build, std::string con realm.AllowedSecurityLevel = allowedSecurityLevel; realm.PopulationLevel = population; if (!realm.ExternalAddress || *realm.ExternalAddress != address) - realm.ExternalAddress = Trinity::make_unique<boost::asio::ip::address>(address); + realm.ExternalAddress = Trinity::make_unique<boost::asio::ip::address>(std::move(address)); if (!realm.LocalAddress || *realm.LocalAddress != localAddr) - realm.LocalAddress = Trinity::make_unique<boost::asio::ip::address>(localAddr); + realm.LocalAddress = Trinity::make_unique<boost::asio::ip::address>(std::move(localAddr)); if (!realm.LocalSubnetMask || *realm.LocalSubnetMask != localSubmask) - realm.LocalSubnetMask = Trinity::make_unique<boost::asio::ip::address>(localSubmask); + realm.LocalSubnetMask = Trinity::make_unique<boost::asio::ip::address>(std::move(localSubmask)); realm.Port = port; } @@ -100,43 +102,34 @@ void RealmList::UpdateRealms(boost::system::error_code const& error) { try { - boost::asio::ip::tcp::resolver::iterator end; - Field* fields = result->Fetch(); uint32 realmId = fields[0].GetUInt32(); std::string name = fields[1].GetString(); - boost::asio::ip::tcp::resolver::query externalAddressQuery(boost::asio::ip::tcp::v4(), fields[2].GetString(), ""); + std::string externalAddressString = fields[2].GetString(); + std::string localAddressString = fields[3].GetString(); + std::string localSubmaskString = fields[4].GetString(); - boost::system::error_code ec; - boost::asio::ip::tcp::resolver::iterator endPoint = _resolver->resolve(externalAddressQuery, ec); - if (endPoint == end || ec) + Optional<boost::asio::ip::tcp::endpoint> externalAddress = Trinity::Net::Resolve(*_resolver, boost::asio::ip::tcp::v4(), externalAddressString, ""); + if (!externalAddress) { - TC_LOG_ERROR("server.authserver", "Could not resolve address %s for realm \"%s\" id %u", fields[2].GetString().c_str(), name.c_str(), realmId); + TC_LOG_ERROR("server.authserver", "Could not resolve address %s for realm \"%s\" id %u", externalAddressString.c_str(), name.c_str(), realmId); continue; } - boost::asio::ip::address externalAddress = endPoint->endpoint().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<boost::asio::ip::tcp::endpoint> localAddress = Trinity::Net::Resolve(*_resolver, boost::asio::ip::tcp::v4(), localAddressString, ""); + if (!localAddress) { - TC_LOG_ERROR("server.authserver", "Could not resolve localAddress %s for realm \"%s\" id %u", fields[3].GetString().c_str(), name.c_str(), realmId); + TC_LOG_ERROR("server.authserver", "Could not resolve localAddress %s for realm \"%s\" id %u", localAddressString.c_str(), name.c_str(), realmId); continue; } - boost::asio::ip::address localAddress = endPoint->endpoint().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<boost::asio::ip::tcp::endpoint> localSubmask = Trinity::Net::Resolve(*_resolver, boost::asio::ip::tcp::v4(), localSubmaskString, ""); + if (!localSubmask) { - TC_LOG_ERROR("server.authserver", "Could not resolve localSubnetMask %s for realm \"%s\" id %u", fields[4].GetString().c_str(), name.c_str(), realmId); + TC_LOG_ERROR("server.authserver", "Could not resolve localSubnetMask %s for realm \"%s\" id %u", localSubmaskString.c_str(), name.c_str(), realmId); continue; } - boost::asio::ip::address localSubmask = endPoint->endpoint().address(); - uint16 port = fields[5].GetUInt16(); uint8 icon = fields[6].GetUInt8(); if (icon == REALM_TYPE_FFA_PVP) @@ -151,13 +144,13 @@ void RealmList::UpdateRealms(boost::system::error_code const& error) RealmHandle id{ realmId }; - UpdateRealm(id, build, name, externalAddress, localAddress, localSubmask, port, icon, flag, + UpdateRealm(id, build, name, externalAddress->address(), localAddress->address(), localSubmask->address(), port, icon, flag, timezone, (allowedSecurityLevel <= SEC_ADMINISTRATOR ? AccountTypes(allowedSecurityLevel) : SEC_ADMINISTRATOR), pop); if (!existingRealms.count(id)) - TC_LOG_INFO("server.authserver", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port); + TC_LOG_INFO("server.authserver", "Added realm \"%s\" at %s:%u.", name.c_str(), externalAddressString.c_str(), port); else - TC_LOG_DEBUG("server.authserver", "Updating realm \"%s\" at %s:%u.", name.c_str(), externalAddress.to_string().c_str(), port); + TC_LOG_DEBUG("server.authserver", "Updating realm \"%s\" at %s:%u.", name.c_str(), externalAddressString.c_str(), port); existingRealms.erase(id); } diff --git a/src/server/shared/Realm/RealmList.h b/src/server/shared/Realm/RealmList.h index c70243f82d8..7b18f6a99a0 100644 --- a/src/server/shared/Realm/RealmList.h +++ b/src/server/shared/Realm/RealmList.h @@ -27,14 +27,17 @@ namespace boost { - namespace asio + namespace system { - class io_service; + class error_code; } +} - namespace system +namespace Trinity +{ + namespace Asio { - class error_code; + class IoContext; } } @@ -48,7 +51,7 @@ public: ~RealmList(); - void Initialize(boost::asio::io_service& ioService, uint32 updateInterval); + void Initialize(Trinity::Asio::IoContext& ioContext, uint32 updateInterval); void Close(); RealmMap const& GetRealms() const { return _realms; } @@ -59,7 +62,7 @@ private: void UpdateRealms(boost::system::error_code const& error); void UpdateRealm(RealmHandle const& id, uint32 build, std::string const& name, - boost::asio::ip::address const& address, boost::asio::ip::address const& localAddr, boost::asio::ip::address const& localSubmask, + boost::asio::ip::address&& address, boost::asio::ip::address&& localAddr, boost::asio::ip::address&& localSubmask, uint16 port, uint8 icon, RealmFlags flag, uint8 timezone, AccountTypes allowedSecurityLevel, float population); RealmMap _realms; 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 <openssl/opensslv.h> #include <openssl/crypto.h> -#include <boost/asio/io_service.hpp> #include <boost/asio/deadline_timer.hpp> #include <boost/asio/signal_set.hpp> #include <boost/filesystem/operations.hpp> @@ -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<FreezeDetector> 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<boost::asio::io_service> ioService = std::make_shared<boost::asio::io_service>(); + std::shared_ptr<Trinity::Asio::IoContext> ioContext = std::make_shared<Trinity::Asio::IoContext>(); sLog->RegisterAppender<AppenderDB>(); - // 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<std::vector<std::thread>> threadPool(new std::vector<std::thread>(), [ioService](std::vector<std::thread>* del) + std::shared_ptr<std::vector<std::thread>> threadPool(new std::vector<std::thread>(), [ioContext](std::vector<std::thread>* 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<AsyncAcceptor> raAcceptor; if (sConfigMgr->GetBoolDefault("Ra.Enable", false)) - raAcceptor.reset(StartRaSocketAcceptor(*ioService)); + raAcceptor.reset(StartRaSocketAcceptor(*ioContext)); // Start soap serving thread if enabled std::shared_ptr<std::thread> 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> freezeDetector; if (int coreStuckTime = sConfigMgr->GetIntDefault("MaxCoreStuckTime", 0)) { - freezeDetector = std::make_shared<FreezeDetector>(*ioService, coreStuckTime * 1000); + freezeDetector = std::make_shared<FreezeDetector>(*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<FreezeDetector> 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<boost::asio::ip::tcp::endpoint> 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<boost::asio::ip::address>((*endPoint).endpoint().address()); + realm.ExternalAddress = Trinity::make_unique<boost::asio::ip::address>(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<boost::asio::ip::tcp::endpoint> 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<boost::asio::ip::address>((*endPoint).endpoint().address()); + realm.LocalAddress = Trinity::make_unique<boost::asio::ip::address>(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<boost::asio::ip::tcp::endpoint> 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<boost::asio::ip::address>((*endPoint).endpoint().address()); + realm.LocalSubnetMask = Trinity::make_unique<boost::asio::ip::address>(localSubmask->address()); realm.Port = fields[5].GetUInt16(); realm.Type = fields[6].GetUInt8(); |
