// // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once #include "td/actor/actor.h" #include "td/actor/PromiseFuture.h" #include "td/utils/port/IPAddress.h" #include "td/utils/Status.h" #include namespace td { class GetHostByNameActor final : public Actor { public: enum class ResolveType { Native, Google, All }; struct Options { static constexpr int32 DEFAULT_CACHE_TIME = 60 * 29; // 29 minutes static constexpr int32 DEFAULT_ERROR_CACHE_TIME = 60 * 5; // 5 minutes ResolveType type{ResolveType::Native}; int32 scheduler_id{-1}; int32 ok_timeout{DEFAULT_CACHE_TIME}; int32 error_timeout{DEFAULT_ERROR_CACHE_TIME}; }; explicit GetHostByNameActor(Options options); void run(std::string host, int port, bool prefer_ipv6, Promise promise); struct ResolveOptions { ResolveType type{ResolveType::Native}; bool prefer_ipv6{false}; int32 scheduler_id{-1}; }; static TD_WARN_UNUSED_RESULT ActorOwn<> resolve(std::string host, ResolveOptions options, Promise promise); private: struct Value { Result ip; double expire_at; ActorOwn<> query; std::vector>> promises; Value(Result ip, double expire_at) : ip(std::move(ip)), expire_at(expire_at) { } Result get_ip_port(int port) const { auto res = ip.clone(); if (res.is_ok()) { res.ok_ref().set_port(port); } return res; } }; std::unordered_map cache_[2]; Options options_; void on_result(std::string host, bool prefer_ipv6, Result res); }; } // namespace td