]> git.localhorst.tv Git - blank.git/commitdiff
show net stats in debug overlay
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 27 Oct 2015 13:20:25 +0000 (14:20 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 27 Oct 2015 13:20:25 +0000 (14:20 +0100)
src/client/InteractiveState.hpp
src/client/client.cpp
src/ui/HUD.hpp
src/ui/ui.cpp

index d5fd4a1859fb3c8b188e7f03bed58023ceff0c49..7368e420829b7e9e49f4af062bf72be5d4607421 100644 (file)
@@ -89,6 +89,7 @@ private:
        ChunkReceiver chunk_receiver;
        ChunkRenderer chunk_renderer;
        CoarseTimer loop_timer;
+       CoarseTimer stat_timer;
 
        SkyBox sky;
 
index 1cbf11ef86e679c6b3cdbc314a42e7cbe4d9f0f0..3c30a62495127a52ace02decb18233c845e463f5 100644 (file)
@@ -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());
index 2302fd2991d2ec954b54a77e41929f00bb5a0d5e..b6ae7d7656cba0298f013ee24833ec0aeab0a0dc 100644 (file)
@@ -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;
index 618aff44742117f4965adeaaaa3a4240e7f2055f..6c7ab6aa49ff53163b734c271e5fc0b92cf4bff9 100644 (file)
@@ -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);
+               }
        }
 }