From 732f53d798c86558e1e625c22e957243bb2d6467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 18 Nov 2016 20:54:41 +0100 Subject: Clean a few more things --- louloulibs/network/dns_handler.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'louloulibs/network/dns_handler.cpp') diff --git a/louloulibs/network/dns_handler.cpp b/louloulibs/network/dns_handler.cpp index fef0cfc..b1807b5 100644 --- a/louloulibs/network/dns_handler.cpp +++ b/louloulibs/network/dns_handler.cpp @@ -8,7 +8,6 @@ #include #include -#include DNSHandler DNSHandler::instance; -- cgit v1.2.3 From 5b56007828f20c763df3f36ceed809188880663e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 6 Jan 2017 22:58:18 +0100 Subject: Use udns instead of c-ares fix #3226 --- louloulibs/network/dns_handler.cpp | 125 +++++++------------------------------ 1 file changed, 21 insertions(+), 104 deletions(-) (limited to 'louloulibs/network/dns_handler.cpp') diff --git a/louloulibs/network/dns_handler.cpp b/louloulibs/network/dns_handler.cpp index b1807b5..ad0ad38 100644 --- a/louloulibs/network/dns_handler.cpp +++ b/louloulibs/network/dns_handler.cpp @@ -1,5 +1,5 @@ #include -#ifdef CARES_FOUND +#ifdef UDNS_FOUND #include #include @@ -7,123 +7,40 @@ #include -#include +#include -DNSHandler DNSHandler::instance; +#include + +class Resolver; using namespace std::string_literals; -DNSHandler::DNSHandler(): - socket_handlers{}, - channel{nullptr} -{ - int ares_error; - if ((ares_error = ::ares_library_init(ARES_LIB_INIT_ALL)) != 0) - throw std::runtime_error("Failed to initialize c-ares lib: "s + ares_strerror(ares_error)); - struct ares_options options = {}; - // The default timeout values are way too high - options.timeout = 1000; - options.tries = 3; - if ((ares_error = ::ares_init_options(&this->channel, - &options, - ARES_OPT_TIMEOUTMS|ARES_OPT_TRIES)) != ARES_SUCCESS) - throw std::runtime_error("Failed to initialize c-ares channel: "s + ares_strerror(ares_error)); -} -ares_channel& DNSHandler::get_channel() -{ - return this->channel; -} +std::unique_ptr DNSHandler::socket_handler{}; -void DNSHandler::destroy() +DNSHandler::DNSHandler(std::shared_ptr poller) { - this->remove_all_sockets_from_poller(); - this->socket_handlers.clear(); - ::ares_destroy(this->channel); - ::ares_library_cleanup(); + dns_init(nullptr, 0); + const auto socket = dns_open(nullptr); + if (socket == -1) + throw std::runtime_error("Failed to initialize udns socket: "s + strerror(errno)); + + DNSHandler::socket_handler = std::make_unique(poller, socket); } -void DNSHandler::gethostbyname(const std::string& name, ares_host_callback callback, - void* data, int family) +void DNSHandler::destroy() { - ::ares_gethostbyname(this->channel, name.data(), family, - callback, data); + DNSHandler::socket_handler.reset(nullptr); + dns_close(nullptr); } -void DNSHandler::watch_dns_sockets(std::shared_ptr& poller) +void DNSHandler::watch() { - fd_set readers; - fd_set writers; - - FD_ZERO(&readers); - FD_ZERO(&writers); - - int ndfs = ::ares_fds(this->channel, &readers, &writers); - // For each existing DNS socket, see if we are still supposed to watch it, - // if not then erase it - this->socket_handlers.erase( - std::remove_if(this->socket_handlers.begin(), this->socket_handlers.end(), - [&readers](const auto& dns_socket) - { - return !FD_ISSET(dns_socket->get_socket(), &readers); - }), - this->socket_handlers.end()); - - for (auto i = 0; i < ndfs; ++i) - { - bool read = FD_ISSET(i, &readers); - bool write = FD_ISSET(i, &writers); - // Look for the DNSSocketHandler with this fd - auto it = std::find_if(this->socket_handlers.begin(), - this->socket_handlers.end(), - [i](const auto& socket_handler) - { - return i == socket_handler->get_socket(); - }); - if (!read && !write) // No need to read or write to it - { // If found, erase it and stop watching it because it is not - // needed anymore - if (it != this->socket_handlers.end()) - // The socket destructor removes it from the poller - this->socket_handlers.erase(it); - } - else // We need to write and/or read to it - { // If not found, create it because we need to watch it - if (it == this->socket_handlers.end()) - { - this->socket_handlers.emplace(this->socket_handlers.begin(), - std::make_unique(poller, *this, i)); - it = this->socket_handlers.begin(); - } - poller->add_socket_handler(it->get()); - if (write) - poller->watch_send_events(it->get()); - } - } - // Cancel previous timer, if any. - TimedEventsManager::instance().cancel("DNS timeout"); - struct timeval tv; - struct timeval* tvp; - tvp = ::ares_timeout(this->channel, NULL, &tv); - if (tvp) - { - auto future_time = std::chrono::steady_clock::now() + std::chrono::seconds(tvp->tv_sec) + \ - std::chrono::microseconds(tvp->tv_usec); - TimedEventsManager::instance().add_event(TimedEvent(std::move(future_time), - [this]() - { - for (auto& dns_socket_handler: this->socket_handlers) - dns_socket_handler->on_recv(); - }, - "DNS timeout")); - } + DNSHandler::socket_handler->watch(); } -void DNSHandler::remove_all_sockets_from_poller() +void DNSHandler::unwatch() { - for (const auto& socket_handler: this->socket_handlers) - { - socket_handler->remove_from_poller(); - } + DNSHandler::socket_handler->unwatch(); } -#endif /* CARES_FOUND */ +#endif /* UDNS_FOUND */ -- cgit v1.2.3 From 6bf9cedbeb30740c267ea283058c93ed63648fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Fri, 6 Jan 2017 23:13:47 +0100 Subject: Add a missing errno include --- louloulibs/network/dns_handler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'louloulibs/network/dns_handler.cpp') diff --git a/louloulibs/network/dns_handler.cpp b/louloulibs/network/dns_handler.cpp index ad0ad38..fbd2763 100644 --- a/louloulibs/network/dns_handler.cpp +++ b/louloulibs/network/dns_handler.cpp @@ -8,7 +8,7 @@ #include #include - +#include #include class Resolver; -- cgit v1.2.3 From f0bc6c83a8eb548d0a3edbf7c16a6922bfd24ba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 8 Mar 2017 19:04:15 +0100 Subject: Pass the shared_ptr by reference, to avoid useless copies --- louloulibs/network/dns_handler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'louloulibs/network/dns_handler.cpp') diff --git a/louloulibs/network/dns_handler.cpp b/louloulibs/network/dns_handler.cpp index fbd2763..641c087 100644 --- a/louloulibs/network/dns_handler.cpp +++ b/louloulibs/network/dns_handler.cpp @@ -17,7 +17,7 @@ using namespace std::string_literals; std::unique_ptr DNSHandler::socket_handler{}; -DNSHandler::DNSHandler(std::shared_ptr poller) +DNSHandler::DNSHandler(std::shared_ptr& poller) { dns_init(nullptr, 0); const auto socket = dns_open(nullptr); -- cgit v1.2.3 From 0ab40dc1ab4e689921da54080b135e1d22b1c586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 14 Mar 2017 21:45:23 +0100 Subject: Refactoring louloulibs and cmake Use OBJECT libraries Remove the louloulibs directory Write FOUND variables in the cache --- louloulibs/network/dns_handler.cpp | 46 -------------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 louloulibs/network/dns_handler.cpp (limited to 'louloulibs/network/dns_handler.cpp') diff --git a/louloulibs/network/dns_handler.cpp b/louloulibs/network/dns_handler.cpp deleted file mode 100644 index 641c087..0000000 --- a/louloulibs/network/dns_handler.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#ifdef UDNS_FOUND - -#include -#include -#include - -#include - -#include -#include -#include - -class Resolver; - -using namespace std::string_literals; - -std::unique_ptr DNSHandler::socket_handler{}; - -DNSHandler::DNSHandler(std::shared_ptr& poller) -{ - dns_init(nullptr, 0); - const auto socket = dns_open(nullptr); - if (socket == -1) - throw std::runtime_error("Failed to initialize udns socket: "s + strerror(errno)); - - DNSHandler::socket_handler = std::make_unique(poller, socket); -} - -void DNSHandler::destroy() -{ - DNSHandler::socket_handler.reset(nullptr); - dns_close(nullptr); -} - -void DNSHandler::watch() -{ - DNSHandler::socket_handler->watch(); -} - -void DNSHandler::unwatch() -{ - DNSHandler::socket_handler->unwatch(); -} - -#endif /* UDNS_FOUND */ -- cgit v1.2.3