uvw  2.12.1
tcp.h
1 #ifndef UVW_TCP_INCLUDE_H
2 #define UVW_TCP_INCLUDE_H
3 
4 #include <chrono>
5 #include <memory>
6 #include <string>
7 #include <type_traits>
8 #include <utility>
9 #include <uv.h>
10 #include "request.hpp"
11 #include "stream.h"
12 #include "util.h"
13 
14 namespace uvw {
15 
16 namespace details {
17 
18 enum class UVTCPFlags : std::underlying_type_t<uv_tcp_flags> {
19  IPV6ONLY = UV_TCP_IPV6ONLY
20 };
21 
22 }
23 
40 class TCPHandle final: public StreamHandle<TCPHandle, uv_tcp_t> {
41 public:
42  using Time = std::chrono::duration<unsigned int>;
43  using Bind = details::UVTCPFlags;
44  using IPv4 = uvw::IPv4;
45  using IPv6 = uvw::IPv6;
46 
47  explicit TCPHandle(ConstructorAccess ca, std::shared_ptr<Loop> ref, unsigned int f = {});
48 
53  bool init();
54 
63  void open(OSSocketHandle socket);
64 
70  bool noDelay(bool value = false);
71 
79  bool keepAlive(bool enable = false, Time time = Time{0});
80 
95  bool simultaneousAccepts(bool enable = true);
96 
113  void bind(const sockaddr &addr, Flags<Bind> opts = Flags<Bind>{});
114 
132  template<typename I = IPv4>
133  void bind(const std::string &ip, unsigned int port, Flags<Bind> opts = Flags<Bind>{});
134 
151  template<typename I = IPv4>
152  void bind(Addr addr, Flags<Bind> opts = Flags<Bind>{});
153 
158  template<typename I = IPv4>
159  Addr sock() const noexcept;
160 
165  template<typename I = IPv4>
166  Addr peer() const noexcept;
167 
181  void connect(const sockaddr &addr);
182 
193  template<typename I = IPv4>
194  void connect(const std::string &ip, unsigned int port);
195 
205  template<typename I = IPv4>
206  void connect(Addr addr);
207 
219  void closeReset();
220 
221 private:
222  enum {
223  DEFAULT,
224  FLAGS
225  } tag;
226 
227  unsigned int flags;
228 };
229 
235 // (extern) explicit instantiations
236 #ifdef UVW_AS_LIB
237 extern template void TCPHandle::bind<IPv4>(const std::string &, unsigned int, Flags<Bind>);
238 extern template void TCPHandle::bind<IPv6>(const std::string &, unsigned int, Flags<Bind>);
239 
240 extern template void TCPHandle::bind<IPv4>(Addr, Flags<Bind>);
241 extern template void TCPHandle::bind<IPv6>(Addr, Flags<Bind>);
242 
243 extern template Addr TCPHandle::sock<IPv4>() const noexcept;
244 extern template Addr TCPHandle::sock<IPv6>() const noexcept;
245 
246 extern template Addr TCPHandle::peer<IPv4>() const noexcept;
247 extern template Addr TCPHandle::peer<IPv6>() const noexcept;
248 
249 extern template void TCPHandle::connect<IPv4>(const std::string &, unsigned int);
250 extern template void TCPHandle::connect<IPv6>(const std::string &, unsigned int);
251 
252 extern template void TCPHandle::connect<IPv4>(Addr addr);
253 extern template void TCPHandle::connect<IPv6>(Addr addr);
254 #endif // UVW_AS_LIB
255 
261 } // namespace uvw
262 
263 #ifndef UVW_AS_LIB
264 # include "tcp.cpp"
265 #endif
266 
267 #endif // UVW_TCP_INCLUDE_H
Utility class to handle flags.
Definition: util.h:79
The StreamHandle handle.
Definition: stream.h:113
The TCPHandle handle.
Definition: tcp.h:40
bool init()
Initializes the handle. No socket is created as of yet.
Addr sock() const noexcept
Gets the current address to which the handle is bound.
void closeReset()
Resets a TCP connection by sending a RST packet.
void open(OSSocketHandle socket)
Opens an existing file descriptor or SOCKET as a TCP handle.
bool keepAlive(bool enable=false, Time time=Time{0})
Enables/Disables TCP keep-alive.
bool noDelay(bool value=false)
Enables/Disables Nagle’s algorithm.
Addr peer() const noexcept
Gets the address of the peer connected to the handle.
bool simultaneousAccepts(bool enable=true)
Enables/Disables simultaneous asynchronous accept requests.
void bind(const std::string &ip, unsigned int port, Flags< Bind > opts=Flags< Bind >{})
Binds the handle to an address and port.
void bind(Addr addr, Flags< Bind > opts=Flags< Bind >{})
Binds the handle to an address and port.
void connect(const sockaddr &addr)
Establishes an IPv4 or IPv6 TCP connection.
void bind(const sockaddr &addr, Flags< Bind > opts=Flags< Bind >{})
Binds the handle to an address and port.
uvw default namespace.
Definition: async.h:8
details::UVTypeWrapper< uv_os_sock_t > OSSocketHandle
Definition: util.h:205
Address representation.
Definition: util.h:331
The IPv4 tag.
Definition: util.h:319
The IPv6 tag.
Definition: util.h:326