8 #ifndef WPIUTIL_WPI_UV_UDP_H_
9 #define WPIUTIL_WPI_UV_UDP_H_
16 #include "wpi/ArrayRef.h"
17 #include "wpi/Signal.h"
18 #include "wpi/Twine.h"
19 #include "wpi/uv/Handle.h"
20 #include "wpi/uv/Request.h"
35 Udp& GetUdp()
const {
return *static_cast<Udp*>(
GetRaw()->handle->data); }
49 struct private_init {};
52 explicit Udp(
const private_init&) {}
53 ~
Udp() noexcept
override =
default;
62 unsigned int flags = AF_UNSPEC);
70 static std::shared_ptr<Udp>
Create(
const std::shared_ptr<Loop>& loop,
71 unsigned int flags = AF_UNSPEC) {
72 return Create(*loop, flags);
80 void Open(uv_os_sock_t sock) { Invoke(&uv_udp_open,
GetRaw(), sock); }
88 void Bind(
const sockaddr& addr,
unsigned int flags = 0) {
89 Invoke(&uv_udp_bind,
GetRaw(), &addr, flags);
92 void Bind(
const sockaddr_in& addr,
unsigned int flags = 0) {
93 Bind(reinterpret_cast<const sockaddr&>(addr), flags);
96 void Bind(
const sockaddr_in6& addr,
unsigned int flags = 0) {
97 Bind(reinterpret_cast<const sockaddr&>(addr), flags);
107 void Bind(
const Twine& ip,
unsigned int port,
unsigned int flags = 0);
116 void Bind6(
const Twine& ip,
unsigned int port,
unsigned int flags = 0);
125 Invoke(&uv_udp_connect,
GetRaw(), &addr);
128 void Connect(
const sockaddr_in& addr) {
129 Connect(reinterpret_cast<const sockaddr&>(addr));
132 void Connect(
const sockaddr_in6& addr) {
133 Connect(reinterpret_cast<const sockaddr&>(addr));
143 void Connect(
const Twine& ip,
unsigned int port);
153 void Connect6(
const Twine& ip,
unsigned int port);
174 void SetMembership(
const Twine& multicastAddr,
const Twine& interfaceAddr,
175 uv_membership membership);
184 Invoke(&uv_udp_set_multicast_loop,
GetRaw(), enabled ? 1 : 0);
193 Invoke(&uv_udp_set_multicast_ttl,
GetRaw(), ttl);
209 Invoke(&uv_udp_set_broadcast,
GetRaw(), enabled ? 1 : 0);
238 const std::shared_ptr<UdpSendReq>& req);
241 const std::shared_ptr<UdpSendReq>& req) {
242 Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
246 const std::shared_ptr<UdpSendReq>& req) {
247 Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
257 void Send(ArrayRef<Buffer> bufs,
const std::shared_ptr<UdpSendReq>& req);
276 void Send(
const sockaddr& addr, ArrayRef<Buffer> bufs,
277 std::function<
void(MutableArrayRef<Buffer>, Error)> callback);
279 void Send(
const sockaddr_in& addr, ArrayRef<Buffer> bufs,
280 std::function<
void(MutableArrayRef<Buffer>, Error)> callback) {
281 Send(reinterpret_cast<const sockaddr&>(addr), bufs, callback);
284 void Send(
const sockaddr_in6& addr, ArrayRef<Buffer> bufs,
285 std::function<
void(MutableArrayRef<Buffer>, Error)> callback) {
286 Send(reinterpret_cast<const sockaddr&>(addr), bufs, callback);
296 void Send(ArrayRef<Buffer> bufs,
297 std::function<
void(MutableArrayRef<Buffer>, Error)> callback);
309 int val = uv_udp_try_send(
GetRaw(), bufs.data(),
310 static_cast<unsigned>(bufs.
size()), &addr);
319 return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
323 return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
334 int val = uv_udp_try_send(
GetRaw(), bufs.data(),
335 static_cast<unsigned>(bufs.
size()),
nullptr);
369 return GetRaw()->send_queue_count;
382 #endif // WPIUTIL_WPI_UV_UDP_H_
uv_udp_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:288
size_t GetSendQueueCount() const noexcept
Gets the amount of queued packets waiting to be sent.
Definition: Udp.h:368
sockaddr_storage GetPeer()
Get the remote IP and port on connected UDP handles.
UDP handle.
Definition: Udp.h:48
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:124
int TrySend(ArrayRef< Buffer > bufs)
Variant of TrySend() for connected sockets.
Definition: Udp.h:333
void SetMulticastLoop(bool enabled)
Set IP multicast loop flag.
Definition: Udp.h:183
void Bind(const sockaddr &addr, unsigned int flags=0)
Bind the handle to an IPv4 or IPv6 address and port.
Definition: Udp.h:88
sig::Signal< Buffer &, size_t, const sockaddr &, unsigned > received
Signal generated for each incoming datagram.
Definition: Udp.h:376
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
void Open(uv_os_sock_t sock)
Open an existing file descriptor or SOCKET as a UDP handle.
Definition: Udp.h:80
void SetTtl(int ttl)
Set the time to live (TTL).
Definition: Udp.h:217
void Bind6(const Twine &ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv6 address and port.
void StopRecv()
Stop listening for incoming datagrams.
Definition: Udp.h:356
void Connect6(const Twine &ip, unsigned int port)
Associate the handle to an IPv6 address and port, so every message sent by this handle is automatical...
sockaddr_storage GetSock()
Get the current address to which the handle is bound.
int TrySend(const sockaddr &addr, ArrayRef< Buffer > bufs)
Same as Send(), but won't queue a send request if it can't be completed immediately.
Definition: Udp.h:308
static std::shared_ptr< Udp > Create(Loop &loop, unsigned int flags=AF_UNSPEC)
Create a UDP handle.
static std::shared_ptr< Udp > Create(const std::shared_ptr< Loop > &loop, unsigned int flags=AF_UNSPEC)
Create a UDP handle.
Definition: Udp.h:70
void SetBroadcast(bool enabled)
Set broadcast on or off.
Definition: Udp.h:208
sig::Signal< Error > complete
Send completed signal.
Definition: Udp.h:41
uv_udp_send_t * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:149
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
void ReportError(int err) const
Report an error.
Definition: Handle.h:238
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:145
Handle.
Definition: Handle.h:273
Request.
Definition: Request.h:134
void SetMulticastInterface(const Twine &interfaceAddr)
Set the multicast interface to send or receive data on.
void Send(const sockaddr &addr, ArrayRef< Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Send data over the UDP socket.
void StartRecv()
Prepare for receiving data.
size_t GetSendQueueSize() const noexcept
Gets the amount of queued bytes waiting to be sent.
Definition: Udp.h:362
Event loop.
Definition: Loop.h:39
void SetMembership(const Twine &multicastAddr, const Twine &interfaceAddr, uv_membership membership)
Set membership for a multicast address.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
UDP send request.
Definition: Udp.h:31
void SetMulticastTtl(int ttl)
Set the multicast TTL.
Definition: Udp.h:192