uvw  2.10.0
tcp.h
1 #ifndef UVW_TCP_INCLUDE_H
2 #define UVW_TCP_INCLUDE_H
3 
4 
5 #include <type_traits>
6 #include <utility>
7 #include <memory>
8 #include <string>
9 #include <chrono>
10 #include <uv.h>
11 #include "request.hpp"
12 #include "stream.h"
13 #include "util.h"
14 
15 
16 namespace uvw {
17 
18 
19 namespace details {
20 
21 
22 enum class UVTCPFlags: std::underlying_type_t<uv_tcp_flags> {
23  IPV6ONLY = UV_TCP_IPV6ONLY
24 };
25 
26 
27 }
28 
29 
46 class TCPHandle final: public StreamHandle<TCPHandle, uv_tcp_t> {
47 public:
48  using Time = std::chrono::duration<unsigned int>;
49  using Bind = details::UVTCPFlags;
50  using IPv4 = uvw::IPv4;
51  using IPv6 = uvw::IPv6;
52 
53  explicit TCPHandle(ConstructorAccess ca, std::shared_ptr<Loop> ref, unsigned int f = {});
54 
59  bool init();
60 
69  void open(OSSocketHandle socket);
70 
76  bool noDelay(bool value = false);
77 
85  bool keepAlive(bool enable = false, Time time = Time{0});
86 
101  bool simultaneousAccepts(bool enable = true);
102 
119  void bind(const sockaddr &addr, Flags<Bind> opts = Flags<Bind>{});
120 
138  template<typename I = IPv4>
139  void bind(const std::string &ip, unsigned int port, Flags<Bind> opts = Flags<Bind>{});
140 
157  template<typename I = IPv4>
158  void bind(Addr addr, Flags<Bind> opts = Flags<Bind>{});
159 
164  template<typename I = IPv4>
165  Addr sock() const noexcept;
166 
171  template<typename I = IPv4>
172  Addr peer() const noexcept;
173 
187  void connect(const sockaddr &addr);
188 
199  template<typename I = IPv4>
200  void connect(const std::string &ip, unsigned int port);
201 
211  template<typename I = IPv4>
212  void connect(Addr addr);
213 
225  void closeReset();
226 
227 private:
228  enum { DEFAULT, FLAGS } tag;
229  unsigned int flags;
230 };
231 
232 
239 // (extern) explicit instantiations
240 #ifdef UVW_AS_LIB
241 extern template void TCPHandle::bind<IPv4>(const std::string &, unsigned int, Flags<Bind>);
242 extern template void TCPHandle::bind<IPv6>(const std::string &, unsigned int, Flags<Bind>);
243 
244 extern template void TCPHandle::bind<IPv4>(Addr, Flags<Bind>);
245 extern template void TCPHandle::bind<IPv6>(Addr, Flags<Bind>);
246 
247 extern template Addr TCPHandle::sock<IPv4>() const noexcept;
248 extern template Addr TCPHandle::sock<IPv6>() const noexcept;
249 
250 extern template Addr TCPHandle::peer<IPv4>() const noexcept;
251 extern template Addr TCPHandle::peer<IPv6>() const noexcept;
252 
253 extern template void TCPHandle::connect<IPv4>(const std::string &, unsigned int);
254 extern template void TCPHandle::connect<IPv6>(const std::string &, unsigned int);
255 
256 extern template void TCPHandle::connect<IPv4>(Addr addr);
257 extern template void TCPHandle::connect<IPv6>(Addr addr);
258 #endif // UVW_AS_LIB
259 
260 
267 }
268 
269 
270 #ifndef UVW_AS_LIB
271 #include "tcp.cpp"
272 #endif
273 
274 
275 #endif // UVW_TCP_INCLUDE_H
Utility class to handle flags.
Definition: util.h:82
The StreamHandle handle.
Definition: stream.h:128
The TCPHandle handle.
Definition: tcp.h:46
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:10
details::UVTypeWrapper< uv_os_sock_t > OSSocketHandle
Definition: util.h:190
Address representation.
Definition: util.h:321
The IPv4 tag.
Definition: util.h:307
The IPv6 tag.
Definition: util.h:315