2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/DcOptions.h"
|
|
|
|
|
2023-08-05 15:09:36 +02:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/IPAddress.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
2024-03-01 12:01:10 +01:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class DcOptionsSet {
|
|
|
|
public:
|
|
|
|
void add_dc_options(DcOptions dc_options);
|
|
|
|
|
|
|
|
DcOptions get_dc_options() const;
|
|
|
|
|
|
|
|
struct Stat {
|
|
|
|
double ok_at{-1000};
|
|
|
|
double error_at{-1001};
|
|
|
|
double check_at{-1002};
|
2018-06-03 01:35:32 +02:00
|
|
|
enum class State : int32 { Ok, Error, Checking };
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void on_ok() {
|
|
|
|
ok_at = Time::now_cached();
|
|
|
|
}
|
|
|
|
void on_error() {
|
|
|
|
error_at = Time::now_cached();
|
|
|
|
}
|
|
|
|
void on_check() {
|
|
|
|
check_at = Time::now_cached();
|
|
|
|
}
|
|
|
|
bool is_ok() const {
|
2018-06-03 01:35:32 +02:00
|
|
|
return state() == State::Ok;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
State state() const {
|
|
|
|
if (ok_at > error_at && ok_at > check_at) {
|
2018-06-03 01:35:32 +02:00
|
|
|
return State::Ok;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (check_at > ok_at && check_at > error_at) {
|
2018-06-03 01:35:32 +02:00
|
|
|
return State::Checking;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-06-03 01:35:32 +02:00
|
|
|
return State::Error;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConnectionInfo {
|
|
|
|
DcOption *option{nullptr};
|
|
|
|
bool use_http{false};
|
|
|
|
size_t order{0};
|
|
|
|
bool should_check{false};
|
|
|
|
Stat *stat{nullptr};
|
|
|
|
};
|
|
|
|
|
2018-07-27 02:54:25 +02:00
|
|
|
vector<ConnectionInfo> find_all_connections(DcId dc_id, bool allow_media_only, bool use_static, bool prefer_ipv6,
|
|
|
|
bool only_http);
|
2018-06-03 01:35:32 +02:00
|
|
|
|
2018-07-27 02:54:25 +02:00
|
|
|
Result<ConnectionInfo> find_connection(DcId dc_id, bool allow_media_only, bool use_static, bool prefer_ipv6,
|
|
|
|
bool only_http);
|
2018-12-31 20:04:05 +01:00
|
|
|
void reset();
|
|
|
|
|
|
|
|
private:
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class State : int32 { Error, Ok, Checking };
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
struct OptionStat {
|
|
|
|
Stat tcp_stat;
|
|
|
|
Stat http_stat;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DcOptionInfo {
|
|
|
|
DcOption option;
|
2024-03-01 12:01:10 +01:00
|
|
|
size_t stat_id = static_cast<size_t>(-1);
|
|
|
|
size_t pos = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t order = 0;
|
|
|
|
|
|
|
|
DcOptionInfo(DcOption &&option, size_t pos) : option(std::move(option)), pos(pos) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DcOptionId {
|
2024-03-01 12:01:10 +01:00
|
|
|
size_t pos = 0;
|
2024-05-08 09:30:02 +02:00
|
|
|
|
2024-05-23 16:09:28 +02:00
|
|
|
explicit constexpr DcOptionId(size_t pos) : pos(pos) {
|
2024-05-08 09:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t as_tie() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
bool operator==(const DcOptionId &other) const {
|
|
|
|
return as_tie() == other.as_tie();
|
|
|
|
}
|
|
|
|
bool operator<(const DcOptionId &other) const {
|
|
|
|
return as_tie() < other.as_tie();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-01 12:01:10 +01:00
|
|
|
vector<unique_ptr<DcOptionInfo>> options_;
|
|
|
|
vector<DcOptionId> ordered_options_;
|
|
|
|
vector<std::pair<IPAddress, unique_ptr<OptionStat>>> option_stats_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
DcOptionInfo *register_dc_option(DcOption &&option);
|
|
|
|
void init_option_stat(DcOptionInfo *option_info);
|
|
|
|
OptionStat *get_option_stat(const DcOptionInfo *option_info);
|
|
|
|
};
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|