|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
A lightweight wrapper around asio::ip::udp::socket, enabling asynchronous datagram operations.
More...
#include <udp_socket.h>


Public Member Functions | |
| udp_socket (asio::ip::udp::socket socket) | |
Constructs a udp_socket by taking ownership of a moved socket. | |
| ~udp_socket ()=default | |
| Default destructor (no special cleanup needed). | |
| 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 | set_error_callback (std::function< void(std::error_code)> callback) -> void |
| Sets a callback to handle socket errors (e.g., receive/send failures). | |
| auto | start_receive () -> void |
| Begins the continuous asynchronous receive loop. | |
| auto | stop_receive () -> void |
| Stops the receive loop to prevent further async operations. | |
| 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 | socket () -> asio::ip::udp::socket & |
Provides direct access to the underlying asio::ip::udp::socket in case advanced operations are needed. | |
| auto | close () -> void |
| Safely closes the socket and stops all async operations. | |
| auto | is_closed () const -> bool |
| Checks if the socket has been closed. | |
Private Member Functions | |
| auto | do_receive () -> void |
Internal function to handle the receive logic with async_receive_from(). | |
Private Attributes | |
| asio::ip::udp::socket | socket_ |
| std::array< uint8_t, 65536 > | read_buffer_ |
| asio::ip::udp::endpoint | sender_endpoint_ |
| std::mutex | callback_mutex_ |
| std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> | receive_callback_ |
| std::function< void(std::error_code)> | error_callback_ |
| std::atomic< bool > | is_receiving_ {false} |
| std::atomic< bool > | is_closed_ {false} |
A lightweight wrapper around asio::ip::udp::socket, enabling asynchronous datagram operations.
socket_ (from ASIO) for UDP communication.set_receive_callback() to handle inbound datagrams along with sender endpoint information.set_error_callback() for error handling.start_receive() begins an ongoing loop of async_receive_from().async_send_to() performs an asynchronous send to a specified endpoint.Definition at line 46 of file udp_socket.h.
| kcenon::network::internal::udp_socket::udp_socket | ( | asio::ip::udp::socket | socket | ) |
Constructs a udp_socket by taking ownership of a moved socket.
| socket | An asio::ip::udp::socket that must be open or at least valid. |
After construction, you can call start_receive() to begin receiving datagrams. For sending, call async_send_to().
Definition at line 12 of file udp_socket.cpp.
|
default |
Default destructor (no special cleanup needed).
| auto kcenon::network::internal::udp_socket::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.
| data | The buffer to send over UDP (moved for efficiency). |
| endpoint | The target asio::ip::udp::endpoint to send to. |
| handler | A completion handler with signature void(std::error_code, std::size_t) invoked upon completion. |
The handler receives:
ec : the std::error_code from the send operation,bytes_transferred : how many bytes were actually sent.Definition at line 123 of file udp_socket.cpp.
| auto kcenon::network::internal::udp_socket::close | ( | ) | -> void |
Safely closes the socket and stops all async operations.
This method atomically sets the closed flag before closing the socket, preventing data races between the close operation and async receive operations. Thread-safe with respect to concurrent async operations.
Definition at line 46 of file udp_socket.cpp.
|
private |
Internal function to handle the receive logic with async_receive_from().
Upon success, it calls receive_callback_ if set with both data and sender endpoint, then schedules another receive. On error, it calls error_callback_ if available.
Definition at line 63 of file udp_socket.cpp.
|
nodiscard |
Checks if the socket has been closed.
Definition at line 58 of file udp_socket.cpp.
References is_closed_.
| auto kcenon::network::internal::udp_socket::set_error_callback | ( | std::function< void(std::error_code)> | callback | ) | -> void |
Sets a callback to handle socket errors (e.g., receive/send failures).
| callback | A function with signature void(std::error_code), invoked when any asynchronous operation fails. |
If no callback is set, errors are not explicitly handled here (beyond stopping receives).
Definition at line 26 of file udp_socket.cpp.
| auto kcenon::network::internal::udp_socket::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.
| callback | A function with signature void(const std::vector<uint8_t>&, const asio::ip::udp::endpoint&), called whenever a datagram is successfully received. The first parameter is the data, the second is the sender's endpoint. |
If no callback is set, received data is effectively discarded.
Definition at line 18 of file udp_socket.cpp.
|
inline |
Provides direct access to the underlying asio::ip::udp::socket in case advanced operations are needed.
asio::ip::udp::socket. Definition at line 137 of file udp_socket.h.
References socket_.
| auto kcenon::network::internal::udp_socket::start_receive | ( | ) | -> void |
Begins the continuous asynchronous receive loop.
Once called, the class repeatedly calls async_receive_from(). If an error occurs, error_callback_ is triggered, stopping further receives.
Definition at line 33 of file udp_socket.cpp.
| auto kcenon::network::internal::udp_socket::stop_receive | ( | ) | -> void |
Stops the receive loop to prevent further async operations.
Definition at line 40 of file udp_socket.cpp.
|
private |
Protects callback registration and access.
Definition at line 169 of file udp_socket.h.
|
private |
Error callback.
Definition at line 173 of file udp_socket.h.
|
private |
Flag to indicate socket is closed.
Definition at line 176 of file udp_socket.h.
Referenced by is_closed().
|
private |
|
private |
Buffer for receiving datagrams (max UDP size).
Definition at line 166 of file udp_socket.h.
|
private |
Inbound datagram callback.
Definition at line 171 of file udp_socket.h.
|
private |
Stores the sender's endpoint during receive.
Definition at line 167 of file udp_socket.h.
|
private |
The underlying ASIO UDP socket.
Definition at line 164 of file udp_socket.h.
Referenced by socket().