Add time ping handling (#503)

client -> server `t[8 bytes]`
server -> client `t[client's 8 bytes][server time 8 bytes]`

---

By creating this pull request, I understand that code that is AI
generated or otherwise automatically generated may be rejected without
further discussion.
I declare that I fully understand all code I pushed into this PR, and
wrote all this code myself and own the rights to this code.
This commit is contained in:
Tixx
2026-08-22 22:04:23 +02:00
committed by GitHub
3 changed files with 35 additions and 0 deletions
+3
View File
@@ -53,7 +53,10 @@ public:
io_context& IoCtx() { return mIoCtxPoller.IoCtx(); }
uint64_t GetServerTimeMS() const { return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - TimeSyncStart).count();}
double GetServerTime() const { return std::chrono::duration<double>(std::chrono::steady_clock::now() - TimeSyncStart).count(); }
private:
std::chrono::time_point<std::chrono::steady_clock> TimeSyncStart;
TIoPollThread mIoCtxPoller;
TClientSet mClients;
mutable RWMutex mClientsMutex;
+8
View File
@@ -1012,6 +1012,14 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
}
MPTable.set_function("Get", &LuaAPI::MP::Get);
MPTable.set_function("GetServerTimeMS", [this](const sol::this_state ts) {
return make_object(sol::state_view(ts), this->mEngine->Server().GetServerTimeMS());
});
MPTable.set_function("GetServerTime", [this](const sol::this_state ts) {
return make_object(sol::state_view(ts), this->mEngine->Server().GetServerTime());
});
auto UtilTable = StateView.create_named_table("Util");
UtilTable.set_function("LogDebug", [this](sol::variadic_args args) {
std::string ToPrint = "";
+24
View File
@@ -123,6 +123,12 @@ TEST_CASE("GetPidVid") {
TServer::TServer(const std::vector<std::string_view>& Arguments) {
beammp_info("BeamMP Server v" + Application::ServerVersionString());
Application::SetSubsystemStatus("Server", Application::Status::Starting);
std::mt19937_64 TimeSyncRandomDevice(std::random_device{}());
TimeSyncStart = std::chrono::steady_clock::now()
- std::chrono::duration_cast<std::chrono::steady_clock::duration>(
std::chrono::duration<double>(std::uniform_real_distribution<double>(0.0, 2592000.0)(TimeSyncRandomDevice))
);
Application::SetSubsystemStatus("Server", Application::Status::Good);
}
@@ -210,6 +216,24 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
return;
}
switch (Code) {
case 't':
if (!udp && Packet.size() == 9) {
auto Time = GetServerTimeMS();
std::vector<uint8_t> ServerTime(sizeof(uint64_t));
std::memcpy(ServerTime.data(), &Time, sizeof(uint64_t));
auto Data = Packet;
beammp_debugf("Sending server time {} to client '{}' ({}), client time: {}", Time, LockedClient->GetName(), LockedClient->GetID(), reinterpret_cast<uint64_t>(Data.data()));
Data.insert(Data.end(), ServerTime.begin(), ServerTime.end());
if (!Network.Respond(*LockedClient, Data, true)) {
Network.DisconnectClient(*LockedClient, "Failed to send time");
}
}
return;
case 'H': // initial connection
if (udp) {
beammp_debugf("Received 'H' packet over UDP from client '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());