2 #ifndef UVCC_REQUEST_DNS__HPP 3 #define UVCC_REQUEST_DNS__HPP 5 #include "uvcc/utility.hpp" 6 #include "uvcc/request-base.hpp" 7 #include "uvcc/loop.hpp" 12 #include <type_traits> 25 friend class request::instance< getaddrinfo >;
29 using uv_t = ::uv_getaddrinfo_t;
30 using on_request_t = std::function<
void(
getaddrinfo _request) >;
38 struct properties :
request::properties
40 uv_t *uv_req =
nullptr;
41 ~properties() {
if (uv_req) ::uv_freeaddrinfo(uv_req->addrinfo); }
51 explicit getaddrinfo(uv_t *_uv_req) :
request(
reinterpret_cast<
request::uv_t* >(_uv_req)) {}
55 ~getaddrinfo() =
default;
58 uv_req = instance::create();
59 static_cast< uv_t* >(uv_req)->type = UV_GETADDRINFO;
60 static_cast< uv_t* >(uv_req)->addrinfo =
nullptr;
61 instance::from(uv_req)->properties().uv_req =
static_cast< uv_t* >(uv_req);
71 template<
typename =
void >
static void getaddrinfo_cb(::uv_getaddrinfo_t*,
int, ::addrinfo*);
73 int run_(
uv::
loop &_loop,
const char *_hostname,
const char *_service,
const ::addrinfo *_hints)
75 ::uv_freeaddrinfo(
static_cast< uv_t* >(uv_req)->addrinfo);
77 auto instance_ptr = instance::from(uv_req);
79 if (!instance_ptr->request_cb_storage.value())
81 return uv_status(::uv_getaddrinfo(
82 static_cast<
uv::
loop::uv_t* >(_loop),
static_cast< uv_t* >(uv_req),
84 _hostname, _service, _hints
92 auto uv_ret = ::uv_getaddrinfo(
93 static_cast< uv::loop::uv_t* >(_loop),
static_cast< uv_t* >(uv_req),
95 _hostname, _service, _hints
100 instance_ptr->unref();
108 on_request_t& on_request()
const noexcept {
return instance::from(uv_req)->request_cb_storage.value(); }
112 uv::
loop loop()
const noexcept {
return uv::loop(
static_cast< uv_t* >(uv_req)->loop); }
115 const ::addrinfo* addrinfo()
const noexcept {
return static_cast< uv_t* >(uv_req)->addrinfo; }
122 int run(
uv::
loop &_loop,
const char *_hostname,
const char *_service,
const ::addrinfo &_hints)
124 return run_(_loop, _hostname, _service, &_hints);
127 int run(
uv::
loop &_loop,
const char *_hostname,
const char *_service)
129 return run_(_loop, _hostname, _service,
nullptr);
133 explicit operator
const uv_t*()
const noexcept {
return static_cast<
const uv_t* >(uv_req); }
134 explicit operator uv_t*()
noexcept {
return static_cast< uv_t* >(uv_req); }
138 void getaddrinfo::getaddrinfo_cb(::uv_getaddrinfo_t *_uv_req,
int _status, ::addrinfo *_result)
140 auto instance_ptr = instance::from(_uv_req);
141 instance_ptr->uv_error = _status;
143 ref_guard< instance > unref_req(*instance_ptr, adopt_ref);
145 auto &getaddrinfo_cb = instance_ptr->request_cb_storage.value();
146 if (getaddrinfo_cb) getaddrinfo_cb(getaddrinfo(_uv_req));
157 friend class request::instance< getnameinfo >;
161 using uv_t = ::uv_getnameinfo_t;
162 using on_request_t = std::function<
void(
getnameinfo _request) >;
171 explicit getnameinfo(uv_t *_uv_req) :
request(
reinterpret_cast<
request::uv_t* >(_uv_req)) {}
175 ~getnameinfo() =
default;
178 uv_req = instance::create();
179 static_cast< uv_t* >(uv_req)->type = UV_GETNAMEINFO;
189 template<
typename =
void >
static void getnameinfo_cb(::uv_getnameinfo_t*,
int,
const char*,
const char*);
192 on_request_t& on_request()
const noexcept {
return instance::from(uv_req)->request_cb_storage.value(); }
196 uv::
loop loop()
const noexcept {
return uv::loop(
static_cast< uv_t* >(uv_req)->loop); }
199 const char (&
host()
const noexcept)[
NI_MAXHOST] {
return static_cast< uv_t* >(uv_req)->host; }
201 const char (&
service()
const noexcept)[
NI_MAXSERV] {
return static_cast< uv_t* >(uv_req)->service; }
217 int run(
uv::
loop &_loop,
const _T_ &_sa,
int _NI_FLAGS = 0)
219 auto instance_ptr = instance::from(uv_req);
221 if (!instance_ptr->request_cb_storage.value())
223 return uv_status(::uv_getnameinfo(
224 static_cast<
uv::
loop::uv_t* >(_loop),
static_cast< uv_t* >(uv_req),
226 reinterpret_cast<
const ::sockaddr* >(&_sa), _NI_FLAGS
234 auto uv_ret = ::uv_getnameinfo(
235 static_cast< uv::loop::uv_t* >(_loop),
static_cast< uv_t* >(uv_req),
237 reinterpret_cast<
const ::sockaddr* >(&_sa), _NI_FLAGS
242 instance_ptr->unref();
250 explicit operator
const uv_t*()
const noexcept {
return static_cast<
const uv_t* >(uv_req); }
251 explicit operator uv_t*()
noexcept {
return static_cast< uv_t* >(uv_req); }
255 void getnameinfo::getnameinfo_cb(::uv_getnameinfo_t *_uv_req,
int _status,
const char* _hostname,
const char* _service)
257 auto instance_ptr = instance::from(_uv_req);
258 instance_ptr->uv_error = _status;
260 ref_guard< instance > unref_req(*instance_ptr, adopt_ref);
262 auto &getnameinfo_cb = instance_ptr->request_cb_storage.value();
263 if (getnameinfo_cb) getnameinfo_cb(getnameinfo(_uv_req));
Namespace for all uvcc definitions.
const char(& service() const noexcept)[NI_MAXSERV]
The char array containing the resulting service. It’s null terminated.
The base class for the libuv requests.
Getaddrinfo request type.
const char(& host() const noexcept)[NI_MAXHOST]
The char array containing the resulting host. It’s null terminated.
int run(uv::loop &_loop, const char *_hostname, const char *_service)
Idem with empty _hints.
int run(uv::loop &_loop, const char *_hostname, const char *_service, const ::addrinfo &_hints)
Run the request.
The I/O event loop class.
A scoped reference counting guard.
int run(uv::loop &_loop, const _T_ &_sa, int _NI_FLAGS=0)
Run the request.
Getnameinfo request type.