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);
131 void SetMembership(
const Twine& multicastAddr,
const Twine& interfaceAddr,
132 uv_membership membership);
141 Invoke(&uv_udp_set_multicast_loop,
GetRaw(), enabled ? 1 : 0);
150 Invoke(&uv_udp_set_multicast_ttl,
GetRaw(), ttl);
166 Invoke(&uv_udp_set_broadcast,
GetRaw(), enabled ? 1 : 0);
195 const std::shared_ptr<UdpSendReq>& req);
198 const std::shared_ptr<UdpSendReq>& req) {
199 Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
203 const std::shared_ptr<UdpSendReq>& req) {
204 Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
224 void Send(
const sockaddr& addr, ArrayRef<Buffer> bufs,
225 std::function<
void(MutableArrayRef<Buffer>, Error)> callback);
227 void Send(
const sockaddr_in& addr, ArrayRef<Buffer> bufs,
228 std::function<
void(MutableArrayRef<Buffer>, Error)> callback) {
229 Send(reinterpret_cast<const sockaddr&>(addr), bufs, callback);
232 void Send(
const sockaddr_in6& addr, ArrayRef<Buffer> bufs,
233 std::function<
void(MutableArrayRef<Buffer>, Error)> callback) {
234 Send(reinterpret_cast<const sockaddr&>(addr), bufs, callback);
247 int val = uv_udp_try_send(
GetRaw(), bufs.data(), bufs.
size(), &addr);
256 return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
260 return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
289 return GetRaw()->send_queue_count;
302 #endif // WPIUTIL_WPI_UV_UDP_H_
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
uv_udp_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:261
UDP send request.
Definition: Udp.h:31
void SetMulticastInterface(const Twine &interfaceAddr)
Set the multicast interface to send or receive data on.
Handle.
Definition: Handle.h:246
void SetMembership(const Twine &multicastAddr, const Twine &interfaceAddr, uv_membership membership)
Set membership for a multicast address.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
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.
uv_udp_send_t * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:149
size_t GetSendQueueSize() const noexcept
Gets the amount of queued bytes waiting to be sent.
Definition: Udp.h:282
void SetMulticastLoop(bool enabled)
Set IP multicast loop flag.
Definition: Udp.h:140
void ReportError(int err)
Report an error.
Definition: Handle.h:213
void SetMulticastTtl(int ttl)
Set the multicast TTL.
Definition: Udp.h:149
sig::Signal< Buffer &, size_t, const sockaddr &, unsigned > received
Signal generated for each incoming datagram.
Definition: Udp.h:296
size_t GetSendQueueCount() const noexcept
Gets the amount of queued packets waiting to be sent.
Definition: Udp.h:288
sig::Signal< Error > complete
Send completed signal.
Definition: Udp.h:41
Event loop.
Definition: Loop.h:39
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:246
static std::shared_ptr< Udp > Create(Loop &loop, unsigned int flags=AF_UNSPEC)
Create a UDP handle.
void Open(uv_os_sock_t sock)
Open an existing file descriptor or SOCKET as a UDP handle.
Definition: Udp.h:80
void Bind(const sockaddr &addr, unsigned int flags=0)
Bind the handle to an IPv4 or IPv6 address and port.
Definition: Udp.h:88
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:276
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
Request.
Definition: Request.h:134
void SetTtl(int ttl)
Set the time to live (TTL).
Definition: Udp.h:174
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
UDP handle.
Definition: Udp.h:48
sockaddr_storage GetSock()
Get the current address to which the handle is bound.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
void SetBroadcast(bool enabled)
Set broadcast on or off.
Definition: Udp.h:165