2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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/StateManager.h"
|
|
|
|
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
#include "td/actor/SleepActor.h"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-05-08 22:02:15 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StateManager::on_synchronized(bool is_synchronized) {
|
|
|
|
if (sync_flag_ != is_synchronized) {
|
|
|
|
sync_flag_ = is_synchronized;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
if (sync_flag_ && !was_sync_) {
|
|
|
|
was_sync_ = true;
|
2022-04-13 16:40:12 +02:00
|
|
|
set_promises(wait_first_sync_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 16:19:59 +02:00
|
|
|
void StateManager::on_network_updated() {
|
|
|
|
do_on_network(network_type_, true /*inc_generation*/);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StateManager::on_network(NetType new_network_type) {
|
|
|
|
do_on_network(new_network_type, true /*inc_generation*/);
|
|
|
|
}
|
2018-05-08 22:02:15 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StateManager::do_on_network(NetType new_network_type, bool inc_generation) {
|
|
|
|
bool new_network_flag = new_network_type != NetType::None;
|
|
|
|
if (network_flag_ != new_network_flag) {
|
|
|
|
network_flag_ = new_network_flag;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
network_type_ = new_network_type;
|
|
|
|
if (inc_generation) {
|
|
|
|
network_generation_++;
|
|
|
|
}
|
2018-04-19 15:08:30 +02:00
|
|
|
notify_flag(Flag::Network);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StateManager::on_online(bool is_online) {
|
|
|
|
online_flag_ = is_online;
|
2018-04-19 15:08:30 +02:00
|
|
|
notify_flag(Flag::Online);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StateManager::on_proxy(bool use_proxy) {
|
|
|
|
use_proxy_ = use_proxy;
|
|
|
|
on_network(network_type_);
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2020-08-05 18:49:34 +02:00
|
|
|
void StateManager::on_logging_out(bool is_logging_out) {
|
|
|
|
is_logging_out_ = is_logging_out;
|
|
|
|
notify_flag(Flag::LoggingOut);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void StateManager::add_callback(unique_ptr<Callback> callback) {
|
|
|
|
if (callback->on_network(network_type_, network_generation_) && callback->on_online(online_flag_) &&
|
2020-08-05 19:36:13 +02:00
|
|
|
callback->on_state(get_real_state()) && callback->on_logging_out(is_logging_out_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
callbacks_.push_back(std::move(callback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void StateManager::wait_first_sync(Promise<> promise) {
|
|
|
|
if (was_sync_) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
wait_first_sync_.push_back(std::move(promise));
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateManager::close() {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2021-09-16 17:23:10 +02:00
|
|
|
ConnectionState StateManager::get_real_state() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!network_flag_) {
|
2021-09-16 17:23:10 +02:00
|
|
|
return ConnectionState::WaitingForNetwork;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!connect_cnt_) {
|
|
|
|
if (use_proxy_ && !connect_proxy_cnt_) {
|
2021-09-16 17:23:10 +02:00
|
|
|
return ConnectionState::ConnectingToProxy;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-16 17:23:10 +02:00
|
|
|
return ConnectionState::Connecting;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!sync_flag_) {
|
2021-09-16 17:23:10 +02:00
|
|
|
return ConnectionState::Updating;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-16 17:23:10 +02:00
|
|
|
return ConnectionState::Ready;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 15:08:30 +02:00
|
|
|
void StateManager::notify_flag(Flag flag) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto it = callbacks_.begin(); it != callbacks_.end();) {
|
2018-04-19 15:08:30 +02:00
|
|
|
bool ok = [&] {
|
|
|
|
switch (flag) {
|
|
|
|
case Flag::Online:
|
|
|
|
return (*it)->on_online(online_flag_);
|
|
|
|
case Flag::State:
|
|
|
|
return (*it)->on_state(flush_state_);
|
|
|
|
case Flag::Network:
|
|
|
|
return (*it)->on_network(network_type_, network_generation_);
|
2020-08-05 18:49:34 +02:00
|
|
|
case Flag::LoggingOut:
|
|
|
|
return (*it)->on_logging_out(is_logging_out_);
|
2018-04-19 15:08:30 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (ok) {
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
it = callbacks_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateManager::on_network_soft() {
|
|
|
|
if (network_type_ == NetType::Unknown) {
|
|
|
|
LOG(INFO) << "Auto set net_type = Other";
|
|
|
|
do_on_network(NetType::Other, false /*inc_generation*/);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateManager::start_up() {
|
|
|
|
create_actor<SleepActor>("SleepActor", 1, PromiseCreator::event(self_closure(this, &StateManager::on_network_soft)))
|
|
|
|
.release();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateManager::loop() {
|
|
|
|
auto now = Time::now();
|
|
|
|
auto state = get_real_state();
|
|
|
|
if (state != pending_state_) {
|
|
|
|
pending_state_ = state;
|
|
|
|
if (!has_timestamp_) {
|
|
|
|
pending_timestamp_ = now;
|
|
|
|
has_timestamp_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pending_state_ != flush_state_) {
|
|
|
|
double delay = 0;
|
2021-09-16 17:23:10 +02:00
|
|
|
if (flush_state_ != ConnectionState::Empty) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (static_cast<int32>(pending_state_) > static_cast<int32>(flush_state_)) {
|
|
|
|
delay = UP_DELAY;
|
|
|
|
} else {
|
|
|
|
delay = DOWN_DELAY;
|
|
|
|
}
|
|
|
|
if (network_type_ == NetType::Unknown) {
|
|
|
|
delay = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(has_timestamp_);
|
|
|
|
if (now >= pending_timestamp_ + delay) {
|
|
|
|
has_timestamp_ = false;
|
|
|
|
flush_state_ = pending_state_;
|
2018-04-19 15:08:30 +02:00
|
|
|
notify_flag(Flag::State);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
set_timeout_at(pending_timestamp_ + delay);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
has_timestamp_ = false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-08 22:02:15 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|