Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
quic_connection_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
11
12#include <atomic>
13#include <condition_variable>
14#include <deque>
15#include <memory>
16#include <mutex>
17#include <string>
18#include <thread>
19
21
43public:
50 std::string_view connection_id);
51
55 ~quic_connection_adapter() override;
56
57 // Non-copyable, non-movable (contains mutex and thread)
62
63 // =========================================================================
64 // i_transport interface implementation
65 // =========================================================================
66
67 [[nodiscard]] auto send(std::span<const std::byte> data) -> VoidResult override;
68 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
69 [[nodiscard]] auto is_connected() const noexcept -> bool override;
70 [[nodiscard]] auto id() const noexcept -> std::string_view override;
71 [[nodiscard]] auto remote_endpoint() const noexcept -> endpoint_info override;
72 [[nodiscard]] auto local_endpoint() const noexcept -> endpoint_info override;
73
74 // =========================================================================
75 // i_connection interface implementation
76 // =========================================================================
77
78 [[nodiscard]] auto connect(const endpoint_info& endpoint) -> VoidResult override;
79 [[nodiscard]] auto connect(std::string_view url) -> VoidResult override;
80 auto close() noexcept -> void override;
81 auto set_callbacks(connection_callbacks callbacks) -> void override;
82 auto set_options(connection_options options) -> void override;
83 auto set_timeout(std::chrono::milliseconds timeout) -> void override;
84 [[nodiscard]] auto is_connecting() const noexcept -> bool override;
85 auto wait_for_stop() -> void override;
86
87private:
91 auto io_thread_func() -> void;
92
96 auto process_outgoing() -> void;
97
101 auto process_incoming() -> void;
102
106 auto handle_state_change() -> void;
107
111 auto parse_url(std::string_view url) -> std::pair<std::string, uint16_t>;
112
113 std::string connection_id_;
114 protocol::quic::quic_config config_;
115
116 // QUIC connection (owned)
117 std::unique_ptr<protocols::quic::connection> quic_conn_;
118
119 // UDP socket for QUIC transport
120 int socket_fd_{-1};
121
122 // I/O thread
123 std::thread io_thread_;
124 std::atomic<bool> running_{false};
125 std::atomic<bool> stop_requested_{false};
126
127 // Callbacks
128 mutable std::mutex callbacks_mutex_;
130
131 // Endpoint information
132 mutable std::mutex endpoint_mutex_;
135
136 // State
137 std::atomic<bool> is_connecting_{false};
138 std::atomic<bool> is_connected_{false};
140
141 // Stop synchronization
142 mutable std::mutex stop_mutex_;
143 std::condition_variable stop_cv_;
144
145 // Send queue
146 mutable std::mutex send_queue_mutex_;
147 std::deque<std::vector<uint8_t>> send_queue_;
148};
149
150} // namespace kcenon::network::unified::adapters
Adapter that wraps QUIC connection to implement i_connection.
quic_connection_adapter & operator=(const quic_connection_adapter &)=delete
quic_connection_adapter(quic_connection_adapter &&)=delete
auto local_endpoint() const noexcept -> endpoint_info override
Gets the local endpoint information.
auto connect(const endpoint_info &endpoint) -> VoidResult override
Connects to a remote endpoint using host/port.
quic_connection_adapter(const quic_connection_adapter &)=delete
auto is_connected() const noexcept -> bool override
Checks if the transport is currently connected.
auto set_timeout(std::chrono::milliseconds timeout) -> void override
Sets the connection timeout.
auto close() noexcept -> void override
Closes the connection gracefully.
~quic_connection_adapter() override
Destructor ensures proper cleanup.
auto parse_url(std::string_view url) -> std::pair< std::string, uint16_t >
Parse URL to extract host and port.
auto remote_endpoint() const noexcept -> endpoint_info override
Gets the remote endpoint information.
auto is_connecting() const noexcept -> bool override
Checks if the connection is in the process of connecting.
std::unique_ptr< protocols::quic::connection > quic_conn_
quic_connection_adapter & operator=(quic_connection_adapter &&)=delete
auto io_thread_func() -> void
I/O processing thread function.
auto set_options(connection_options options) -> void override
Sets connection options.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
auto handle_state_change() -> void
Handle connection state changes.
auto set_callbacks(connection_callbacks callbacks) -> void override
Sets all connection callbacks at once.
auto send(std::span< const std::byte > data) -> VoidResult override
Sends raw data to the remote endpoint.
quic_connection_adapter(const protocol::quic::quic_config &config, std::string_view connection_id)
Constructs an adapter with QUIC configuration.
Core interface for active network connections.
tracing_config config
Definition exporters.cpp:29
Core interface for active network connections.
QUIC protocol connection factory and configuration.
Configuration options for QUIC connections.
Definition quic.h:45
Callback functions for connection events.
Definition types.h:154
Configuration options for connections.
Definition types.h:198
Network endpoint information (host/port or URL)
Definition types.h:56