Move StateManager::State to ConnectionState.h.

This commit is contained in:
levlam 2021-09-16 18:23:10 +03:00
parent 34094328bd
commit 7d26a30bd0
8 changed files with 83 additions and 47 deletions

View File

@ -291,6 +291,7 @@ set(TDLIB_SOURCE
td/telegram/ClientActor.cpp
td/telegram/ConfigManager.cpp
td/telegram/ConfigShared.cpp
td/telegram/ConnectionState.cpp
td/telegram/Contact.cpp
td/telegram/ContactsManager.cpp
td/telegram/CountryInfoManager.cpp
@ -467,6 +468,7 @@ set(TDLIB_SOURCE
td/telegram/ClientActor.h
td/telegram/ConfigManager.h
td/telegram/ConfigShared.h
td/telegram/ConnectionState.h
td/telegram/Contact.h
td/telegram/ContactsManager.h
td/telegram/CountryInfoManager.h

View File

@ -8,6 +8,7 @@
#include "td/telegram/AuthManager.h"
#include "td/telegram/ConfigShared.h"
#include "td/telegram/ConnectionState.h"
#include "td/telegram/Global.h"
#include "td/telegram/JsonValue.h"
#include "td/telegram/LinkManager.h"
@ -850,8 +851,8 @@ class ConfigRecoverer final : public Actor {
public:
explicit StateCallback(ActorId<ConfigRecoverer> parent) : parent_(std::move(parent)) {
}
bool on_state(StateManager::State state) final {
send_closure(parent_, &ConfigRecoverer::on_connecting, state == StateManager::State::Connecting);
bool on_state(ConnectionState state) final {
send_closure(parent_, &ConfigRecoverer::on_connecting, state == ConnectionState::Connecting);
return parent_.is_alive();
}
bool on_network(NetType network_type, uint32 network_generation) final {

View File

@ -0,0 +1,38 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// 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)
//
#include "td/telegram/ConnectionState.h"
#include "td/utils/logging.h"
namespace td {
static td_api::object_ptr<td_api::ConnectionState> get_connection_state_object(ConnectionState state) {
switch (state) {
case ConnectionState::Empty:
UNREACHABLE();
return nullptr;
case ConnectionState::WaitingForNetwork:
return td_api::make_object<td_api::connectionStateWaitingForNetwork>();
case ConnectionState::ConnectingToProxy:
return td_api::make_object<td_api::connectionStateConnectingToProxy>();
case ConnectionState::Connecting:
return td_api::make_object<td_api::connectionStateConnecting>();
case ConnectionState::Updating:
return td_api::make_object<td_api::connectionStateUpdating>();
case ConnectionState::Ready:
return td_api::make_object<td_api::connectionStateReady>();
default:
LOG(FATAL) << "State = " << static_cast<int32>(state);
return nullptr;
}
}
td_api::object_ptr<td_api::updateConnectionState> get_update_connection_state_object(ConnectionState state) {
return td_api::make_object<td_api::updateConnectionState>(get_connection_state_object(state));
}
} // namespace td

View File

@ -0,0 +1,19 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// 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
#include "td/telegram/td_api.h"
#include "td/utils/common.h"
namespace td {
enum class ConnectionState : int32 { WaitingForNetwork, ConnectingToProxy, Connecting, Updating, Ready, Empty };
td_api::object_ptr<td_api::updateConnectionState> get_update_connection_state_object(ConnectionState state);
} // namespace td

View File

@ -100,20 +100,20 @@ void StateManager::close() {
stop();
}
StateManager::State StateManager::get_real_state() const {
ConnectionState StateManager::get_real_state() const {
if (!network_flag_) {
return State::WaitingForNetwork;
return ConnectionState::WaitingForNetwork;
}
if (!connect_cnt_) {
if (use_proxy_ && !connect_proxy_cnt_) {
return State::ConnectingToProxy;
return ConnectionState::ConnectingToProxy;
}
return State::Connecting;
return ConnectionState::Connecting;
}
if (!sync_flag_) {
return State::Updating;
return ConnectionState::Updating;
}
return State::Ready;
return ConnectionState::Ready;
}
void StateManager::notify_flag(Flag flag) {
@ -166,7 +166,7 @@ void StateManager::loop() {
}
if (pending_state_ != flush_state_) {
double delay = 0;
if (flush_state_ != State::Empty) {
if (flush_state_ != ConnectionState::Empty) {
if (static_cast<int32>(pending_state_) > static_cast<int32>(flush_state_)) {
delay = UP_DELAY;
} else {

View File

@ -9,6 +9,7 @@
#include "td/actor/actor.h"
#include "td/actor/PromiseFuture.h"
#include "td/telegram/ConnectionState.h"
#include "td/telegram/net/NetType.h"
#include "td/utils/common.h"
@ -17,15 +18,13 @@ namespace td {
class StateManager final : public Actor {
public:
enum class State : int32 { WaitingForNetwork, ConnectingToProxy, Connecting, Updating, Ready, Empty };
class Callback {
public:
Callback() = default;
Callback(const Callback &) = delete;
Callback &operator=(const Callback &) = delete;
virtual ~Callback() = default;
virtual bool on_state(State state) {
virtual bool on_state(ConnectionState state) {
return true;
}
virtual bool on_network(NetType network_type, uint32 generation) {
@ -114,15 +113,15 @@ class StateManager final : public Actor {
static constexpr double UP_DELAY = 0.05;
static constexpr double DOWN_DELAY = 0.3;
State pending_state_ = State::Empty;
ConnectionState pending_state_ = ConnectionState::Empty;
bool has_timestamp_ = false;
double pending_timestamp_ = 0;
State flush_state_ = State::Empty;
ConnectionState flush_state_ = ConnectionState::Empty;
vector<unique_ptr<Callback>> callbacks_;
bool was_sync_ = false;
std::vector<Promise<>> wait_first_sync_;
vector<Promise<>> wait_first_sync_;
void inc_connect();
void dec_connect();
@ -136,7 +135,7 @@ class StateManager final : public Actor {
void on_network_soft();
void do_on_network(NetType new_network_type, bool inc_generation);
State get_real_state() const;
ConnectionState get_real_state() const;
static ConnectionToken connection_impl(ActorId<StateManager> state_manager, int mode) {
auto actor = ActorShared<StateManager>(state_manager, mode);

View File

@ -3517,28 +3517,7 @@ void Td::on_config_option_updated(const string &name) {
send_update(make_tl_object<td_api::updateOption>(name, G()->shared_config().get_option_value(name)));
}
tl_object_ptr<td_api::ConnectionState> Td::get_connection_state_object(StateManager::State state) {
switch (state) {
case StateManager::State::Empty:
UNREACHABLE();
return nullptr;
case StateManager::State::WaitingForNetwork:
return make_tl_object<td_api::connectionStateWaitingForNetwork>();
case StateManager::State::ConnectingToProxy:
return make_tl_object<td_api::connectionStateConnectingToProxy>();
case StateManager::State::Connecting:
return make_tl_object<td_api::connectionStateConnecting>();
case StateManager::State::Updating:
return make_tl_object<td_api::connectionStateUpdating>();
case StateManager::State::Ready:
return make_tl_object<td_api::connectionStateReady>();
default:
LOG(FATAL) << "State = " << static_cast<int32>(state);
return nullptr;
}
}
void Td::on_connection_state_changed(StateManager::State new_state) {
void Td::on_connection_state_changed(ConnectionState new_state) {
if (new_state == connection_state_) {
LOG(ERROR) << "State manager sends update about unchanged state " << static_cast<int32>(new_state);
return;
@ -3548,8 +3527,7 @@ void Td::on_connection_state_changed(StateManager::State new_state) {
}
connection_state_ = new_state;
send_closure(actor_id(this), &Td::send_update,
make_tl_object<td_api::updateConnectionState>(get_connection_state_object(connection_state_)));
send_closure(actor_id(this), &Td::send_update, get_update_connection_state_object(connection_state_));
}
void Td::start_up() {
@ -4097,7 +4075,7 @@ void Td::init_options_and_network() {
public:
explicit StateManagerCallback(ActorShared<Td> td) : td_(std::move(td)) {
}
bool on_state(StateManager::State state) final {
bool on_state(ConnectionState state) final {
send_closure(td_, &Td::on_connection_state_changed, state);
return td_.is_alive();
}
@ -4709,7 +4687,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(td_api::make_object<td_api::updateConnectionState>(get_connection_state_object(connection_state_)));
updates.push_back(get_update_connection_state_object(connection_state_));
if (auth_manager_->is_authorized()) {
contacts_manager_->get_current_state(updates);

View File

@ -6,11 +6,11 @@
//
#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"
#include "td/telegram/net/NetQueryStats.h"
#include "td/telegram/StateManager.h"
#include "td/telegram/TdCallback.h"
#include "td/telegram/TdParameters.h"
#include "td/telegram/TermsOfService.h"
@ -67,6 +67,7 @@ class PrivacyManager;
class SecureManager;
class SecretChatsManager;
class SponsoredMessageManager;
class StateManager;
class StickersManager;
class StorageManager;
class ThemeManager;
@ -250,7 +251,7 @@ class Td final : public Actor {
static constexpr int64 TERMS_OF_SERVICE_ALARM_ID = -2;
static constexpr int64 PROMO_DATA_ALARM_ID = -3;
void on_connection_state_changed(StateManager::State new_state);
void on_connection_state_changed(ConnectionState new_state);
void send_result(uint64 id, tl_object_ptr<td_api::Object> object);
void send_error(uint64 id, Status error);
@ -278,7 +279,7 @@ class Td final : public Actor {
TdParameters parameters_;
StateManager::State connection_state_ = StateManager::State::Empty;
ConnectionState connection_state_ = ConnectionState::Empty;
std::unordered_multiset<uint64> request_set_;
int actor_refcnt_ = 0;
@ -349,8 +350,6 @@ class Td final : public Actor {
void on_config_option_updated(const string &name);
static tl_object_ptr<td_api::ConnectionState> get_connection_state_object(StateManager::State state);
void send(NetQueryPtr &&query);
class OnRequest;