2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
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
|
|
|
|
|
|
|
|
#include "td/mtproto/AuthKey.h"
|
|
|
|
#include "td/mtproto/NoCryptoStorer.h"
|
|
|
|
#include "td/mtproto/RawConnection.h"
|
|
|
|
#include "td/mtproto/utils.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/port/Fd.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/Status.h"
|
2018-06-05 19:44:41 +02:00
|
|
|
#include "td/utils/Time.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/mtproto/mtproto_api.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
2018-06-04 18:58:54 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class PingConnection : private RawConnection::Callback {
|
|
|
|
public:
|
2018-06-04 18:58:54 +02:00
|
|
|
PingConnection(std::unique_ptr<RawConnection> raw_connection, size_t ping_count)
|
|
|
|
: raw_connection_(std::move(raw_connection)), ping_count_(ping_count) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-14 09:42:40 +02:00
|
|
|
PollableFdInfo &get_poll_info() {
|
|
|
|
return raw_connection_->get_poll_info();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<RawConnection> move_as_raw_connection() {
|
|
|
|
return std::move(raw_connection_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close() {
|
|
|
|
raw_connection_->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status flush() {
|
|
|
|
if (!was_ping_) {
|
|
|
|
UInt128 nonce;
|
|
|
|
Random::secure_bytes(nonce.raw, sizeof(nonce));
|
2018-02-22 01:36:40 +01:00
|
|
|
raw_connection_->send_no_crypto(PacketStorer<NoCryptoImpl>(1, create_storer(mtproto_api::req_pq_multi(nonce))));
|
2018-12-31 20:04:05 +01:00
|
|
|
was_ping_ = true;
|
2018-06-04 18:58:54 +02:00
|
|
|
if (ping_count_ == 1) {
|
|
|
|
start_time_ = Time::now();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return raw_connection_->flush(AuthKey(), *this);
|
|
|
|
}
|
|
|
|
bool was_pong() const {
|
2018-06-04 18:58:54 +02:00
|
|
|
return finish_time_ > 0;
|
|
|
|
}
|
|
|
|
double rtt() const {
|
|
|
|
return finish_time_ - start_time_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) override {
|
|
|
|
if (packet.size() < 12) {
|
|
|
|
return Status::Error("Result is too small");
|
|
|
|
}
|
|
|
|
packet.confirm_read(12);
|
|
|
|
// TODO: fetch_result
|
2018-06-04 18:58:54 +02:00
|
|
|
|
|
|
|
if (--ping_count_ > 0) {
|
|
|
|
was_ping_ = false;
|
|
|
|
return flush();
|
|
|
|
} else {
|
|
|
|
finish_time_ = Time::now();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<RawConnection> raw_connection_;
|
2018-06-04 18:58:54 +02:00
|
|
|
size_t ping_count_ = 1;
|
|
|
|
double start_time_ = 0.0;
|
|
|
|
double finish_time_ = 0.0;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool was_ping_ = false;
|
|
|
|
};
|
2018-06-04 18:58:54 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|