mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-10-25 13:30:43 +00:00
replace all clocks with a generic TimeType
This commit is contained in:
10 files changed
+39
-33
No files matched your search
+2
-2
@@ -101,7 +101,7 @@ public:
|
|||||||
// bytes sent on TCP
|
// bytes sent on TCP
|
||||||
std::atomic_size_t TcpSent = 0;
|
std::atomic_size_t TcpSent = 0;
|
||||||
|
|
||||||
std::chrono::system_clock::time_point ConnectionTime{};
|
TimeType::time_point ConnectionTime{};
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -127,7 +127,7 @@ private:
|
|||||||
std::string mRole;
|
std::string mRole;
|
||||||
std::string mDID;
|
std::string mDID;
|
||||||
int mID = -1;
|
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);
|
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/container/flat_map.hpp>
|
||||||
#include <boost/variant.hpp>
|
#include <boost/variant.hpp>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
using TimeType = std::chrono::system_clock;
|
||||||
|
|
||||||
// General
|
// General
|
||||||
constexpr std::string_view StrDebug = "Debug";
|
constexpr std::string_view StrDebug = "Debug";
|
||||||
constexpr std::string_view StrPrivate = "Private";
|
constexpr std::string_view StrPrivate = "Private";
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results,
|
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);
|
void ReportErrors(const std::vector<std::shared_ptr<TLuaResult>>& Results);
|
||||||
bool HasState(TLuaStateId StateId);
|
bool HasState(TLuaStateId StateId);
|
||||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script);
|
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script);
|
||||||
@@ -260,8 +260,8 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct TimedEvent {
|
struct TimedEvent {
|
||||||
std::chrono::high_resolution_clock::duration Duration {};
|
TimeType::duration Duration {};
|
||||||
std::chrono::high_resolution_clock::time_point LastCompletion {};
|
TimeType::time_point LastCompletion {};
|
||||||
std::string EventName;
|
std::string EventName;
|
||||||
TLuaStateId StateId;
|
TLuaStateId StateId;
|
||||||
CallStrategy Strategy;
|
CallStrategy Strategy;
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "Common.h"
|
||||||
|
|
||||||
class TScopedTimer {
|
class TScopedTimer {
|
||||||
public:
|
public:
|
||||||
TScopedTimer();
|
TScopedTimer();
|
||||||
@@ -11,7 +13,7 @@ public:
|
|||||||
TScopedTimer(std::function<void(size_t)> OnDestroy);
|
TScopedTimer(std::function<void(size_t)> OnDestroy);
|
||||||
~TScopedTimer();
|
~TScopedTimer();
|
||||||
auto GetElapsedTime() const {
|
auto GetElapsedTime() const {
|
||||||
auto EndTime = std::chrono::system_clock::now();
|
auto EndTime = TimeType::now();
|
||||||
auto Delta = EndTime - mStartTime;
|
auto Delta = EndTime - mStartTime;
|
||||||
size_t TimeDelta = Delta / std::chrono::milliseconds(1);
|
size_t TimeDelta = Delta / std::chrono::milliseconds(1);
|
||||||
return TimeDelta;
|
return TimeDelta;
|
||||||
@@ -20,6 +22,6 @@ public:
|
|||||||
std::function<void(size_t /* time_ms */)> OnDestroy { nullptr };
|
std::function<void(size_t /* time_ms */)> OnDestroy { nullptr };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::system_clock::time_point mStartTime;
|
TimeType::time_point mStartTime;
|
||||||
std::string Name;
|
std::string Name;
|
||||||
};
|
};
|
||||||
+3
-3
@@ -119,7 +119,7 @@ TClient::TClient(TServer& Server, ip::tcp::socket&& Socket)
|
|||||||
: mServer(Server)
|
: mServer(Server)
|
||||||
, mSocket(std::move(Socket))
|
, mSocket(std::move(Socket))
|
||||||
, mDownSocket(ip::tcp::socket(Server.IoCtx()))
|
, mDownSocket(ip::tcp::socket(Server.IoCtx()))
|
||||||
, mLastPingTime(std::chrono::high_resolution_clock::now()) {
|
, mLastPingTime(TimeType::now()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TClient::~TClient() {
|
TClient::~TClient() {
|
||||||
@@ -127,11 +127,11 @@ TClient::~TClient() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TClient::UpdatePingTime() {
|
void TClient::UpdatePingTime() {
|
||||||
mLastPingTime = std::chrono::high_resolution_clock::now();
|
mLastPingTime = TimeType::now();
|
||||||
}
|
}
|
||||||
int TClient::SecondsSinceLastPing() {
|
int TClient::SecondsSinceLastPing() {
|
||||||
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(
|
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(
|
||||||
std::chrono::high_resolution_clock::now() - mLastPingTime)
|
TimeType::now() - mLastPingTime)
|
||||||
.count();
|
.count();
|
||||||
return int(seconds);
|
return int(seconds);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -84,8 +84,8 @@ static inline void SplitString(std::string const& str, const char delim, std::ve
|
|||||||
}
|
}
|
||||||
|
|
||||||
static std::string GetDate() {
|
static std::string GetDate() {
|
||||||
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
TimeType::time_point now = TimeType::now();
|
||||||
time_t tt = std::chrono::high_resolution_clock::to_time_t(now);
|
time_t tt = TimeType::to_time_t(now);
|
||||||
auto local_tm = std::localtime(&tt);
|
auto local_tm = std::localtime(&tt);
|
||||||
char buf[30];
|
char buf[30];
|
||||||
std::string date;
|
std::string date;
|
||||||
@@ -323,10 +323,10 @@ void TConsole::Command_Debug(const std::string&, const std::vector<std::string>&
|
|||||||
}
|
}
|
||||||
State += "Disconnected";
|
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);
|
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)",
|
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,
|
||||||
Seconds.count());
|
Seconds.count());
|
||||||
Application::Console().WriteRaw(fmt::format(
|
Application::Console().WriteRaw(fmt::format(
|
||||||
|
|||||||
@@ -17,14 +17,14 @@ void THeartbeatThread::operator()() {
|
|||||||
// these are "hot-change" related variables
|
// these are "hot-change" related variables
|
||||||
static std::string Last;
|
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;
|
bool isAuth = false;
|
||||||
size_t UpdateReminderCounter = 0;
|
size_t UpdateReminderCounter = 0;
|
||||||
while (!Application::IsShuttingDown()) {
|
while (!Application::IsShuttingDown()) {
|
||||||
++UpdateReminderCounter;
|
++UpdateReminderCounter;
|
||||||
Body = GenerateCall();
|
Body = GenerateCall();
|
||||||
// a hot-change occurs when a setting has changed, to update the backend of that change.
|
// 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;
|
bool Unchanged = Last == Body;
|
||||||
auto TimePassed = (Now - LastNormalUpdateTime);
|
auto TimePassed = (Now - LastNormalUpdateTime);
|
||||||
auto Threshold = Unchanged ? 30 : 5;
|
auto Threshold = Unchanged ? 30 : 5;
|
||||||
|
|||||||
+12
-12
@@ -77,7 +77,7 @@ void TLuaEngine::operator()() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
// event loop
|
// event loop
|
||||||
auto Before = std::chrono::high_resolution_clock::now();
|
auto Before = TimeType::now();
|
||||||
while (!Application::IsShuttingDown()) {
|
while (!Application::IsShuttingDown()) {
|
||||||
{ // Timed Events Scope
|
{ // Timed Events Scope
|
||||||
std::unique_lock Lock(mTimedEventsMutex);
|
std::unique_lock Lock(mTimedEventsMutex);
|
||||||
@@ -110,14 +110,14 @@ void TLuaEngine::operator()() {
|
|||||||
} else {
|
} else {
|
||||||
constexpr double NsFactor = 1000000.0;
|
constexpr double NsFactor = 1000000.0;
|
||||||
constexpr double Expected = 10.0; // ms
|
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) {
|
if (Diff < Expected) {
|
||||||
std::this_thread::sleep_for(std::chrono::nanoseconds(size_t((Expected - Diff) * NsFactor)));
|
std::this_thread::sleep_for(std::chrono::nanoseconds(size_t((Expected - Diff) * NsFactor)));
|
||||||
} else {
|
} else {
|
||||||
beammp_tracef("Event loop cannot keep up! Running {}ms behind", Diff);
|
beammp_tracef("Event loop cannot keep up! Running {}ms behind", Diff);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Before = std::chrono::high_resolution_clock::now();
|
Before = TimeType::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ResultCheckThread.joinable()) {
|
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) {
|
for (const auto& Result : Results) {
|
||||||
bool Cancelled = false;
|
bool Cancelled = false;
|
||||||
size_t ms = 0;
|
size_t ms = 0;
|
||||||
@@ -777,14 +777,14 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
|
|||||||
MPTable.set_function("CreateTimer", [&]() -> sol::table {
|
MPTable.set_function("CreateTimer", [&]() -> sol::table {
|
||||||
sol::state_view StateView(mState);
|
sol::state_view StateView(mState);
|
||||||
sol::table Result = StateView.create_table();
|
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 {
|
Result.set_function("GetCurrent", [&](const sol::table& Table) -> float {
|
||||||
auto End = std::chrono::high_resolution_clock::now();
|
auto End = TimeType::now();
|
||||||
auto Start = Table.get<std::chrono::high_resolution_clock::time_point>("__StartTime");
|
auto Start = Table.get<TimeType::time_point>("__StartTime");
|
||||||
return std::chrono::duration_cast<std::chrono::microseconds>(End - Start).count() / 1000000.0f;
|
return std::chrono::duration_cast<std::chrono::microseconds>(End - Start).count() / 1000000.0f;
|
||||||
});
|
});
|
||||||
Result.set_function("Start", [&](sol::table Table) {
|
Result.set_function("Start", [&](sol::table Table) {
|
||||||
Table["__StartTime"] = std::chrono::high_resolution_clock::now();
|
Table["__StartTime"] = TimeType::now();
|
||||||
});
|
});
|
||||||
return Result;
|
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) {
|
void TLuaEngine::CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy) {
|
||||||
std::unique_lock Lock(mTimedEventsMutex);
|
std::unique_lock Lock(mTimedEventsMutex);
|
||||||
TimedEvent Event {
|
TimedEvent Event {
|
||||||
std::chrono::high_resolution_clock::duration { std::chrono::milliseconds(IntervalMS) },
|
TimeType::duration { std::chrono::milliseconds(IntervalMS) },
|
||||||
std::chrono::high_resolution_clock::now(),
|
TimeType::now(),
|
||||||
EventName,
|
EventName,
|
||||||
StateId,
|
StateId,
|
||||||
Strategy
|
Strategy
|
||||||
@@ -1141,10 +1141,10 @@ TLuaChunk::TLuaChunk(std::shared_ptr<std::string> Content, std::string FileName,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool TLuaEngine::TimedEvent::Expired() {
|
bool TLuaEngine::TimedEvent::Expired() {
|
||||||
auto Waited = (std::chrono::high_resolution_clock::now() - LastCompletion);
|
auto Waited = (TimeType::now() - LastCompletion);
|
||||||
return Waited >= Duration;
|
return Waited >= Duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::TimedEvent::Reset() {
|
void TLuaEngine::TimedEvent::Reset() {
|
||||||
LastCompletion = std::chrono::high_resolution_clock::now();
|
LastCompletion = TimeType::now();
|
||||||
}
|
}
|
||||||
+1
-1
@@ -227,7 +227,7 @@ void TNetwork::HandleDownload(TConnection&& Conn) {
|
|||||||
|
|
||||||
std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
|
std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
|
||||||
auto Client = CreateClient(std::move(RawConnection.Socket));
|
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());
|
Client->SetIdentifier("ip", RawConnection.SockAddr.address().to_string());
|
||||||
beammp_tracef("This thread is ip {}", RawConnection.SockAddr.address().to_string());
|
beammp_tracef("This thread is ip {}", RawConnection.SockAddr.address().to_string());
|
||||||
|
|
||||||
|
|||||||
@@ -2,21 +2,21 @@
|
|||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
|
|
||||||
TScopedTimer::TScopedTimer()
|
TScopedTimer::TScopedTimer()
|
||||||
: mStartTime(std::chrono::system_clock::now()) {
|
: mStartTime(TimeType::now()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TScopedTimer::TScopedTimer(const std::string& mName)
|
TScopedTimer::TScopedTimer(const std::string& mName)
|
||||||
: mStartTime(std::chrono::system_clock::now())
|
: mStartTime(TimeType::now())
|
||||||
, Name(mName) {
|
, Name(mName) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TScopedTimer::TScopedTimer(std::function<void(size_t)> OnDestroy)
|
TScopedTimer::TScopedTimer(std::function<void(size_t)> OnDestroy)
|
||||||
: OnDestroy(OnDestroy)
|
: OnDestroy(OnDestroy)
|
||||||
, mStartTime(std::chrono::system_clock::now()) {
|
, mStartTime(TimeType::now()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TScopedTimer::~TScopedTimer() {
|
TScopedTimer::~TScopedTimer() {
|
||||||
auto EndTime = std::chrono::system_clock::now();
|
auto EndTime = TimeType::now();
|
||||||
auto Delta = EndTime - mStartTime;
|
auto Delta = EndTime - mStartTime;
|
||||||
size_t TimeDelta = Delta / std::chrono::milliseconds(1);
|
size_t TimeDelta = Delta / std::chrono::milliseconds(1);
|
||||||
if (OnDestroy) {
|
if (OnDestroy) {
|
||||||
|
|||||||
Reference in new issue
Block a user