Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
quic_client_adapter.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
8
9#include <atomic>
10#include <chrono>
11#include <memory>
12#include <mutex>
13#include <optional>
14#include <string>
15#include <vector>
16
17namespace kcenon::network::core {
18 class messaging_quic_client;
19}
20
22
52public:
57 explicit quic_client_adapter(std::string_view client_id);
58
62 ~quic_client_adapter() override;
63
64 // Non-copyable, non-movable
69
70 // =========================================================================
71 // QUIC-specific Configuration
72 // =========================================================================
73
80 auto set_alpn_protocols(const std::vector<std::string>& protocols) -> void;
81
86 auto set_ca_cert_path(std::string_view path) -> void;
87
93 auto set_client_cert(std::string_view cert_path, std::string_view key_path) -> void;
94
99 auto set_verify_server(bool verify) -> void;
100
105 auto set_max_idle_timeout(uint64_t timeout_ms) -> void;
106
107 // =========================================================================
108 // i_network_component interface implementation
109 // =========================================================================
110
111 [[nodiscard]] auto is_running() const -> bool override;
112 auto wait_for_stop() -> void override;
113
114 // =========================================================================
115 // i_protocol_client interface implementation
116 // =========================================================================
117
118 [[nodiscard]] auto start(std::string_view host, uint16_t port) -> VoidResult override;
119 [[nodiscard]] auto stop() -> VoidResult override;
120 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
121 [[nodiscard]] auto is_connected() const -> bool override;
122
123 auto set_observer(std::shared_ptr<interfaces::connection_observer> observer) -> void override;
124
125 auto set_receive_callback(receive_callback_t callback) -> void override;
126 auto set_connected_callback(connected_callback_t callback) -> void override;
127 auto set_disconnected_callback(disconnected_callback_t callback) -> void override;
128 auto set_error_callback(error_callback_t callback) -> void override;
129
130private:
134 auto setup_internal_callbacks() -> void;
135
136 std::string client_id_;
137 std::shared_ptr<core::messaging_quic_client> client_;
138
139 // QUIC-specific configuration
140 std::vector<std::string> alpn_protocols_;
141 std::optional<std::string> ca_cert_path_;
142 std::optional<std::string> client_cert_path_;
143 std::optional<std::string> client_key_path_;
144 bool verify_server_{true};
145 uint64_t max_idle_timeout_ms_{30000};
146
147 mutable std::mutex callbacks_mutex_;
148 std::shared_ptr<interfaces::connection_observer> observer_;
153};
154
155} // namespace kcenon::network::internal::adapters
Unified interface for all protocol client implementations.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
std::function< void()> connected_callback_t
Callback type for connection established.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
Adapter that wraps messaging_quic_client to implement i_protocol_client.
auto is_connected() const -> bool override
Checks if the client is connected to the server.
quic_client_adapter & operator=(quic_client_adapter &&)=delete
std::shared_ptr< interfaces::connection_observer > observer_
auto set_max_idle_timeout(uint64_t timeout_ms) -> void
Sets max idle timeout in milliseconds.
auto set_connected_callback(connected_callback_t callback) -> void override
Sets the callback for connection established.
auto set_verify_server(bool verify) -> void
Sets whether to verify server certificate.
quic_client_adapter(std::string_view client_id)
Constructs an adapter with a unique client ID.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
auto stop() -> VoidResult override
Stops the client and closes the connection.
quic_client_adapter & operator=(const quic_client_adapter &)=delete
auto set_client_cert(std::string_view cert_path, std::string_view key_path) -> void
Sets client certificate path for mutual TLS.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
auto is_running() const -> bool override
Checks if the component is currently running.
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge QUIC callbacks to i_protocol_client callbacks.
auto set_observer(std::shared_ptr< interfaces::connection_observer > observer) -> void override
Sets the connection observer for unified event handling.
auto set_alpn_protocols(const std::vector< std::string > &protocols) -> void
Sets the ALPN protocols for negotiation.
auto set_disconnected_callback(disconnected_callback_t callback) -> void override
Sets the callback for disconnection.
quic_client_adapter(const quic_client_adapter &)=delete
std::shared_ptr< core::messaging_quic_client > client_
~quic_client_adapter() override
Destructor ensures proper cleanup.
auto set_ca_cert_path(std::string_view path) -> void
Sets CA certificate path for server verification.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
auto start(std::string_view host, uint16_t port) -> VoidResult override
Starts the client and connects to the specified server.
Protocol-specific client interface extending i_client.