Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_messaging_client.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#ifndef KCENON_NETWORK_INTERNAL_CORE_UNIFIED_MESSAGING_CLIENT_H_
7#define KCENON_NETWORK_INTERNAL_CORE_UNIFIED_MESSAGING_CLIENT_H_
8
9#include <atomic>
10#include <concepts>
11#include <functional>
12#include <future>
13#include <memory>
14#include <mutex>
15#include <span>
16#include <string>
17#include <string_view>
18#include <type_traits>
19#include <vector>
20
21#include <asio.hpp>
22
29
30#ifdef BUILD_TLS_SUPPORT
31#include <asio/ssl.hpp>
32#endif
33
35{
36 class tcp_socket;
37#ifdef BUILD_TLS_SUPPORT
38 class secure_tcp_socket;
39#endif
40}
41
43{
44
81template <protocol::Protocol Protocol, policy::TlsPolicy TlsPolicy = policy::no_tls>
82 requires std::same_as<Protocol, protocol::tcp_protocol>
84 : public std::enable_shared_from_this<unified_messaging_client<Protocol, TlsPolicy>>
85{
86public:
89
91 using receive_callback_t = std::function<void(const std::vector<uint8_t>&)>;
93 using connected_callback_t = std::function<void()>;
95 using disconnected_callback_t = std::function<void()>;
97 using error_callback_t = std::function<void(std::error_code)>;
98
103 explicit unified_messaging_client(std::string_view client_id);
104
112 unified_messaging_client(std::string_view client_id, const TlsPolicy& tls_config)
114
119
120 // Non-copyable, non-movable
125
126 // =====================================================================
127 // Lifecycle Management
128 // =====================================================================
129
138 [[nodiscard]] auto start_client(std::string_view host, uint16_t port) -> VoidResult;
139
146 [[nodiscard]] auto stop_client() -> VoidResult;
147
151 auto wait_for_stop() -> void;
152
157 [[nodiscard]] auto is_running() const noexcept -> bool;
158
163 [[nodiscard]] auto is_connected() const noexcept -> bool;
164
169 [[nodiscard]] auto client_id() const -> const std::string&;
170
171 // =====================================================================
172 // Data Transfer
173 // =====================================================================
174
182 [[nodiscard]] auto send_packet(std::vector<uint8_t>&& data) -> VoidResult;
183
184 // =====================================================================
185 // Callback Setters
186 // =====================================================================
187
193
199
205
210 auto set_error_callback(error_callback_t callback) -> void;
211
212private:
213 // =====================================================================
214 // Internal Socket Type Selection
215 // =====================================================================
216
217#ifdef BUILD_TLS_SUPPORT
218 using socket_type = std::conditional_t<
219 is_secure,
222 >;
223#else
225#endif
226
227 // =====================================================================
228 // Internal Implementation Methods
229 // =====================================================================
230
231 auto do_start_impl(std::string_view host, uint16_t port) -> VoidResult;
233 auto do_send_impl(std::vector<uint8_t>&& data) -> VoidResult;
234
235 // =====================================================================
236 // Internal Callback Helpers
237 // =====================================================================
238
239 auto set_connected(bool connected) -> void;
240 auto invoke_receive_callback(const std::vector<uint8_t>& data) -> void;
243 auto invoke_error_callback(std::error_code ec) -> void;
244
245 // =====================================================================
246 // Internal Connection Handlers
247 // =====================================================================
248
249 auto do_connect(std::string_view host, uint16_t port) -> void;
250 auto on_connect(std::error_code ec) -> void;
251 auto on_receive(std::span<const uint8_t> data) -> void;
252 auto on_error(std::error_code ec) -> void;
253 auto on_connection_failed(std::error_code ec) -> void;
254
255#ifdef BUILD_TLS_SUPPORT
256 auto on_handshake_complete(std::error_code ec) -> void
257 requires is_secure;
258#endif
259
260private:
263
270 >;
271
272 // =====================================================================
273 // Member Variables
274 // =====================================================================
275
276 std::string client_id_;
279 std::atomic<bool> is_connected_{false};
280 std::atomic<bool> stop_initiated_{false};
282 std::shared_ptr<asio::io_context> io_context_;
283 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
285 std::future<void> io_context_future_;
287#ifdef BUILD_TLS_SUPPORT
288 std::unique_ptr<asio::ssl::context> ssl_context_;
289 [[no_unique_address]] TlsPolicy tls_config_;
290#endif
291
292 auto get_socket() const -> std::shared_ptr<socket_type>;
293
294 mutable std::mutex socket_mutex_;
295 std::shared_ptr<socket_type> socket_;
297 mutable std::mutex pending_mutex_;
298 std::shared_ptr<asio::ip::tcp::resolver> pending_resolver_;
299 std::shared_ptr<asio::ip::tcp::socket> pending_socket_;
300};
301
302// =====================================================================
303// Type Aliases for Convenience
304// =====================================================================
305
311using tcp_client = unified_messaging_client<protocol::tcp_protocol, policy::no_tls>;
312
313#ifdef BUILD_TLS_SUPPORT
320#endif
321
322} // namespace kcenon::network::core
323
324// Include template implementation
325#include "unified_messaging_client.inl"
326
327#endif // KCENON_NETWORK_INTERNAL_CORE_UNIFIED_MESSAGING_CLIENT_H_
Thread-safe callback registration and invocation manager.
Unified TCP client template parameterized by protocol and TLS policy.
auto invoke_error_callback(std::error_code ec) -> void
auto do_start_impl(std::string_view host, uint16_t port) -> VoidResult
auto set_receive_callback(receive_callback_t callback) -> void
Sets the callback for received data.
auto set_connected(bool connected) -> void
std::shared_ptr< asio::ip::tcp::socket > pending_socket_
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
auto on_receive(std::span< const uint8_t > data) -> void
static constexpr bool is_secure
Indicates whether TLS is enabled for this client.
~unified_messaging_client() noexcept
Destructor; automatically calls stop_client() if still running.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
auto do_connect(std::string_view host, uint16_t port) -> void
unified_messaging_client(std::string_view client_id, const TlsPolicy &tls_config)
Constructs a secure client with TLS configuration.
auto set_error_callback(error_callback_t callback) -> void
Sets the callback for errors.
auto is_connected() const noexcept -> bool
Checks if the client is connected to the server.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
auto set_disconnected_callback(disconnected_callback_t callback) -> void
Sets the callback for disconnection.
auto get_socket() const -> std::shared_ptr< socket_type >
auto stop_client() -> VoidResult
Stops the client and disconnects from the server.
auto is_running() const noexcept -> bool
Checks if the client is currently running.
auto invoke_receive_callback(const std::vector< uint8_t > &data) -> void
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends data to the connected server.
auto set_connected_callback(connected_callback_t callback) -> void
Sets the callback for connection established.
auto wait_for_stop() -> void
Blocks until stop_client() is called.
std::function< void()> connected_callback_t
Callback type for connection established.
unified_messaging_client(std::string_view client_id)
Constructs a client with a given identifier.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
auto start_client(std::string_view host, uint16_t port) -> VoidResult
Starts the client and connects to the specified host and port.
auto do_send_impl(std::vector< uint8_t > &&data) -> VoidResult
auto on_connection_failed(std::error_code ec) -> void
auto client_id() const -> const std::string &
Returns the client identifier.
auto on_error(std::error_code ec) -> void
auto on_connect(std::error_code ec) -> void
std::shared_ptr< asio::ip::tcp::resolver > pending_resolver_
A lightweight wrapper around asio::ssl::stream<asio::ip::tcp::socket>, enabling asynchronous TLS/SSL ...
A lightweight wrapper around asio::ip::tcp::socket, enabling asynchronous read and write operations.
Definition tcp_socket.h:44
Thread-safe lifecycle state management for network components.
Component lifecycle management (start, stop, restart).
constexpr bool is_tls_enabled_v
Helper variable template to check if TLS is enabled at compile time.
Definition tls_policy.h:96
Result< std::monostate > VoidResult
tcp_client_callback
Callback indices for messaging_client and secure_messaging_client.
std::mutex mutex
Protocol tag types for compile-time protocol selection.
Network-specific error and result type definitions.
Policy-based TLS configuration (no_tls, require_tls, optional_tls).