Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
secure_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
14#include <atomic>
15#include <functional>
16#include <future>
17#include <memory>
18#include <mutex>
19#include <string>
20#include <string_view>
21#include <vector>
22
23#include <asio.hpp>
24#include <asio/ssl.hpp>
25
32
34{
35
71 class secure_messaging_client
72 : public std::enable_shared_from_this<secure_messaging_client>
73 {
74 public:
76 using receive_callback_t = std::function<void(const std::vector<uint8_t>&)>;
78 using connected_callback_t = std::function<void()>;
80 using disconnected_callback_t = std::function<void()>;
82 using error_callback_t = std::function<void(std::error_code)>;
83
89 explicit secure_messaging_client(std::string_view client_id,
90 bool verify_cert = true);
91
95 ~secure_messaging_client() noexcept;
96
97 // Non-copyable, non-movable
102
103 // =====================================================================
104 // Lifecycle Management
105 // =====================================================================
106
116 [[nodiscard]] auto start_client(std::string_view host, unsigned short port) -> VoidResult;
117
123 [[nodiscard]] auto stop_client() -> VoidResult;
124
128 auto wait_for_stop() -> void;
129
134 [[nodiscard]] auto is_running() const noexcept -> bool;
135
140 [[nodiscard]] auto is_connected() const noexcept -> bool;
141
146 [[nodiscard]] auto client_id() const -> const std::string&;
147
148 // =====================================================================
149 // Data Transfer
150 // =====================================================================
151
159 [[nodiscard]] auto send_packet(std::vector<uint8_t>&& data) -> VoidResult;
160
161 // =====================================================================
162 // Callback Setters
163 // =====================================================================
164
169 auto set_receive_callback(receive_callback_t callback) -> void;
170
175 auto set_connected_callback(connected_callback_t callback) -> void;
176
182
187 auto set_error_callback(error_callback_t callback) -> void;
188
189 private:
190 // =====================================================================
191 // Internal Implementation Methods
192 // =====================================================================
193
203 auto do_start_impl(std::string_view host, unsigned short port) -> VoidResult;
204
209 auto do_stop_impl() -> VoidResult;
210
216 auto do_send_impl(std::vector<uint8_t>&& data) -> VoidResult;
217
218 // =====================================================================
219 // Internal Callback Helpers
220 // =====================================================================
221
226 auto set_connected(bool connected) -> void;
227
231 auto invoke_receive_callback(const std::vector<uint8_t>& data) -> void;
232
236 auto invoke_connected_callback() -> void;
237
241 auto invoke_disconnected_callback() -> void;
242
246 auto invoke_error_callback(std::error_code ec) -> void;
247
248 // =====================================================================
249 // Internal Socket Handlers
250 // =====================================================================
251
256 auto on_receive(const std::vector<uint8_t>& data) -> void;
257
262 auto on_error(std::error_code ec) -> void;
263
264 private:
267
269 using callbacks_t = utils::callback_manager<
274 >;
275
276 // =====================================================================
277 // Member Variables
278 // =====================================================================
279
280 std::string client_id_;
281 utils::lifecycle_manager lifecycle_;
283 std::atomic<bool> is_connected_{false};
285 std::unique_ptr<asio::io_context>
287 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
290 std::shared_ptr<integration::thread_pool_interface>
292 std::future<void>
295 std::unique_ptr<asio::ssl::context>
298 std::shared_ptr<internal::secure_tcp_socket>
301 bool verify_cert_{true};
302 };
303
304} // namespace kcenon::network::core
Thread-safe callback registration and invocation manager.
A secure client for establishing TLS/SSL encrypted TCP connections to a server.
Definition ssl.cppm:144
std::function< void()> connected_callback_t
Callback type for connection established.
~secure_messaging_client() noexcept
Destructor. Calls stop_client() if still connected.
auto on_receive(const std::vector< uint8_t > &data) -> void
Callback for when encrypted data arrives from the server.
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends data to the connected server.
auto invoke_disconnected_callback() -> void
Invokes the disconnected callback.
auto client_id() const -> const std::string &
Returns the client identifier.
auto invoke_connected_callback() -> void
Invokes the connected callback.
auto set_receive_callback(receive_callback_t callback) -> void
Sets the callback for received data.
auto set_connected_callback(connected_callback_t callback) -> void
Sets the callback for connection established.
auto on_error(std::error_code ec) -> void
Callback for handling socket errors.
auto stop_client() -> VoidResult
Stops the client and disconnects from the server.
auto do_stop_impl() -> VoidResult
Secure TCP-specific implementation of client stop.
std::shared_ptr< internal::secure_tcp_socket > socket_
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
auto do_send_impl(std::vector< uint8_t > &&data) -> VoidResult
Secure TCP-specific implementation of data send.
auto invoke_receive_callback(const std::vector< uint8_t > &data) -> void
Invokes the receive callback.
auto wait_for_stop() -> void
Blocks until stop_client() is called.
secure_messaging_client(std::string_view client_id, bool verify_cert=true)
Constructs a secure messaging client.
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
auto is_running() const noexcept -> bool
Checks if the client is currently running.
auto set_error_callback(error_callback_t callback) -> void
Sets the callback for errors.
std::shared_ptr< integration::thread_pool_interface > thread_pool_
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
auto start_client(std::string_view host, unsigned short port) -> VoidResult
Starts the client and connects to the specified host and port.
auto set_connected(bool connected) -> void
Sets the connected state.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
std::unique_ptr< asio::ssl::context > ssl_context_
auto is_connected() const noexcept -> bool
Checks if the client is connected to the server.
auto invoke_error_callback(std::error_code ec) -> void
Invokes the error callback.
auto do_start_impl(std::string_view host, unsigned short port) -> VoidResult
Secure TCP-specific implementation of client start.
auto set_disconnected_callback(disconnected_callback_t callback) -> void
Sets the callback for disconnection.
Component lifecycle management (start, stop, restart).
tcp_client_callback
Callback indices for messaging_client and secure_messaging_client.
Network-specific error and result type definitions.
Thread system integration interface for network_system.