mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-16 07:30:42 +01:00
Core/Misc: Replace shared_ptr with unique_ptr with deleter for cleanups in main() functions
This commit is contained in:
@@ -35,6 +35,7 @@
|
||||
#include "IpNetwork.h"
|
||||
#include "Locales.h"
|
||||
#include "LoginRESTService.h"
|
||||
#include "Memory.h"
|
||||
#include "MySQLThreading.h"
|
||||
#include "OpenSSLCrypto.h"
|
||||
#include "ProcessPriority.h"
|
||||
@@ -102,9 +103,11 @@ int main(int argc, char** argv)
|
||||
if (vm.count("help") || vm.count("version"))
|
||||
return 0;
|
||||
|
||||
uint32 dummy = 0;
|
||||
|
||||
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
||||
|
||||
std::shared_ptr<void> protobufHandle(nullptr, [](void*) { google::protobuf::ShutdownProtobufLibrary(); });
|
||||
auto protobufHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { google::protobuf::ShutdownProtobufLibrary(); });
|
||||
|
||||
#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
|
||||
if (winServiceAction == "install")
|
||||
@@ -161,7 +164,7 @@ int main(int argc, char** argv)
|
||||
|
||||
OpenSSLCrypto::threadsSetup(boost::dll::program_location().remove_filename());
|
||||
|
||||
std::shared_ptr<void> opensslHandle(nullptr, [](void*) { OpenSSLCrypto::threadsCleanup(); });
|
||||
auto opensslHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { OpenSSLCrypto::threadsCleanup(); });
|
||||
|
||||
// bnetserver PID file creation
|
||||
std::string pidFile = sConfigMgr->GetStringDefault("PidFile", "");
|
||||
@@ -186,7 +189,7 @@ int main(int argc, char** argv)
|
||||
if (!StartDB())
|
||||
return 1;
|
||||
|
||||
std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); });
|
||||
auto dbHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { StopDB(); });
|
||||
|
||||
if (vm.count("update-databases-only"))
|
||||
return 0;
|
||||
@@ -214,7 +217,7 @@ int main(int argc, char** argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::shared_ptr<void> sLoginServiceHandle(nullptr, [](void*) { sLoginService.StopNetwork(); });
|
||||
auto sLoginServiceHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { sLoginService.StopNetwork(); });
|
||||
|
||||
// Start the listening port (acceptor) for auth connections
|
||||
int32 bnport = sConfigMgr->GetIntDefault("BattlenetPort", 1119);
|
||||
@@ -227,7 +230,7 @@ int main(int argc, char** argv)
|
||||
// Get the list of realms for the server
|
||||
sRealmList->Initialize(*ioContext, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 10));
|
||||
|
||||
std::shared_ptr<void> sRealmListHandle(nullptr, [](void*) { sRealmList->Close(); });
|
||||
auto sRealmListHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { sRealmList->Close(); });
|
||||
|
||||
std::string bindIp = sConfigMgr->GetStringDefault("BindIP", "0.0.0.0");
|
||||
|
||||
@@ -237,7 +240,7 @@ int main(int argc, char** argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::shared_ptr<void> sSessionMgrHandle(nullptr, [](void*) { sSessionMgr.StopNetwork(); });
|
||||
auto sSessionMgrHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { sSessionMgr.StopNetwork(); });
|
||||
|
||||
// Set signal handlers
|
||||
boost::asio::signal_set signals(*ioContext, SIGINT, SIGTERM);
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include "IpNetwork.h"
|
||||
#include "Locales.h"
|
||||
#include "MapManager.h"
|
||||
#include "Memory.h"
|
||||
#include "Metric.h"
|
||||
#include "MySQLThreading.h"
|
||||
#include "OpenSSLCrypto.h"
|
||||
@@ -125,7 +126,8 @@ bool StartDB();
|
||||
void StopDB();
|
||||
void WorldUpdateLoop();
|
||||
void ClearOnlineAccounts();
|
||||
void ShutdownCLIThread(std::thread* cliThread);
|
||||
struct ShutdownTCSoapThread { void operator()(std::thread* thread) const; };
|
||||
struct ShutdownCLIThread { void operator()(std::thread* cliThread) const; };
|
||||
bool LoadRealmInfo();
|
||||
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, std::string& winServiceAction);
|
||||
|
||||
@@ -147,9 +149,11 @@ extern int main(int argc, char** argv)
|
||||
if (vm.count("help") || vm.count("version"))
|
||||
return 0;
|
||||
|
||||
uint32 dummy = 0;
|
||||
|
||||
GOOGLE_PROTOBUF_VERIFY_VERSION;
|
||||
|
||||
std::shared_ptr<void> protobufHandle(nullptr, [](void*) { google::protobuf::ShutdownProtobufLibrary(); });
|
||||
auto protobufHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { google::protobuf::ShutdownProtobufLibrary(); });
|
||||
|
||||
#ifdef _WIN32
|
||||
if (winServiceAction == "install")
|
||||
@@ -161,7 +165,7 @@ extern int main(int argc, char** argv)
|
||||
|
||||
Optional<UINT> newTimerResolution;
|
||||
boost::system::error_code dllError;
|
||||
std::shared_ptr<boost::dll::shared_library> winmm(new boost::dll::shared_library("winmm.dll", dllError, boost::dll::load_mode::search_system_folders), [&](boost::dll::shared_library* lib)
|
||||
auto winmm = Trinity::make_unique_ptr_with_deleter(new boost::dll::shared_library("winmm.dll", dllError, boost::dll::load_mode::search_system_folders), [&](boost::dll::shared_library* lib)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -247,7 +251,7 @@ extern int main(int argc, char** argv)
|
||||
|
||||
OpenSSLCrypto::threadsSetup(boost::dll::program_location().remove_filename());
|
||||
|
||||
std::shared_ptr<void> opensslHandle(nullptr, [](void*) { OpenSSLCrypto::threadsCleanup(); });
|
||||
auto opensslHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { OpenSSLCrypto::threadsCleanup(); });
|
||||
|
||||
// Seed the OpenSSL's PRNG here.
|
||||
// That way it won't auto-seed when calling BigNumber::SetRand and slow down the first world login
|
||||
@@ -279,12 +283,12 @@ extern int main(int argc, char** argv)
|
||||
if (numThreads < 1)
|
||||
numThreads = 1;
|
||||
|
||||
std::shared_ptr<Trinity::ThreadPool> threadPool = std::make_shared<Trinity::ThreadPool>(numThreads);
|
||||
std::unique_ptr<Trinity::ThreadPool> threadPool = std::make_unique<Trinity::ThreadPool>(numThreads);
|
||||
|
||||
for (int i = 0; i < numThreads; ++i)
|
||||
threadPool->PostWork([ioContext]() { ioContext->run(); });
|
||||
|
||||
std::shared_ptr<void> ioContextStopHandle(nullptr, [ioContext](void*) { ioContext->stop(); });
|
||||
auto ioContextStopHandle = Trinity::make_unique_ptr_with_deleter(ioContext.get(), [](Trinity::Asio::IoContext* ctx) { ctx->stop(); });
|
||||
|
||||
// Set process priority according to configuration settings
|
||||
SetProcessPriority("server.worldserver", sConfigMgr->GetIntDefault(CONFIG_PROCESSOR_AFFINITY, 0), sConfigMgr->GetBoolDefault(CONFIG_HIGH_PRIORITY, false));
|
||||
@@ -293,7 +297,7 @@ extern int main(int argc, char** argv)
|
||||
if (!StartDB())
|
||||
return 1;
|
||||
|
||||
std::shared_ptr<void> dbHandle(nullptr, [](void*) { StopDB(); });
|
||||
auto dbHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { StopDB(); });
|
||||
|
||||
if (vm.count("update-databases-only"))
|
||||
return 0;
|
||||
@@ -305,7 +309,7 @@ extern int main(int argc, char** argv)
|
||||
|
||||
sRealmList->Initialize(*ioContext, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 10));
|
||||
|
||||
std::shared_ptr<void> sRealmListHandle(nullptr, [](void*) { sRealmList->Close(); });
|
||||
auto sRealmListHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*) { sRealmList->Close(); });
|
||||
|
||||
LoadRealmInfo();
|
||||
|
||||
@@ -319,14 +323,14 @@ extern int main(int argc, char** argv)
|
||||
|
||||
TC_METRIC_EVENT("events", "Worldserver started", "");
|
||||
|
||||
std::shared_ptr<void> sMetricHandle(nullptr, [](void*)
|
||||
auto sMetricHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*)
|
||||
{
|
||||
TC_METRIC_EVENT("events", "Worldserver shutdown", "");
|
||||
sMetric->Unload();
|
||||
});
|
||||
|
||||
sScriptMgr->SetScriptLoader(AddScripts);
|
||||
std::shared_ptr<void> sScriptMgrHandle(nullptr, [](void*)
|
||||
auto sScriptMgrHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*)
|
||||
{
|
||||
sScriptMgr->Unload();
|
||||
sScriptReloadMgr->Unload();
|
||||
@@ -336,7 +340,7 @@ extern int main(int argc, char** argv)
|
||||
sSecretMgr->Initialize(SECRET_OWNER_WORLDSERVER);
|
||||
sWorld->SetInitialWorldSettings();
|
||||
|
||||
std::shared_ptr<void> mapManagementHandle(nullptr, [](void*)
|
||||
auto mapManagementHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*)
|
||||
{
|
||||
// unload battleground templates before different singletons destroyed
|
||||
sBattlegroundMgr->DeleteAllBattlegrounds();
|
||||
@@ -353,16 +357,9 @@ extern int main(int argc, char** argv)
|
||||
raAcceptor.reset(StartRaSocketAcceptor(*ioContext));
|
||||
|
||||
// Start soap serving thread if enabled
|
||||
std::shared_ptr<std::thread> soapThread;
|
||||
std::unique_ptr<std::thread, ShutdownTCSoapThread> soapThread;
|
||||
if (sConfigMgr->GetBoolDefault("SOAP.Enabled", false))
|
||||
{
|
||||
soapThread.reset(new std::thread(TCSoapThread, sConfigMgr->GetStringDefault("SOAP.IP", "127.0.0.1"), uint16(sConfigMgr->GetIntDefault("SOAP.Port", 7878))),
|
||||
[](std::thread* thr)
|
||||
{
|
||||
thr->join();
|
||||
delete thr;
|
||||
});
|
||||
}
|
||||
soapThread.reset(new std::thread(TCSoapThread, sConfigMgr->GetStringDefault("SOAP.IP", "127.0.0.1"), uint16(sConfigMgr->GetIntDefault("SOAP.Port", 7878))));
|
||||
|
||||
// Launch the worldserver listener socket
|
||||
uint16 worldPort = uint16(sWorld->getIntConfig(CONFIG_PORT_WORLD));
|
||||
@@ -385,7 +382,7 @@ extern int main(int argc, char** argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::shared_ptr<void> sWorldSocketMgrHandle(nullptr, [](void*)
|
||||
auto sWorldSocketMgrHandle = Trinity::make_unique_ptr_with_deleter(&dummy, [](void*)
|
||||
{
|
||||
sWorld->KickAll(); // save and kick all players
|
||||
sWorld->UpdateSessions(1); // real players unload required UpdateSessions call
|
||||
@@ -415,14 +412,14 @@ extern int main(int argc, char** argv)
|
||||
TC_LOG_INFO("server.worldserver", "{} (worldserver-daemon) ready...", GitRevision::GetFullVersion());
|
||||
|
||||
// Launch CliRunnable thread
|
||||
std::shared_ptr<std::thread> cliThread;
|
||||
std::unique_ptr<std::thread, ShutdownCLIThread> cliThread;
|
||||
#ifdef _WIN32
|
||||
if (sConfigMgr->GetBoolDefault("Console.Enable", true) && (m_ServiceStatus == -1)/* need disable console in service mode*/)
|
||||
#else
|
||||
if (sConfigMgr->GetBoolDefault("Console.Enable", true))
|
||||
#endif
|
||||
{
|
||||
cliThread.reset(new std::thread(CliThread), &ShutdownCLIThread);
|
||||
cliThread.reset(new std::thread(CliThread));
|
||||
}
|
||||
|
||||
WorldUpdateLoop();
|
||||
@@ -451,7 +448,13 @@ extern int main(int argc, char** argv)
|
||||
return World::GetExitCode();
|
||||
}
|
||||
|
||||
void ShutdownCLIThread(std::thread* cliThread)
|
||||
void ShutdownTCSoapThread::operator()(std::thread* thread) const
|
||||
{
|
||||
thread->join();
|
||||
delete thread;
|
||||
}
|
||||
|
||||
void ShutdownCLIThread::operator()(std::thread* cliThread) const
|
||||
{
|
||||
if (cliThread != nullptr)
|
||||
{
|
||||
@@ -695,7 +698,6 @@ void ClearOnlineAccounts()
|
||||
// Battleground instance ids reset at server restart
|
||||
CharacterDatabase.DirectExecute("UPDATE character_battleground_data SET instanceId = 0");
|
||||
}
|
||||
|
||||
/// @}
|
||||
|
||||
variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile, fs::path& configDir, [[maybe_unused]] std::string& winServiceAction)
|
||||
|
||||
Reference in New Issue
Block a user