add server mspt (+procotol format)

This commit is contained in:
2023-01-02 15:07:34 +01:00
parent 5a547b6514
commit 512fb23d0e
7 changed files with 43 additions and 32 deletions

View File

@@ -12,6 +12,7 @@ private:
std::uint8_t m_ConnectionID; std::uint8_t m_ConnectionID;
std::string m_DisconnectReason; std::string m_DisconnectReason;
float m_ServerTPS; float m_ServerTPS;
float m_ServerMSPT;
int m_Ping = 0; int m_Ping = 0;
public: public:
ClientConnexion(); ClientConnexion();
@@ -25,6 +26,7 @@ public:
const std::string& GetDisconnectReason() const { return m_DisconnectReason; } const std::string& GetDisconnectReason() const { return m_DisconnectReason; }
float GetServerTPS() const { return m_ServerTPS; } float GetServerTPS() const { return m_ServerTPS; }
float GetServerMSPT() const { return m_ServerMSPT; }
int GetServerPing() const { return m_Ping; } int GetServerPing() const { return m_Ping; }
REMOVE_COPY(ClientConnexion); REMOVE_COPY(ClientConnexion);

View File

@@ -22,6 +22,7 @@ typedef std::map<std::uint8_t, ServerConnexion> ConnexionMap;
class TickCounter { class TickCounter {
private: private:
float m_TPS; float m_TPS;
float m_MSPT;
std::uint64_t m_LastTPSTime; std::uint64_t m_LastTPSTime;
std::uint8_t m_TickCount; std::uint8_t m_TickCount;
public: public:
@@ -46,6 +47,9 @@ public:
} }
float GetTPS() const { return m_TPS; } float GetTPS() const { return m_TPS; }
float GetMSPT() const { return m_MSPT; }
void SetMSPT(float mspt) { m_MSPT = mspt; }
}; };
class Server { class Server {

View File

@@ -373,10 +373,11 @@ public:
class ServerTpsPacket : public Packet { class ServerTpsPacket : public Packet {
private: private:
float m_TPS; float m_TPS;
float m_MSPT;
std::uint64_t m_PacketSendTime; // used to calculate ping std::uint64_t m_PacketSendTime; // used to calculate ping
public: public:
ServerTpsPacket() {} ServerTpsPacket() {}
ServerTpsPacket(float tps, std::uint64_t sendTime) : m_TPS(tps), m_PacketSendTime(sendTime) {} ServerTpsPacket(float tps, float mspt, std::uint64_t sendTime) : m_TPS(tps), m_MSPT(mspt), m_PacketSendTime(sendTime) {}
virtual ~ServerTpsPacket() {} virtual ~ServerTpsPacket() {}
virtual DataBuffer Serialize(bool packetID = true) const; virtual DataBuffer Serialize(bool packetID = true) const;
@@ -384,6 +385,7 @@ public:
virtual void Dispatch(PacketHandler* handler) const; virtual void Dispatch(PacketHandler* handler) const;
float GetTPS() const { return m_TPS; } float GetTPS() const { return m_TPS; }
float GetMSPT() const { return m_MSPT; }
std::uint64_t GetPacketSendTime() const { return m_PacketSendTime; } std::uint64_t GetPacketSendTime() const { return m_PacketSendTime; }
virtual PacketType GetType() const { return PacketType::ServerTps; } virtual PacketType GetType() const { return PacketType::ServerTps; }

View File

@@ -27,6 +27,7 @@ void ClientConnexion::HandlePacket(const protocol::ConnexionInfoPacket* packet)
void ClientConnexion::HandlePacket(const protocol::ServerTpsPacket* packet) { void ClientConnexion::HandlePacket(const protocol::ServerTpsPacket* packet) {
m_ServerTPS = packet->GetTPS(); m_ServerTPS = packet->GetTPS();
m_ServerMSPT = packet->GetMSPT();
m_Ping = utils::GetTime() - packet->GetPacketSendTime(); m_Ping = utils::GetTime() - packet->GetPacketSendTime();
} }

View File

@@ -26,6 +26,7 @@ void Server::StartThread() {
if (delta >= SERVER_TICK) { if (delta >= SERVER_TICK) {
Tick(delta); Tick(delta);
lastTime = td::utils::GetTime(); lastTime = td::utils::GetTime();
m_TickCounter.SetMSPT(lastTime - time);
std::uint64_t sleepTime = SERVER_TICK - (delta - SERVER_TICK); std::uint64_t sleepTime = SERVER_TICK - (delta - SERVER_TICK);
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime)); std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
} }
@@ -85,7 +86,7 @@ void Server::Tick(std::uint64_t delta) {
m_Lobby.Tick(); m_Lobby.Tick();
m_Game.Tick(delta); m_Game.Tick(delta);
if (m_TickCounter.Update()) { if (m_TickCounter.Update()) {
protocol::ServerTpsPacket packet(m_TickCounter.GetTPS(), utils::GetTime()); protocol::ServerTpsPacket packet(m_TickCounter.GetTPS(), m_TickCounter.GetMSPT(), utils::GetTime());
BroadcastPacket(&packet); BroadcastPacket(&packet);
} }
} }

View File

@@ -425,12 +425,12 @@ DataBuffer ServerTpsPacket::Serialize(bool packetID) const {
DataBuffer data; DataBuffer data;
WritePacketID(data, packetID); WritePacketID(data, packetID);
data << m_TPS << m_PacketSendTime; data << m_TPS << m_MSPT << m_PacketSendTime;
return data; return data;
} }
void ServerTpsPacket::Deserialize(DataBuffer& data) { void ServerTpsPacket::Deserialize(DataBuffer& data) {
data >> m_TPS >> m_PacketSendTime; data >> m_TPS >> m_MSPT >> m_PacketSendTime;
} }
DataBuffer SpawnMobPacket::Serialize(bool packetID) const { DataBuffer SpawnMobPacket::Serialize(bool packetID) const {

View File

@@ -85,6 +85,7 @@ void GameMenu::ShowLobbyProgress() {
void GameMenu::ShowTPS() { void GameMenu::ShowTPS() {
ImGui::Text("Server TPS : %.1f", GetClient()->GetConnexion().GetServerTPS()); ImGui::Text("Server TPS : %.1f", GetClient()->GetConnexion().GetServerTPS());
ImGui::Text("Server MSPT : %i", (int)GetClient()->GetConnexion().GetServerMSPT());
ImGui::Text("Server Ping : %i", GetClient()->GetConnexion().GetServerPing()); ImGui::Text("Server Ping : %i", GetClient()->GetConnexion().GetServerPing());
} }