mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-08-27 12:29:53 +00:00
Add time ping handling
This commit is contained in:
@@ -156,6 +156,13 @@ size_t TServer::ClientCount() const {
|
|||||||
return mClients.size();
|
return mClients.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::mt19937_64 TimeSyncRandomDevice(std::random_device{}());
|
||||||
|
|
||||||
|
auto 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))
|
||||||
|
);
|
||||||
|
|
||||||
void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TPPSMonitor& PPSMonitor, TNetwork& Network, bool udp) {
|
void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TPPSMonitor& PPSMonitor, TNetwork& Network, bool udp) {
|
||||||
constexpr std::string_view ABG = "ABG:";
|
constexpr std::string_view ABG = "ABG:";
|
||||||
if (Packet.size() >= ABG.size() && std::equal(Packet.begin(), Packet.begin() + ABG.size(), ABG.begin(), ABG.end())) {
|
if (Packet.size() >= ABG.size() && std::equal(Packet.begin(), Packet.begin() + ABG.size(), ABG.begin(), ABG.end())) {
|
||||||
@@ -210,6 +217,24 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (Code) {
|
switch (Code) {
|
||||||
|
case 't':
|
||||||
|
if (!udp && Packet.size() == 9) {
|
||||||
|
uint64_t Time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - TimeSyncStart).count();
|
||||||
|
|
||||||
|
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
|
case 'H': // initial connection
|
||||||
if (udp) {
|
if (udp) {
|
||||||
beammp_debugf("Received 'H' packet over UDP from client '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
|
beammp_debugf("Received 'H' packet over UDP from client '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
|
||||||
|
|||||||
Reference in New Issue
Block a user