Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/TServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class TServer final {

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;
Expand Down
8 changes: 8 additions & 0 deletions src/TLuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
24 changes: 24 additions & 0 deletions src/TServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment thread
WiserTixx marked this conversation as resolved.
- 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);
}

Expand Down Expand Up @@ -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());
Expand Down
Loading