WPILibC++  2020.3.2
Tcp.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_TCP_H_
9 #define WPIUTIL_WPI_UV_TCP_H_
10 
11 #include <uv.h>
12 
13 #include <chrono>
14 #include <functional>
15 #include <memory>
16 
17 #include "wpi/Twine.h"
18 #include "wpi/uv/NetworkStream.h"
19 
20 namespace wpi {
21 namespace uv {
22 
23 class Loop;
24 class TcpConnectReq;
25 
30 class Tcp final : public NetworkStreamImpl<Tcp, uv_tcp_t> {
31  struct private_init {};
32 
33  public:
34  using Time = std::chrono::duration<uint64_t, std::milli>;
35 
36  explicit Tcp(const private_init&) {}
37  ~Tcp() noexcept override = default;
38 
45  static std::shared_ptr<Tcp> Create(Loop& loop,
46  unsigned int flags = AF_UNSPEC);
47 
54  static std::shared_ptr<Tcp> Create(const std::shared_ptr<Loop>& loop,
55  unsigned int flags = AF_UNSPEC) {
56  return Create(*loop, flags);
57  }
58 
69  void Reuse(std::function<void()> callback, unsigned int flags = AF_UNSPEC);
70 
86  std::shared_ptr<Tcp> Accept();
87 
104  bool Accept(const std::shared_ptr<Tcp>& client) {
105  return NetworkStream::Accept(client);
106  }
107 
116  void Open(uv_os_sock_t sock) { Invoke(&uv_tcp_open, GetRaw(), sock); }
117 
123  bool SetNoDelay(bool enable) { return uv_tcp_nodelay(GetRaw(), enable) == 0; }
124 
132  bool SetKeepAlive(bool enable, Time time = Time{0}) {
133  return uv_tcp_keepalive(GetRaw(), enable,
134  static_cast<unsigned>(time.count())) == 0;
135  }
136 
151  bool SetSimultaneousAccepts(bool enable) {
152  return uv_tcp_simultaneous_accepts(GetRaw(), enable) == 0;
153  }
154 
166  void Bind(const sockaddr& addr, unsigned int flags = 0) {
167  Invoke(&uv_tcp_bind, GetRaw(), &addr, flags);
168  }
169 
170  void Bind(const sockaddr_in& addr, unsigned int flags = 0) {
171  Bind(reinterpret_cast<const sockaddr&>(addr), flags);
172  }
173 
174  void Bind(const sockaddr_in6& addr, unsigned int flags = 0) {
175  Bind(reinterpret_cast<const sockaddr&>(addr), flags);
176  }
177 
192  void Bind(const Twine& ip, unsigned int port, unsigned int flags = 0);
193 
208  void Bind6(const Twine& ip, unsigned int port, unsigned int flags = 0);
209 
214  sockaddr_storage GetSock();
215 
220  sockaddr_storage GetPeer();
221 
237  void Connect(const sockaddr& addr, const std::shared_ptr<TcpConnectReq>& req);
238 
239  void Connect(const sockaddr_in& addr,
240  const std::shared_ptr<TcpConnectReq>& req) {
241  Connect(reinterpret_cast<const sockaddr&>(addr), req);
242  }
243 
244  void Connect(const sockaddr_in6& addr,
245  const std::shared_ptr<TcpConnectReq>& req) {
246  Connect(reinterpret_cast<const sockaddr&>(addr), req);
247  }
248 
262  void Connect(const sockaddr& addr, std::function<void()> callback);
263 
264  void Connect(const sockaddr_in& addr, std::function<void()> callback) {
265  Connect(reinterpret_cast<const sockaddr&>(addr), callback);
266  }
267 
268  void Connect(const sockaddr_in6& addr, std::function<void()> callback) {
269  Connect(reinterpret_cast<const sockaddr&>(addr), callback);
270  }
271 
288  void Connect(const Twine& ip, unsigned int port,
289  const std::shared_ptr<TcpConnectReq>& req);
290 
305  void Connect(const Twine& ip, unsigned int port,
306  std::function<void()> callback);
307 
324  void Connect6(const Twine& ip, unsigned int port,
325  const std::shared_ptr<TcpConnectReq>& req);
326 
341  void Connect6(const Twine& ip, unsigned int port,
342  std::function<void()> callback);
343 
344  private:
345  Tcp* DoAccept() override;
346 
347  struct ReuseData {
348  std::function<void()> callback;
349  unsigned int flags;
350  };
351  std::unique_ptr<ReuseData> m_reuseData;
352 };
353 
357 class TcpConnectReq : public ConnectReq {
358  public:
359  Tcp& GetStream() const {
360  return *static_cast<Tcp*>(&ConnectReq::GetStream());
361  }
362 };
363 
364 } // namespace uv
365 } // namespace wpi
366 
367 #endif // WPIUTIL_WPI_UV_TCP_H_
wpi::uv::Tcp::Create
static std::shared_ptr< Tcp > Create(const std::shared_ptr< Loop > &loop, unsigned int flags=AF_UNSPEC)
Create a TCP handle.
Definition: Tcp.h:54
wpi::uv::ConnectReq
Connection request.
Definition: NetworkStream.h:27
wpi::uv::Tcp::Create
static std::shared_ptr< Tcp > Create(Loop &loop, unsigned int flags=AF_UNSPEC)
Create a TCP handle.
wpi::uv::Tcp::Connect
void Connect(const sockaddr &addr, const std::shared_ptr< TcpConnectReq > &req)
Establish an IPv4 or IPv6 TCP connection.
wpi::uv::Tcp::Bind6
void Bind6(const Twine &ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv6 address and port.
wpi::uv::Tcp::SetKeepAlive
bool SetKeepAlive(bool enable, Time time=Time{0})
Enable/Disable TCP keep-alive.
Definition: Tcp.h:132
wpi::uv::Tcp::Accept
bool Accept(const std::shared_ptr< Tcp > &client)
Accept incoming connection.
Definition: Tcp.h:104
wpi::uv::Tcp::Open
void Open(uv_os_sock_t sock)
Open an existing file descriptor or SOCKET as a TCP handle.
Definition: Tcp.h:116
wpi::uv::Tcp::GetSock
sockaddr_storage GetSock()
Get the current address to which the handle is bound.
wpi::uv::Tcp::SetNoDelay
bool SetNoDelay(bool enable)
Enable/Disable Nagle's algorithm.
Definition: Tcp.h:123
wpi::uv::Tcp::Reuse
void Reuse(std::function< void()> callback, unsigned int flags=AF_UNSPEC)
Reuse this handle.
wpi::uv::NetworkStreamImpl
Definition: NetworkStream.h:130
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
wpi::uv::NetworkStream::Accept
std::shared_ptr< NetworkStream > Accept()
Accept incoming connection.
Definition: NetworkStream.h:94
wpi::uv::Tcp
TCP handle.
Definition: Tcp.h:30
wpi::uv::Tcp::Bind
void Bind(const sockaddr &addr, unsigned int flags=0)
Bind the handle to an IPv4 or IPv6 address and port.
Definition: Tcp.h:166
wpi::uv::Tcp::GetPeer
sockaddr_storage GetPeer()
Get the address of the peer connected to the handle.
wpi::uv::Tcp::Connect6
void Connect6(const Twine &ip, unsigned int port, const std::shared_ptr< TcpConnectReq > &req)
Establish an IPv6 TCP connection.
wpi::uv::NetworkStreamImpl< Tcp, uv_tcp_t >::GetRaw
uv_tcp_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: NetworkStream.h:145
wpi::uv::Tcp::Accept
std::shared_ptr< Tcp > Accept()
Accept incoming connection.
wpi::uv::Loop
Event loop.
Definition: Loop.h:39
wpi::uv::TcpConnectReq
TCP connection request.
Definition: Tcp.h:357
wpi::uv::Tcp::SetSimultaneousAccepts
bool SetSimultaneousAccepts(bool enable)
Enable/Disable simultaneous asynchronous accept requests.
Definition: Tcp.h:151