diff --git a/src/common/Asio/DeadlineTimer.h b/src/common/Asio/DeadlineTimer.h new file mode 100644 index 00000000000000..09e377e6d4594f --- /dev/null +++ b/src/common/Asio/DeadlineTimer.h @@ -0,0 +1,34 @@ +/* + * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by the + * Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef DeadlineTimer_h__ +#define DeadlineTimer_h__ + +#include + +#define DeadlineTimerBase boost::asio::basic_deadline_timer, boost::asio::io_context::executor_type> + +namespace Acore::Asio +{ + class DeadlineTimer : public DeadlineTimerBase + { + public: + using DeadlineTimerBase::basic_deadline_timer; + }; +} + +#endif // DeadlineTimer_h__ diff --git a/src/common/Asio/IoContext.h b/src/common/Asio/IoContext.h index fa85c67155867f..46dfa8516fec60 100644 --- a/src/common/Asio/IoContext.h +++ b/src/common/Asio/IoContext.h @@ -19,6 +19,7 @@ #define IoContext_h__ #include + #include #include #define IoContextBaseNamespace boost::asio @@ -51,9 +52,9 @@ namespace Acore::Asio } template - inline boost::asio::io_context& get_io_context(T&& ioObject) + inline decltype(auto) get_io_context(T&& ioObject) { - return static_cast(ioObject.get_executor().context()); + return ioObject.get_executor().context(); } } diff --git a/src/common/Metric/Metric.cpp b/src/common/Metric/Metric.cpp index a6cd4b1f276ea8..8a2ecabd82695f 100644 --- a/src/common/Metric/Metric.cpp +++ b/src/common/Metric/Metric.cpp @@ -17,6 +17,7 @@ #include "Metric.h" #include "Config.h" +#include "DeadlineTimer.h" #include "Log.h" #include "Strand.h" #include "Tokenize.h" @@ -41,8 +42,8 @@ void Metric::Initialize(std::string const& realmName, Acore::Asio::IoContext& io { _dataStream = std::make_unique(); _realmName = FormatInfluxDBTagValue(realmName); - _batchTimer = std::make_unique(ioContext); - _overallStatusTimer = std::make_unique(ioContext); + _batchTimer = std::make_unique(ioContext); + _overallStatusTimer = std::make_unique(ioContext); _overallStatusLogger = overallStatusLogger; LoadFromConfigs(); } @@ -246,9 +247,7 @@ void Metric::ScheduleSend() { if (_enabled) { - // Calculate the expiration time - auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(_updateInterval); - _batchTimer->expires_at(expirationTime); + _batchTimer->expires_from_now(boost::posix_time::seconds(_updateInterval)); _batchTimer->async_wait(std::bind(&Metric::SendBatch, this)); } else @@ -281,9 +280,7 @@ void Metric::ScheduleOverallStatusLog() { if (_enabled) { - // Calculate the expiration time _overallStatusTimerInterval from now - auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(_overallStatusTimerInterval); - _overallStatusTimer->expires_at(expirationTime); + _overallStatusTimer->expires_from_now(boost::posix_time::seconds(_overallStatusTimerInterval)); _overallStatusTimer->async_wait([this](const boost::system::error_code&) { _overallStatusTimerTriggered = true; diff --git a/src/common/Metric/Metric.h b/src/common/Metric/Metric.h index 729d2f7fc5a170..eca14f52c8cc53 100644 --- a/src/common/Metric/Metric.h +++ b/src/common/Metric/Metric.h @@ -21,7 +21,6 @@ #include "Define.h" #include "Duration.h" #include "MPSCQueue.h" -#include #include #include // NOTE: this import is NEEDED (even though some IDEs report it as unused) #include @@ -31,6 +30,7 @@ namespace Acore::Asio { class IoContext; + class DeadlineTimer; } enum MetricDataType @@ -62,8 +62,8 @@ class AC_COMMON_API Metric std::iostream& GetDataStream() { return *_dataStream; } std::unique_ptr _dataStream; MPSCQueue _queuedData; - std::unique_ptr _batchTimer; - std::unique_ptr _overallStatusTimer; + std::unique_ptr _batchTimer; + std::unique_ptr _overallStatusTimer; int32 _updateInterval = 0; int32 _overallStatusTimerInterval = 0; bool _enabled = false; diff --git a/src/server/apps/authserver/Main.cpp b/src/server/apps/authserver/Main.cpp index 6030d9114220ae..dd07cf0ae989f1 100644 --- a/src/server/apps/authserver/Main.cpp +++ b/src/server/apps/authserver/Main.cpp @@ -29,6 +29,7 @@ #include "Config.h" #include "DatabaseEnv.h" #include "DatabaseLoader.h" +#include "DeadlineTimer.h" #include "IPLocation.h" #include "IoContext.h" #include "Log.h" @@ -59,8 +60,8 @@ namespace fs = std::filesystem; bool StartDB(); void StopDB(); void SignalHandler(std::weak_ptr ioContextRef, boost::system::error_code const& error, int signalNumber); -void KeepDatabaseAliveHandler(std::weak_ptr dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error); -void BanExpiryHandler(std::weak_ptr banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error); +void KeepDatabaseAliveHandler(std::weak_ptr dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error); +void BanExpiryHandler(std::weak_ptr banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error); variables_map GetConsoleArguments(int argc, char** argv, fs::path& configFile); /// Launch the auth server @@ -177,20 +178,14 @@ int main(int argc, char** argv) // Enabled a timed callback for handling the database keep alive ping int32 dbPingInterval = sConfigMgr->GetOption("MaxPingTime", 30); - std::shared_ptr dbPingTimer = std::make_shared(*ioContext); - - // Calculate the expiration time - auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(dbPingInterval); - dbPingTimer->expires_at(expirationTime); - dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, std::weak_ptr(dbPingTimer), dbPingInterval, std::placeholders::_1)); + std::shared_ptr dbPingTimer = std::make_shared(*ioContext); + dbPingTimer->expires_from_now(boost::posix_time::minutes(dbPingInterval)); + dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, std::weak_ptr(dbPingTimer), dbPingInterval, std::placeholders::_1)); int32 banExpiryCheckInterval = sConfigMgr->GetOption("BanExpiryCheckInterval", 60); - std::shared_ptr banExpiryCheckTimer = std::make_shared(*ioContext); - - // Calculate the expiration time - auto expirationTimeBanExpiry = std::chrono::steady_clock::now() + std::chrono::seconds(banExpiryCheckInterval); - banExpiryCheckTimer->expires_at(expirationTimeBanExpiry); - banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, std::weak_ptr(banExpiryCheckTimer), banExpiryCheckInterval, std::placeholders::_1)); + std::shared_ptr banExpiryCheckTimer = std::make_shared(*ioContext); + banExpiryCheckTimer->expires_from_now(boost::posix_time::seconds(banExpiryCheckInterval)); + banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, std::weak_ptr(banExpiryCheckTimer), banExpiryCheckInterval, std::placeholders::_1)); // Start the io service worker loop ioContext->run(); @@ -243,35 +238,31 @@ void SignalHandler(std::weak_ptr ioContextRef, boost::sy } } -void KeepDatabaseAliveHandler(std::weak_ptr dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error) +void KeepDatabaseAliveHandler(std::weak_ptr dbPingTimerRef, int32 dbPingInterval, boost::system::error_code const& error) { if (!error) { - if (std::shared_ptr dbPingTimer = dbPingTimerRef.lock()) + if (std::shared_ptr dbPingTimer = dbPingTimerRef.lock()) { LOG_INFO("server.authserver", "Ping MySQL to keep connection alive"); LoginDatabase.KeepAlive(); - // Calculate the expiration time - auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(dbPingInterval); - dbPingTimer->expires_at(expirationTime); + dbPingTimer->expires_from_now(boost::posix_time::minutes(dbPingInterval)); dbPingTimer->async_wait(std::bind(&KeepDatabaseAliveHandler, dbPingTimerRef, dbPingInterval, std::placeholders::_1)); } } } -void BanExpiryHandler(std::weak_ptr banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error) +void BanExpiryHandler(std::weak_ptr banExpiryCheckTimerRef, int32 banExpiryCheckInterval, boost::system::error_code const& error) { if (!error) { - if (std::shared_ptr banExpiryCheckTimer = banExpiryCheckTimerRef.lock()) + if (std::shared_ptr banExpiryCheckTimer = banExpiryCheckTimerRef.lock()) { LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_DEL_EXPIRED_IP_BANS)); LoginDatabase.Execute(LoginDatabase.GetPreparedStatement(LOGIN_UPD_EXPIRED_ACCOUNT_BANS)); - // Calculate the expiration time - auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(banExpiryCheckInterval); - banExpiryCheckTimer->expires_at(expirationTime); + banExpiryCheckTimer->expires_from_now(boost::posix_time::seconds(banExpiryCheckInterval)); banExpiryCheckTimer->async_wait(std::bind(&BanExpiryHandler, banExpiryCheckTimerRef, banExpiryCheckInterval, std::placeholders::_1)); } } diff --git a/src/server/apps/worldserver/Main.cpp b/src/server/apps/worldserver/Main.cpp index e95aadc7babfd9..f3db3add62f05a 100644 --- a/src/server/apps/worldserver/Main.cpp +++ b/src/server/apps/worldserver/Main.cpp @@ -30,6 +30,7 @@ #include "Config.h" #include "DatabaseEnv.h" #include "DatabaseLoader.h" +#include "DeadlineTimer.h" #include "GitRevision.h" #include "IoContext.h" #include "MapMgr.h" @@ -90,16 +91,14 @@ class FreezeDetector static void Start(std::shared_ptr const& freezeDetector) { - // Calculate the expiration time 5seconds from now - auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(5); - freezeDetector->_timer.expires_at(expirationTime); + freezeDetector->_timer.expires_from_now(boost::posix_time::seconds(5)); freezeDetector->_timer.async_wait(std::bind(&FreezeDetector::Handler, std::weak_ptr(freezeDetector), std::placeholders::_1)); } static void Handler(std::weak_ptr freezeDetectorRef, boost::system::error_code const& error); private: - boost::asio::steady_timer _timer; + Acore::Asio::DeadlineTimer _timer; uint32 _worldLoopCounter; uint32 _lastChangeMsTime; uint32 _maxCoreStuckTimeInMs; @@ -632,9 +631,7 @@ void FreezeDetector::Handler(std::weak_ptr freezeDetectorRef, bo } } - // Calculate the expiration time - auto expirationTime = std::chrono::steady_clock::now() + std::chrono::seconds(1); - freezeDetector->_timer.expires_at(expirationTime); + freezeDetector->_timer.expires_from_now(boost::posix_time::seconds(1)); freezeDetector->_timer.async_wait(std::bind(&FreezeDetector::Handler, freezeDetectorRef, std::placeholders::_1)); } } diff --git a/src/server/shared/Network/NetworkThread.h b/src/server/shared/Network/NetworkThread.h index b280c16d4f0f9d..aca77faab05a3f 100644 --- a/src/server/shared/Network/NetworkThread.h +++ b/src/server/shared/Network/NetworkThread.h @@ -18,14 +18,14 @@ #ifndef NetworkThread_h__ #define NetworkThread_h__ +#include "DeadlineTimer.h" #include "Define.h" #include "Errors.h" #include "IoContext.h" #include "Log.h" #include "Socket.h" -#include -#include #include +#include #include #include #include @@ -179,7 +179,7 @@ class NetworkThread { LOG_DEBUG("misc", "Network Thread Starting"); - _updateTimer.expires_at(std::chrono::steady_clock::now()); + _updateTimer.expires_from_now(boost::posix_time::milliseconds(1)); _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); }); _ioContext.run(); @@ -193,7 +193,7 @@ class NetworkThread if (_stopped) return; - _updateTimer.expires_at(std::chrono::steady_clock::now()); + _updateTimer.expires_from_now(boost::posix_time::milliseconds(1)); _updateTimer.async_wait([this](boost::system::error_code const&) { Update(); }); AddNewSockets(); @@ -230,7 +230,7 @@ class NetworkThread Acore::Asio::IoContext _ioContext; tcp::socket _acceptSocket; - boost::asio::steady_timer _updateTimer; + Acore::Asio::DeadlineTimer _updateTimer; bool _proxyHeaderReadingEnabled; }; diff --git a/src/server/shared/Realms/RealmList.cpp b/src/server/shared/Realms/RealmList.cpp index a1c258edd3d3e1..5576cb26247f20 100644 --- a/src/server/shared/Realms/RealmList.cpp +++ b/src/server/shared/Realms/RealmList.cpp @@ -17,6 +17,7 @@ #include "RealmList.h" #include "DatabaseEnv.h" +#include "DeadlineTimer.h" #include "Log.h" #include "Resolver.h" #include "QueryResult.h" @@ -36,7 +37,7 @@ RealmList* RealmList::Instance() void RealmList::Initialize(Acore::Asio::IoContext& ioContext, uint32 updateInterval) { _updateInterval = updateInterval; - _updateTimer = std::make_unique(ioContext); + _updateTimer = std::make_unique(ioContext); _resolver = std::make_unique(ioContext); LoadBuildInfo(); @@ -227,9 +228,7 @@ void RealmList::UpdateRealms(boost::system::error_code const& error) if (_updateInterval) { - // Calculate the expiration time _updateInterval from now - auto expiration_time = std::chrono::steady_clock::now() + std::chrono::seconds(_updateInterval); - _updateTimer->expires_at(expiration_time); + _updateTimer->expires_from_now(boost::posix_time::seconds(_updateInterval)); _updateTimer->async_wait([this](boost::system::error_code const& errorCode){ UpdateRealms(errorCode); }); } } diff --git a/src/server/shared/Realms/RealmList.h b/src/server/shared/Realms/RealmList.h index 9d8912ad2ce461..76456c3e34806e 100644 --- a/src/server/shared/Realms/RealmList.h +++ b/src/server/shared/Realms/RealmList.h @@ -20,22 +20,11 @@ #include "Define.h" #include "Realm.h" -#include #include #include #include // NOTE: this import is NEEDED (even though some IDEs report it as unused) #include -namespace Acore::Asio -{ - class IoContext; -} - -namespace boost::system -{ - class error_code; -} - struct RealmBuildInfo { uint32 Build; @@ -47,6 +36,11 @@ struct RealmBuildInfo std::array MacHash; }; +namespace boost::system +{ + class error_code; +} + /// Storage object for the list of realms on the server class AC_SHARED_API RealmList { @@ -76,7 +70,7 @@ class AC_SHARED_API RealmList std::vector _builds; RealmMap _realms; uint32 _updateInterval{0}; - std::unique_ptr _updateTimer; + std::unique_ptr _updateTimer; std::unique_ptr _resolver; };