mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-24 19:06:49 +01:00
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 dfd2660a85)
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user