Improve PingConnection.

GitOrigin-RevId: be82fa18e2f9a154d3d276b5618d0ca72b9e4282
This commit is contained in:
levlam 2018-06-04 19:58:54 +03:00
parent e5385cbd0b
commit 2c5f5a8587
3 changed files with 28 additions and 11 deletions

View File

@ -20,9 +20,11 @@
namespace td { namespace td {
namespace mtproto { namespace mtproto {
class PingConnection : private RawConnection::Callback { class PingConnection : private RawConnection::Callback {
public: public:
explicit PingConnection(std::unique_ptr<RawConnection> raw_connection) : raw_connection_(std::move(raw_connection)) { PingConnection(std::unique_ptr<RawConnection> raw_connection, size_t ping_count)
: raw_connection_(std::move(raw_connection)), ping_count_(ping_count) {
} }
Fd &get_pollable() { Fd &get_pollable() {
@ -43,11 +45,17 @@ class PingConnection : private RawConnection::Callback {
Random::secure_bytes(nonce.raw, sizeof(nonce)); Random::secure_bytes(nonce.raw, sizeof(nonce));
raw_connection_->send_no_crypto(PacketStorer<NoCryptoImpl>(1, create_storer(mtproto_api::req_pq_multi(nonce)))); raw_connection_->send_no_crypto(PacketStorer<NoCryptoImpl>(1, create_storer(mtproto_api::req_pq_multi(nonce))));
was_ping_ = true; was_ping_ = true;
if (ping_count_ == 1) {
start_time_ = Time::now();
}
} }
return raw_connection_->flush(AuthKey(), *this); return raw_connection_->flush(AuthKey(), *this);
} }
bool was_pong() const { bool was_pong() const {
return was_pong_; return finish_time_ > 0;
}
double rtt() const {
return finish_time_ - start_time_;
} }
Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) override { Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) override {
@ -56,14 +64,23 @@ class PingConnection : private RawConnection::Callback {
} }
packet.confirm_read(12); packet.confirm_read(12);
// TODO: fetch_result // TODO: fetch_result
was_pong_ = true;
return Status::OK(); if (--ping_count_ > 0) {
was_ping_ = false;
return flush();
} else {
finish_time_ = Time::now();
return Status::OK();
}
} }
private: private:
std::unique_ptr<RawConnection> raw_connection_; std::unique_ptr<RawConnection> raw_connection_;
size_t ping_count_ = 1;
double start_time_ = 0.0;
double finish_time_ = 0.0;
bool was_ping_ = false; bool was_ping_ = false;
bool was_pong_ = false;
}; };
} // namespace mtproto } // namespace mtproto
} // namespace td } // namespace td

View File

@ -85,19 +85,17 @@ class PingActor : public Actor {
PingActor(std::unique_ptr<mtproto::RawConnection> raw_connection, PingActor(std::unique_ptr<mtproto::RawConnection> raw_connection,
Promise<std::unique_ptr<mtproto::RawConnection>> promise, ActorShared<> parent) Promise<std::unique_ptr<mtproto::RawConnection>> promise, ActorShared<> parent)
: promise_(std::move(promise)), parent_(std::move(parent)) { : promise_(std::move(promise)), parent_(std::move(parent)) {
ping_connection_ = std::make_unique<mtproto::PingConnection>(std::move(raw_connection)); ping_connection_ = std::make_unique<mtproto::PingConnection>(std::move(raw_connection), 2);
} }
private: private:
std::unique_ptr<mtproto::PingConnection> ping_connection_; std::unique_ptr<mtproto::PingConnection> ping_connection_;
Promise<std::unique_ptr<mtproto::RawConnection>> promise_; Promise<std::unique_ptr<mtproto::RawConnection>> promise_;
ActorShared<> parent_; ActorShared<> parent_;
double start_at_;
void start_up() override { void start_up() override {
ping_connection_->get_pollable().set_observer(this); ping_connection_->get_pollable().set_observer(this);
subscribe(ping_connection_->get_pollable()); subscribe(ping_connection_->get_pollable());
start_at_ = Time::now();
set_timeout_in(10); set_timeout_in(10);
yield(); yield();
} }
@ -144,7 +142,7 @@ class PingActor : public Actor {
raw_connection->close(); raw_connection->close();
promise_.set_error(std::move(status)); promise_.set_error(std::move(status));
} else { } else {
raw_connection->rtt_ = Time::now() - start_at_; raw_connection->rtt_ = ping_connection_->rtt();
if (raw_connection->stats_callback()) { if (raw_connection->stats_callback()) {
raw_connection->stats_callback()->on_pong(); raw_connection->stats_callback()->on_pong();
} }

View File

@ -88,8 +88,10 @@ class TestPingActor : public Actor {
Status *result_; Status *result_;
void start_up() override { void start_up() override {
ping_connection_ = std::make_unique<mtproto::PingConnection>(std::make_unique<mtproto::RawConnection>( ping_connection_ = std::make_unique<mtproto::PingConnection>(
SocketFd::open(ip_address_).move_as_ok(), mtproto::TransportType{mtproto::TransportType::Tcp, 0, ""}, nullptr)); std::make_unique<mtproto::RawConnection>(SocketFd::open(ip_address_).move_as_ok(),
mtproto::TransportType{mtproto::TransportType::Tcp, 0, ""}, nullptr),
3);
ping_connection_->get_pollable().set_observer(this); ping_connection_->get_pollable().set_observer(this);
subscribe(ping_connection_->get_pollable()); subscribe(ping_connection_->get_pollable());