2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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
|
2018-07-03 21:29:04 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
namespace td {
|
2019-01-24 18:08:29 +01:00
|
|
|
|
|
|
|
class GetHostByNameActor final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2019-02-04 02:36:13 +01:00
|
|
|
enum class ResolverType { Native, Google };
|
2019-02-04 00:29:37 +01:00
|
|
|
|
2019-01-24 14:07:11 +01:00
|
|
|
struct Options {
|
2019-02-04 00:29:37 +01:00
|
|
|
static constexpr int32 DEFAULT_CACHE_TIME = 60 * 29; // 29 minutes
|
|
|
|
static constexpr int32 DEFAULT_ERROR_CACHE_TIME = 60 * 5; // 5 minutes
|
|
|
|
|
2019-02-04 02:36:13 +01:00
|
|
|
vector<ResolverType> resolver_types{ResolverType::Native};
|
2019-02-04 00:29:37 +01:00
|
|
|
int32 scheduler_id{-1};
|
|
|
|
int32 ok_timeout{DEFAULT_CACHE_TIME};
|
|
|
|
int32 error_timeout{DEFAULT_ERROR_CACHE_TIME};
|
2019-01-24 14:07:11 +01:00
|
|
|
};
|
2019-02-04 00:29:37 +01:00
|
|
|
|
2019-02-04 01:12:40 +01:00
|
|
|
explicit GetHostByNameActor(Options options);
|
2019-02-04 00:29:37 +01:00
|
|
|
|
2019-01-24 18:08:29 +01:00
|
|
|
void run(std::string host, int port, bool prefer_ipv6, Promise<IPAddress> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2019-02-04 02:32:32 +01:00
|
|
|
void on_query_result(std::string host, bool prefer_ipv6, Result<IPAddress> res);
|
2019-02-04 01:19:31 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
struct Value {
|
2019-01-24 18:08:29 +01:00
|
|
|
Result<IPAddress> ip;
|
2018-12-31 20:04:05 +01:00
|
|
|
double expire_at;
|
2018-06-27 18:24:42 +02:00
|
|
|
|
2019-01-24 18:08:29 +01:00
|
|
|
Value(Result<IPAddress> ip, double expire_at) : ip(std::move(ip)), expire_at(expire_at) {
|
2018-06-27 18:24:42 +02:00
|
|
|
}
|
2019-01-24 13:18:23 +01:00
|
|
|
|
2019-02-04 01:12:40 +01:00
|
|
|
Result<IPAddress> get_ip_port(int port) const {
|
2019-01-24 13:18:23 +01:00
|
|
|
auto res = ip.clone();
|
|
|
|
if (res.is_ok()) {
|
|
|
|
res.ok_ref().set_port(port);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-07-01 03:12:20 +02:00
|
|
|
std::unordered_map<string, Value> cache_[2];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-04 01:19:31 +01:00
|
|
|
struct Query {
|
|
|
|
ActorOwn<> query;
|
2019-02-04 02:32:32 +01:00
|
|
|
size_t pos = 0;
|
2019-02-04 01:19:31 +01:00
|
|
|
std::vector<std::pair<int, Promise<IPAddress>>> promises;
|
|
|
|
};
|
|
|
|
std::unordered_map<string, Query> active_queries_[2];
|
2018-07-01 03:12:20 +02:00
|
|
|
|
2019-02-04 01:19:31 +01:00
|
|
|
Options options_;
|
2019-02-04 02:32:32 +01:00
|
|
|
|
|
|
|
void run_query(std::string host, bool prefer_ipv6, Query &query);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-10-15 10:23:51 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|