diff --git a/td/telegram/ConnectionStateManager.cpp b/td/telegram/ConnectionStateManager.cpp index 405f7c5c8..f760fb4a1 100644 --- a/td/telegram/ConnectionStateManager.cpp +++ b/td/telegram/ConnectionStateManager.cpp @@ -6,6 +6,12 @@ // #include "td/telegram/ConnectionStateManager.h" +#include "td/telegram/Global.h" +#include "td/telegram/StateManager.h" +#include "td/telegram/Td.h" + +#include "td/utils/logging.h" + namespace td { ConnectionStateManager::ConnectionStateManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { @@ -15,4 +21,41 @@ void ConnectionStateManager::tear_down() { parent_.reset(); } +void ConnectionStateManager::start_up() { + class StateCallback final : public StateManager::Callback { + public: + explicit StateCallback(ActorId parent) : parent_(std::move(parent)) { + } + bool on_state(ConnectionState state) final { + send_closure(parent_, &ConnectionStateManager::on_connection_state_changed, state); + return parent_.is_alive(); + } + + private: + ActorId parent_; + }; + send_closure(td_->state_manager_, &StateManager::add_callback, make_unique(actor_id(this))); +} + +void ConnectionStateManager::on_connection_state_changed(ConnectionState new_state) { + if (G()->close_flag()) { + return; + } + if (new_state == connection_state_) { + LOG(ERROR) << "State manager sent update about unchanged state " << static_cast(new_state); + return; + } + connection_state_ = new_state; + + send_closure(G()->td(), &Td::send_update, get_update_connection_state_object(connection_state_)); +} + +void ConnectionStateManager::get_current_state(vector> &updates) const { + if (connection_state_ == ConnectionState::Empty) { + return; + } + + updates.push_back(get_update_connection_state_object(connection_state_)); +} + } // namespace td diff --git a/td/telegram/ConnectionStateManager.h b/td/telegram/ConnectionStateManager.h index 75bb6e14c..c3ec35dcf 100644 --- a/td/telegram/ConnectionStateManager.h +++ b/td/telegram/ConnectionStateManager.h @@ -6,6 +6,8 @@ // #pragma once +#include "td/telegram/ConnectionState.h" + #include "td/actor/actor.h" #include "td/utils/common.h" @@ -18,9 +20,17 @@ class ConnectionStateManager final : public Actor { public: ConnectionStateManager(Td *td, ActorShared<> parent); + void get_current_state(vector> &updates) const; + private: void tear_down() final; + void start_up() final; + + void on_connection_state_changed(ConnectionState new_state); + + ConnectionState connection_state_ = ConnectionState::Empty; + Td *td_; ActorShared<> parent_; }; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 9c2a6eaf5..0aafc47f0 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -2588,19 +2588,6 @@ void Td::on_result(NetQueryPtr query) { } } -void Td::on_connection_state_changed(ConnectionState new_state) { - if (G()->close_flag()) { - return; - } - if (new_state == connection_state_) { - LOG(ERROR) << "State manager sends update about unchanged state " << static_cast(new_state); - return; - } - connection_state_ = new_state; - - send_closure(actor_id(this), &Td::send_update, get_update_connection_state_object(connection_state_)); -} - void Td::start_up() { uint64 check_endianness = 0x0706050403020100; auto check_endianness_raw = reinterpret_cast(&check_endianness); @@ -3216,20 +3203,7 @@ void Td::process_binlog_events(TdDb::OpenedDatabase &&events) { void Td::init_options_and_network() { VLOG(td_init) << "Create StateManager"; - class StateManagerCallback final : public StateManager::Callback { - public: - explicit StateManagerCallback(ActorShared td) : td_(std::move(td)) { - } - bool on_state(ConnectionState state) final { - send_closure(td_, &Td::on_connection_state_changed, state); - return td_.is_alive(); - } - - private: - ActorShared td_; - }; state_manager_ = create_actor("State manager", create_reference()); - send_closure(state_manager_, &StateManager::add_callback, make_unique(create_reference())); G()->set_state_manager(state_manager_.get()); VLOG(td_init) << "Create OptionManager"; @@ -3823,7 +3797,7 @@ void Td::on_request(uint64 id, const td_api::getCurrentState &request) { updates.push_back(td_api::make_object(std::move(state))); } - updates.push_back(get_update_connection_state_object(connection_state_)); + connection_state_manager_->get_current_state(updates); if (auth_manager_->is_authorized()) { user_manager_->get_current_state(updates); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index a3fce5c5e..145269de8 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -6,7 +6,6 @@ // #pragma once -#include "td/telegram/ConnectionState.h" #include "td/telegram/files/FileId.h" #include "td/telegram/net/MtprotoHeader.h" #include "td/telegram/net/NetQuery.h" @@ -341,8 +340,6 @@ class Td final : public Actor { static constexpr int64 PING_SERVER_ALARM_ID = -1; static constexpr int32 PING_SERVER_TIMEOUT = 300; - void on_connection_state_changed(ConnectionState new_state); - void run_request(uint64 id, td_api::object_ptr function); void do_run_request(uint64 id, td_api::object_ptr &&function); @@ -371,8 +368,6 @@ class Td final : public Actor { MtprotoHeader::Options options_; - ConnectionState connection_state_ = ConnectionState::Empty; - std::unordered_multimap request_set_; int actor_refcnt_ = 0; int request_actor_refcnt_ = 0;