From 225a7e66ed3f3f03ab458ab39c914ed55cd69600 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 27 Oct 2015 14:20:25 +0100 Subject: [PATCH] show net stats in debug overlay --- src/client/InteractiveState.hpp | 1 + src/client/client.cpp | 6 +++++ src/ui/HUD.hpp | 10 ++++++++ src/ui/ui.cpp | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/src/client/InteractiveState.hpp b/src/client/InteractiveState.hpp index d5fd4a1..7368e42 100644 --- a/src/client/InteractiveState.hpp +++ b/src/client/InteractiveState.hpp @@ -89,6 +89,7 @@ private: ChunkReceiver chunk_receiver; ChunkRenderer chunk_renderer; CoarseTimer loop_timer; + CoarseTimer stat_timer; SkyBox sky; diff --git a/src/client/client.cpp b/src/client/client.cpp index 1cbf11e..3c30a62 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -59,6 +59,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) , chunk_receiver(world.Chunks(), save) , chunk_renderer(player.GetChunks()) , loop_timer(16) +, stat_timer(1000) , sky(master.GetEnv().loader.LoadCubeMap("skybox")) , update_status() , chat(master.GetEnv(), *this, *this) { @@ -75,6 +76,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id) chunk_renderer.LoadTextures(master.GetEnv().loader, res.tex_index); chunk_renderer.FogDensity(master.GetWorldConf().fog_density); loop_timer.Start(); + stat_timer.Start(); if (save.Exists(player)) { save.Read(player); } @@ -141,6 +143,7 @@ void InteractiveState::Handle(const SDL_Event &event) { void InteractiveState::Update(int dt) { loop_timer.Update(dt); + stat_timer.Update(dt); master.Update(dt); chunk_receiver.Update(dt); @@ -163,6 +166,9 @@ void InteractiveState::Update(int dt) { input.PushPlayerUpdate(world_dt); } hud.Display(res.block_types[player.GetInventorySlot() + 1]); + if (stat_timer.Hit()) { + hud.UpdateNetStats(master); + } hud.Update(dt); glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords()); diff --git a/src/ui/HUD.hpp b/src/ui/HUD.hpp index 2302fd2..b6ae7d7 100644 --- a/src/ui/HUD.hpp +++ b/src/ui/HUD.hpp @@ -14,6 +14,7 @@ namespace blank { class Block; class BlockTypeRegistry; class Config; +class ConnectionHandler; class Environment; class Font; class Player; @@ -42,6 +43,9 @@ public: void UpdatePosition(); void UpdateOrientation(); + // net stats + void UpdateNetStats(const ConnectionHandler &); + // message box void PostMessage(const char *); void PostMessage(const std::string &msg) { @@ -79,6 +83,12 @@ private: bool show_block; bool show_entity; + // net stats + FixedText bandwidth_text; + FixedText rtt_text; + FixedText packet_loss_text; + bool show_net; + // message box MessageBox messages; CoarseTimer msg_timer; diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 618aff4..6c7ab6a 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -17,6 +17,7 @@ #include "../graphics/Viewport.hpp" #include "../io/TokenStreamReader.hpp" #include "../model/bounds.hpp" +#include "../net/ConnectionHandler.hpp" #include "../world/BlockLookup.hpp" #include "../world/World.hpp" #include "../world/WorldManipulator.hpp" @@ -208,6 +209,11 @@ HUD::HUD(Environment &env, Config &config, const Player &player) , block_text() , show_block(false) , show_entity(false) +// net stats +, bandwidth_text() +, rtt_text() +, packet_loss_text() +, show_net(false) // message box , messages(env.assets.small_ui_font) , msg_timer(5000) @@ -248,6 +254,20 @@ HUD::HUD(Environment &env, Config &config, const Player &player) entity_text.Background(glm::vec4(0.5f)); entity_text.Set(env.assets.small_ui_font, "Entity: none"); + // net stats + bandwidth_text.Position(glm::vec3(-25.0f, 25.0f + 6 * ls, 0.0f), Gravity::NORTH_EAST); + bandwidth_text.Foreground(glm::vec4(1.0f)); + bandwidth_text.Background(glm::vec4(0.5f)); + bandwidth_text.Set(env.assets.small_ui_font, "TX: 0.0KB/s RX: 0.0KB/s"); + rtt_text.Position(glm::vec3(-25.0f, 25.0f + 7 * ls, 0.0f), Gravity::NORTH_EAST); + rtt_text.Foreground(glm::vec4(1.0f)); + rtt_text.Background(glm::vec4(0.5f)); + rtt_text.Set(env.assets.small_ui_font, "RTT: unavailable"); + packet_loss_text.Position(glm::vec3(-25.0f, 25.0f + 8 * ls, 0.0f), Gravity::NORTH_EAST); + packet_loss_text.Foreground(glm::vec4(1.0f)); + packet_loss_text.Background(glm::vec4(0.5f)); + packet_loss_text.Set(env.assets.small_ui_font, "Packet loss: 0.0%"); + // message box messages.Position(glm::vec3(25.0f, -25.0f - 2 * ls, 0.0f), Gravity::SOUTH_WEST); messages.Foreground(glm::vec4(1.0f)); @@ -361,6 +381,25 @@ void HUD::PostMessage(const char *msg) { } +void HUD::UpdateNetStats(const ConnectionHandler &conn) { + std::stringstream s; + s << std::fixed << std::setprecision(1) + << "TX: " << conn.Upstream() + << "KB/s, RX: " << conn.Downstream() << "KB/s"; + bandwidth_text.Set(env.assets.small_ui_font, s.str()); + + s.str(""); + s << "RTT: " << conn.RoundTripTime() << "ms"; + rtt_text.Set(env.assets.small_ui_font, s.str()); + + s.str(""); + s << "Packet loss: " << (conn.PacketLoss() * 100.0f) << "%"; + packet_loss_text.Set(env.assets.small_ui_font, s.str()); + + show_net = true; +} + + void HUD::Update(int dt) { msg_timer.Update(dt); if (msg_timer.HitOnce()) { @@ -424,6 +463,11 @@ void HUD::Render(Viewport &viewport) noexcept { } else if (show_entity) { entity_text.Render(viewport); } + if (show_net) { + bandwidth_text.Render(viewport); + rtt_text.Render(viewport); + packet_loss_text.Render(viewport); + } } } -- 2.39.2