From 5bb91fe180e9bdd4fdc829bd08c7231580580ec4 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 29 Jul 2016 14:05:42 +0200 Subject: [PATCH] WIP: dns, added GetAddrInfoReq --- src/uvw.hpp | 1 + src/uvw/dns.hpp | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/uvw/dns.hpp diff --git a/src/uvw.hpp b/src/uvw.hpp index 15994e58..b8830a65 100644 --- a/src/uvw.hpp +++ b/src/uvw.hpp @@ -1,5 +1,6 @@ #include "uvw/async.hpp" #include "uvw/check.hpp" +#include "uvw/dns.hpp" #include "uvw/event.hpp" #include "uvw/fs.hpp" #include "uvw/fs_event.hpp" diff --git a/src/uvw/dns.hpp b/src/uvw/dns.hpp new file mode 100644 index 00000000..00e7db72 --- /dev/null +++ b/src/uvw/dns.hpp @@ -0,0 +1,109 @@ +#pragma once + + +#include +#include +#include +#include "event.hpp" +#include "request.hpp" +#include "util.hpp" + + +namespace uvw { + + +struct AddrInfoEvent: Event { + AddrInfoEvent(std::unique_ptr ptr) + : dt{std::move(ptr)} + { } + + addrinfo& data() const noexcept { return *dt; } + +private: + std::unique_ptr dt; +}; + + +class GetAddrInfoReq final: public Request { + static void getAddrInfoCallback(uv_getaddrinfo_t *req, int status, addrinfo *res) { + auto ptr = reserve(reinterpret_cast(req)); + + if(status) { + ptr->publish(ErrorEvent{status}); + } else { + auto data = std::unique_ptr{ + res, [](addrinfo *res){ uv_freeaddrinfo(res); }}; + + ptr->publish(AddrInfoEvent{std::move(data)}); + } + } + + using Request::Request; + + void getNodeAddrInfo(const char *node, const char *service, addrinfo *hints = nullptr) { + invoke(&uv_getaddrinfo, parent(), get(), &getAddrInfoCallback, node, service, hints); + } + + auto getNodeAddrInfoSync(const char *node, const char *service, addrinfo *hints = nullptr) { + auto req = get(); + auto err = uv_getaddrinfo(parent(), req, nullptr, node, service, hints); + auto ptr = std::unique_ptr{req->addrinfo, [](addrinfo *res){ uv_freeaddrinfo(res); }}; + return std::make_pair(ErrorEvent{err}, AddrInfoEvent{std::move(ptr)}); + } + +public: + template + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new GetAddrInfoReq{std::forward(args)...}}; + } + + void getNodeAddrInfo(std::string node, addrinfo *hints = nullptr) { + getNodeAddrInfo(node.data(), nullptr, hints); + } + + auto getNodeAddrInfoSync(std::string node, addrinfo *hints = nullptr) { + return getNodeAddrInfoSync(node.data(), nullptr, hints); + } + + void getServiceAddrInfo(std::string service, addrinfo *hints = nullptr) { + getNodeAddrInfo(nullptr, service.data(), hints); + } + + auto getServiceAddrInfoSync(std::string service, addrinfo *hints = nullptr) { + return getNodeAddrInfo(nullptr, service.data(), hints); + } + + void getAddrInfo(std::string node, std::string service, addrinfo *hints = nullptr) { + getNodeAddrInfo(node.data(), service.data(), hints); + } + + auto getAddrInfoSync(std::string node, std::string service, addrinfo *hints = nullptr) { + return getNodeAddrInfo(node.data(), service.data(), hints); + } +}; + + +class GetNameInfoReq final: public Request { + static void getNameInfoCallback(uv_getnameinfo_t *req, int status, const char *hostname, const char *service) { + // TODO + } + + using Request::Request; + +public: + template + static std::shared_ptr create(Args&&... args) { + return std::shared_ptr{new GetNameInfoReq{std::forward(args)...}}; + } + + void getNameInfo() { + // TODO + } + + auto getNameInfoSync(int flags) { + // TODO + } +}; + + +}