Move connection state management to ConnectionStateManager.

This commit is contained in:
levlam 2024-08-01 14:53:24 +03:00
parent 7ce682984c
commit 86d6be1c59
4 changed files with 54 additions and 32 deletions

View File

@ -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<ConnectionStateManager> 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<ConnectionStateManager> parent_;
};
send_closure(td_->state_manager_, &StateManager::add_callback, make_unique<StateCallback>(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<int32>(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<td_api::object_ptr<td_api::Update>> &updates) const {
if (connection_state_ == ConnectionState::Empty) {
return;
}
updates.push_back(get_update_connection_state_object(connection_state_));
}
} // namespace td

View File

@ -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<td_api::object_ptr<td_api::Update>> &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_;
};

View File

@ -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<int32>(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<const unsigned char *>(&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) : 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> td_;
};
state_manager_ = create_actor<StateManager>("State manager", create_reference());
send_closure(state_manager_, &StateManager::add_callback, make_unique<StateManagerCallback>(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<td_api::updateAuthorizationState>(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);

View File

@ -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<td_api::Function> function);
void do_run_request(uint64 id, td_api::object_ptr<td_api::Function> &&function);
@ -371,8 +368,6 @@ class Td final : public Actor {
MtprotoHeader::Options options_;
ConnectionState connection_state_ = ConnectionState::Empty;
std::unordered_multimap<uint64, int32> request_set_;
int actor_refcnt_ = 0;
int request_actor_refcnt_ = 0;