Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
messaging_udp_server.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 <string>
19#include <string_view>
20#include <vector>
21
22#include <asio.hpp>
23
30
32{
33 class udp_socket;
34}
35
37{
93 class messaging_udp_server
94 : public std::enable_shared_from_this<messaging_udp_server>
95 , public interfaces::i_udp_server
96 {
97 public:
99 using receive_callback_t = std::function<void(const std::vector<uint8_t>&,
100 const asio::ip::udp::endpoint&)>;
102 using error_callback_t = std::function<void(std::error_code)>;
103
108 explicit messaging_udp_server(std::string_view server_id);
109
113 ~messaging_udp_server() noexcept override;
114
115 // Non-copyable, non-movable
117 messaging_udp_server& operator=(const messaging_udp_server&) = delete;
120
121 // ========================================================================
122 // Lifecycle Management
123 // ========================================================================
124
133 [[nodiscard]] auto start_server(uint16_t port) -> VoidResult;
134
140 [[nodiscard]] auto stop_server() -> VoidResult;
141
146 [[nodiscard]] auto server_id() const -> const std::string&;
147
148 // ========================================================================
149 // i_network_component interface implementation
150 // ========================================================================
151
158 [[nodiscard]] auto is_running() const -> bool override;
159
165 auto wait_for_stop() -> void override;
166
167 // ========================================================================
168 // i_udp_server interface implementation
169 // ========================================================================
170
178 [[nodiscard]] auto start(uint16_t port) -> VoidResult override;
179
186 [[nodiscard]] auto stop() -> VoidResult override;
187
197 [[nodiscard]] auto send_to(
198 const interfaces::i_udp_server::endpoint_info& endpoint,
199 std::vector<uint8_t>&& data,
200 interfaces::i_udp_server::send_callback_t handler = nullptr) -> VoidResult override;
201
208 auto set_receive_callback(interfaces::i_udp_server::receive_callback_t callback) -> void override;
209
217 auto set_receive_callback(receive_callback_t callback) -> void;
218
226 auto set_error_callback(error_callback_t callback) -> void override;
227
228 private:
229 // =====================================================================
230 // Internal Implementation Methods
231 // =====================================================================
232
240 auto do_start_impl(uint16_t port) -> VoidResult;
241
248 auto do_stop_impl() -> VoidResult;
249
250 // =====================================================================
251 // Internal Callback Helpers
252 // =====================================================================
253
259 auto invoke_receive_callback(const std::vector<uint8_t>& data,
260 const asio::ip::udp::endpoint& endpoint) -> void;
261
266 auto invoke_error_callback(std::error_code ec) -> void;
267
272 [[nodiscard]] auto get_receive_callback() const -> receive_callback_t;
273
278 [[nodiscard]] auto get_error_callback() const -> error_callback_t;
279
282
284 using callbacks_t = utils::callback_manager<
287 >;
288
289 // =====================================================================
290 // Member Variables
291 // =====================================================================
292
293 std::string server_id_;
294 utils::lifecycle_manager lifecycle_;
297 std::unique_ptr<asio::io_context> io_context_;
298 std::shared_ptr<internal::udp_socket> socket_;
300 std::shared_ptr<integration::thread_pool_interface> thread_pool_;
301 std::future<void> io_context_future_;
302 };
303
304} // namespace kcenon::network::core
Thread-safe callback registration and invocation manager.
A UDP server that receives datagrams and routes them based on sender endpoint.
Definition udp.cppm:219
auto start_server(uint16_t port) -> VoidResult
Starts the server on the specified port.
auto stop_server() -> VoidResult
Stops the server and releases all resources.
auto do_start_impl(uint16_t port) -> VoidResult
UDP-specific implementation of server start.
auto is_running() const -> bool override
Checks if the server is currently running.
auto get_receive_callback() const -> receive_callback_t
Gets a copy of the receive callback.
~messaging_udp_server() noexcept override
Destructor. If the server is still running, stop_server() is invoked.
auto invoke_error_callback(std::error_code ec) -> void
Invokes the error callback with the given error code.
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 stop() -> VoidResult override
Stops the UDP server.
auto start(uint16_t port) -> VoidResult override
Starts the UDP server on the specified port.
auto send_to(const interfaces::i_udp_server::endpoint_info &endpoint, std::vector< uint8_t > &&data, interfaces::i_udp_server::send_callback_t handler=nullptr) -> VoidResult override
Sends a datagram to the specified endpoint.
auto get_error_callback() const -> error_callback_t
Gets a copy of the error callback.
messaging_udp_server(std::string_view server_id)
Constructs a messaging_udp_server with an identifier.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
auto wait_for_stop() -> void override
Blocks until stop_server() is called.
std::unique_ptr< asio::io_context > io_context_
auto set_receive_callback(interfaces::i_udp_server::receive_callback_t callback) -> void override
Sets the callback for received datagrams (interface version).
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 do_stop_impl() -> VoidResult
UDP-specific implementation of server stop.
std::shared_ptr< internal::udp_socket > socket_
std::shared_ptr< integration::thread_pool_interface > thread_pool_
auto server_id() const -> const std::string &
Returns the server identifier.
std::function< void(std::error_code, std::size_t)> send_callback_t
Callback type for send completion.
Component lifecycle management (start, stop, restart).
udp_server_callback
Callback indices for messaging_udp_server.
Network-specific error and result type definitions.
Endpoint information for UDP datagrams.
Thread system integration interface for network_system.