From 3f4b29bfafab4dc8cda9fb2a9bdb2ad5877e14e5 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Thu, 9 May 2019 20:13:40 +0200 Subject: [PATCH] Calculate rtt in new Ping GitOrigin-RevId: 9d4ef780ac99ea6c1331dc4e93014657a27f7848 --- td/mtproto/PingConnection.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/td/mtproto/PingConnection.cpp b/td/mtproto/PingConnection.cpp index 7df0a886e..b05b830b2 100644 --- a/td/mtproto/PingConnection.cpp +++ b/td/mtproto/PingConnection.cpp @@ -83,7 +83,8 @@ class PingConnectionPingPong private: unique_ptr auth_data_; unique_ptr connection_; - bool was_pong_{false}; + int pong_cnt_{0}; + double rtt_; bool is_closed_{false}; Status status_; void on_connected() override { @@ -113,7 +114,13 @@ class PingConnectionPingPong void on_container_sent(uint64 container_id, vector msgs_id) override { } Status on_pong() override { - was_pong_ = true; + 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(); } @@ -141,7 +148,7 @@ class PingConnectionPingPong return connection_->move_as_raw_connection(); } Status flush() override { - if (was_pong_) { + if (was_pong()) { return Status::OK(); } connection_->flush(this); @@ -151,10 +158,10 @@ class PingConnectionPingPong return Status::OK(); } bool was_pong() const override { - return was_pong_; + return pong_cnt_ >= 2; } double rtt() const override { - return 1; + return rtt_; } };