]> git.localhorst.tv Git - blank.git/blob - src/net/CongestionControl.hpp
73d1d859ec0aaed8cafdcd8ba448786a7b047e0f
[blank.git] / src / net / CongestionControl.hpp
1 #ifndef BLANK_NET_CONGESTIONCONTROL_HPP_
2 #define BLANK_NET_CONGESTIONCONTROL_HPP_
3
4 #include <cstdint>
5 #include <SDL_net.h>
6
7
8 namespace blank {
9
10 class CongestionControl {
11
12 public:
13         CongestionControl();
14
15         /// packet loss as factor
16         float PacketLoss() const noexcept { return packet_loss; }
17         /// smooth average round trip time in milliseconds
18         float RoundTripTime() const noexcept { return rtt; }
19         /// estimated kilobytes transferred per second
20         float Upstream() const noexcept { return tx_kbps; }
21         /// estimated kilobytes received per second
22         float Downstream() const noexcept { return rx_kbps; }
23
24         void PacketSent(std::uint16_t) noexcept;
25         void PacketLost(std::uint16_t) noexcept;
26         void PacketReceived(std::uint16_t) noexcept;
27
28         void PacketIn(const UDPpacket &) noexcept;
29         void PacketOut(const UDPpacket &) noexcept;
30
31 private:
32         void UpdatePacketLoss() noexcept;
33         void UpdateRTT(std::uint16_t) noexcept;
34         bool SamplePacket(std::uint16_t) const noexcept;
35         int HeadDiff(std::uint16_t) const noexcept;
36         void UpdateStats() noexcept;
37
38 private:
39         const unsigned int packet_overhead;
40         const unsigned int sample_skip;
41
42         unsigned int packets_lost;
43         unsigned int packets_received;
44         float packet_loss;
45
46         Uint32 stamps[16];
47         std::size_t stamp_cursor;
48         std::uint16_t stamp_last;
49         float rtt;
50
51         Uint32 next_sample;
52         std::size_t tx_bytes;
53         std::size_t rx_bytes;
54         float tx_kbps;
55         float rx_kbps;
56
57 };
58
59 }
60
61 #endif