From ddd8e6fa0fc4884498e5ebf28b810f7cb605091c Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 19 Dec 2019 04:23:14 +0300 Subject: [PATCH] Move help_getAppConfig receiving to ConfigManager. GitOrigin-RevId: 310743ea038dffebd59c9b612438bc7cfc7553a4 --- td/telegram/ConfigManager.cpp | 42 +++++++++++++++++++++++++++++++---- td/telegram/ConfigManager.h | 5 +++++ td/telegram/Td.cpp | 29 +----------------------- 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index fbdb54d35..8dfa1c2c8 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -8,6 +8,7 @@ #include "td/telegram/ConfigShared.h" #include "td/telegram/Global.h" +#include "td/telegram/JsonValue.h" #include "td/telegram/logevent/LogEvent.h" #include "td/telegram/net/AuthDataShared.h" #include "td/telegram/net/ConnectionCreator.h" @@ -867,12 +868,10 @@ ConfigManager::ConfigManager(ActorShared<> parent) : parent_(std::move(parent)) } void ConfigManager::start_up() { - // TODO there are some problems when many ConfigRecoverers starts at the same time - // if (G()->parameters().use_file_db) { ref_cnt_++; config_recoverer_ = create_actor("Recoverer", actor_shared()); send_closure(config_recoverer_, &ConfigRecoverer::on_dc_options_update, load_dc_options_update()); - // } + auto expire_time = load_config_expire_time(); if (expire_time.is_in_past()) { request_config(); @@ -886,22 +885,26 @@ void ConfigManager::hangup_shared() { ref_cnt_--; try_stop(); } + void ConfigManager::hangup() { ref_cnt_--; config_recoverer_.reset(); try_stop(); } + void ConfigManager::loop() { if (expire_time_ && expire_time_.is_in_past()) { request_config(); expire_time_ = {}; } } + void ConfigManager::try_stop() { if (ref_cnt_ == 0) { stop(); } } + void ConfigManager::request_config() { if (config_sent_cnt_ != 0) { return; @@ -909,6 +912,17 @@ void ConfigManager::request_config() { request_config_from_dc_impl(DcId::main()); } +void ConfigManager::get_app_config(Promise> &&promise) { + get_app_config_queries_.push_back(std::move(promise)); + if (get_app_config_queries_.size() == 1) { + G()->net_query_dispatcher().dispatch_with_callback( + G()->net_query_creator().create(create_storer(telegram_api::help_getAppConfig()), DcId::main(), + NetQuery::Type::Common, NetQuery::AuthFlag::Off, NetQuery::GzipFlag::On, + 60 * 60 * 24), + actor_shared(this, 1)); + } +} + void ConfigManager::on_dc_options_update(DcOptions dc_options) { save_dc_options_update(dc_options); send_closure(config_recoverer_, &ConfigRecoverer::on_dc_options_update, std::move(dc_options)); @@ -925,10 +939,30 @@ void ConfigManager::request_config_from_dc_impl(DcId dc_id) { G()->net_query_dispatcher().dispatch_with_callback( G()->net_query_creator().create(create_storer(telegram_api::help_getConfig()), dc_id, NetQuery::Type::Common, NetQuery::AuthFlag::Off, NetQuery::GzipFlag::On, 60 * 60 * 24), - actor_shared(this)); + actor_shared(this, 0)); } void ConfigManager::on_result(NetQueryPtr res) { + auto token = get_link_token(); + if (token == 1) { + auto promises = std::move(get_app_config_queries_); + get_app_config_queries_.clear(); + CHECK(!promises.empty()); + auto result_ptr = fetch_result(std::move(res)); + if (result_ptr.is_error()) { + for (auto &promise : promises) { + promise.set_error(result_ptr.error().clone()); + } + } + + auto result = result_ptr.move_as_ok(); + for (auto &promise : promises) { + promise.set_value(convert_json_value_object(result)); + } + return; + } + + CHECK(token == 0); CHECK(config_sent_cnt_ > 0); config_sent_cnt_--; auto r_config = fetch_result(std::move(res)); diff --git a/td/telegram/ConfigManager.h b/td/telegram/ConfigManager.h index 7c3e659a6..8f99d1cc7 100644 --- a/td/telegram/ConfigManager.h +++ b/td/telegram/ConfigManager.h @@ -10,6 +10,7 @@ #include "td/telegram/net/DcOptions.h" #include "td/telegram/net/NetQuery.h" +#include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" #include "td/actor/actor.h" @@ -82,6 +83,8 @@ class ConfigManager : public NetQueryCallback { void request_config(); + void get_app_config(Promise> &&promise); + void on_dc_options_update(DcOptions dc_options); private: @@ -91,6 +94,8 @@ class ConfigManager : public NetQueryCallback { int ref_cnt_{1}; Timestamp expire_time_; + vector>> get_app_config_queries_; + void start_up() override; void hangup_shared() override; void hangup() override; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index a769eb745..4f53c3a43 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -469,33 +469,6 @@ class GetDeepLinkInfoQuery : public Td::ResultHandler { } }; -class GetAppConfigQuery : public Td::ResultHandler { - Promise> promise_; - - public: - explicit GetAppConfigQuery(Promise> &&promise) : promise_(std::move(promise)) { - } - - void send() { - send_query(G()->net_query_creator().create(create_storer(telegram_api::help_getAppConfig()), DcId::main(), - NetQuery::Type::Common, NetQuery::AuthFlag::Off)); - } - - void on_result(uint64 id, BufferSlice packet) override { - auto result_ptr = fetch_result(packet); - if (result_ptr.is_error()) { - return on_error(id, result_ptr.move_as_error()); - } - - auto result = result_ptr.move_as_ok(); - promise_.set_value(convert_json_value_object(result)); - } - - void on_error(uint64 id, Status status) override { - promise_.set_error(std::move(status)); - } -}; - class SaveAppLogQuery : public Td::ResultHandler { Promise promise_; @@ -7627,7 +7600,7 @@ void Td::on_request(uint64 id, td_api::getDeepLinkInfo &request) { void Td::on_request(uint64 id, const td_api::getApplicationConfig &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); - create_handler(std::move(promise))->send(); + send_closure(G()->config_manager(), &ConfigManager::get_app_config, std::move(promise)); } void Td::on_request(uint64 id, td_api::saveApplicationLogEvent &request) {