Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
messaging_udp_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
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
31
33{
34 class udp_socket;
35}
36
38{
94 class messaging_udp_client
95 : public std::enable_shared_from_this<messaging_udp_client>
96 , public interfaces::i_udp_client
97 {
98 public:
100 using receive_callback_t = std::function<void(const std::vector<uint8_t>&,
101 const asio::ip::udp::endpoint&)>;
103 using error_callback_t = std::function<void(std::error_code)>;
104
109 explicit messaging_udp_client(std::string_view client_id);
110
114 ~messaging_udp_client() noexcept override;
115
116 // Non-copyable, non-movable
118 messaging_udp_client& operator=(const messaging_udp_client&) = delete;
121
122 // ========================================================================
123 // Lifecycle Management
124 // ========================================================================
125
135 [[nodiscard]] auto start_client(std::string_view host, uint16_t port) -> VoidResult;
136
142 [[nodiscard]] auto stop_client() -> VoidResult;
143
148 [[nodiscard]] auto client_id() const -> const std::string&;
149
150 // ========================================================================
151 // i_network_component interface implementation
152 // ========================================================================
153
160 [[nodiscard]] auto is_running() const -> bool override;
161
167 auto wait_for_stop() -> void override;
168
169 // ========================================================================
170 // i_udp_client interface implementation
171 // ========================================================================
172
181 [[nodiscard]] auto start(std::string_view host, uint16_t port) -> VoidResult override;
182
189 [[nodiscard]] auto stop() -> VoidResult override;
190
199 [[nodiscard]] auto send(
200 std::vector<uint8_t>&& data,
201 interfaces::i_udp_client::send_callback_t handler = nullptr) -> VoidResult override;
202
211 [[nodiscard]] auto set_target(std::string_view host, uint16_t port) -> VoidResult override;
212
219 auto set_receive_callback(interfaces::i_udp_client::receive_callback_t callback) -> void override;
220
228 auto set_receive_callback(receive_callback_t callback) -> void;
229
237 auto set_error_callback(error_callback_t callback) -> void override;
238
239 private:
240 // =====================================================================
241 // Internal Implementation Methods
242 // =====================================================================
243
252 auto do_start_impl(std::string_view host, uint16_t port) -> VoidResult;
253
260 auto do_stop_impl() -> VoidResult;
261
262 // =====================================================================
263 // Internal Callback Helpers
264 // =====================================================================
265
271 auto invoke_receive_callback(const std::vector<uint8_t>& data,
272 const asio::ip::udp::endpoint& endpoint) -> void;
273
278 auto invoke_error_callback(std::error_code ec) -> void;
279
284 [[nodiscard]] auto get_receive_callback() const -> receive_callback_t;
285
290 [[nodiscard]] auto get_error_callback() const -> error_callback_t;
291
294
296 using callbacks_t = utils::callback_manager<
299 >;
300
301 // =====================================================================
302 // Member Variables
303 // =====================================================================
304
305 std::string client_id_;
306 utils::lifecycle_manager lifecycle_;
309 std::unique_ptr<asio::io_context> io_context_;
310 std::shared_ptr<internal::udp_socket> socket_;
312 std::shared_ptr<integration::thread_pool_interface> thread_pool_;
313 std::future<void> io_context_future_;
316 asio::ip::udp::endpoint target_endpoint_;
318 mutable std::mutex socket_mutex_;
319 };
320
321} // namespace kcenon::network::core
Thread-safe callback registration and invocation manager.
A UDP client that sends datagrams to a target endpoint and can receive responses.
Definition udp.cppm:80
auto invoke_receive_callback(const std::vector< uint8_t > &data, const asio::ip::udp::endpoint &endpoint) -> void
Invokes the receive callback with the given data and endpoint.
auto client_id() const -> const std::string &
Returns the client identifier.
auto send(std::vector< uint8_t > &&data, interfaces::i_udp_client::send_callback_t handler=nullptr) -> VoidResult override
Sends a datagram to the configured target endpoint.
auto wait_for_stop() -> void override
Blocks until stop_client() is called.
std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> receive_callback_t
Callback type for received datagrams with sender endpoint.
auto invoke_error_callback(std::error_code ec) -> void
Invokes the error callback with the given error code.
auto set_target(std::string_view host, uint16_t port) -> VoidResult override
Changes the target endpoint for future sends.
auto stop_client() -> VoidResult
Stops the client and releases all resources.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
auto do_stop_impl() -> VoidResult
UDP-specific implementation of client stop.
std::unique_ptr< asio::io_context > io_context_
auto get_receive_callback() const -> receive_callback_t
Gets a copy of the receive callback.
messaging_udp_client(std::string_view client_id)
Constructs a messaging_udp_client with an identifier.
~messaging_udp_client() noexcept override
Destructor. Automatically calls stop_client() if the client is still running.
auto stop() -> VoidResult override
Stops the UDP client.
auto do_start_impl(std::string_view host, uint16_t port) -> VoidResult
UDP-specific implementation of client start.
auto start(std::string_view host, uint16_t port) -> VoidResult override
Starts the UDP client targeting the specified endpoint.
auto is_running() const -> bool override
Checks if the client is currently running.
auto start_client(std::string_view host, uint16_t port) -> VoidResult
Starts the client by resolving target host and port.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
auto get_error_callback() const -> error_callback_t
Gets a copy of the error callback.
std::shared_ptr< integration::thread_pool_interface > thread_pool_
auto set_receive_callback(interfaces::i_udp_client::receive_callback_t callback) -> void override
Sets the callback for received datagrams (interface version).
std::shared_ptr< internal::udp_socket > socket_
std::function< void(std::error_code, std::size_t)> send_callback_t
Callback type for send completion.
Component lifecycle management (start, stop, restart).
udp_client_callback
Callback indices for messaging_udp_client.
std::mutex mutex
Network-specific error and result type definitions.
Thread system integration interface for network_system.