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.

99 : loop_{&loop}, host_{std::move(target_host_address)}, af_hint_{af_hint},
100 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.

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

◆ TcpClient() [3/4]

uvco::TcpClient::TcpClient ( TcpClient && other)
noexcept
106 : loop_{other.loop_}, host_{std::move(other.host_)},
107 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.

116 {
117 AddressHandle address;
118 if (resolvedAddress_.has_value()) {
119 address = resolvedAddress_.value();
120 } else {
121 Resolver resolver{*loop_};
122 address = co_await resolver.gai(host_, port_, af_hint_);
123 }
124
125 ConnectAwaiter_ awaiter;
126 const OnExit onExit{[&awaiter, req = awaiter.req_.get()] {
127 if (awaiter.handle_ != nullptr) {
128 resetRequestData(req);
129 }
130 }};
131
132 const uv_status initStatus =
133 uv_tcp_init(loop_->uvloop(), awaiter.socket_.get());
134 if (initStatus < 0) {
135 throw UvcoException(initStatus,
136 "TcpClient::connect() uv_tcp_init() failed");
137 }
138
139 // onConnect callback gets ownership of req_.
140 const uv_status connectStatus =
141 uv_tcp_connect(awaiter.req_.release(), awaiter.socket_.get(),
142 address.sockaddr(), onConnect);
143 if (connectStatus < 0) {
144 // Clean up handle if connect failed.
145 throw UvcoException(connectStatus,
146 "TcpClient::connect() failed immediately");
147 }
148
149 std::optional<UvcoException> maybeError;
150 try {
151 co_return (co_await awaiter);
152 } catch (const UvcoException &e) {
153 maybeError = e;
154 }
155
156 std::unique_ptr<uv_tcp_t> tcpSocket = std::move(awaiter.socket_);
157 closeHandle(tcpSocket.release(), uvCloseReset);
158 BOOST_ASSERT(maybeError.has_value());
159 throw std::move(maybeError.value());
160}
static void onConnect(uv_connect_t *req, uv_status status)
Definition tcp.cc:162
void closeHandle(Handle *handle, void(*closer)(CloserArg *, void(*)(uv_handle_t *)))
Definition close.h:37
void resetRequestData(Request *req)
Definition internal_utils.h:95
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
162 {
163 std::unique_ptr<uv_connect_t> reqPtr{req};
164 auto *awaiter = getRequestDataOrNull<ConnectAwaiter_>(reqPtr.get());
165 if (awaiter == nullptr) {
166 return;
167 }
168 awaiter->onConnect(status);
169 resetRequestData(reqPtr.get());
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
109 {
110 loop_ = other.loop_;
111 host_ = std::move(other.host_);
112 port_ = other.port_;
113 return *this;
114}

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: