aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/ConnectionStateManager.cpp
blob: 2b5e907afe4f3c17b03d3c940bb1186d9db84740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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/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)) {
}

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