WPILibC++  2019.1.1-beta-4-32-g6bdd7ce
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Udp.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 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 
122  sockaddr_storage GetSock();
123 
131  void SetMembership(const Twine& multicastAddr, const Twine& interfaceAddr,
132  uv_membership membership);
133 
140  void SetMulticastLoop(bool enabled) {
141  Invoke(&uv_udp_set_multicast_loop, GetRaw(), enabled ? 1 : 0);
142  }
143 
149  void SetMulticastTtl(int ttl) {
150  Invoke(&uv_udp_set_multicast_ttl, GetRaw(), ttl);
151  }
152 
158  void SetMulticastInterface(const Twine& interfaceAddr);
159 
165  void SetBroadcast(bool enabled) {
166  Invoke(&uv_udp_set_broadcast, GetRaw(), enabled ? 1 : 0);
167  }
168 
174  void SetTtl(int ttl) { Invoke(&uv_udp_set_ttl, GetRaw(), ttl); }
175 
194  void Send(const sockaddr& addr, ArrayRef<Buffer> bufs,
195  const std::shared_ptr<UdpSendReq>& req);
196 
197  void Send(const sockaddr_in& addr, ArrayRef<Buffer> bufs,
198  const std::shared_ptr<UdpSendReq>& req) {
199  Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
200  }
201 
202  void Send(const sockaddr_in6& addr, ArrayRef<Buffer> bufs,
203  const std::shared_ptr<UdpSendReq>& req) {
204  Send(reinterpret_cast<const sockaddr&>(addr), bufs, req);
205  }
206 
224  void Send(const sockaddr& addr, ArrayRef<Buffer> bufs,
225  std::function<void(MutableArrayRef<Buffer>, Error)> callback);
226 
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);
230  }
231 
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);
235  }
236 
246  int TrySend(const sockaddr& addr, ArrayRef<Buffer> bufs) {
247  int val = uv_udp_try_send(GetRaw(), bufs.data(), bufs.size(), &addr);
248  if (val < 0) {
249  this->ReportError(val);
250  return 0;
251  }
252  return val;
253  }
254 
255  int TrySend(const sockaddr_in& addr, ArrayRef<Buffer> bufs) {
256  return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
257  }
258 
259  int TrySend(const sockaddr_in6& addr, ArrayRef<Buffer> bufs) {
260  return TrySend(reinterpret_cast<const sockaddr&>(addr), bufs);
261  }
262 
271  void StartRecv();
272 
276  void StopRecv() { Invoke(&uv_udp_recv_stop, GetRaw()); }
277 
282  size_t GetSendQueueSize() const noexcept { return GetRaw()->send_queue_size; }
283 
288  size_t GetSendQueueCount() const noexcept {
289  return GetRaw()->send_queue_count;
290  }
291 
297 };
298 
299 } // namespace uv
300 } // namespace wpi
301 
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:264
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:249
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:215
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