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