5#ifndef WPINET_UV_TCP_H_
6#define WPINET_UV_TCP_H_
28 struct private_init {};
31 using Time = std::chrono::duration<uint64_t, std::milli>;
33 explicit Tcp(
const private_init&) {}
34 ~Tcp() noexcept override = default;
43 unsigned int flags = AF_UNSPEC);
52 unsigned int flags = AF_UNSPEC) {
66 void Reuse(std::function<
void()> callback,
unsigned int flags = AF_UNSPEC);
101 bool Accept(
const std::shared_ptr<Tcp>& client) {
131 static_cast<unsigned>(time.count())) == 0;
163 void Bind(
const sockaddr& addr,
unsigned int flags = 0) {
167 void Bind(
const sockaddr_in& addr,
unsigned int flags = 0) {
168 Bind(
reinterpret_cast<const sockaddr&
>(addr),
flags);
171 void Bind(
const sockaddr_in6& addr,
unsigned int flags = 0) {
172 Bind(
reinterpret_cast<const sockaddr&
>(addr),
flags);
234 void Connect(
const sockaddr& addr,
const std::shared_ptr<TcpConnectReq>& req);
237 const std::shared_ptr<TcpConnectReq>& req) {
238 Connect(
reinterpret_cast<const sockaddr&
>(addr), req);
242 const std::shared_ptr<TcpConnectReq>& req) {
243 Connect(
reinterpret_cast<const sockaddr&
>(addr), req);
259 void Connect(
const sockaddr& addr, std::function<
void()> callback);
261 void Connect(
const sockaddr_in& addr, std::function<
void()> callback) {
262 Connect(
reinterpret_cast<const sockaddr&
>(addr), std::move(callback));
265 void Connect(
const sockaddr_in6& addr, std::function<
void()> callback) {
266 Connect(
reinterpret_cast<const sockaddr&
>(addr), std::move(callback));
286 const std::shared_ptr<TcpConnectReq>& req);
303 std::function<
void()> callback);
322 const std::shared_ptr<TcpConnectReq>& req);
339 std::function<
void()> callback);
350 Tcp* DoAccept()
override;
353 std::function<void()> callback;
356 std::unique_ptr<ReuseData> m_reuseData;
Connection request.
Definition: NetworkStream.h:25
NetworkStream & GetStream() const
Definition: NetworkStream.h:29
bool Invoke(F &&f, Args &&... args) const
Definition: Handle.h:267
Event loop.
Definition: Loop.h:37
std::shared_ptr< NetworkStream > Accept()
Accept incoming connection.
Definition: NetworkStream.h:92
Definition: NetworkStream.h:128
uv_tcp_t * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: NetworkStream.h:143
TCP connection request.
Definition: Tcp.h:362
Tcp & GetStream() const
Definition: Tcp.h:364
TCP handle.
Definition: Tcp.h:27
static std::shared_ptr< Tcp > Create(Loop &loop, unsigned int flags=AF_UNSPEC)
Create a TCP handle.
void Connect6(std::string_view ip, unsigned int port, const std::shared_ptr< TcpConnectReq > &req)
Establish an IPv6 TCP connection.
std::chrono::duration< uint64_t, std::milli > Time
Definition: Tcp.h:31
void Connect(const sockaddr_in &addr, const std::shared_ptr< TcpConnectReq > &req)
Definition: Tcp.h:236
void Connect(const sockaddr_in6 &addr, const std::shared_ptr< TcpConnectReq > &req)
Definition: Tcp.h:241
~Tcp() noexcept override=default
void Bind(std::string_view ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv4 address and port.
bool SetNoDelay(bool enable)
Enable no delay operation (turns off Nagle's algorithm).
Definition: Tcp.h:120
bool SetSimultaneousAccepts(bool enable)
Enable/Disable simultaneous asynchronous accept requests.
Definition: Tcp.h:148
void Connect(const sockaddr_in &addr, std::function< void()> callback)
Definition: Tcp.h:261
bool Accept(const std::shared_ptr< Tcp > &client)
Accept incoming connection.
Definition: Tcp.h:101
void Connect(const sockaddr_in6 &addr, std::function< void()> callback)
Definition: Tcp.h:265
Tcp(const private_init &)
Definition: Tcp.h:33
void Connect(std::string_view ip, unsigned int port, std::function< void()> callback)
Establish an IPv4 TCP connection.
void Connect6(std::string_view ip, unsigned int port, std::function< void()> callback)
Establish an IPv6 TCP connection.
void Connect(const sockaddr &addr, std::function< void()> callback)
Establish an IPv4 or IPv6 TCP connection.
void Bind(const sockaddr_in6 &addr, unsigned int flags=0)
Definition: Tcp.h:171
void Open(uv_os_sock_t sock)
Open an existing file descriptor or SOCKET as a TCP handle.
Definition: Tcp.h:113
void Connect(std::string_view ip, unsigned int port, const std::shared_ptr< TcpConnectReq > &req)
Establish an IPv4 TCP connection.
void Bind6(std::string_view ip, unsigned int port, unsigned int flags=0)
Bind the handle to an IPv6 address and port.
std::shared_ptr< Tcp > Accept()
Accept incoming connection.
void Bind(const sockaddr_in &addr, unsigned int flags=0)
Definition: Tcp.h:167
void Connect(const sockaddr &addr, const std::shared_ptr< TcpConnectReq > &req)
Establish an IPv4 or IPv6 TCP connection.
sockaddr_storage GetSock()
Get the current address to which the handle is bound.
sockaddr_storage GetPeer()
Get the address of the peer connected to the handle.
bool SetKeepAlive(bool enable, Time time=Time{0})
Enable/Disable TCP keep-alive.
Definition: Tcp.h:129
void Reuse(std::function< void()> callback, unsigned int flags=AF_UNSPEC)
Reuse this handle.
void CloseReset()
Resets a TCP connection by sending a RST packet.
void Bind(const sockaddr &addr, unsigned int flags=0)
Bind the handle to an IPv4 or IPv6 address and port.
Definition: Tcp.h:163
basic_string_view< char > string_view
Definition: core.h:501
Definition: WebSocket.h:27
flags
Definition: http_parser.h:206
int uv_os_sock_t
Definition: unix.h:118
UV_EXTERN int uv_tcp_keepalive(uv_tcp_t *handle, int enable, unsigned int delay)
UV_EXTERN int uv_tcp_open(uv_tcp_t *handle, uv_os_sock_t sock)
UV_EXTERN int uv_tcp_nodelay(uv_tcp_t *handle, int enable)
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t *handle, int enable)