uvw 3.1.0
Loading...
Searching...
No Matches
udp.h
1#ifndef UVW_UDP_INCLUDE_H
2#define UVW_UDP_INCLUDE_H
3
4#include <cstddef>
5#include <memory>
6#include <string>
7#include <type_traits>
8#include <utility>
9#include <uv.h>
10#include "config.h"
11#include "enum.hpp"
12#include "handle.hpp"
13#include "request.hpp"
14#include "util.h"
15
16namespace uvw {
17
19struct send_event {};
20
23 explicit udp_data_event(socket_address sndr, std::unique_ptr<char[]> buf, std::size_t len, bool part) noexcept;
24
25 std::unique_ptr<char[]> data;
26 std::size_t length;
28 bool partial;
29};
30
31namespace details {
32
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,
41 _UVW_ENUM = 0
42};
43
44enum class uvw_membership : std::underlying_type_t<uv_membership> {
45 LEAVE_GROUP = UV_LEAVE_GROUP,
46 JOIN_GROUP = UV_JOIN_GROUP
47};
48
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);
51
52public:
53 using deleter = void (*)(char *);
54
55 send_req(loop::token token, std::shared_ptr<loop> parent, std::unique_ptr<char[], deleter> dt, unsigned int len);
56
57 int send(uv_udp_t *hndl, const struct sockaddr *addr);
58
59private:
60 std::unique_ptr<char[], deleter> data;
61 uv_buf_t buf;
62};
63
64} // namespace details
65
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);
84
85public:
86 using membership = details::uvw_membership;
87 using udp_flags = details::uvw_udp_flags;
88 using ipv4 = uvw::ipv4;
89 using ipv6 = uvw::ipv6;
90
91 explicit udp_handle(loop::token token, std::shared_ptr<loop> ref, unsigned int f = {});
92
97 int init() final;
98
115
128 int connect(const sockaddr &addr);
129
143 int connect(const std::string &ip, unsigned int port);
144
158
167
173 socket_address peer() const noexcept;
174
196 int bind(const sockaddr &addr, udp_flags opts = udp_flags::_UVW_ENUM);
197
220 int bind(const std::string &ip, unsigned int port, udp_flags opts = udp_flags::_UVW_ENUM);
221
243 int bind(socket_address addr, udp_flags opts = udp_flags::_UVW_ENUM);
244
250 socket_address sock() const noexcept;
251
265 bool multicast_membership(const std::string &multicast, const std::string &iface, membership ms);
266
275 bool multicast_loop(bool enable = true);
276
282 bool multicast_ttl(int val);
283
289 bool multicast_interface(const std::string &iface);
290
296 bool broadcast(bool enable = false);
297
303 bool ttl(int val);
304
322 int send(const sockaddr &addr, std::unique_ptr<char[]> data, unsigned int len);
323
342 int send(const std::string &ip, unsigned int port, std::unique_ptr<char[]> data, unsigned int len);
343
361 int send(socket_address addr, std::unique_ptr<char[]> data, unsigned int len);
362
380 int send(const sockaddr &addr, char *data, unsigned int len);
381
400 int send(const std::string &ip, unsigned int port, char *data, unsigned int len);
401
419 int send(socket_address addr, char *data, unsigned int len);
420
432 int try_send(const sockaddr &addr, std::unique_ptr<char[]> data, unsigned int len);
433
446 int try_send(const std::string &ip, unsigned int port, std::unique_ptr<char[]> data, unsigned int len);
447
459 int try_send(socket_address addr, std::unique_ptr<char[]> data, unsigned int len);
460
472 int try_send(const sockaddr &addr, char *data, unsigned int len);
473
486 int try_send(const std::string &ip, unsigned int port, char *data, unsigned int len);
487
499 int try_send(socket_address addr, char *data, unsigned int len);
500
512 int recv();
513
518 int stop();
519
527 size_t send_queue_size() const noexcept;
528
534 size_t send_queue_count() const noexcept;
535
536private:
537 enum {
538 DEFAULT,
539 FLAGS
540 } tag{DEFAULT};
541
542 unsigned int flags{};
543};
544
545} // namespace uvw
546
547#ifndef UVW_AS_LIB
548# include "udp.cpp"
549#endif
550
551#endif // UVW_UDP_INCLUDE_H
Handle base class.
Definition: handle.hpp:23
std::shared_ptr< R > data() const
Gets user-defined data. uvw won't use this field in any case.
Definition: resource.hpp:47
The UDP handle.
Definition: udp.h:82
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.
uvw default namespace.
Definition: async.h:8
details::uv_type_wrapper< uv_os_sock_t > os_socket_handle
Definition: util.h:86
The IPv4 tag.
Definition: util.h:202
The IPv6 tag.
Definition: util.h:209
Send event.
Definition: udp.h:19
Address representation.
Definition: util.h:214
UDP data event.
Definition: udp.h:22
bool partial
Definition: udp.h:28
socket_address sender
Definition: udp.h:27
std::size_t length
Definition: udp.h:26
std::unique_ptr< char[]> data
Definition: udp.h:25
loop & parent() const noexcept
Gets the loop from which the resource was originated.
Definition: uv_type.hpp:40