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-25 23:10:53 +02:00
|
|
|
|
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/NetQuery.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-03-14 15:43:00 +01:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <atomic>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class DcAuthManager;
|
2022-02-01 17:34:14 +01:00
|
|
|
class MultiSequenceDispatcher;
|
2018-06-26 01:43:11 +02:00
|
|
|
class NetQueryDelayer;
|
2018-12-31 20:04:05 +01:00
|
|
|
class PublicRsaKeyWatchdog;
|
2018-06-26 01:43:11 +02:00
|
|
|
class SessionMultiProxy;
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// Not just dispatcher.
|
|
|
|
class NetQueryDispatcher {
|
|
|
|
public:
|
2021-10-19 17:11:16 +02:00
|
|
|
explicit NetQueryDispatcher(const std::function<ActorShared<>()> &create_reference);
|
2018-12-31 20:04:05 +01:00
|
|
|
NetQueryDispatcher(const NetQueryDispatcher &) = delete;
|
|
|
|
NetQueryDispatcher &operator=(const NetQueryDispatcher &) = delete;
|
|
|
|
NetQueryDispatcher(NetQueryDispatcher &&) = delete;
|
|
|
|
NetQueryDispatcher &operator=(NetQueryDispatcher &&) = delete;
|
|
|
|
~NetQueryDispatcher();
|
|
|
|
|
|
|
|
void dispatch(NetQueryPtr net_query);
|
|
|
|
void dispatch_with_callback(NetQueryPtr net_query, ActorShared<NetQueryCallback> callback);
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
void update_session_count();
|
2017-12-29 21:34:39 +01:00
|
|
|
void destroy_auth_keys(Promise<> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
void update_use_pfs();
|
2018-05-10 20:02:24 +02:00
|
|
|
void update_mtproto_header();
|
|
|
|
|
2021-10-05 23:55:22 +02:00
|
|
|
DcId get_main_dc_id() const {
|
|
|
|
return DcId::internal(main_dc_id_.load(std::memory_order_relaxed));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:35:58 +01:00
|
|
|
void set_main_dc_id(int32 new_main_dc_id);
|
2022-05-02 17:34:01 +02:00
|
|
|
void check_authorization_is_ok();
|
2019-12-17 00:35:58 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
std::atomic<bool> stop_flag_{false};
|
2017-12-29 21:34:39 +01:00
|
|
|
bool need_destroy_auth_key_{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorOwn<NetQueryDelayer> delayer_;
|
|
|
|
ActorOwn<DcAuthManager> dc_auth_manager_;
|
2022-02-01 16:51:20 +01:00
|
|
|
ActorOwn<MultiSequenceDispatcher> sequence_dispatcher_;
|
2018-12-31 20:04:05 +01:00
|
|
|
struct Dc {
|
2017-12-29 21:34:39 +01:00
|
|
|
DcId id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::atomic<bool> is_valid_{false};
|
|
|
|
std::atomic<bool> is_inited_{false}; // TODO: cache in scheduler local storage :D
|
|
|
|
|
|
|
|
ActorOwn<SessionMultiProxy> main_session_;
|
|
|
|
ActorOwn<SessionMultiProxy> download_session_;
|
|
|
|
ActorOwn<SessionMultiProxy> download_small_session_;
|
|
|
|
ActorOwn<SessionMultiProxy> upload_session_;
|
|
|
|
};
|
2023-10-23 14:40:57 +02:00
|
|
|
std::array<Dc, DcId::MAX_RAW_DC_ID> dcs_;
|
2018-12-31 20:04:05 +01:00
|
|
|
#if TD_EMSCRIPTEN // FIXME
|
|
|
|
std::atomic<int32> main_dc_id_{2};
|
|
|
|
#else
|
|
|
|
std::atomic<int32> main_dc_id_{1};
|
|
|
|
#endif
|
|
|
|
ActorOwn<PublicRsaKeyWatchdog> public_rsa_key_watchdog_;
|
2024-05-15 14:38:51 +02:00
|
|
|
std::mutex mutex_;
|
2018-03-13 14:40:02 +01:00
|
|
|
std::shared_ptr<Guard> td_guard_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Status wait_dc_init(DcId dc_id, bool force);
|
|
|
|
bool is_dc_inited(int32 raw_dc_id);
|
|
|
|
|
2024-02-02 11:32:15 +01:00
|
|
|
static int32 get_main_session_scheduler_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
static int32 get_session_count();
|
|
|
|
static bool get_use_pfs();
|
|
|
|
|
2018-04-19 19:21:26 +02:00
|
|
|
static void complete_net_query(NetQueryPtr net_query);
|
2024-05-15 14:38:51 +02:00
|
|
|
bool check_stop_flag(NetQueryPtr &net_query) const;
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void try_fix_migrate(NetQueryPtr &net_query);
|
|
|
|
};
|
2018-04-19 19:21:26 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|