6#ifndef KCENON_NETWORK_INTERNAL_CORE_MESSAGING_CLIENT_H_
7#define KCENON_NETWORK_INTERNAL_CORE_MESSAGING_CLIENT_H_
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
133 [[nodiscard]] auto
start(std::string_view host, uint16_t port) ->
VoidResult override;
146 [[nodiscard]] auto
send(std::vector<uint8_t>&& data) ->
VoidResult override;
152 [[nodiscard]] auto
is_connected() const ->
bool override;
158 auto
set_observer(std::shared_ptr<interfaces::connection_observer> observer) ->
void override;
189 [[nodiscard]] auto
client_id() const -> const std::
string&;
222 [[nodiscard]] static constexpr auto
component_name() noexcept -> std::string_view
296 auto do_connect(std::string_view host,
unsigned short port) -> void;
320 auto on_receive(std::span<const uint8_t> data) -> void;
328 auto on_error(std::error_code ec) -> void;
360 std::shared_ptr<interfaces::connection_observer>
observer_;
362 std::shared_ptr<asio::io_context>
364 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
369 auto get_socket() const -> std::shared_ptr<internal::tcp_socket>;
372 std::shared_ptr<internal::tcp_socket>
Thread-safe callback registration and invocation manager.
A basic TCP client that connects to a remote host, sends/receives data using asynchronous operations,...
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::future< void > io_context_future_
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_
std::atomic< bool > is_connected_
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.
Network-specific error and result type definitions.
Base class for components with start/stop lifecycle.