Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
udp_socket.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
7#include <asio.hpp>
8#include <functional>
9#include <memory>
10#include <vector>
11#include <array>
12#include <system_error>
13#include <mutex>
14#include <atomic>
15
17
19{
46 class udp_socket : public std::enable_shared_from_this<udp_socket>
47 {
48 public:
56 udp_socket(asio::ip::udp::socket socket);
57
61 ~udp_socket() = default;
62
73 std::function<void(const std::vector<uint8_t>&,
74 const asio::ip::udp::endpoint&)> callback) -> void;
75
83 auto set_error_callback(std::function<void(std::error_code)> callback) -> void;
84
91 auto start_receive() -> void;
92
96 auto stop_receive() -> void;
97
127 auto async_send_to(
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;
131
137 auto socket() -> asio::ip::udp::socket& { return socket_; }
138
146 auto close() -> void;
147
152 [[nodiscard]] auto is_closed() const -> bool;
153
154 private:
161 auto do_receive() -> void;
162
163 private:
164 asio::ip::udp::socket socket_;
166 std::array<uint8_t, 65536> read_buffer_;
167 asio::ip::udp::endpoint sender_endpoint_;
170 std::function<void(const std::vector<uint8_t>&, const asio::ip::udp::endpoint&)>
172 std::function<void(std::error_code)>
175 std::atomic<bool> is_receiving_{false};
176 std::atomic<bool> is_closed_{false};
177 };
178} // namespace kcenon::network::internal
A lightweight wrapper around asio::ip::udp::socket, enabling asynchronous datagram operations.
Definition udp_socket.h:47
auto do_receive() -> void
Internal function to handle the receive logic with async_receive_from().
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...
Definition udp_socket.h:137
asio::ip::udp::endpoint sender_endpoint_
Definition udp_socket.h:167
std::array< uint8_t, 65536 > read_buffer_
Definition udp_socket.h:166
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_
Definition udp_socket.h:171
auto start_receive() -> void
Begins the continuous asynchronous receive loop.
udp_socket(asio::ip::udp::socket socket)
Constructs a udp_socket by taking ownership of a moved socket.
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_
Definition udp_socket.h:173
std::mutex mutex