WPILibC++  2020.3.2
Udp.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_UV_UDP_H_
9 #define WPIUTIL_WPI_UV_UDP_H_
10 
11 #include <uv.h>
12 
13 #include <functional>
14 #include <memory>
15 
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"
21 
22 namespace wpi {
23 namespace uv {
24 
25 class Loop;
26 class Udp;
27 
31 class UdpSendReq : public RequestImpl<UdpSendReq, uv_udp_send_t> {
32  public:
33  UdpSendReq();
34 
35  Udp& GetUdp() const { return *static_cast<Udp*>(GetRaw()->handle->data); }
36 
42 };
43 
48 class Udp final : public HandleImpl<Udp, uv_udp_t> {
49  struct private_init {};
50 
51  public:
52  explicit Udp(const private_init&) {}
53  ~Udp() noexcept override = default;
54 
61  static std::shared_ptr<Udp> Create(Loop& loop,
62  unsigned int flags = AF_UNSPEC);
63 
70  static std::shared_ptr<Udp> Create(const std::shared_ptr<Loop>& loop,
71  unsigned int flags = AF_UNSPEC) {
72  return Create(*loop, flags);
73  }
74 
80  void Open(uv_os_sock_t sock) { Invoke(&uv_udp_open, GetRaw(), sock); }
81 
88  void Bind(const sockaddr& addr, unsigned int flags = 0) {
89  Invoke(&uv_udp_bind, GetRaw(), &addr, flags);
90  }
91 
92  void Bind(const sockaddr_in& addr, unsigned int flags = 0) {
93  Bind(reinterpret_cast<const sockaddr&>(addr), flags);
94  }
95 
96  void Bind(const sockaddr_in6& addr, unsigned int flags = 0) {
97  Bind(reinterpret_cast<const sockaddr&>(addr), flags);
98  }
99 
107  void Bind(const Twine& ip, unsigned int port, unsigned int flags = 0);
108 
116  void Bind6(const Twine& ip, unsigned int port, unsigned int flags = 0);
117 
124  void Connect(const sockaddr& addr) {
125  Invoke(&uv_udp_connect, GetRaw(), &addr);
126  }
127 
128  void Connect(const sockaddr_in& addr) {
129  Connect(reinterpret_cast<const sockaddr&>(addr));
130  }
131 
132  void Connect(const sockaddr_in6& addr) {
133  Connect(reinterpret_cast<const sockaddr&>(addr));
134  }
135 
143  void Connect(const Twine& ip, unsigned int port);
144 
153  void Connect6(const Twine& ip, unsigned int port);
154 
159  sockaddr_storage GetPeer();
160 
165  sockaddr_storage GetSock();
166 
174  void SetMembership(const Twine& multicastAddr, const Twine& interfaceAddr,
175  uv_membership membership);
176 
183  void SetMulticastLoop(bool enabled) {
184  Invoke(&uv_udp_set_multicast_loop, GetRaw(), enabled ? 1 : 0);
185  }
186 
192  void SetMulticastTtl(int ttl) {
193  Invoke(&uv_udp_set_multicast_ttl, GetRaw(), ttl);
194  }
195 
201  void SetMulticastInterface(const Twine& interfaceAddr);
202 
208  void SetBroadcast(bool enabled) {
209  Invoke(&uv_udp_set_broadcast, GetRaw(), enabled ? 1 : 0);
210  }
211 
217  void SetTtl(int ttl) { Invoke(&uv_udp_set_ttl, GetRaw(), ttl); }
218 
237  void Send(const sockaddr& addr, ArrayRef<Buffer> bufs,
238  const std::shared_ptr<UdpSendReq>& req);
239 
240  void Send(const sockaddr_in& addr, ArrayRef<Buffer> bufs,
241  const std::shared_ptr<UdpSendReq>& req) {
242  Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
243  }
244 
245  void Send(const sockaddr_in6& addr, ArrayRef<Buffer> bufs,
246  const std::shared_ptr<UdpSendReq>& req) {
247  Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
248  }
249 
257  void Send(ArrayRef<Buffer> bufs, const std::shared_ptr<UdpSendReq>& req);
258 
276  void Send(const sockaddr& addr, ArrayRef<Buffer> bufs,
277  std::function<void(MutableArrayRef<Buffer>, Error)> callback);
278 
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);
282  }
283 
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);
287  }
288 
296  void Send(ArrayRef<Buffer> bufs,
297  std::function<void(MutableArrayRef<Buffer>, Error)> callback);
298 
308  int TrySend(const sockaddr& addr, ArrayRef<Buffer> bufs) {
309  int val = uv_udp_try_send(GetRaw(), bufs.data(),
310  static_cast<unsigned>(bufs.size()), &addr);
311  if (val < 0) {
312  this->ReportError(val);
313  return 0;
314  }
315  return val;
316  }
317 
318  int TrySend(const sockaddr_in& addr, ArrayRef<Buffer> bufs) {
319  return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
320  }
321 
322  int TrySend(const sockaddr_in6& addr, ArrayRef<Buffer> bufs) {
323  return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
324  }
325 
334  int val = uv_udp_try_send(GetRaw(), bufs.data(),
335  static_cast<unsigned>(bufs.size()), nullptr);
336  if (val < 0) {
337  this->ReportError(val);
338  return 0;
339  }
340  return val;
341  }
342 
351  void StartRecv();
352 
356  void StopRecv() { Invoke(&uv_udp_recv_stop, GetRaw()); }
357 
362  size_t GetSendQueueSize() const noexcept { return GetRaw()->send_queue_size; }
363 
368  size_t GetSendQueueCount() const noexcept {
369  return GetRaw()->send_queue_count;
370  }
371 
377 };
378 
379 } // namespace uv
380 } // namespace wpi
381 
382 #endif // WPIUTIL_WPI_UV_UDP_H_
wpi::uv::HandleImpl< Udp, uv_udp_t >::GetRaw
uv_udp_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:288
wpi::uv::Udp::GetSendQueueCount
size_t GetSendQueueCount() const noexcept
Gets the amount of queued packets waiting to be sent.
Definition: Udp.h:368
wpi::uv::Udp::GetPeer
sockaddr_storage GetPeer()
Get the remote IP and port on connected UDP handles.
wpi::uv::Udp
UDP handle.
Definition: Udp.h:48
wpi::uv::Udp::Connect
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
wpi::uv::Udp::TrySend
int TrySend(ArrayRef< Buffer > bufs)
Variant of TrySend() for connected sockets.
Definition: Udp.h:333
wpi::uv::Udp::SetMulticastLoop
void SetMulticastLoop(bool enabled)
Set IP multicast loop flag.
Definition: Udp.h:183
wpi::uv::Udp::Bind
void Bind(const sockaddr &addr, unsigned int flags=0)
Bind the handle to an IPv4 or IPv6 address and port.
Definition: Udp.h:88
wpi::uv::Udp::received
sig::Signal< Buffer &, size_t, const sockaddr &, unsigned > received
Signal generated for each incoming datagram.
Definition: Udp.h:376
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
wpi::uv::Udp::Open
void Open(uv_os_sock_t sock)
Open an existing file descriptor or SOCKET as a UDP handle.
Definition: Udp.h:80
wpi::uv::Udp::SetTtl
void SetTtl(int ttl)
Set the time to live (TTL).
Definition: Udp.h:217
wpi::uv::Udp::Bind6
void Bind6(const Twine &ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv6 address and port.
wpi::uv::Udp::StopRecv
void StopRecv()
Stop listening for incoming datagrams.
Definition: Udp.h:356
wpi::uv::Udp::Connect6
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...
wpi::uv::Udp::GetSock
sockaddr_storage GetSock()
Get the current address to which the handle is bound.
wpi::uv::Udp::TrySend
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
wpi::uv::Udp::Create
static std::shared_ptr< Udp > Create(Loop &loop, unsigned int flags=AF_UNSPEC)
Create a UDP handle.
wpi::uv::Udp::Create
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
wpi::uv::Udp::SetBroadcast
void SetBroadcast(bool enabled)
Set broadcast on or off.
Definition: Udp.h:208
wpi::uv::UdpSendReq::complete
sig::Signal< Error > complete
Send completed signal.
Definition: Udp.h:41
wpi::uv::RequestImpl< UdpSendReq, uv_udp_send_t >::GetRaw
uv_udp_send_t * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:149
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
wpi::uv::Handle::ReportError
void ReportError(int err) const
Report an error.
Definition: Handle.h:238
wpi::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:145
wpi::uv::HandleImpl
Handle.
Definition: Handle.h:273
wpi::uv::RequestImpl
Request.
Definition: Request.h:134
wpi::uv::Udp::SetMulticastInterface
void SetMulticastInterface(const Twine &interfaceAddr)
Set the multicast interface to send or receive data on.
wpi::uv::Udp::Send
void Send(const sockaddr &addr, ArrayRef< Buffer > bufs, const std::shared_ptr< UdpSendReq > &req)
Send data over the UDP socket.
wpi::uv::Udp::StartRecv
void StartRecv()
Prepare for receiving data.
wpi::uv::Udp::GetSendQueueSize
size_t GetSendQueueSize() const noexcept
Gets the amount of queued bytes waiting to be sent.
Definition: Udp.h:362
wpi::uv::Loop
Event loop.
Definition: Loop.h:39
wpi::uv::Udp::SetMembership
void SetMembership(const Twine &multicastAddr, const Twine &interfaceAddr, uv_membership membership)
Set membership for a multicast address.
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
wpi::sig::SignalBase
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
wpi::uv::UdpSendReq
UDP send request.
Definition: Udp.h:31
wpi::uv::Udp::SetMulticastTtl
void SetMulticastTtl(int ttl)
Set the multicast TTL.
Definition: Udp.h:192