uvco 0.1
Loading...
Searching...
No Matches
uvco::TcpClient Class Reference

A client for connecting to a TCP peer. More...

#include <tcp.h>

Collaboration diagram for uvco::TcpClient:

Classes

struct  ConnectAwaiter_

Public Member Functions

 TcpClient (const Loop &loop, std::string target_host_address, uint16_t target_host_port, int af_hint=AF_UNSPEC)
 TcpClient (const Loop &loop, AddressHandle address)
 Create a TCP client connecting to the given address.
 TcpClient (TcpClient &&other) noexcept
 TcpClient (const TcpClient &)=delete
TcpClientoperator= (TcpClient &&other) noexcept
TcpClientoperator= (const TcpClient &)=delete
 ~TcpClient ()=default
Promise< TcpStreamconnect ()

Static Private Member Functions

static void onConnect (uv_connect_t *req, uv_status status)

Private Attributes

const Looploop_
std::optional< AddressHandleresolvedAddress_
std::string host_
int af_hint_ {}
uint16_t port_ {}

Detailed Description

A client for connecting to a TCP peer.

Constructor & Destructor Documentation

◆ TcpClient() [1/4]

uvco::TcpClient::TcpClient ( const Loop & loop,
std::string target_host_address,
uint16_t target_host_port,
int af_hint = AF_UNSPEC )

Create a client; call connect() to obtain a TcpStream. Address can be given as domain name, IP, etc.

103 : loop_{&loop}, host_{std::move(target_host_address)}, af_hint_{af_hint},
104 port_{target_host_port} {}
uint16_t port_
Definition tcp.h:54
int af_hint_
Definition tcp.h:53
const Loop * loop_
Definition tcp.h:48
std::string host_
Definition tcp.h:52

◆ TcpClient() [2/4]

uvco::TcpClient::TcpClient ( const Loop & loop,
AddressHandle address )

Create a TCP client connecting to the given address.

107 : loop_{&loop}, resolvedAddress_{address} {}
std::optional< AddressHandle > resolvedAddress_
Definition tcp.h:50

◆ TcpClient() [3/4]

uvco::TcpClient::TcpClient ( TcpClient && other)
noexcept
110 : loop_{other.loop_}, host_{std::move(other.host_)},
111 af_hint_{other.af_hint_}, port_{other.port_} {}

◆ TcpClient() [4/4]

uvco::TcpClient::TcpClient ( const TcpClient & )
delete

◆ ~TcpClient()

uvco::TcpClient::~TcpClient ( )
default

Member Function Documentation

◆ connect()

Promise< TcpStream > uvco::TcpClient::connect ( )

Connect to the peer specified in the constructor. The TcpClient object is meaningless after this call and can be destroyed.

120 {
121 AddressHandle address;
122 if (resolvedAddress_.has_value()) {
123 address = resolvedAddress_.value();
124 } else {
125 Resolver resolver{*loop_};
126 address = co_await resolver.gai(host_, port_, af_hint_);
127 }
128
130
131 const uv_status initStatus =
132 uv_tcp_init(loop_->uvloop(), connect.socket_.get());
133 if (initStatus < 0) {
134 throw UvcoException(initStatus,
135 "TcpClient::connect() uv_tcp_init() failed");
136 }
137
138 const uv_status connectStatus = uv_tcp_connect(
139 connect.req_.get(), connect.socket_.get(), address.sockaddr(), onConnect);
140 if (connectStatus < 0) {
141 // Clean up handle if connect failed.
142 throw UvcoException(connectStatus,
143 "TcpClient::connect() failed immediately");
144 }
145
146 std::optional<UvcoException> maybeError;
147 try {
148 co_return (co_await connect);
149 } catch (const UvcoException &e) {
150 maybeError = e;
151 }
152
153 std::unique_ptr<uv_tcp_t> tcpSocket = std::move(connect.socket_);
154 closeHandle(tcpSocket.release(), uvCloseReset);
155 BOOST_ASSERT(maybeError.has_value());
156 throw std::move(maybeError.value());
157}
Promise< TcpStream > connect()
Definition tcp.cc:120
static void onConnect(uv_connect_t *req, uv_status status)
Definition tcp.cc:159
void closeHandle(Handle *handle, void(*closer)(CloserArg *, void(*)(uv_handle_t *)))
Definition close.h:37
int uv_status
Result of a libuv operation, an errno error code.
Definition internal_utils.h:22

◆ onConnect()

void uvco::TcpClient::onConnect ( uv_connect_t * req,
uv_status status )
staticprivate
159 {
161 if (connect == nullptr) {
162 // Cancelled
163 delete req;
164 return;
165 }
166 if (status == UV_ECANCELED) {
167 return;
168 }
169 connect->onConnect(status);
170}
Into * getRequestDataOrNull(const Request *req)
Definition internal_utils.h:69

◆ operator=() [1/2]

TcpClient & uvco::TcpClient::operator= ( const TcpClient & )
delete

◆ operator=() [2/2]

TcpClient & uvco::TcpClient::operator= ( TcpClient && other)
noexcept
113 {
114 loop_ = other.loop_;
115 host_ = std::move(other.host_);
116 port_ = other.port_;
117 return *this;
118}

Member Data Documentation

◆ af_hint_

int uvco::TcpClient::af_hint_ {}
private
53{};

◆ host_

std::string uvco::TcpClient::host_
private

◆ loop_

const Loop* uvco::TcpClient::loop_
private

◆ port_

uint16_t uvco::TcpClient::port_ {}
private
54{};

◆ resolvedAddress_

std::optional<AddressHandle> uvco::TcpClient::resolvedAddress_
private

The documentation for this class was generated from the following files: