mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-10-11 19:14:30 +00:00
Player ping packet
This commit is contained in:
@@ -85,10 +85,12 @@ public:
|
|||||||
[[nodiscard]] std::string GetName() const { return mName; }
|
[[nodiscard]] std::string GetName() const { return mName; }
|
||||||
void SetUnicycleID(int ID) { mUnicycleID = ID; }
|
void SetUnicycleID(int ID) { mUnicycleID = ID; }
|
||||||
void SetID(int ID) { mID = ID; }
|
void SetID(int ID) { mID = ID; }
|
||||||
|
void SetSelfPing(uint16_t ping ) { mSelfPing = ping; }
|
||||||
[[nodiscard]] int GetOpenCarID() const;
|
[[nodiscard]] int GetOpenCarID() const;
|
||||||
[[nodiscard]] int GetCarCount() const;
|
[[nodiscard]] int GetCarCount() const;
|
||||||
void ClearCars();
|
void ClearCars();
|
||||||
[[nodiscard]] int GetID() const { return mID; }
|
[[nodiscard]] int GetID() const { return mID; }
|
||||||
|
[[nodiscard]] uint16_t GetSelfPing() const { return mSelfPing; }
|
||||||
[[nodiscard]] int GetUnicycleID() const { return mUnicycleID; }
|
[[nodiscard]] int GetUnicycleID() const { return mUnicycleID; }
|
||||||
[[nodiscard]] bool IsUDPConnected() const { return mIsUDPConnected; }
|
[[nodiscard]] bool IsUDPConnected() const { return mIsUDPConnected; }
|
||||||
[[nodiscard]] bool IsSynced() const { return mIsSynced; }
|
[[nodiscard]] bool IsSynced() const { return mIsSynced; }
|
||||||
@@ -139,6 +141,7 @@ private:
|
|||||||
std::string mRole;
|
std::string mRole;
|
||||||
std::string mDID;
|
std::string mDID;
|
||||||
int mID = -1;
|
int mID = -1;
|
||||||
|
uint16_t mSelfPing = 0;
|
||||||
std::chrono::time_point<std::chrono::high_resolution_clock> mLastPingTime = std::chrono::high_resolution_clock::now();
|
std::chrono::time_point<std::chrono::high_resolution_clock> mLastPingTime = std::chrono::high_resolution_clock::now();
|
||||||
std::vector<uint8_t> mMagic;
|
std::vector<uint8_t> mMagic;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -819,6 +819,39 @@ void TNetwork::UpdatePlayer(TClient& Client) {
|
|||||||
});
|
});
|
||||||
Packet = Packet.substr(0, Packet.length() - 1);
|
Packet = Packet.substr(0, Packet.length() - 1);
|
||||||
Client.EnqueuePacket(StringToVector(Packet));
|
Client.EnqueuePacket(StringToVector(Packet));
|
||||||
|
|
||||||
|
std::vector<uint8_t> PingsPacket = StringToVector("Sp");
|
||||||
|
|
||||||
|
PingsPacket.push_back(0);
|
||||||
|
|
||||||
|
uint8_t amount = 0;
|
||||||
|
|
||||||
|
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||||
|
if (amount == UINT8_MAX) {
|
||||||
|
beammp_debugf("Client ping packet overflow");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
|
if (const auto c = ClientPtr.lock()) {
|
||||||
|
int ID = c->GetID();
|
||||||
|
if (ID < 0 || ID > UINT8_MAX) {
|
||||||
|
beammp_debugf("Invalid ID found: {}", ID);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
PingsPacket.push_back(static_cast<uint8_t>(ID));
|
||||||
|
const uint16_t ping = c->GetSelfPing();
|
||||||
|
|
||||||
|
PingsPacket.push_back(static_cast<uint8_t>(ping));
|
||||||
|
PingsPacket.push_back(static_cast<uint8_t>(ping >> 8));
|
||||||
|
|
||||||
|
amount++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
PingsPacket[2] = amount;
|
||||||
|
|
||||||
|
Client.EnqueuePacket(PingsPacket);
|
||||||
//(void)Respond(Client, Packet, true);
|
//(void)Respond(Client, Packet, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-2
@@ -217,12 +217,18 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uin
|
|||||||
}
|
}
|
||||||
switch (Code) {
|
switch (Code) {
|
||||||
case 't':
|
case 't':
|
||||||
if (!udp && Packet.size() == 9) {
|
if (!udp && Packet.size() == 11) {
|
||||||
auto Time = GetServerTimeMS();
|
uint64_t Time = GetServerTimeMS();
|
||||||
|
|
||||||
std::vector<uint8_t> ServerTime(sizeof(uint64_t));
|
std::vector<uint8_t> ServerTime(sizeof(uint64_t));
|
||||||
std::memcpy(ServerTime.data(), &Time, sizeof(uint64_t));
|
std::memcpy(ServerTime.data(), &Time, sizeof(uint64_t));
|
||||||
|
|
||||||
|
uint16_t selfPing = 0;
|
||||||
|
std::memcpy(&selfPing, &Packet[Packet.size() - 2], sizeof(uint16_t));
|
||||||
|
|
||||||
|
LockedClient->SetSelfPing(selfPing);
|
||||||
|
|
||||||
|
Packet.erase(Packet.end() - 2,Packet.end());
|
||||||
auto Data = Packet;
|
auto Data = Packet;
|
||||||
|
|
||||||
beammp_debugf("Sending server time {} to client '{}' ({}), client time: {}", Time, LockedClient->GetName(), LockedClient->GetID(), reinterpret_cast<uint64_t>(Data.data()));
|
beammp_debugf("Sending server time {} to client '{}' ({}), client time: {}", Time, LockedClient->GetName(), LockedClient->GetID(), reinterpret_cast<uint64_t>(Data.data()));
|
||||||
|
|||||||
Reference in New Issue
Block a user