tdlight/td/mtproto/PingConnection.cpp

202 lines
5.5 KiB
C++
Raw Normal View History

//
2022-12-31 22:28:08 +01:00
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
//
// 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"
#include "td/mtproto/AuthData.h"
#include "td/mtproto/AuthKey.h"
#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"
#include "td/mtproto/SessionConnection.h"
#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"
namespace td {
namespace mtproto {
namespace detail {
class PingConnectionReqPQ final
: 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 {
return raw_connection_->get_poll_info();
}
2021-07-03 22:51:36 +02:00
unique_ptr<RawConnection> move_as_raw_connection() final {
return std::move(raw_connection_);
}
2021-07-03 22:51:36 +02:00
Status flush() final {
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 {
return finish_time_ > 0;
}
2021-07-03 22:51:36 +02:00
double rtt() const final {
return finish_time_ - start_time_;
}
2021-07-03 22:51:36 +02:00
Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) final {
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;
};
class PingConnectionPingPong final
: public PingConnection
, private SessionConnection::Callback {
public:
PingConnectionPingPong(unique_ptr<RawConnection> raw_connection, unique_ptr<AuthData> auth_data)
: auth_data_(std::move(auth_data)) {
auth_data_->set_header("");
auth_data_->clear_seq_no();
connection_ =
make_unique<SessionConnection>(SessionConnection::Mode::Tcp, std::move(raw_connection), auth_data_.get());
}
private:
unique_ptr<AuthData> auth_data_;
unique_ptr<SessionConnection> connection_;
int pong_cnt_{0};
double rtt_;
bool is_closed_{false};
Status status_;
2021-07-03 22:51:36 +02:00
void on_connected() final {
}
2021-07-03 22:51:36 +02:00
void on_closed(Status status) final {
is_closed_ = true;
CHECK(status.is_error());
status_ = std::move(status);
}
2021-07-03 22:51:36 +02:00
void on_auth_key_updated() final {
}
2021-07-03 22:51:36 +02:00
void on_tmp_auth_key_updated() final {
}
2021-07-03 22:51:36 +02:00
void on_server_salt_updated() final {
}
void on_server_time_difference_updated(bool force) final {
}
2023-09-10 00:12:40 +02:00
void on_new_session_created(uint64 unique_id, uint64 first_message_id) final {
}
2021-07-03 22:51:36 +02:00
void on_session_failed(Status status) final {
}
2021-07-03 22:51:36 +02:00
void on_container_sent(uint64 container_id, vector<uint64> msgs_id) final {
}
2021-07-03 22:51:36 +02:00
Status on_pong() final {
pong_cnt_++;
if (pong_cnt_ == 1) {
rtt_ = Time::now();
connection_->set_online(false, false);
} else if (pong_cnt_ == 2) {
rtt_ = Time::now() - rtt_;
}
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 {
}
2021-07-03 22:51:36 +02:00
Status on_message_result_ok(uint64 id, BufferSlice packet, size_t original_size) final {
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 {
}
2021-07-03 22:51:36 +02:00
void on_message_failed(uint64 id, Status status) final {
}
2023-09-01 15:11:21 +02:00
void on_message_info(uint64 id, int32 state, uint64 answer_id, int32 answer_size, int32 source) final {
}
2021-07-03 22:51:36 +02:00
Status on_destroy_auth_key() final {
LOG(ERROR) << "Destroy auth key";
return Status::OK();
}
2021-07-03 22:51:36 +02:00
PollableFdInfo &get_poll_info() final {
return connection_->get_poll_info();
}
2021-07-03 22:51:36 +02:00
unique_ptr<RawConnection> move_as_raw_connection() final {
return connection_->move_as_raw_connection();
}
2021-07-03 22:51:36 +02:00
Status flush() final {
if (was_pong()) {
return Status::OK();
}
CHECK(!is_closed_);
connection_->flush(this);
if (is_closed_) {
CHECK(status_.is_error());
return std::move(status_);
}
return Status::OK();
}
2021-07-03 22:51:36 +02:00
bool was_pong() const final {
return pong_cnt_ >= 2;
}
2021-07-03 22:51:36 +02:00
double rtt() const final {
return rtt_;
}
};
} // namespace detail
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);
}
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));
}
} // namespace mtproto
} // namespace td