Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
quic_session.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
12#pragma once
13
22#include <atomic>
23#include <functional>
24#include <memory>
25#include <mutex>
26#include <string>
27#include <string_view>
28#include <vector>
29
30#include <asio.hpp>
31
32// Enable experimental QUIC API for quic_connection_stats type
33#ifndef NETWORK_USE_EXPERIMENTAL
34#define NETWORK_USE_EXPERIMENTAL
35#endif
36#include "kcenon/network/internal/experimental/quic_client.h"
37#include "kcenon/network/internal/interfaces/i_quic_server.h"
40
42{
43 class quic_socket;
44} // namespace kcenon::network::internal
45
47{
48
70 : public std::enable_shared_from_this<quic_session>
72 {
73 public:
79 quic_session(std::shared_ptr<internal::quic_socket> socket,
80 std::string_view session_id);
81
85 ~quic_session() noexcept;
86
87 // Disable copy
88 quic_session(const quic_session&) = delete;
90
91 // =====================================================================
92 // Session Information
93 // =====================================================================
94
99 [[nodiscard]] auto session_id() const -> const std::string&;
100
105 [[nodiscard]] auto remote_endpoint() const -> asio::ip::udp::endpoint;
106
111 [[nodiscard]] auto is_active() const noexcept -> bool;
112
113 // =====================================================================
114 // Data Transfer (Legacy API)
115 // =====================================================================
116
122 [[nodiscard]] auto send(std::string_view data) -> VoidResult;
123
124 // =====================================================================
125 // Session Management (Legacy API)
126 // =====================================================================
127
133 [[nodiscard]] auto close(uint64_t error_code) -> VoidResult;
134
135 // =====================================================================
136 // Statistics
137 // =====================================================================
138
143 [[nodiscard]] auto stats() const -> core::quic_connection_stats;
144
145 // =====================================================================
146 // Callbacks
147 // =====================================================================
148
154 std::function<void(const std::vector<uint8_t>&)> callback) -> void;
155
161 std::function<void(uint64_t, const std::vector<uint8_t>&, bool)>
162 callback) -> void;
163
168 auto set_close_callback(std::function<void()> callback) -> void;
169
170 // =====================================================================
171 // Internal Methods (for server use)
172 // =====================================================================
173
177 auto start_session() -> void;
178
183 auto handle_packet(std::span<const uint8_t> data) -> void;
184
190 [[nodiscard]] auto matches_connection_id(
191 const protocols::quic::connection_id& conn_id) const -> bool;
192
193 // =====================================================================
194 // i_quic_session interface implementation
195 // =====================================================================
196
203 [[nodiscard]] auto id() const -> std::string_view override {
204 return session_id_;
205 }
206
213 [[nodiscard]] auto is_connected() const -> bool override {
214 return is_active();
215 }
216
224 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
225
231 auto close() -> void override {
232 auto result = close(0);
233 (void)result;
234 }
235
242 [[nodiscard]] auto create_stream() -> Result<uint64_t> override;
243
250 [[nodiscard]] auto create_unidirectional_stream() -> Result<uint64_t> override;
251
261 [[nodiscard]] auto send_on_stream(
262 uint64_t stream_id,
263 std::vector<uint8_t>&& data,
264 bool fin = false) -> VoidResult override;
265
273 [[nodiscard]] auto close_stream(uint64_t stream_id) -> VoidResult override;
274
275 private:
276 // =====================================================================
277 // Internal Callbacks
278 // =====================================================================
279
280 auto on_stream_data(uint64_t stream_id,
281 std::span<const uint8_t> data,
282 bool fin) -> void;
283
284 auto on_error(std::error_code ec) -> void;
285
286 auto on_close(uint64_t error_code, const std::string& reason) -> void;
287
288 // =====================================================================
289 // Member Variables
290 // =====================================================================
291
292 std::string session_id_;
293
294 mutable std::mutex socket_mutex_;
295 std::shared_ptr<internal::quic_socket> socket_;
296
297 std::atomic<bool> is_active_{true};
298
300
301 // Callbacks
302 std::function<void(const std::vector<uint8_t>&)> receive_callback_;
303 std::function<void(uint64_t, const std::vector<uint8_t>&, bool)>
305 std::function<void()> close_callback_;
306
307 mutable std::mutex callback_mutex_;
308 };
309
310} // namespace kcenon::network::session
Interface for a QUIC session on the server side.
Represents a single QUIC client session on the server side.
std::function< void()> close_callback_
auto close() -> void override
Closes the session (interface version).
auto close_stream(uint64_t stream_id) -> VoidResult override
Closes a stream (interface version).
auto matches_connection_id(const protocols::quic::connection_id &conn_id) const -> bool
Check if this session matches a connection ID.
auto set_receive_callback(std::function< void(const std::vector< uint8_t > &)> callback) -> void
Set callback for received data on the default stream.
auto on_error(std::error_code ec) -> void
auto handle_packet(std::span< const uint8_t > data) -> void
Handle incoming packet (called by server).
auto create_stream() -> Result< uint64_t > override
Creates a new bidirectional stream (interface version).
std::function< void(const std::vector< uint8_t > &)> receive_callback_
quic_session(const quic_session &)=delete
auto set_stream_receive_callback(std::function< void(uint64_t, const std::vector< uint8_t > &, bool)> callback) -> void
Set callback for stream data reception.
quic_session(std::shared_ptr< internal::quic_socket > socket, std::string_view session_id)
Constructs a QUIC session with an existing socket.
auto send_on_stream(uint64_t stream_id, std::vector< uint8_t > &&data, bool fin=false) -> VoidResult override
Sends data on a specific stream (interface version).
~quic_session() noexcept
Destructor; closes the session if still active.
std::function< void(uint64_t, const std::vector< uint8_t > &, bool)> stream_receive_callback_
auto create_unidirectional_stream() -> Result< uint64_t > override
Creates a new unidirectional stream (interface version).
auto on_stream_data(uint64_t stream_id, std::span< const uint8_t > data, bool fin) -> void
auto session_id() const -> const std::string &
Get the unique session identifier.
auto is_connected() const -> bool override
Checks if the session is currently connected (interface version).
auto start_session() -> void
Start receiving data (called by server).
auto remote_endpoint() const -> asio::ip::udp::endpoint
Get the remote endpoint address.
auto is_active() const noexcept -> bool
Check if the session is currently active.
auto send(std::string_view data) -> VoidResult
Send string data on the default stream.
quic_session & operator=(const quic_session &)=delete
std::shared_ptr< internal::quic_socket > socket_
auto stats() const -> core::quic_connection_stats
Get connection statistics.
auto set_close_callback(std::function< void()> callback) -> void
Set callback when session closes.
auto on_close(uint64_t error_code, const std::string &reason) -> void
QUIC connection identifier type.
Network-specific error and result type definitions.