uvco 0.1
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
uvco::TcpClient Class Reference

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

#include <tcp.h>

Collaboration diagram for uvco::TcpClient:
Collaboration graph
[legend]

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::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.

30 : loop_{&loop}, host_{std::move(target_host_address)}, af_hint_{af_hint},
31 port_{target_host_port} {}
uint16_t port_
Definition tcp.h:54
int af_hint_
Definition tcp.h:53
const Loop * loop_
Definition tcp.h:49
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.

34 : loop_{&loop}, host_{address.address()}, af_hint_{AF_UNSPEC},
35 port_{address.port()} {}

◆ TcpClient() [3/4]

uvco::TcpClient::TcpClient ( TcpClient &&  other)
noexcept
38 : loop_{other.loop_}, host_{std::move(other.host_)},
39 af_hint_{other.af_hint_}, port_{other.port_} {}
std::unique_ptr< uv_loop_t > loop_
Definition loop.h:59

◆ 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.

48 {
49 Resolver resolver{*loop_};
50
51 AddressHandle address =
52 co_await resolver.gai(host_, fmt::format("{}", port_), af_hint_);
53
54 uv_connect_t req;
55 ConnectAwaiter_ connect{};
56 req.data = &connect;
57
58 auto tcp = std::make_unique<uv_tcp_t>();
59
60 uv_tcp_init(loop_->uvloop(), tcp.get());
61 const uv_status connectStatus =
62 uv_tcp_connect(&req, tcp.get(), address.sockaddr(), onConnect);
63 if (connectStatus < 0) {
64 // Clean up handle if connect failed.
65 co_await closeHandle(tcp.get());
66 throw UvcoException(connectStatus,
67 "TcpClient::connect() failed immediately");
68 }
69
70 const uv_status awaitStatus = co_await connect;
71 if (awaitStatus < 0) {
72 co_await closeHandle(tcp.get());
73 throw UvcoException(awaitStatus, "TcpClient::connect() failed");
74 }
75
76 co_return TcpStream{std::move(tcp)};
77}
uv_loop_t * uvloop() const
Get a non-owned pointer to the loop.
Definition loop.cc:62
Promise< TcpStream > connect()
Definition tcp.cc:48
static void onConnect(uv_connect_t *req, uv_status status)
Definition tcp.cc:79
int uv_status
Result of a libuv operation, an errno error code.
Definition internal_utils.h:22
Promise< void > closeHandle(T *handle, C closer)
Definition close.h:28

◆ onConnect()

void uvco::TcpClient::onConnect ( uv_connect_t *  req,
uv_status  status 
)
staticprivate
79 {
80 auto *connect = getRequestData<ConnectAwaiter_>(req);
81 connect->onConnect(status);
82}

◆ operator=() [1/2]

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

◆ operator=() [2/2]

TcpClient & uvco::TcpClient::operator= ( TcpClient &&  other)
noexcept
41 {
42 loop_ = other.loop_;
43 host_ = std::move(other.host_);
44 port_ = other.port_;
45 return *this;
46}

Member Data Documentation

◆ af_hint_

int uvco::TcpClient::af_hint_
private

◆ host_

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

◆ loop_

const Loop* uvco::TcpClient::loop_
private

◆ port_

uint16_t uvco::TcpClient::port_
private

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