From 015bb899f8e3fb014bfa69fdcbb30db1c1163384 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 27 Oct 2015 13:47:08 +0100 Subject: [PATCH] collect network bandwidth usage stats --- src/net/ConnectionHandler.hpp | 16 +++++++++++++++- src/net/net.cpp | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/net/ConnectionHandler.hpp b/src/net/ConnectionHandler.hpp index be01d10..76103fe 100644 --- a/src/net/ConnectionHandler.hpp +++ b/src/net/ConnectionHandler.hpp @@ -17,11 +17,18 @@ public: float PacketLoss() const noexcept { return packet_loss; } /// smooth average round trip time in milliseconds float RoundTripTime() const noexcept { return rtt; } + /// estimated kilobytes transferred per second + float Upstream() const noexcept { return tx_kbps; } + /// estimated kilobytes received per second + float Downstream() const noexcept { return rx_kbps; } - void PacketSent(std::uint16_t); + void PacketSent(std::uint16_t) noexcept; void PacketLost(std::uint16_t); void PacketReceived(std::uint16_t); + void PacketIn(const UDPpacket &) noexcept; + void PacketOut(const UDPpacket &) noexcept; + void Handle(const UDPpacket &); virtual void OnTimeout() { } @@ -31,6 +38,7 @@ private: void UpdateRTT(std::uint16_t) noexcept; bool SamplePacket(std::uint16_t) const noexcept; int HeadDiff(std::uint16_t) const noexcept; + void UpdateStats() noexcept; // called as soon as the remote end ack'd given packet virtual void OnPacketReceived(std::uint16_t) { } @@ -61,6 +69,12 @@ private: std::uint16_t stamp_last; float rtt; + Uint32 next_sample; + std::size_t tx_bytes; + std::size_t rx_bytes; + float tx_kbps; + float rx_kbps; + }; } diff --git a/src/net/net.cpp b/src/net/net.cpp index 59176aa..0686d54 100644 --- a/src/net/net.cpp +++ b/src/net/net.cpp @@ -85,6 +85,7 @@ uint16_t Connection::Send(UDPpacket &udp_pack, UDPsocket sock) { } if (HasHandler()) { + Handler().PacketOut(udp_pack); Handler().PacketSent(seq); } @@ -115,6 +116,7 @@ void Connection::Received(const UDPpacket &udp_pack) { } Packet::TControl ctrl_new = pack.header.ctrl; + Handler().PacketIn(udp_pack); Handler().Handle(udp_pack); if (diff > 0) { @@ -158,14 +160,20 @@ ConnectionHandler::ConnectionHandler() , packet_loss(0.0f) , stamp_cursor(15) , stamp_last(0) -, rtt(64.0f) { +, rtt(64.0f) +, next_sample(1000) +, tx_bytes(0) +, rx_bytes(0) +, tx_kbps(0.0f) +, rx_kbps(0.0f) { Uint32 now = SDL_GetTicks(); for (Uint32 &s : stamps) { s = now; } + next_sample += now; } -void ConnectionHandler::PacketSent(uint16_t seq) { +void ConnectionHandler::PacketSent(uint16_t seq) noexcept { if (!SamplePacket(seq)) { return; } @@ -218,6 +226,27 @@ int ConnectionHandler::HeadDiff(std::uint16_t seq) const noexcept { return diff / 8; } +void ConnectionHandler::PacketIn(const UDPpacket &pack) noexcept { + rx_bytes += pack.len + 20; // I know, I know, it's an estimate (about 48 for IPv6) + UpdateStats(); +} + +void ConnectionHandler::PacketOut(const UDPpacket &pack) noexcept { + tx_bytes += pack.len + 20; + UpdateStats(); +} + +void ConnectionHandler::UpdateStats() noexcept { + Uint32 now = SDL_GetTicks(); + if (now >= next_sample) { + tx_kbps = float(tx_bytes) * (1.0f / 1024.0f); + rx_kbps = float(rx_bytes) * (1.0f / 1024.0f); + tx_bytes = 0; + rx_bytes = 0; + next_sample += 1000; + } +} + ostream &operator <<(ostream &out, const IPaddress &addr) { const unsigned char *host = reinterpret_cast(&addr.host); -- 2.39.2