1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #include "td/mtproto/Ping.h"
8
9 #include "td/mtproto/AuthData.h"
10 #include "td/mtproto/PingConnection.h"
11 #include "td/mtproto/RawConnection.h"
12
13 #include "td/actor/actor.h"
14 #include "td/actor/PromiseFuture.h"
15
16 #include "td/utils/SliceBuilder.h"
17 #include "td/utils/Status.h"
18
19 namespace td {
20 namespace mtproto {
21
create_ping_actor(Slice actor_name,unique_ptr<RawConnection> raw_connection,unique_ptr<AuthData> auth_data,Promise<unique_ptr<RawConnection>> promise,ActorShared<> parent)22 ActorOwn<> create_ping_actor(Slice actor_name, unique_ptr<RawConnection> raw_connection, unique_ptr<AuthData> auth_data,
23 Promise<unique_ptr<RawConnection>> promise, ActorShared<> parent) {
24 class PingActor final : public Actor {
25 public:
26 PingActor(unique_ptr<RawConnection> raw_connection, unique_ptr<AuthData> auth_data,
27 Promise<unique_ptr<RawConnection>> promise, ActorShared<> parent)
28 : promise_(std::move(promise)), parent_(std::move(parent)) {
29 if (auth_data) {
30 ping_connection_ = PingConnection::create_ping_pong(std::move(raw_connection), std::move(auth_data));
31 } else {
32 ping_connection_ = PingConnection::create_req_pq(std::move(raw_connection), 2);
33 }
34 }
35
36 private:
37 unique_ptr<PingConnection> ping_connection_;
38 Promise<unique_ptr<RawConnection>> promise_;
39 ActorShared<> parent_;
40
41 void start_up() final {
42 Scheduler::subscribe(ping_connection_->get_poll_info().extract_pollable_fd(this));
43 set_timeout_in(10);
44 yield();
45 }
46
47 void hangup() final {
48 finish(Status::Error("Canceled"));
49 stop();
50 }
51
52 void tear_down() final {
53 finish(Status::OK());
54 }
55
56 void loop() final {
57 auto status = ping_connection_->flush();
58 if (status.is_error()) {
59 finish(std::move(status));
60 return stop();
61 }
62 if (ping_connection_->was_pong()) {
63 finish(Status::OK());
64 return stop();
65 }
66 }
67
68 void timeout_expired() final {
69 finish(Status::Error("Pong timeout expired"));
70 stop();
71 }
72
73 void finish(Status status) {
74 auto raw_connection = ping_connection_->move_as_raw_connection();
75 if (!raw_connection) {
76 CHECK(!promise_);
77 return;
78 }
79 Scheduler::unsubscribe(raw_connection->get_poll_info().get_pollable_fd_ref());
80 if (promise_) {
81 if (status.is_error()) {
82 if (raw_connection->stats_callback()) {
83 raw_connection->stats_callback()->on_error();
84 }
85 raw_connection->close();
86 promise_.set_error(std::move(status));
87 } else {
88 raw_connection->extra().rtt = ping_connection_->rtt();
89 if (raw_connection->stats_callback()) {
90 raw_connection->stats_callback()->on_pong();
91 }
92 promise_.set_value(std::move(raw_connection));
93 }
94 } else {
95 if (raw_connection->stats_callback()) {
96 raw_connection->stats_callback()->on_error();
97 }
98 raw_connection->close();
99 }
100 }
101 };
102 return ActorOwn<>(create_actor<PingActor>(PSLICE() << "PingActor<" << actor_name << ">", std::move(raw_connection),
103 std::move(auth_data), std::move(promise), std::move(parent)));
104 }
105
106 } // namespace mtproto
107 } // namespace td
108