mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-04 14:56:04 +00:00
replace all clocks with a generic TimeType
This commit is contained in:
@@ -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<std::chrono::high_resolution_clock> mLastPingTime;
|
||||
std::chrono::time_point<TimeType> mLastPingTime;
|
||||
};
|
||||
|
||||
std::optional<std::weak_ptr<TClient>> GetClient(class TServer& Server, int ID);
|
||||
|
||||
@@ -27,6 +27,10 @@ namespace fs = std::filesystem;
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
using TimeType = std::chrono::system_clock;
|
||||
|
||||
// General
|
||||
constexpr std::string_view StrDebug = "Debug";
|
||||
constexpr std::string_view StrPrivate = "Private";
|
||||
|
||||
@@ -133,7 +133,7 @@ public:
|
||||
}
|
||||
|
||||
static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results,
|
||||
const std::optional<std::chrono::high_resolution_clock::duration>& Max = std::nullopt);
|
||||
const std::optional<TimeType::duration>& Max = std::nullopt);
|
||||
void ReportErrors(const std::vector<std::shared_ptr<TLuaResult>>& Results);
|
||||
bool HasState(TLuaStateId StateId);
|
||||
[[nodiscard]] std::shared_ptr<TLuaResult> 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;
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "Common.h"
|
||||
|
||||
class TScopedTimer {
|
||||
public:
|
||||
TScopedTimer();
|
||||
@@ -11,7 +13,7 @@ public:
|
||||
TScopedTimer(std::function<void(size_t)> 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<void(size_t /* time_ms */)> OnDestroy { nullptr };
|
||||
|
||||
private:
|
||||
std::chrono::system_clock::time_point mStartTime;
|
||||
TimeType::time_point mStartTime;
|
||||
std::string Name;
|
||||
};
|
||||
|
||||
@@ -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::seconds>(
|
||||
std::chrono::high_resolution_clock::now() - mLastPingTime)
|
||||
TimeType::now() - mLastPingTime)
|
||||
.count();
|
||||
return int(seconds);
|
||||
}
|
||||
|
||||
@@ -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<std::string>&
|
||||
}
|
||||
State += "Disconnected";
|
||||
}
|
||||
auto Now = std::chrono::system_clock::now();
|
||||
auto Now = TimeType::now();
|
||||
auto Seconds = std::chrono::duration_cast<std::chrono::seconds>(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(
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<std::string> TLuaEngine::StateThreadData::GetStateTableKeys(const st
|
||||
|
||||
*/
|
||||
|
||||
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::optional<std::chrono::high_resolution_clock::duration>& Max) {
|
||||
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::optional<TimeType::duration>& 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<std::chrono::high_resolution_clock::time_point>("__StartTime");
|
||||
auto End = TimeType::now();
|
||||
auto Start = Table.get<TimeType::time_point>("__StartTime");
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(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::QueuedFunction> 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<std::string> 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();
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ void TNetwork::HandleDownload(TConnection&& Conn) {
|
||||
|
||||
std::shared_ptr<TClient> 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());
|
||||
|
||||
|
||||
@@ -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<void(size_t)> 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) {
|
||||
|
||||
Reference in New Issue
Block a user