Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
congestion_controller.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include "loss_detector.h"
8#include "rtt_estimator.h"
9
10#include <chrono>
11#include <cstddef>
12#include <cstdint>
13#include <limits>
14
16{
17
28
32[[nodiscard]] auto congestion_state_to_string(congestion_state state) noexcept
33 -> const char*;
34
43{
44public:
54
60
66 [[nodiscard]] auto can_send(size_t bytes = 0) const noexcept -> bool;
67
72 [[nodiscard]] auto available_window() const noexcept -> size_t;
73
78 auto on_packet_sent(size_t bytes) -> void;
79
85 auto on_packet_acked(const sent_packet& packet,
86 std::chrono::steady_clock::time_point ack_time) -> void;
87
92 auto on_packet_lost(const sent_packet& packet) -> void;
93
100 auto on_congestion_event(std::chrono::steady_clock::time_point sent_time) -> void;
101
113 auto on_ecn_congestion(std::chrono::steady_clock::time_point sent_time) -> void;
114
119 auto on_persistent_congestion(const rtt_estimator& rtt) -> void;
120
125 [[nodiscard]] auto cwnd() const noexcept -> size_t
126 {
127 return cwnd_;
128 }
129
134 [[nodiscard]] auto ssthresh() const noexcept -> size_t
135 {
136 return ssthresh_;
137 }
138
143 [[nodiscard]] auto bytes_in_flight() const noexcept -> size_t
144 {
145 return bytes_in_flight_;
146 }
147
152 [[nodiscard]] auto state() const noexcept -> congestion_state
153 {
154 return state_;
155 }
156
161 [[nodiscard]] auto max_datagram_size() const noexcept -> size_t
162 {
163 return max_datagram_size_;
164 }
165
170 auto set_max_datagram_size(size_t size) -> void;
171
175 auto reset() -> void;
176
177private:
182 [[nodiscard]] auto is_in_recovery(
183 std::chrono::steady_clock::time_point sent_time) const noexcept -> bool;
184
187
189 size_t cwnd_;
190
192 size_t ssthresh_{std::numeric_limits<size_t>::max()};
193
196
199
201 std::chrono::steady_clock::time_point congestion_recovery_start_;
202
205
208
210 static constexpr double kLossReductionFactor = 0.5;
211
213 static constexpr size_t kDefaultMaxDatagramSize = 1200;
214
216 static constexpr size_t kInitialWindowPackets = 10;
217
219 static constexpr size_t kMinimumWindowPackets = 2;
220};
221
222} // namespace kcenon::network::protocols::quic
QUIC congestion control (RFC 9002 Section 7)
auto on_packet_acked(const sent_packet &packet, std::chrono::steady_clock::time_point ack_time) -> void
Handle packet acknowledgment (RFC 9002 Section 7.3)
auto set_max_datagram_size(size_t size) -> void
Set max datagram size.
auto on_packet_lost(const sent_packet &packet) -> void
Handle packet loss (RFC 9002 Section 7.3.2)
auto on_congestion_event(std::chrono::steady_clock::time_point sent_time) -> void
Handle congestion event (RFC 9002 Section 7.3.2)
auto on_persistent_congestion(const rtt_estimator &rtt) -> void
Handle persistent congestion detection (RFC 9002 Section 7.6)
auto ssthresh() const noexcept -> size_t
Get slow start threshold.
auto can_send(size_t bytes=0) const noexcept -> bool
Check if we can send more data.
static constexpr size_t kDefaultMaxDatagramSize
Default max datagram size (QUIC minimum guaranteed MTU)
auto bytes_in_flight() const noexcept -> size_t
Get bytes in flight.
auto available_window() const noexcept -> size_t
Get available congestion window.
auto on_packet_sent(size_t bytes) -> void
Record bytes sent.
auto max_datagram_size() const noexcept -> size_t
Get max datagram size.
static constexpr size_t kMinimumWindowPackets
Minimum window in packets.
auto state() const noexcept -> congestion_state
Get current congestion state.
static constexpr double kLossReductionFactor
Loss reduction factor (RFC 9002 Section 7.3.2)
auto cwnd() const noexcept -> size_t
Get current congestion window.
auto reset() -> void
Reset congestion controller to initial state.
auto on_ecn_congestion(std::chrono::steady_clock::time_point sent_time) -> void
Handle ECN congestion signal (RFC 9002 Section 7.1)
auto is_in_recovery(std::chrono::steady_clock::time_point sent_time) const noexcept -> bool
Check if currently in recovery period.
static constexpr size_t kInitialWindowPackets
Number of datagrams for initial window.
std::chrono::steady_clock::time_point congestion_recovery_start_
Start of the current congestion recovery period.
RTT estimation for QUIC (RFC 9002 Section 5)
congestion_state
States of the congestion controller (RFC 9002 Section 7)
@ recovery
Congestion recovery after loss.
auto congestion_state_to_string(congestion_state state) noexcept -> const char *
Convert congestion state to string.
Information about a sent packet for loss detection (RFC 9002 Section A.1.1)