WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
Udp.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_UV_UDP_H_
6#define WPINET_UV_UDP_H_
7
8#include <uv.h>
9
10#include <functional>
11#include <memory>
12#include <span>
13#include <string_view>
14#include <utility>
15
16#include <wpi/Signal.h>
17
18#include "wpinet/uv/Handle.h"
19#include "wpinet/uv/Request.h"
20
21namespace wpi::uv {
22
23class Loop;
24class Udp;
25
26/**
27 * UDP send request.
28 */
29class UdpSendReq : public RequestImpl<UdpSendReq, uv_udp_send_t> {
30 public:
32
33 Udp& GetUdp() const { return *static_cast<Udp*>(GetRaw()->handle->data); }
34
35 /**
36 * Send completed signal. This is called even if an error occurred.
37 * @param err error value
38 */
40};
41
42/**
43 * UDP handle.
44 * UDP handles encapsulate UDP communication for both clients and servers.
45 */
46class Udp final : public HandleImpl<Udp, uv_udp_t> {
47 struct private_init {};
48
49 public:
50 explicit Udp(const private_init&) {}
51 ~Udp() noexcept override = default;
52
53 /**
54 * Create a UDP handle.
55 *
56 * @param loop Loop object where this handle runs.
57 * @param flags Flags
58 */
59 static std::shared_ptr<Udp> Create(Loop& loop,
60 unsigned int flags = AF_UNSPEC);
61
62 /**
63 * Create a UDP handle.
64 *
65 * @param loop Loop object where this handle runs.
66 * @param flags Flags
67 */
68 static std::shared_ptr<Udp> Create(const std::shared_ptr<Loop>& loop,
69 unsigned int flags = AF_UNSPEC) {
70 return Create(*loop, flags);
71 }
72
73 /**
74 * Open an existing file descriptor or SOCKET as a UDP handle.
75 *
76 * @param sock A valid socket handle (either a file descriptor or a SOCKET).
77 */
78 void Open(uv_os_sock_t sock) { Invoke(&uv_udp_open, GetRaw(), sock); }
79
80 /**
81 * Bind the handle to an IPv4 or IPv6 address and port.
82 *
83 * @param addr Initialized `sockaddr_in` or `sockaddr_in6` data structure.
84 * @param flags Optional additional flags.
85 */
86 void Bind(const sockaddr& addr, unsigned int flags = 0) {
87 Invoke(&uv_udp_bind, GetRaw(), &addr, flags);
88 }
89
90 void Bind(const sockaddr_in& addr, unsigned int flags = 0) {
91 Bind(reinterpret_cast<const sockaddr&>(addr), flags);
92 }
93
94 void Bind(const sockaddr_in6& addr, unsigned int flags = 0) {
95 Bind(reinterpret_cast<const sockaddr&>(addr), flags);
96 }
97
98 /**
99 * Bind the handle to an IPv4 address and port.
100 *
101 * @param ip The address to which to bind.
102 * @param port The port to which to bind.
103 * @param flags Optional additional flags.
104 */
105 void Bind(std::string_view ip, unsigned int port, unsigned int flags = 0);
106
107 /**
108 * Bind the handle to an IPv6 address and port.
109 *
110 * @param ip The address to which to bind.
111 * @param port The port to which to bind.
112 * @param flags Optional additional flags.
113 */
114 void Bind6(std::string_view ip, unsigned int port, unsigned int flags = 0);
115
116 /**
117 * Associate the handle to a remote address and port, so every message sent
118 * by this handle is automatically sent to that destination.
119 *
120 * @param addr Initialized `sockaddr_in` or `sockaddr_in6` data structure.
121 */
122 void Connect(const sockaddr& addr) {
123 Invoke(&uv_udp_connect, GetRaw(), &addr);
124 }
125
126 void Connect(const sockaddr_in& addr) {
127 Connect(reinterpret_cast<const sockaddr&>(addr));
128 }
129
130 void Connect(const sockaddr_in6& addr) {
131 Connect(reinterpret_cast<const sockaddr&>(addr));
132 }
133
134 /**
135 * Associate the handle to an IPv4 address and port, so every message sent
136 * by this handle is automatically sent to that destination.
137 *
138 * @param ip The address to which to bind.
139 * @param port The port to which to bind.
140 */
141 void Connect(std::string_view ip, unsigned int port);
142
143 /**
144 * Associate the handle to an IPv6 address and port, so every message sent
145 * by this handle is automatically sent to that destination.
146 *
147 * @param ip The address to which to bind.
148 * @param port The port to which to bind.
149 */
150 void Connect6(std::string_view ip, unsigned int port);
151
152 /**
153 * Get the remote IP and port on connected UDP handles.
154 * @return The address (will be zeroed if an error occurred).
155 */
156 sockaddr_storage GetPeer();
157
158 /**
159 * Get the current address to which the handle is bound.
160 * @return The address (will be zeroed if an error occurred).
161 */
162 sockaddr_storage GetSock();
163
164 /**
165 * Set membership for a multicast address.
166 *
167 * @param multicastAddr Multicast address to set membership for
168 * @param interfaceAddr Interface address
169 * @param membership Should be UV_JOIN_GROUP or UV_LEAVE_GROUP
170 */
171 void SetMembership(std::string_view multicastAddr,
172 std::string_view interfaceAddr, uv_membership membership);
173
174 /**
175 * Set membership for a source-specific multicast group.
176 *
177 * @param multicastAddr Multicast address to set membership for
178 * @param interfaceAddr Interface address
179 * @param sourceAddr Source address
180 * @param membership Should be UV_JOIN_GROUP or UV_LEAVE_GROUP
181 */
182 void SetSourceMembership(std::string_view multicastAddr,
183 std::string_view interfaceAddr,
184 std::string_view sourceAddr,
185 uv_membership membership);
186
187 /**
188 * Set IP multicast loop flag. Makes multicast packets loop back to local
189 * sockets.
190 *
191 * @param enabled True for enabled, false for disabled
192 */
193 void SetMulticastLoop(bool enabled) {
194 Invoke(&uv_udp_set_multicast_loop, GetRaw(), enabled ? 1 : 0);
195 }
196
197 /**
198 * Set the multicast TTL.
199 *
200 * @param ttl Time to live (1-255)
201 */
202 void SetMulticastTtl(int ttl) {
204 }
205
206 /**
207 * Set the multicast interface to send or receive data on.
208 *
209 * @param interfaceAddr Interface address
210 */
211 void SetMulticastInterface(std::string_view interfaceAddr);
212
213 /**
214 * Set broadcast on or off.
215 *
216 * @param enabled True for enabled, false for disabled
217 */
218 void SetBroadcast(bool enabled) {
219 Invoke(&uv_udp_set_broadcast, GetRaw(), enabled ? 1 : 0);
220 }
221
222 /**
223 * Set the time to live (TTL).
224 *
225 * @param ttl Time to live (1-255)
226 */
227 void SetTtl(int ttl) { Invoke(&uv_udp_set_ttl, GetRaw(), ttl); }
228
229 /**
230 * Send data over the UDP socket. If the socket has not previously been bound
231 * with Bind() it will be bound to 0.0.0.0 (the "all interfaces" IPv4 address)
232 * and a random port number.
233 *
234 * Data are written in order. The lifetime of the data pointers passed in
235 * the `bufs` parameter must exceed the lifetime of the send request.
236 * The callback can be used to free data after the request completes.
237 *
238 * HandleSendComplete() will be called on the request object when the data
239 * has been written. HandleSendComplete() is called even if an error occurs.
240 * HandleError() will be called on the request object in case of errors.
241 *
242 * @param addr sockaddr_in or sockaddr_in6 with the address and port of the
243 * remote peer.
244 * @param bufs The buffers to be written to the stream.
245 * @param req write request
246 */
247 void Send(const sockaddr& addr, std::span<const Buffer> bufs,
248 const std::shared_ptr<UdpSendReq>& req);
249
250 void Send(const sockaddr_in& addr, std::span<const Buffer> bufs,
251 const std::shared_ptr<UdpSendReq>& req) {
252 Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
253 }
254
255 void Send(const sockaddr_in6& addr, std::span<const Buffer> bufs,
256 const std::shared_ptr<UdpSendReq>& req) {
257 Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
258 }
259
260 /**
261 * Variant of Send() for connected sockets. Cannot be used with
262 * connectionless sockets.
263 *
264 * @param bufs The buffers to be written to the stream.
265 * @param req write request
266 */
267 void Send(std::span<const Buffer> bufs,
268 const std::shared_ptr<UdpSendReq>& req);
269
270 /**
271 * Send data over the UDP socket. If the socket has not previously been bound
272 * with Bind() it will be bound to 0.0.0.0 (the "all interfaces" IPv4 address)
273 * and a random port number.
274 *
275 * Data are written in order. The lifetime of the data pointers passed in
276 * the `bufs` parameter must exceed the lifetime of the send request.
277 * The callback can be used to free data after the request completes.
278 *
279 * The callback will be called when the data has been sent. Errors will
280 * be reported via the error signal.
281 *
282 * @param addr sockaddr_in or sockaddr_in6 with the address and port of the
283 * remote peer.
284 * @param bufs The buffers to be sent.
285 * @param callback Callback function to call when the data has been sent.
286 */
287 void Send(const sockaddr& addr, std::span<const Buffer> bufs,
288 std::function<void(std::span<Buffer>, Error)> callback);
289
290 void Send(const sockaddr_in& addr, std::span<const Buffer> bufs,
291 std::function<void(std::span<Buffer>, Error)> callback) {
292 Send(reinterpret_cast<const sockaddr&>(addr), bufs, std::move(callback));
293 }
294
295 void Send(const sockaddr_in6& addr, std::span<const Buffer> bufs,
296 std::function<void(std::span<Buffer>, Error)> callback) {
297 Send(reinterpret_cast<const sockaddr&>(addr), bufs, std::move(callback));
298 }
299
300 /**
301 * Variant of Send() for connected sockets. Cannot be used with
302 * connectionless sockets.
303 *
304 * @param bufs The buffers to be written to the stream.
305 * @param callback Callback function to call when the data has been sent.
306 */
307 void Send(std::span<const Buffer> bufs,
308 std::function<void(std::span<Buffer>, Error)> callback);
309
310 /**
311 * Same as Send(), but won't queue a send request if it can't be completed
312 * immediately.
313 *
314 * @param addr sockaddr_in or sockaddr_in6 with the address and port of the
315 * remote peer.
316 * @param bufs The buffers to be send.
317 * @return Number of bytes sent.
318 */
319 int TrySend(const sockaddr& addr, std::span<const Buffer> bufs) {
320 int val = uv_udp_try_send(GetRaw(), bufs.data(),
321 static_cast<unsigned>(bufs.size()), &addr);
322 if (val < 0) {
323 this->ReportError(val);
324 return 0;
325 }
326 return val;
327 }
328
329 int TrySend(const sockaddr_in& addr, std::span<const Buffer> bufs) {
330 return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
331 }
332
333 int TrySend(const sockaddr_in6& addr, std::span<const Buffer> bufs) {
334 return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
335 }
336
337 /**
338 * Variant of TrySend() for connected sockets. Cannot be used with
339 * connectionless sockets.
340 *
341 * @param bufs The buffers to be written to the stream.
342 * @return Number of bytes sent.
343 */
344 int TrySend(std::span<const Buffer> bufs) {
345 int val = uv_udp_try_send(GetRaw(), bufs.data(),
346 static_cast<unsigned>(bufs.size()), nullptr);
347 if (val < 0) {
348 this->ReportError(val);
349 return 0;
350 }
351 return val;
352 }
353
354 /**
355 * Prepare for receiving data. If the socket has not previously been bound
356 * with Bind() it is bound to 0.0.0.0 (the "all interfaces" IPv4 address) and
357 * a random port number.
358 *
359 * A received signal will be emitted for each received data packet until
360 * `StopRecv()` is called.
361 */
362 void StartRecv();
363
364 /**
365 * Stop listening for incoming datagrams.
366 */
368
369 /**
370 * Returns true if the UDP handle was created with the UV_UDP_RECVMMSG flag
371 * and the platform supports recvmmsg(2), false otherwise.
372 * @return True if the UDP handle is using recvmmsg.
373 */
374 bool IsUsingRecvmmsg() const { return uv_udp_using_recvmmsg(GetRaw()); }
375
376 /**
377 * Gets the amount of queued bytes waiting to be sent.
378 * @return Amount of queued bytes waiting to be sent.
379 */
380 size_t GetSendQueueSize() const noexcept { return GetRaw()->send_queue_size; }
381
382 /**
383 * Gets the amount of queued packets waiting to be sent.
384 * @return Amount of queued packets waiting to be sent.
385 */
386 size_t GetSendQueueCount() const noexcept {
387 return GetRaw()->send_queue_count;
388 }
389
390 /**
391 * Signal generated for each incoming datagram. Parameters are the buffer,
392 * the number of bytes received, the address of the sender, and flags.
393 */
395};
396
397} // namespace wpi::uv
398
399#endif // WPINET_UV_UDP_H_
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition Signal.h:495
Error code.
Definition Error.h:15
void ReportError(int err) const
Report an error.
Definition Handle.h:252
bool Invoke(F &&f, Args &&... args) const
Definition Handle.h:267
Handle.
Definition Handle.h:290
uv_udp_t * GetRaw() const noexcept
Definition Handle.h:305
Event loop.
Definition Loop.h:37
Request.
Definition Request.h:135
uv_udp_send_t * GetRaw() noexcept
Definition Request.h:150
UDP handle.
Definition Udp.h:46
void SetSourceMembership(std::string_view multicastAddr, std::string_view interfaceAddr, std::string_view sourceAddr, uv_membership membership)
Set membership for a source-specific multicast group.
sockaddr_storage GetPeer()
Get the remote IP and port on connected UDP handles.
void Open(uv_os_sock_t sock)
Open an existing file descriptor or SOCKET as a UDP handle.
Definition Udp.h:78
int TrySend(const sockaddr_in6 &addr, std::span< const Buffer > bufs)
Definition Udp.h:333
void Bind(std::string_view ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv4 address and port.
sig::Signal< Buffer &, size_t, const sockaddr &, unsigned > received
Signal generated for each incoming datagram.
Definition Udp.h:394
void Send(const sockaddr_in &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Definition Udp.h:250
void SetBroadcast(bool enabled)
Set broadcast on or off.
Definition Udp.h:218
void Connect6(std::string_view ip, unsigned int port)
Associate the handle to an IPv6 address and port, so every message sent by this handle is automatical...
void Bind(const sockaddr_in6 &addr, unsigned int flags=0)
Definition Udp.h:94
sockaddr_storage GetSock()
Get the current address to which the handle is bound.
Udp(const private_init &)
Definition Udp.h:50
void Bind(const sockaddr &addr, unsigned int flags=0)
Bind the handle to an IPv4 or IPv6 address and port.
Definition Udp.h:86
size_t GetSendQueueCount() const noexcept
Gets the amount of queued packets waiting to be sent.
Definition Udp.h:386
bool IsUsingRecvmmsg() const
Returns true if the UDP handle was created with the UV_UDP_RECVMMSG flag and the platform supports re...
Definition Udp.h:374
void SetMulticastLoop(bool enabled)
Set IP multicast loop flag.
Definition Udp.h:193
void Bind(const sockaddr_in &addr, unsigned int flags=0)
Definition Udp.h:90
void Bind6(std::string_view ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv6 address and port.
size_t GetSendQueueSize() const noexcept
Gets the amount of queued bytes waiting to be sent.
Definition Udp.h:380
void Send(std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Variant of Send() for connected sockets.
void SetMembership(std::string_view multicastAddr, std::string_view interfaceAddr, uv_membership membership)
Set membership for a multicast address.
void SetTtl(int ttl)
Set the time to live (TTL).
Definition Udp.h:227
void Send(const sockaddr &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Send data over the UDP socket.
int TrySend(const sockaddr_in &addr, std::span< const Buffer > bufs)
Definition Udp.h:329
int TrySend(std::span< const Buffer > bufs)
Variant of TrySend() for connected sockets.
Definition Udp.h:344
void Send(std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Variant of Send() for connected sockets.
void Connect(std::string_view ip, unsigned int port)
Associate the handle to an IPv4 address and port, so every message sent by this handle is automatical...
int TrySend(const sockaddr &addr, std::span< const Buffer > bufs)
Same as Send(), but won't queue a send request if it can't be completed immediately.
Definition Udp.h:319
void Send(const sockaddr_in &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Definition Udp.h:290
void Connect(const sockaddr &addr)
Associate the handle to a remote address and port, so every message sent by this handle is automatica...
Definition Udp.h:122
void Send(const sockaddr &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Send data over the UDP socket.
void Send(const sockaddr_in6 &addr, std::span< const Buffer > bufs, std::function< void(std::span< Buffer >, Error)> callback)
Definition Udp.h:295
void SetMulticastTtl(int ttl)
Set the multicast TTL.
Definition Udp.h:202
void StartRecv()
Prepare for receiving data.
void Connect(const sockaddr_in &addr)
Definition Udp.h:126
~Udp() noexcept override=default
void StopRecv()
Stop listening for incoming datagrams.
Definition Udp.h:367
void Connect(const sockaddr_in6 &addr)
Definition Udp.h:130
void Send(const sockaddr_in6 &addr, std::span< const Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Definition Udp.h:255
void SetMulticastInterface(std::string_view interfaceAddr)
Set the multicast interface to send or receive data on.
static std::shared_ptr< Udp > Create(Loop &loop, unsigned int flags=AF_UNSPEC)
Create a UDP handle.
UDP send request.
Definition Udp.h:29
Udp & GetUdp() const
Definition Udp.h:33
sig::Signal< Error > complete
Send completed signal.
Definition Udp.h:39
Definition PointerIntPair.h:280
Definition Loop.h:22
flags
Definition http_parser.h:206
size_t send_queue_count
Definition uv.h:724
UV_HANDLE_FIELDS size_t send_queue_size
Definition uv.h:720
UV_REQ_FIELDS uv_udp_t * handle
Definition uv.h:731
UV_EXTERN int uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int flags)
UV_EXTERN int uv_udp_set_ttl(uv_udp_t *handle, int ttl)
UV_EXTERN int uv_udp_recv_stop(uv_udp_t *handle)
uv_membership
Definition uv.h:414
UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t *handle, int ttl)
UV_EXTERN int uv_udp_open(uv_udp_t *handle, uv_os_sock_t sock)
UV_EXTERN int uv_udp_try_send(uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr)
UV_EXTERN int uv_udp_connect(uv_udp_t *handle, const struct sockaddr *addr)
UV_EXTERN int uv_udp_using_recvmmsg(const uv_udp_t *handle)
UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t *handle, int on)
UV_EXTERN int uv_udp_set_broadcast(uv_udp_t *handle, int on)