]> git.localhorst.tv Git - blank.git/blobdiff - src/net/ConnectionHandler.hpp
collect network bandwidth usage stats
[blank.git] / src / net / ConnectionHandler.hpp
index 29595e5061e01c9bac2a8671158191cd6e768d68..76103fecdf11a5db2f36aaf1088e42c00f5d158b 100644 (file)
@@ -11,18 +11,69 @@ namespace blank {
 class ConnectionHandler {
 
 public:
-       void Handle(const UDPpacket &);
+       ConnectionHandler();
 
-       virtual void OnPacketLost(std::uint16_t) { }
+       /// packet loss as factor
+       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) 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() { }
 
 private:
+       void UpdatePacketLoss() noexcept;
+       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) { }
+       // called if the remote end probably didn't get given packet
+       virtual void OnPacketLost(std::uint16_t) { }
+
        virtual void On(const Packet::Ping &) { }
        virtual void On(const Packet::Login &) { }
        virtual void On(const Packet::Join &) { }
        virtual void On(const Packet::Part &) { }
        virtual void On(const Packet::PlayerUpdate &) { }
+       virtual void On(const Packet::SpawnEntity &) { }
+       virtual void On(const Packet::DespawnEntity &) { }
+       virtual void On(const Packet::EntityUpdate &) { }
+       virtual void On(const Packet::PlayerCorrection &) { }
+       virtual void On(const Packet::ChunkBegin &) { }
+       virtual void On(const Packet::ChunkData &) { }
+       virtual void On(const Packet::BlockUpdate &) { }
+       virtual void On(const Packet::Message &) { }
+
+private:
+       unsigned int packets_lost;
+       unsigned int packets_received;
+       float packet_loss;
+
+       Uint32 stamps[16];
+       std::size_t stamp_cursor;
+       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;
 
 };