1#ifndef UVW_UDP_INCLUDE_H
2#define UVW_UDP_INCLUDE_H
25 std::unique_ptr<char[]>
data;
33enum class uvw_udp_flags : std::underlying_type_t<uv_udp_flags> {
34 IPV6ONLY = UV_UDP_IPV6ONLY,
35 UDP_PARTIAL = UV_UDP_PARTIAL,
36 REUSEADDR = UV_UDP_REUSEADDR,
37 UDP_MMSG_CHUNK = UV_UDP_MMSG_CHUNK,
38 UDP_MMSG_FREE = UV_UDP_MMSG_FREE,
39 UDP_LINUX_RECVERR = UV_UDP_LINUX_RECVERR,
40 UDP_RECVMMSG = UV_UDP_RECVMMSG,
44enum class uvw_membership : std::underlying_type_t<uv_membership> {
45 LEAVE_GROUP = UV_LEAVE_GROUP,
46 JOIN_GROUP = UV_JOIN_GROUP
49class send_req final:
public request<send_req, uv_udp_send_t, send_event> {
50 static void udp_send_callback(uv_udp_send_t *req,
int status);
53 using deleter = void (*)(
char *);
55 send_req(loop::token token, std::shared_ptr<loop>
parent, std::unique_ptr<
char[], deleter> dt,
unsigned int len);
57 int send(uv_udp_t *hndl,
const struct sockaddr *addr);
60 std::unique_ptr<char[], deleter>
data;
82class udp_handle final:
public handle<udp_handle, uv_udp_t, send_event, udp_data_event> {
83 static void recv_callback(uv_udp_t *hndl, ssize_t nread,
const uv_buf_t *buf,
const sockaddr *addr,
unsigned flags);
86 using membership = details::uvw_membership;
87 using udp_flags = details::uvw_udp_flags;
91 explicit udp_handle(loop::token token, std::shared_ptr<loop> ref,
unsigned int f = {});
143 int connect(const std::
string &ip,
unsigned int port);
196 int bind(const sockaddr &addr, udp_flags opts = udp_flags::_UVW_ENUM);
220 int bind(const std::
string &ip,
unsigned int port, udp_flags opts = udp_flags::_UVW_ENUM);
322 int send(const sockaddr &addr, std::unique_ptr<
char[]>
data,
unsigned int len);
342 int send(const std::
string &ip,
unsigned int port, std::unique_ptr<
char[]>
data,
unsigned int len);
380 int send(const sockaddr &addr,
char *
data,
unsigned int len);
400 int send(const std::
string &ip,
unsigned int port,
char *
data,
unsigned int len);
432 int try_send(const sockaddr &addr, std::unique_ptr<
char[]>
data,
unsigned int len);
446 int try_send(const std::
string &ip,
unsigned int port, std::unique_ptr<
char[]>
data,
unsigned int len);
486 int try_send(const std::
string &ip,
unsigned int port,
char *
data,
unsigned int len);
542 unsigned int flags{};
std::shared_ptr< R > data() const
Gets user-defined data. uvw won't use this field in any case.
bool broadcast(bool enable=false)
Sets broadcast on or off.
bool multicast_loop(bool enable=true)
Sets IP multicast loop flag.
socket_address peer() const noexcept
Gets the remote address to which the handle is connected, if any.
int send(const sockaddr &addr, std::unique_ptr< char[]> data, unsigned int len)
Sends data over the UDP socket.
int disconnect()
Disconnects the handle.
size_t send_queue_size() const noexcept
Gets the number of bytes queued for sending.
bool multicast_membership(const std::string &multicast, const std::string &iface, membership ms)
Sets membership for a multicast address.
int init() final
Initializes the handle. The actual socket is created lazily.
int bind(const sockaddr &addr, udp_flags opts=udp_flags::_UVW_ENUM)
Binds the UDP handle to an IP address and port.
int stop()
Stops listening for incoming datagrams.
int connect(const sockaddr &addr)
Associates the handle to a remote address and port (either IPv4 or IPv6).
int try_send(const sockaddr &addr, std::unique_ptr< char[]> data, unsigned int len)
Sends data over the UDP socket.
size_t send_queue_count() const noexcept
Number of send requests currently in the queue awaiting to be processed.
bool multicast_interface(const std::string &iface)
Sets the multicast interface to send or receive data on.
bool multicast_ttl(int val)
Sets the multicast ttl.
int recv()
Prepares for receiving data.
int open(os_socket_handle socket)
Opens an existing file descriptor or SOCKET as a UDP handle.
bool ttl(int val)
Sets the time to live.
socket_address sock() const noexcept
Get the local IP and port of the UDP handle.
details::uv_type_wrapper< uv_os_sock_t > os_socket_handle
std::unique_ptr< char[]> data
loop & parent() const noexcept
Gets the loop from which the resource was originated.