Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
stream_manager.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 "stream.h"
9
10#include <cstdint>
11#include <functional>
12#include <map>
13#include <memory>
14#include <mutex>
15#include <shared_mutex>
16#include <vector>
17
19{
20
30{
31public:
37 explicit stream_manager(bool is_server, uint64_t initial_max_stream_data = 65536);
38
39 ~stream_manager() = default;
40
41 // Non-copyable
43 auto operator=(const stream_manager&) -> stream_manager& = delete;
44
45 // Non-movable (due to mutex)
48
49 // ========================================================================
50 // Stream Creation
51 // ========================================================================
52
57 [[nodiscard]] auto create_bidirectional_stream() -> Result<uint64_t>;
58
63 [[nodiscard]] auto create_unidirectional_stream() -> Result<uint64_t>;
64
65 // ========================================================================
66 // Stream Access
67 // ========================================================================
68
74 [[nodiscard]] auto get_stream(uint64_t stream_id) -> stream*;
75
81 [[nodiscard]] auto get_stream(uint64_t stream_id) const -> const stream*;
82
91 [[nodiscard]] auto get_or_create_stream(uint64_t stream_id) -> Result<stream*>;
92
97 [[nodiscard]] auto has_stream(uint64_t stream_id) const -> bool;
98
102 [[nodiscard]] auto stream_ids() const -> std::vector<uint64_t>;
103
107 [[nodiscard]] auto stream_count() const -> size_t;
108
109 // ========================================================================
110 // Stream Limits (Transport Parameters)
111 // ========================================================================
112
117 void set_peer_max_streams_bidi(uint64_t max);
118
123 void set_peer_max_streams_uni(uint64_t max);
124
128 [[nodiscard]] auto local_max_streams_bidi() const noexcept -> uint64_t
129 {
131 }
132
136 [[nodiscard]] auto local_max_streams_uni() const noexcept -> uint64_t
137 {
139 }
140
145 void set_local_max_streams_bidi(uint64_t max);
146
151 void set_local_max_streams_uni(uint64_t max);
152
156 [[nodiscard]] auto peer_max_streams_bidi() const noexcept -> uint64_t
157 {
159 }
160
164 [[nodiscard]] auto peer_max_streams_uni() const noexcept -> uint64_t
165 {
167 }
168
169 // ========================================================================
170 // Stream Queries
171 // ========================================================================
172
177 [[nodiscard]] auto streams_with_pending_data() -> std::vector<stream*>;
178
183 [[nodiscard]] auto streams_needing_flow_control_update() -> std::vector<stream*>;
184
189 void for_each_stream(const std::function<void(stream&)>& callback);
190
195 void for_each_stream(const std::function<void(const stream&)>& callback) const;
196
197 // ========================================================================
198 // Stream Lifecycle
199 // ========================================================================
200
205 auto remove_closed_streams() -> size_t;
206
211 void close_all_streams(uint64_t error_code);
212
216 void reset();
217
218 // ========================================================================
219 // Statistics
220 // ========================================================================
221
225 [[nodiscard]] auto local_bidi_streams_count() const noexcept -> uint64_t
226 {
227 return next_local_bidi_id_ / 4;
228 }
229
233 [[nodiscard]] auto local_uni_streams_count() const noexcept -> uint64_t
234 {
235 return (next_local_uni_id_ - 2) / 4;
236 }
237
241 [[nodiscard]] auto peer_bidi_streams_count() const noexcept -> uint64_t
242 {
243 return (highest_peer_bidi_id_ + 4 - peer_bidi_type_) / 4;
244 }
245
249 [[nodiscard]] auto peer_uni_streams_count() const noexcept -> uint64_t
250 {
251 return (highest_peer_uni_id_ + 4 - peer_uni_type_) / 4;
252 }
253
254private:
257
258 // Stream ID generators
259 // Client: bidi=0,4,8..., uni=2,6,10...
260 // Server: bidi=1,5,9..., uni=3,7,11...
263
264 // Peer stream ID tracking
269
270 // Stream limits
275
276 // Active streams
277 mutable std::shared_mutex streams_mutex_;
278 std::map<uint64_t, std::unique_ptr<stream>> streams_;
279
280 // Internal helpers
281 [[nodiscard]] auto validate_stream_id(uint64_t stream_id) const -> VoidResult;
282 [[nodiscard]] auto is_local_stream(uint64_t stream_id) const noexcept -> bool;
283 [[nodiscard]] auto can_create_local_stream(bool bidirectional) const -> bool;
284 [[nodiscard]] auto can_accept_peer_stream(uint64_t stream_id) const -> bool;
285};
286
287} // namespace kcenon::network::protocols::quic
Manages QUIC streams within a connection.
auto remove_closed_streams() -> size_t
Remove closed/terminal streams.
auto stream_ids() const -> std::vector< uint64_t >
Get all active stream IDs.
auto has_stream(uint64_t stream_id) const -> bool
Check if a stream exists.
void close_all_streams(uint64_t error_code)
Close all streams with an error code.
auto peer_max_streams_uni() const noexcept -> uint64_t
Get peer's maximum unidirectional streams (limits our creation)
auto peer_max_streams_bidi() const noexcept -> uint64_t
Get peer's maximum bidirectional streams (limits our creation)
auto get_stream(uint64_t stream_id) -> stream *
Get a stream by ID.
auto local_max_streams_uni() const noexcept -> uint64_t
Get our maximum unidirectional streams (advertised to peer)
auto local_max_streams_bidi() const noexcept -> uint64_t
Get our maximum bidirectional streams (advertised to peer)
auto create_bidirectional_stream() -> Result< uint64_t >
Create a new locally-initiated bidirectional stream.
auto streams_needing_flow_control_update() -> std::vector< stream * >
Get streams that need MAX_STREAM_DATA updates.
auto local_bidi_streams_count() const noexcept -> uint64_t
Get count of locally-initiated bidirectional streams.
stream_manager(const stream_manager &)=delete
stream_manager(bool is_server, uint64_t initial_max_stream_data=65536)
Construct a stream manager.
auto streams_with_pending_data() -> std::vector< stream * >
Get streams with pending data to send.
void set_peer_max_streams_uni(uint64_t max)
Set maximum unidirectional streams peer can initiate.
auto operator=(const stream_manager &) -> stream_manager &=delete
auto create_unidirectional_stream() -> Result< uint64_t >
Create a new locally-initiated unidirectional stream.
auto peer_uni_streams_count() const noexcept -> uint64_t
Get count of peer-initiated unidirectional streams.
void set_local_max_streams_uni(uint64_t max)
Set our maximum unidirectional streams (advertised to peer)
auto can_accept_peer_stream(uint64_t stream_id) const -> bool
void set_local_max_streams_bidi(uint64_t max)
Set our maximum bidirectional streams (advertised to peer)
void reset()
Reset manager state (for connection close)
auto stream_count() const -> size_t
Get number of active streams.
void set_peer_max_streams_bidi(uint64_t max)
Set maximum bidirectional streams peer can initiate.
auto get_or_create_stream(uint64_t stream_id) -> Result< stream * >
Get or create a stream for a peer-initiated stream ID.
void for_each_stream(const std::function< void(stream &)> &callback)
Iterate over all streams.
auto is_local_stream(uint64_t stream_id) const noexcept -> bool
auto can_create_local_stream(bool bidirectional) const -> bool
auto local_uni_streams_count() const noexcept -> uint64_t
Get count of locally-initiated unidirectional streams.
auto peer_bidi_streams_count() const noexcept -> uint64_t
Get count of peer-initiated bidirectional streams.
auto validate_stream_id(uint64_t stream_id) const -> VoidResult
auto operator=(stream_manager &&) -> stream_manager &=delete
std::map< uint64_t, std::unique_ptr< stream > > streams_
QUIC stream implementation (RFC 9000 Sections 2-4)
Definition stream.h:147
Result< std::monostate > VoidResult
Network-specific error and result type definitions.