]> git.localhorst.tv Git - blank.git/blob - src/net/CongestionControl.hpp
rate network conditions
[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         enum Mode {
14                 GOOD,
15                 BAD,
16                 UGLY,
17         };
18
19 public:
20         CongestionControl();
21
22         /// get recommended mode of operation
23         Mode GetMode() const noexcept { return mode; }
24
25         /// packet loss as factor
26         float PacketLoss() const noexcept { return packet_loss; }
27         /// smooth average round trip time in milliseconds
28         float RoundTripTime() const noexcept { return rtt; }
29         /// estimated kilobytes transferred per second
30         float Upstream() const noexcept { return tx_kbps; }
31         /// estimated kilobytes received per second
32         float Downstream() const noexcept { return rx_kbps; }
33
34         void PacketSent(std::uint16_t) noexcept;
35         void PacketLost(std::uint16_t) noexcept;
36         void PacketReceived(std::uint16_t) noexcept;
37
38         void PacketIn(const UDPpacket &) noexcept;
39         void PacketOut(const UDPpacket &) noexcept;
40
41 private:
42         void UpdatePacketLoss() noexcept;
43
44         void UpdateRTT(std::uint16_t) noexcept;
45         bool SamplePacket(std::uint16_t) const noexcept;
46
47         void UpdateStats() noexcept;
48
49         void UpdateMode() noexcept;
50         void CheckUpgrade(Mode) noexcept;
51         void ChangeMode(Mode) noexcept;
52         void KeepMode() noexcept;
53
54         Mode Conditions() const noexcept;
55
56 private:
57         const unsigned int packet_overhead;
58         const unsigned int sample_skip;
59
60         unsigned int packets_lost;
61         unsigned int packets_received;
62         float packet_loss;
63
64         Uint32 stamps[16];
65         std::size_t stamp_cursor;
66         std::uint16_t stamp_last;
67         float rtt;
68
69         Uint32 next_sample;
70         std::size_t tx_bytes;
71         std::size_t rx_bytes;
72         float tx_kbps;
73         float rx_kbps;
74
75         Mode mode;
76         float bad_rtt;
77         float bad_loss;
78         float ugly_rtt;
79         float ugly_loss;
80         Uint32 mode_entered;
81         Uint32 mode_reset;
82         Uint32 mode_keep_time;
83         Uint32 mode_step;
84
85 };
86
87 }
88
89 #endif