12#include <system_error>
46 class udp_socket :
public std::enable_shared_from_this<udp_socket>
73 std::function<
void(
const std::vector<uint8_t>&,
74 const asio::ip::udp::endpoint&)> callback) -> void;
128 std::vector<uint8_t>&& data,
129 const asio::ip::udp::endpoint& endpoint,
130 std::function<
void(std::error_code, std::size_t)> handler) -> void;
146 auto close() -> void;
152 [[nodiscard]]
auto is_closed() const ->
bool;
170 std::function<
void(const std::vector<uint8_t>&, const asio::ip::udp::endpoint&)>
172 std::function<
void(std::error_code)>
A lightweight wrapper around asio::ip::udp::socket, enabling asynchronous datagram operations.
auto do_receive() -> void
Internal function to handle the receive logic with async_receive_from().
asio::ip::udp::socket socket_
auto async_send_to(std::vector< uint8_t > &&data, const asio::ip::udp::endpoint &endpoint, std::function< void(std::error_code, std::size_t)> handler) -> void
Initiates an asynchronous send of the given data to endpoint.
auto is_closed() const -> bool
Checks if the socket has been closed.
auto socket() -> asio::ip::udp::socket &
Provides direct access to the underlying asio::ip::udp::socket in case advanced operations are needed...
asio::ip::udp::endpoint sender_endpoint_
std::array< uint8_t, 65536 > read_buffer_
auto stop_receive() -> void
Stops the receive loop to prevent further async operations.
~udp_socket()=default
Default destructor (no special cleanup needed).
std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> receive_callback_
auto start_receive() -> void
Begins the continuous asynchronous receive loop.
std::atomic< bool > is_receiving_
udp_socket(asio::ip::udp::socket socket)
Constructs a udp_socket by taking ownership of a moved socket.
std::mutex callback_mutex_
std::atomic< bool > is_closed_
auto set_receive_callback(std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> callback) -> void
Sets a callback to receive inbound datagrams.
auto close() -> void
Safely closes the socket and stops all async operations.
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
Sets a callback to handle socket errors (e.g., receive/send failures).
std::function< void(std::error_code)> error_callback_