diff --git a/include/Client.h b/include/Client.h index 6b50a7b..ea23e6b 100644 --- a/include/Client.h +++ b/include/Client.h @@ -101,7 +101,7 @@ public: // bytes sent on TCP std::atomic_size_t TcpSent = 0; - std::chrono::system_clock::time_point ConnectionTime{}; + TimeType::time_point ConnectionTime{}; private: @@ -127,7 +127,7 @@ private: std::string mRole; std::string mDID; int mID = -1; - std::chrono::time_point mLastPingTime; + std::chrono::time_point mLastPingTime; }; std::optional> GetClient(class TServer& Server, int ID); diff --git a/include/Common.h b/include/Common.h index dfc0861..553f727 100644 --- a/include/Common.h +++ b/include/Common.h @@ -27,6 +27,10 @@ namespace fs = std::filesystem; #include #include +#include + +using TimeType = std::chrono::system_clock; + // General constexpr std::string_view StrDebug = "Debug"; constexpr std::string_view StrPrivate = "Private"; diff --git a/include/TLuaEngine.h b/include/TLuaEngine.h index c035b8c..45f329a 100644 --- a/include/TLuaEngine.h +++ b/include/TLuaEngine.h @@ -133,7 +133,7 @@ public: } static void WaitForAll(std::vector>& Results, - const std::optional& Max = std::nullopt); + const std::optional& Max = std::nullopt); void ReportErrors(const std::vector>& Results); bool HasState(TLuaStateId StateId); [[nodiscard]] std::shared_ptr EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script); @@ -260,8 +260,8 @@ private: }; struct TimedEvent { - std::chrono::high_resolution_clock::duration Duration {}; - std::chrono::high_resolution_clock::time_point LastCompletion {}; + TimeType::duration Duration {}; + TimeType::time_point LastCompletion {}; std::string EventName; TLuaStateId StateId; CallStrategy Strategy; diff --git a/include/TScopedTimer.h b/include/TScopedTimer.h index 0f1ad85..99042ca 100644 --- a/include/TScopedTimer.h +++ b/include/TScopedTimer.h @@ -4,6 +4,8 @@ #include #include +#include "Common.h" + class TScopedTimer { public: TScopedTimer(); @@ -11,7 +13,7 @@ public: TScopedTimer(std::function OnDestroy); ~TScopedTimer(); auto GetElapsedTime() const { - auto EndTime = std::chrono::system_clock::now(); + auto EndTime = TimeType::now(); auto Delta = EndTime - mStartTime; size_t TimeDelta = Delta / std::chrono::milliseconds(1); return TimeDelta; @@ -20,6 +22,6 @@ public: std::function OnDestroy { nullptr }; private: - std::chrono::system_clock::time_point mStartTime; + TimeType::time_point mStartTime; std::string Name; }; diff --git a/src/Client.cpp b/src/Client.cpp index 44710e8..8a107a7 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -119,7 +119,7 @@ TClient::TClient(TServer& Server, ip::tcp::socket&& Socket) : mServer(Server) , mSocket(std::move(Socket)) , mDownSocket(ip::tcp::socket(Server.IoCtx())) - , mLastPingTime(std::chrono::high_resolution_clock::now()) { + , mLastPingTime(TimeType::now()) { } TClient::~TClient() { @@ -127,11 +127,11 @@ TClient::~TClient() { } void TClient::UpdatePingTime() { - mLastPingTime = std::chrono::high_resolution_clock::now(); + mLastPingTime = TimeType::now(); } int TClient::SecondsSinceLastPing() { auto seconds = std::chrono::duration_cast( - std::chrono::high_resolution_clock::now() - mLastPingTime) + TimeType::now() - mLastPingTime) .count(); return int(seconds); } diff --git a/src/TConsole.cpp b/src/TConsole.cpp index 6983684..a11af1f 100644 --- a/src/TConsole.cpp +++ b/src/TConsole.cpp @@ -84,8 +84,8 @@ static inline void SplitString(std::string const& str, const char delim, std::ve } static std::string GetDate() { - std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); - time_t tt = std::chrono::high_resolution_clock::to_time_t(now); + TimeType::time_point now = TimeType::now(); + time_t tt = TimeType::to_time_t(now); auto local_tm = std::localtime(&tt); char buf[30]; std::string date; @@ -323,10 +323,10 @@ void TConsole::Command_Debug(const std::string&, const std::vector& } State += "Disconnected"; } - auto Now = std::chrono::system_clock::now(); + auto Now = TimeType::now(); auto Seconds = std::chrono::duration_cast(Now - Locked->ConnectionTime); std::string ConnectedSince = fmt::format("{:%Y/%m/%d %H:%M:%S}, {:%H:%M:%S} ago ({} seconds)", - fmt::localtime(std::chrono::high_resolution_clock::to_time_t(Locked->ConnectionTime)), + fmt::localtime(TimeType::to_time_t(Locked->ConnectionTime)), Seconds, Seconds.count()); Application::Console().WriteRaw(fmt::format( diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 2beff9f..f7aaecc 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -17,14 +17,14 @@ void THeartbeatThread::operator()() { // these are "hot-change" related variables static std::string Last; - static std::chrono::high_resolution_clock::time_point LastNormalUpdateTime = std::chrono::high_resolution_clock::now(); + static TimeType::time_point LastNormalUpdateTime = TimeType::now(); bool isAuth = false; size_t UpdateReminderCounter = 0; while (!Application::IsShuttingDown()) { ++UpdateReminderCounter; Body = GenerateCall(); // a hot-change occurs when a setting has changed, to update the backend of that change. - auto Now = std::chrono::high_resolution_clock::now(); + auto Now = TimeType::now(); bool Unchanged = Last == Body; auto TimePassed = (Now - LastNormalUpdateTime); auto Threshold = Unchanged ? 30 : 5; diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index e386d34..6881236 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -77,7 +77,7 @@ void TLuaEngine::operator()() { } }); // event loop - auto Before = std::chrono::high_resolution_clock::now(); + auto Before = TimeType::now(); while (!Application::IsShuttingDown()) { { // Timed Events Scope std::unique_lock Lock(mTimedEventsMutex); @@ -110,14 +110,14 @@ void TLuaEngine::operator()() { } else { constexpr double NsFactor = 1000000.0; constexpr double Expected = 10.0; // ms - const auto Diff = (std::chrono::high_resolution_clock::now() - Before).count() / NsFactor; + const auto Diff = (TimeType::now() - Before).count() / NsFactor; if (Diff < Expected) { std::this_thread::sleep_for(std::chrono::nanoseconds(size_t((Expected - Diff) * NsFactor))); } else { beammp_tracef("Event loop cannot keep up! Running {}ms behind", Diff); } } - Before = std::chrono::high_resolution_clock::now(); + Before = TimeType::now(); } if (ResultCheckThread.joinable()) { @@ -281,7 +281,7 @@ std::vector TLuaEngine::StateThreadData::GetStateTableKeys(const st */ -void TLuaEngine::WaitForAll(std::vector>& Results, const std::optional& Max) { +void TLuaEngine::WaitForAll(std::vector>& Results, const std::optional& Max) { for (const auto& Result : Results) { bool Cancelled = false; size_t ms = 0; @@ -777,14 +777,14 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI MPTable.set_function("CreateTimer", [&]() -> sol::table { sol::state_view StateView(mState); sol::table Result = StateView.create_table(); - Result["__StartTime"] = std::chrono::high_resolution_clock::now(); + Result["__StartTime"] = TimeType::now(); Result.set_function("GetCurrent", [&](const sol::table& Table) -> float { - auto End = std::chrono::high_resolution_clock::now(); - auto Start = Table.get("__StartTime"); + auto End = TimeType::now(); + auto Start = Table.get("__StartTime"); return std::chrono::duration_cast(End - Start).count() / 1000000.0f; }); Result.set_function("Start", [&](sol::table Table) { - Table["__StartTime"] = std::chrono::high_resolution_clock::now(); + Table["__StartTime"] = TimeType::now(); }); return Result; }); @@ -1097,8 +1097,8 @@ std::vector TLuaEngine::StateThreadData::Debug_GetSt void TLuaEngine::CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy) { std::unique_lock Lock(mTimedEventsMutex); TimedEvent Event { - std::chrono::high_resolution_clock::duration { std::chrono::milliseconds(IntervalMS) }, - std::chrono::high_resolution_clock::now(), + TimeType::duration { std::chrono::milliseconds(IntervalMS) }, + TimeType::now(), EventName, StateId, Strategy @@ -1141,10 +1141,10 @@ TLuaChunk::TLuaChunk(std::shared_ptr Content, std::string FileName, } bool TLuaEngine::TimedEvent::Expired() { - auto Waited = (std::chrono::high_resolution_clock::now() - LastCompletion); + auto Waited = (TimeType::now() - LastCompletion); return Waited >= Duration; } void TLuaEngine::TimedEvent::Reset() { - LastCompletion = std::chrono::high_resolution_clock::now(); + LastCompletion = TimeType::now(); } diff --git a/src/TNetwork.cpp b/src/TNetwork.cpp index c8155e6..ce320d9 100644 --- a/src/TNetwork.cpp +++ b/src/TNetwork.cpp @@ -227,7 +227,7 @@ void TNetwork::HandleDownload(TConnection&& Conn) { std::shared_ptr TNetwork::Authentication(TConnection&& RawConnection) { auto Client = CreateClient(std::move(RawConnection.Socket)); - Client->ConnectionTime = std::chrono::high_resolution_clock::now(); + Client->ConnectionTime = TimeType::now(); Client->SetIdentifier("ip", RawConnection.SockAddr.address().to_string()); beammp_tracef("This thread is ip {}", RawConnection.SockAddr.address().to_string()); diff --git a/src/TScopedTimer.cpp b/src/TScopedTimer.cpp index f8cd297..9c8d626 100644 --- a/src/TScopedTimer.cpp +++ b/src/TScopedTimer.cpp @@ -2,21 +2,21 @@ #include "Common.h" TScopedTimer::TScopedTimer() - : mStartTime(std::chrono::system_clock::now()) { + : mStartTime(TimeType::now()) { } TScopedTimer::TScopedTimer(const std::string& mName) - : mStartTime(std::chrono::system_clock::now()) + : mStartTime(TimeType::now()) , Name(mName) { } TScopedTimer::TScopedTimer(std::function OnDestroy) : OnDestroy(OnDestroy) - , mStartTime(std::chrono::system_clock::now()) { + , mStartTime(TimeType::now()) { } TScopedTimer::~TScopedTimer() { - auto EndTime = std::chrono::system_clock::now(); + auto EndTime = TimeType::now(); auto Delta = EndTime - mStartTime; size_t TimeDelta = Delta / std::chrono::milliseconds(1); if (OnDestroy) {