Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
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_MESSAGING_CLIENT_H_
7#define KCENON_NETWORK_INTERNAL_CORE_MESSAGING_CLIENT_H_
8
17#include <atomic>
18#include <functional>
19#include <future>
20#include <memory>
21#include <mutex>
22#include <span>
23#include <string>
24#include <string_view>
25#include <type_traits>
26
27#include <asio.hpp>
28
37
38// Use nested namespace definition in C++17
40{
41
67 class messaging_client
68 : public std::enable_shared_from_this<messaging_client>
69 , public utils::startable_base<messaging_client>
70 , public interfaces::i_protocol_client
71 {
72 friend class utils::startable_base<messaging_client>;
73
74 public:
75 // =====================================================================
76 // INetworkComponent Interface Implementation (via IProtocolClient)
77 // =====================================================================
78
83 [[nodiscard]] auto is_running() const -> bool override
84 {
86 }
87
91 auto wait_for_stop() -> void override
92 {
94 }
96 using receive_callback_t = std::function<void(const std::vector<uint8_t>&)>;
98 using connected_callback_t = std::function<void()>;
100 using disconnected_callback_t = std::function<void()>;
102 using error_callback_t = std::function<void(std::error_code)>;
103
109 explicit messaging_client(std::string_view client_id);
110
115 ~messaging_client() noexcept;
116
117 // Non-copyable, non-movable
119 messaging_client& operator=(const messaging_client&) = delete;
121 messaging_client& operator=(messaging_client&&) = delete;
122
123 // =====================================================================
124 // IProtocolClient Interface Implementation
125 // =====================================================================
126
133 [[nodiscard]] auto start(std::string_view host, uint16_t port) -> VoidResult override;
134
139 [[nodiscard]] auto stop() -> VoidResult override;
140
146 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
147
152 [[nodiscard]] auto is_connected() const -> bool override;
153
158 auto set_observer(std::shared_ptr<interfaces::connection_observer> observer) -> void override;
159
160 // =====================================================================
161 // Lifecycle Management (Legacy API)
162 // =====================================================================
163
172 [[nodiscard]] auto start_client(std::string_view host, unsigned short port) -> VoidResult;
173
180 [[nodiscard]] auto stop_client() -> VoidResult;
181
182 // Note: wait_for_stop() and is_running() are inherited from startable_base
183 // Note: is_connected() is declared in IProtocolClient interface section
184
189 [[nodiscard]] auto client_id() const -> const std::string&;
190
191 // =====================================================================
192 // Data Transfer
193 // =====================================================================
194
202 [[nodiscard]] auto send_packet(std::vector<uint8_t>&& data) -> VoidResult;
203
204 // =====================================================================
205 // Callback Setters (IProtocolClient interface)
206 // =====================================================================
207
208 auto set_receive_callback(receive_callback_t callback) -> void override;
209 auto set_connected_callback(connected_callback_t callback) -> void override;
210 auto set_disconnected_callback(disconnected_callback_t callback) -> void override;
211 auto set_error_callback(error_callback_t callback) -> void override;
212
213 private:
214 // =====================================================================
215 // startable_base CRTP interface
216 // =====================================================================
217
222 [[nodiscard]] static constexpr auto component_name() noexcept -> std::string_view
223 {
224 return "Client";
225 }
226
231 auto on_stopped() -> void;
232
233 // =====================================================================
234 // Internal Implementation Methods
235 // =====================================================================
236
243 auto do_start_impl(std::string_view host, unsigned short port) -> VoidResult;
244
249 auto do_stop_impl() -> VoidResult;
250
256 auto do_send_impl(std::vector<uint8_t>&& data) -> VoidResult;
257
258 // =====================================================================
259 // Internal Callback Helpers
260 // =====================================================================
261
266 auto set_connected(bool connected) -> void;
267
271 auto invoke_receive_callback(const std::vector<uint8_t>& data) -> void;
272
276 auto invoke_connected_callback() -> void;
277
281 auto invoke_disconnected_callback() -> void;
282
286 auto invoke_error_callback(std::error_code ec) -> void;
287
288 // =====================================================================
289 // Internal Connection Handlers
290 // =====================================================================
291
296 auto do_connect(std::string_view host, unsigned short port) -> void;
297
302 auto on_connect(std::error_code ec) -> void;
303
320 auto on_receive(std::span<const uint8_t> data) -> void;
321
328 auto on_error(std::error_code ec) -> void;
329
336 auto on_connection_failed(std::error_code ec) -> void;
337
338 private:
341
348 >;
349
350 // =====================================================================
351 // Member Variables
352 // =====================================================================
353
354 std::string client_id_;
355 // Note: lifecycle_ and stop_initiated_ are managed by startable_base
357 std::atomic<bool> is_connected_{false};
360 std::shared_ptr<interfaces::connection_observer> observer_;
361
362 std::shared_ptr<asio::io_context>
364 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
366 std::future<void>
369 auto get_socket() const -> std::shared_ptr<internal::tcp_socket>;
370
371 mutable std::mutex socket_mutex_;
372 std::shared_ptr<internal::tcp_socket>
381 mutable std::mutex pending_mutex_;
382 std::shared_ptr<asio::ip::tcp::resolver> pending_resolver_;
383 std::shared_ptr<asio::ip::tcp::socket> pending_socket_;
384 };
385
386} // namespace kcenon::network::core
387
388#endif // KCENON_NETWORK_INTERNAL_CORE_MESSAGING_CLIENT_H_
Thread-safe callback registration and invocation manager.
A basic TCP client that connects to a remote host, sends/receives data using asynchronous operations,...
Definition tcp.cppm:84
auto set_connected(bool connected) -> void
Sets the connected state.
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
auto do_start_impl(std::string_view host, unsigned short port) -> VoidResult
TCP-specific implementation of client start.
auto client_id() const -> const std::string &
Returns the client identifier.
auto wait_for_stop() -> void override
Waits for the client to stop.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
auto do_send_impl(std::vector< uint8_t > &&data) -> VoidResult
TCP-specific implementation of data send.
auto get_socket() const -> std::shared_ptr< internal::tcp_socket >
std::shared_ptr< asio::io_context > io_context_
auto is_connected() const -> bool override
Checks if the client is connected to the server (IProtocolClient interface).
auto invoke_connected_callback() -> void
Invokes the connected callback.
auto start_client(std::string_view host, unsigned short port) -> VoidResult
Starts the client and connects to the specified host and port.
auto start(std::string_view host, uint16_t port) -> VoidResult override
Starts the client and connects to the specified server (IProtocolClient interface).
auto stop_client() -> VoidResult
Stops the client and disconnects from the server.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
std::shared_ptr< internal::tcp_socket > socket_
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends data to the connected server.
~messaging_client() noexcept
Destructor; automatically calls stop_client() if the client is still running.
std::shared_ptr< asio::ip::tcp::socket > pending_socket_
auto set_observer(std::shared_ptr< interfaces::connection_observer > observer) -> void override
Sets the connection observer for unified event handling.
auto send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the connected server (IProtocolClient interface).
auto invoke_receive_callback(const std::vector< uint8_t > &data) -> void
Invokes the receive callback.
auto set_disconnected_callback(disconnected_callback_t callback) -> void override
Sets the callback for disconnection.
static constexpr auto component_name() noexcept -> std::string_view
Returns the component name for error messages.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
auto on_error(std::error_code ec) -> void
Callback for handling socket errors from tcp_socket.
auto invoke_disconnected_callback() -> void
Invokes the disconnected callback.
std::function< void()> connected_callback_t
Callback type for connection established.
messaging_client(std::string_view client_id)
Constructs a client with a given client_id used for logging or identification.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
std::shared_ptr< asio::ip::tcp::resolver > pending_resolver_
auto on_connection_failed(std::error_code ec) -> void
Handles connection failure during async resolve or connect.
auto on_connect(std::error_code ec) -> void
Callback invoked upon completion of an async connect.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
auto is_running() const -> bool override
Checks if the client is currently running.
std::shared_ptr< interfaces::connection_observer > observer_
Connection observer for unified event handling (IProtocolClient interface)
auto on_stopped() -> void
Called after stop operation completes. Invokes the disconnected callback.
auto stop() -> VoidResult override
Stops the client and closes the connection (IProtocolClient interface).
auto do_stop_impl() -> VoidResult
TCP-specific implementation of client stop.
auto on_receive(std::span< const uint8_t > data) -> void
Callback for receiving data from the tcp_socket.
std::mutex pending_mutex_
Pending connection resources that need explicit cleanup. These are stored as members to allow cancell...
auto do_connect(std::string_view host, unsigned short port) -> void
Internally attempts to resolve and connect to the remote host:port.
auto invoke_error_callback(std::error_code ec) -> void
Invokes the error callback.
auto set_connected_callback(connected_callback_t callback) -> void override
Sets the callback for connection established.
auto is_running() const noexcept -> bool
Checks if the component is currently running.
auto wait_for_stop() -> void
Blocks until stop is called.
Observer interface for connection state change notifications.
Protocol-specific client interface extending i_client.
Unified io_context thread management for network components.
Result< std::monostate > VoidResult
tcp_client_callback
Callback indices for messaging_client and secure_messaging_client.
std::mutex mutex
Network-specific error and result type definitions.
Base class for components with start/stop lifecycle.