From d75f0a4e944f259701b7581989283102d4c5ad6a Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 16 Dec 2021 15:18:51 +0300 Subject: [PATCH] Move "unix_time" option handling to OptionManager. --- td/telegram/Global.cpp | 4 ++-- td/telegram/OptionManager.cpp | 25 ++++++++++++++++++++++--- td/telegram/OptionManager.h | 8 ++++++++ td/telegram/Td.cpp | 25 ++++++------------------- td/telegram/Td.h | 4 ---- 5 files changed, 38 insertions(+), 28 deletions(-) diff --git a/td/telegram/Global.cpp b/td/telegram/Global.cpp index b10aa0aa674a..21b6860133ee 100644 --- a/td/telegram/Global.cpp +++ b/td/telegram/Global.cpp @@ -11,8 +11,8 @@ #include "td/telegram/net/MtprotoHeader.h" #include "td/telegram/net/NetQueryDispatcher.h" #include "td/telegram/net/TempAuthKeyWatchdog.h" +#include "td/telegram/OptionManager.h" #include "td/telegram/StateManager.h" -#include "td/telegram/Td.h" #include "td/telegram/TdDb.h" #include "td/actor/PromiseFuture.h" @@ -149,7 +149,7 @@ void Global::update_server_time_difference(double diff) { do_save_server_time_difference(); CHECK(Scheduler::instance()); - send_closure(td(), &Td::on_update_server_time_difference); + send_closure(option_manager(), &OptionManager::on_update_server_time_difference); } } diff --git a/td/telegram/OptionManager.cpp b/td/telegram/OptionManager.cpp index 9033c5ac277d..b25abce76ed5 100644 --- a/td/telegram/OptionManager.cpp +++ b/td/telegram/OptionManager.cpp @@ -28,11 +28,13 @@ #include "td/utils/misc.h" #include "td/utils/SliceBuilder.h" +#include #include namespace td { OptionManager::OptionManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { + send_unix_time_update(); } void OptionManager::tear_down() { @@ -41,6 +43,23 @@ void OptionManager::tear_down() { OptionManager::~OptionManager() = default; +td_api::object_ptr OptionManager::get_unix_time_option_value_object() { + return td_api::make_object(G()->unix_time()); +} + +void OptionManager::send_unix_time_update() { + last_sent_server_time_difference_ = G()->get_server_time_difference(); + td_->send_update(td_api::make_object("unix_time", get_unix_time_option_value_object())); +} + +void OptionManager::on_update_server_time_difference() { + if (std::abs(G()->get_server_time_difference() - last_sent_server_time_difference_) < 0.5) { + return; + } + + send_unix_time_update(); +} + void OptionManager::clear_options() { for (auto &option : G()->shared_config().get_options()) { if (!is_internal_option(option.first)) { @@ -296,7 +315,7 @@ void OptionManager::get_option(const string &name, Promise(G()->unix_time())); + return promise.set_value(get_unix_time_option_value_object()); } break; case 'v': @@ -608,8 +627,8 @@ void OptionManager::get_current_state(vector> updates.push_back(td_api::make_object( "online", td_api::make_object(td_->is_online()))); - updates.push_back(td_api::make_object( - "unix_time", td_api::make_object(G()->unix_time()))); + + updates.push_back(td_api::make_object("unix_time", get_unix_time_option_value_object())); for (const auto &option : G()->shared_config().get_options()) { if (!is_internal_option(option.first)) { diff --git a/td/telegram/OptionManager.h b/td/telegram/OptionManager.h index 5631516f27a2..4dbbec97e9f2 100644 --- a/td/telegram/OptionManager.h +++ b/td/telegram/OptionManager.h @@ -28,6 +28,8 @@ class OptionManager final : public Actor { OptionManager &operator=(OptionManager &&) = delete; ~OptionManager() final; + void on_update_server_time_difference(); + void on_option_updated(const string &name); void get_option(const string &name, Promise> &&promise); @@ -43,8 +45,14 @@ class OptionManager final : public Actor { static bool is_internal_option(Slice name); + static td_api::object_ptr get_unix_time_option_value_object(); + + void send_unix_time_update(); + Td *td_; ActorShared<> parent_; + + double last_sent_server_time_difference_ = 1e100; }; } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 11327521b589..da9c01fe0944 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -147,7 +147,6 @@ #include "td/utils/tl_parsers.h" #include "td/utils/utf8.h" -#include #include #include #include @@ -2704,17 +2703,6 @@ void Td::on_alarm_timeout_callback(void *td_ptr, int64 alarm_id) { send_closure_later(td_id, &Td::on_alarm_timeout, alarm_id); } -void Td::on_update_server_time_difference() { - auto diff = G()->get_server_time_difference(); - if (std::abs(diff - last_sent_server_time_difference_) < 0.5) { - return; - } - - last_sent_server_time_difference_ = diff; - send_update(td_api::make_object( - "unix_time", td_api::make_object(G()->unix_time()))); -} - void Td::on_alarm_timeout(int64 alarm_id) { if (alarm_id == ONLINE_ALARM_ID) { on_online_updated(false, true); @@ -3677,9 +3665,6 @@ Status Td::init(DbKey key) { VLOG(td_init) << "Successfully inited database"; G()->init(parameters_, actor_id(this), r_td_db.move_as_ok()).ensure(); - last_sent_server_time_difference_ = G()->get_server_time_difference(); - send_update(td_api::make_object( - "unix_time", td_api::make_object(G()->unix_time()))); init_options_and_network(); @@ -3865,11 +3850,16 @@ void Td::init_options_and_network() { config_manager_ = create_actor("ConfigManager", create_reference()); G()->set_config_manager(config_manager_.get()); + VLOG(td_init) << "Create OptionManager"; + option_manager_ = make_unique(this, create_reference()); + option_manager_actor_ = register_actor("OptionManager", option_manager_.get()); + G()->set_option_manager(option_manager_actor_.get()); + VLOG(td_init) << "Set ConfigShared callback"; class ConfigSharedCallback final : public ConfigShared::Callback { public: void on_option_updated(const string &name, const string &value) const final { - send_closure(G()->option_manager(), &OptionManager::on_option_updated, name); + send_closure_later(G()->option_manager(), &OptionManager::on_option_updated, name); } ~ConfigSharedCallback() final { LOG(INFO) << "Destroy ConfigSharedCallback"; @@ -4019,9 +4009,6 @@ void Td::init_managers() { notification_manager_ = make_unique(this, create_reference()); notification_manager_actor_ = register_actor("NotificationManager", notification_manager_.get()); G()->set_notification_manager(notification_manager_actor_.get()); - option_manager_ = make_unique(this, create_reference()); - option_manager_actor_ = register_actor("OptionManager", option_manager_.get()); - G()->set_option_manager(option_manager_actor_.get()); poll_manager_ = make_unique(this, create_reference()); poll_manager_actor_ = register_actor("PollManager", poll_manager_.get()); sponsored_message_manager_ = make_unique(this, create_reference()); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index b43b31b9202c..2010b66b0a44 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -118,8 +118,6 @@ class Td final : public Actor { void on_result(NetQueryPtr query); - void on_update_server_time_difference(); - void on_online_updated(bool force, bool send_update); void on_update_status_success(bool is_online); @@ -318,8 +316,6 @@ class Td final : public Actor { TermsOfService pending_terms_of_service_; - double last_sent_server_time_difference_ = 1e100; - struct DownloadInfo { int32 offset = -1; int32 limit = -1;