uvw  2.6.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(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(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 
241 extern template void TCPHandle::bind<IPv4>(std::string, unsigned int, Flags<Bind>);
242 extern template void TCPHandle::bind<IPv6>(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>(std::string, unsigned int);
254 extern template void TCPHandle::connect<IPv6>(std::string, unsigned int);
255 
256 extern template void TCPHandle::connect<IPv4>(Addr addr);
257 extern template void TCPHandle::connect<IPv6>(Addr addr);
258 
259 
266 }
267 
268 
269 #ifndef UVW_AS_LIB
270 #include "tcp.cpp"
271 #endif
272 
273 #endif // UVW_TCP_INCLUDE_H
uvw::StreamHandle
The StreamHandle handle.
Definition: stream.h:128
uvw::TCPHandle::closeReset
void closeReset()
Resets a TCP connection by sending a RST packet.
uvw
uvw default namespace.
Definition: async.h:10
uvw::TCPHandle
The TCPHandle handle.
Definition: tcp.h:46
uvw::TCPHandle::bind
void bind(const sockaddr &addr, Flags< Bind > opts=Flags< Bind >{})
Binds the handle to an address and port.
uvw::TCPHandle::simultaneousAccepts
bool simultaneousAccepts(bool enable=true)
Enables/Disables simultaneous asynchronous accept requests.
uvw::TCPHandle::connect
void connect(const sockaddr &addr)
Establishes an IPv4 or IPv6 TCP connection.
uvw::TCPHandle::sock
Addr sock() const noexcept
Gets the current address to which the handle is bound.
uvw::TCPHandle::peer
Addr peer() const noexcept
Gets the address of the peer connected to the handle.
uvw::TCPHandle::noDelay
bool noDelay(bool value=false)
Enables/Disables Nagle’s algorithm.
uvw::IPv4
The IPv4 tag.
Definition: util.h:307
uvw::Flags
Utility class to handle flags.
Definition: util.h:82
uvw::Addr
Address representation.
Definition: util.h:321
uvw::TCPHandle::init
bool init()
Initializes the handle. No socket is created as of yet.
uvw::TCPHandle::open
void open(OSSocketHandle socket)
Opens an existing file descriptor or SOCKET as a TCP handle.
uvw::OSSocketHandle
details::UVTypeWrapper< uv_os_sock_t > OSSocketHandle
Definition: util.h:190
uvw::TCPHandle::keepAlive
bool keepAlive(bool enable=false, Time time=Time{0})
Enables/Disables TCP keep-alive.
uvw::IPv6
The IPv6 tag.
Definition: util.h:315