2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-16 17:23:10 +02:00
|
|
|
#include "td/telegram/ConnectionState.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/NetType.h"
|
|
|
|
|
2021-09-16 18:09:39 +02:00
|
|
|
#include "td/mtproto/ConnectionManager.h"
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
namespace td {
|
2018-05-08 22:02:15 +02:00
|
|
|
|
2021-09-16 18:09:39 +02:00
|
|
|
class StateManager final : public mtproto::ConnectionManager {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
virtual ~Callback() = default;
|
2021-09-16 17:23:10 +02:00
|
|
|
virtual bool on_state(ConnectionState state) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
virtual bool on_network(NetType network_type, uint32 generation) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
virtual bool on_online(bool is_online) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-05 18:49:34 +02:00
|
|
|
virtual bool on_logging_out(bool is_logging_out) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2020-07-26 13:24:30 +02:00
|
|
|
explicit StateManager(ActorShared<> parent) : parent_(std::move(parent)) {
|
2020-07-19 18:29:43 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_synchronized(bool is_synchronized);
|
|
|
|
|
2018-07-01 16:19:59 +02:00
|
|
|
void on_network_updated();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_network(NetType new_network_type);
|
|
|
|
|
|
|
|
void on_online(bool is_online);
|
|
|
|
|
|
|
|
void on_proxy(bool use_proxy);
|
|
|
|
|
2020-08-05 18:49:34 +02:00
|
|
|
void on_logging_out(bool is_logging_out);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void add_callback(unique_ptr<Callback> net_callback);
|
|
|
|
|
|
|
|
void wait_first_sync(Promise<> promise);
|
|
|
|
|
|
|
|
void close();
|
|
|
|
|
|
|
|
private:
|
2020-07-19 18:29:43 +02:00
|
|
|
ActorShared<> parent_;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool sync_flag_ = true;
|
|
|
|
bool network_flag_ = true;
|
|
|
|
NetType network_type_ = NetType::Unknown;
|
|
|
|
uint32 network_generation_ = 1;
|
|
|
|
bool online_flag_ = false;
|
|
|
|
bool use_proxy_ = false;
|
2020-08-05 19:36:13 +02:00
|
|
|
bool is_logging_out_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
static constexpr double UP_DELAY = 0.05;
|
|
|
|
static constexpr double DOWN_DELAY = 0.3;
|
|
|
|
|
2021-09-16 17:23:10 +02:00
|
|
|
ConnectionState pending_state_ = ConnectionState::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool has_timestamp_ = false;
|
|
|
|
double pending_timestamp_ = 0;
|
2021-09-16 17:23:10 +02:00
|
|
|
ConnectionState flush_state_ = ConnectionState::Empty;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
vector<unique_ptr<Callback>> callbacks_;
|
|
|
|
|
|
|
|
bool was_sync_ = false;
|
2021-09-16 17:23:10 +02:00
|
|
|
vector<Promise<>> wait_first_sync_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void inc_connect();
|
|
|
|
void dec_connect();
|
|
|
|
|
2020-08-05 18:49:34 +02:00
|
|
|
enum class Flag : int32 { Online, State, Network, LoggingOut };
|
2018-04-19 15:08:30 +02:00
|
|
|
void notify_flag(Flag flag);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
|
|
|
void loop() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void on_network_soft();
|
|
|
|
void do_on_network(NetType new_network_type, bool inc_generation);
|
|
|
|
|
2021-09-16 17:23:10 +02:00
|
|
|
ConnectionState get_real_state() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-05-08 22:02:15 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|