2019-05-06 18:59:49 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2019-05-06 18:59:49 +02: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/mtproto/PingConnection.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
|
|
|
|
#include "td/mtproto/AuthData.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/mtproto/AuthKey.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/mtproto/mtproto_api.h"
|
|
|
|
#include "td/mtproto/NoCryptoStorer.h"
|
|
|
|
#include "td/mtproto/PacketInfo.h"
|
|
|
|
#include "td/mtproto/PacketStorer.h"
|
|
|
|
#include "td/mtproto/PingConnection.h"
|
|
|
|
#include "td/mtproto/RawConnection.h"
|
2019-05-06 18:59:49 +02:00
|
|
|
#include "td/mtproto/SessionConnection.h"
|
2019-05-22 20:17:24 +02:00
|
|
|
#include "td/mtproto/utils.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
#include "td/utils/UInt.h"
|
|
|
|
|
2019-05-06 18:59:49 +02:00
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
|
|
|
namespace detail {
|
2019-05-22 20:17:24 +02:00
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class PingConnectionReqPQ final
|
2019-05-06 18:59:49 +02:00
|
|
|
: public PingConnection
|
|
|
|
, private RawConnection::Callback {
|
|
|
|
public:
|
|
|
|
PingConnectionReqPQ(unique_ptr<RawConnection> raw_connection, size_t ping_count)
|
|
|
|
: raw_connection_(std::move(raw_connection)), ping_count_(ping_count) {
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
PollableFdInfo &get_poll_info() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
return raw_connection_->get_poll_info();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
unique_ptr<RawConnection> move_as_raw_connection() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
return std::move(raw_connection_);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status flush() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
if (!was_ping_) {
|
|
|
|
UInt128 nonce;
|
|
|
|
Random::secure_bytes(nonce.raw, sizeof(nonce));
|
|
|
|
raw_connection_->send_no_crypto(PacketStorer<NoCryptoImpl>(1, create_storer(mtproto_api::req_pq_multi(nonce))));
|
|
|
|
was_ping_ = true;
|
|
|
|
if (ping_count_ == 1) {
|
|
|
|
start_time_ = Time::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return raw_connection_->flush(AuthKey(), *this);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool was_pong() const final {
|
2019-05-06 18:59:49 +02:00
|
|
|
return finish_time_ > 0;
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
double rtt() const final {
|
2019-05-06 18:59:49 +02:00
|
|
|
return finish_time_ - start_time_;
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
if (packet.size() < 12) {
|
|
|
|
return Status::Error("Result is too small");
|
|
|
|
}
|
|
|
|
packet.confirm_read(12);
|
|
|
|
// TODO: fetch_result
|
|
|
|
|
|
|
|
if (--ping_count_ > 0) {
|
|
|
|
was_ping_ = false;
|
|
|
|
return flush();
|
|
|
|
} else {
|
|
|
|
finish_time_ = Time::now();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unique_ptr<RawConnection> raw_connection_;
|
|
|
|
size_t ping_count_ = 1;
|
|
|
|
double start_time_ = 0.0;
|
|
|
|
double finish_time_ = 0.0;
|
|
|
|
bool was_ping_ = false;
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class PingConnectionPingPong final
|
2019-05-06 18:59:49 +02:00
|
|
|
: public PingConnection
|
|
|
|
, private SessionConnection::Callback {
|
|
|
|
public:
|
2019-05-22 20:17:24 +02:00
|
|
|
PingConnectionPingPong(unique_ptr<RawConnection> raw_connection, unique_ptr<AuthData> auth_data)
|
2019-05-06 18:59:49 +02:00
|
|
|
: auth_data_(std::move(auth_data)) {
|
2019-05-06 20:53:39 +02:00
|
|
|
auth_data_->set_header("");
|
2019-06-04 17:52:26 +02:00
|
|
|
auth_data_->clear_seq_no();
|
2019-05-06 18:59:49 +02:00
|
|
|
connection_ =
|
|
|
|
make_unique<SessionConnection>(SessionConnection::Mode::Tcp, std::move(raw_connection), auth_data_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-22 20:17:24 +02:00
|
|
|
unique_ptr<AuthData> auth_data_;
|
|
|
|
unique_ptr<SessionConnection> connection_;
|
2019-05-09 20:13:40 +02:00
|
|
|
int pong_cnt_{0};
|
|
|
|
double rtt_;
|
2019-05-06 18:59:49 +02:00
|
|
|
bool is_closed_{false};
|
|
|
|
Status status_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_connected() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_closed(Status status) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
is_closed_ = true;
|
2019-05-09 21:57:35 +02:00
|
|
|
CHECK(status.is_error());
|
2019-05-06 18:59:49 +02:00
|
|
|
status_ = std::move(status);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_auth_key_updated() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_tmp_auth_key_updated() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_server_salt_updated() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_server_time_difference_updated() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_session_created(uint64 unique_id, uint64 first_id) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_session_failed(Status status) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_container_sent(uint64 container_id, vector<uint64> msgs_id) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
Status on_pong() final {
|
2019-05-09 20:13:40 +02:00
|
|
|
pong_cnt_++;
|
|
|
|
if (pong_cnt_ == 1) {
|
|
|
|
rtt_ = Time::now();
|
|
|
|
connection_->set_online(false, false);
|
|
|
|
} else if (pong_cnt_ == 2) {
|
|
|
|
rtt_ = Time::now() - rtt_;
|
|
|
|
}
|
2019-05-06 18:59:49 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-09-10 15:31:04 +02:00
|
|
|
Status on_update(BufferSlice packet) final {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_message_ack(uint64 id) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
Status on_message_result_ok(uint64 id, BufferSlice packet, size_t original_size) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
LOG(ERROR) << "Unexpected message";
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-07-26 18:58:31 +02:00
|
|
|
void on_message_result_error(uint64 id, int code, string message) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_message_failed(uint64 id, Status status) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_message_info(uint64 id, int32 state, uint64 answer_id, int32 answer_size) final {
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
Status on_destroy_auth_key() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
LOG(ERROR) << "Destroy auth key";
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
PollableFdInfo &get_poll_info() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
return connection_->get_poll_info();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
unique_ptr<RawConnection> move_as_raw_connection() final {
|
2019-05-06 18:59:49 +02:00
|
|
|
return connection_->move_as_raw_connection();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
Status flush() final {
|
2019-05-09 20:13:40 +02:00
|
|
|
if (was_pong()) {
|
2019-05-06 18:59:49 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-05-09 21:57:35 +02:00
|
|
|
CHECK(!is_closed_);
|
2019-05-06 18:59:49 +02:00
|
|
|
connection_->flush(this);
|
|
|
|
if (is_closed_) {
|
2019-05-09 21:57:35 +02:00
|
|
|
CHECK(status_.is_error());
|
2019-05-06 18:59:49 +02:00
|
|
|
return std::move(status_);
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
bool was_pong() const final {
|
2019-05-09 20:13:40 +02:00
|
|
|
return pong_cnt_ >= 2;
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
double rtt() const final {
|
2019-05-09 20:13:40 +02:00
|
|
|
return rtt_;
|
2019-05-06 18:59:49 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
2019-05-22 20:17:24 +02:00
|
|
|
|
2019-05-06 18:59:49 +02:00
|
|
|
unique_ptr<PingConnection> PingConnection::create_req_pq(unique_ptr<RawConnection> raw_connection, size_t ping_count) {
|
|
|
|
return make_unique<detail::PingConnectionReqPQ>(std::move(raw_connection), ping_count);
|
|
|
|
}
|
2019-05-22 20:17:24 +02:00
|
|
|
|
2019-05-06 18:59:49 +02:00
|
|
|
unique_ptr<PingConnection> PingConnection::create_ping_pong(unique_ptr<RawConnection> raw_connection,
|
|
|
|
unique_ptr<AuthData> auth_data) {
|
|
|
|
return make_unique<detail::PingConnectionPingPong>(std::move(raw_connection), std::move(auth_data));
|
|
|
|
}
|
2019-05-22 20:17:24 +02:00
|
|
|
|
2019-05-06 18:59:49 +02:00
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|