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

Asynchronous name resolution using the libuv getaddrinfo(3) interface. More...

#include <name_resolution.h>

Collaboration diagram for uvco::Resolver:

Public Member Functions

 Resolver (const Loop &loop)
 Instantiate a resolver based on an event loop.
Promise< AddressHandlegai (std::string_view host, std::string_view port, int af_hint=AF_UNSPEC)
 Resolve a host and port string. Throws an UvcoException upon error.
Promise< AddressHandlegai (std::string_view host, uint16_t port, int af_hint=AF_UNSPEC)

Static Private Member Functions

static void onAddrinfo (uv_getaddrinfo_t *req, uv_status status, struct addrinfo *result)
 Called after finishing a getaddrinfo call.

Private Attributes

const Looploop_

Detailed Description

Asynchronous name resolution using the libuv getaddrinfo(3) interface.

Constructor & Destructor Documentation

◆ Resolver()

uvco::Resolver::Resolver ( const Loop & loop)
inlineexplicit

Instantiate a resolver based on an event loop.

81: loop_{&loop} {}
const Loop * loop_
Definition name_resolution.h:92

Member Function Documentation

◆ gai() [1/2]

Promise< AddressHandle > uvco::Resolver::gai ( std::string_view host,
std::string_view port,
int af_hint = AF_UNSPEC )

Resolve a host and port string. Throws an UvcoException upon error.

228 {
229 AddrinfoAwaiter_ awaiter;
230 struct addrinfo hints{};
231 hints.ai_family = af_hint;
232 hints.ai_socktype = SOCK_STREAM;
233
234 uv_getaddrinfo(loop_->uvloop(), awaiter.req_.get(), onAddrinfo, host.data(),
235 port.data(), &hints);
236 // Npte: we rely on libuv not resuming before awaiting the result.
237 struct addrinfo *result = co_await awaiter;
238
239 const uv_status status = awaiter.status_.value();
240 if (status != 0) {
241 throw UvcoException{status, "getaddrinfo()"};
242 }
243
244 co_return AddressHandle{result};
245}
static void onAddrinfo(uv_getaddrinfo_t *req, uv_status status, struct addrinfo *result)
Called after finishing a getaddrinfo call.
Definition name_resolution.cc:247
int uv_status
Result of a libuv operation, an errno error code.
Definition internal_utils.h:22

◆ gai() [2/2]

Promise< AddressHandle > uvco::Resolver::gai ( std::string_view host,
uint16_t port,
int af_hint = AF_UNSPEC )

Resolve a host string and numeric port. Throws an UvcoException upon error.

222 {
223 const std::string portStr = std::to_string(port);
224 co_return (co_await gai(host, portStr, af_hint));
225}
Promise< AddressHandle > gai(std::string_view host, std::string_view port, int af_hint=AF_UNSPEC)
Resolve a host and port string. Throws an UvcoException upon error.
Definition name_resolution.cc:227

◆ onAddrinfo()

void uvco::Resolver::onAddrinfo ( uv_getaddrinfo_t * req,
uv_status status,
struct addrinfo * result )
staticprivate

Called after finishing a getaddrinfo call.

248 {
249 // asserts request data not null; the only time this could be the case is
250 // after a lookup is cancelled.
251 auto *awaiter = getRequestDataOrNull<AddrinfoAwaiter_>(req);
252 if (awaiter == nullptr) {
253 // cancelled
254 delete req;
255 uv_freeaddrinfo(result);
256 return;
257 }
258 if (status == UV_ECANCELED) {
259 return;
260 }
261 awaiter->addrinfo_ = result;
262 awaiter->status_ = status;
263 BOOST_ASSERT(awaiter->handle_ != nullptr);
264 Loop::enqueue(awaiter->handle_);
265}
static void enqueue(std::coroutine_handle<> handle)
Definition loop.cc:94
Into * getRequestDataOrNull(const Request *req)
Definition internal_utils.h:69

Member Data Documentation

◆ loop_

const Loop* uvco::Resolver::loop_
private

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