diff --git a/td/mtproto/AuthData.h b/td/mtproto/AuthData.h index e9aec948e..1f7b07334 100644 --- a/td/mtproto/AuthData.h +++ b/td/mtproto/AuthData.h @@ -203,7 +203,7 @@ class AuthData { // server_time_difference >= max{diff} bool update_server_time_difference(double diff); - void set_server_time_difference(double diff) { + void reset_server_time_difference(double diff) { server_time_difference_was_updated_ = false; server_time_difference_ = diff; } diff --git a/td/mtproto/PingConnection.cpp b/td/mtproto/PingConnection.cpp index 58ffe9f20..a08a1d61c 100644 --- a/td/mtproto/PingConnection.cpp +++ b/td/mtproto/PingConnection.cpp @@ -119,7 +119,7 @@ class PingConnectionPingPong final } void on_server_salt_updated() final { } - void on_server_time_difference_updated() final { + void on_server_time_difference_updated(bool force) final { } void on_session_created(uint64 unique_id, uint64 first_id) final { diff --git a/td/mtproto/SessionConnection.cpp b/td/mtproto/SessionConnection.cpp index abfc230e6..b3b9763ee 100644 --- a/td/mtproto/SessionConnection.cpp +++ b/td/mtproto/SessionConnection.cpp @@ -330,6 +330,8 @@ Status SessionConnection::on_packet(const MsgInfo &info, LOG(WARNING) << bad_info << ": MessageId is too high. Session will be closed"; // All this queries will be re-sent by parent to_send_.clear(); + auth_data_->reset_server_time_difference(static_cast(info.message_id >> 32) - Time::now()); + callback_->on_server_time_difference_updated(true); callback_->on_session_failed(Status::Error("MessageId is too high")); return Status::Error("MessageId is too high"); } @@ -692,7 +694,7 @@ Status SessionConnection::on_raw_packet(const PacketInfo &info, BufferSlice pack auto status = auth_data_->check_packet(info.session_id, info.message_id, Time::now_cached(), time_difference_was_updated); if (time_difference_was_updated) { - callback_->on_server_time_difference_updated(); + callback_->on_server_time_difference_updated(false); } if (status.is_error()) { if (status.code() == 1) { diff --git a/td/mtproto/SessionConnection.h b/td/mtproto/SessionConnection.h index 0a505a8ff..9cbe7a8f0 100644 --- a/td/mtproto/SessionConnection.h +++ b/td/mtproto/SessionConnection.h @@ -106,7 +106,7 @@ class SessionConnection final virtual void on_auth_key_updated() = 0; virtual void on_tmp_auth_key_updated() = 0; virtual void on_server_salt_updated() = 0; - virtual void on_server_time_difference_updated() = 0; + virtual void on_server_time_difference_updated(bool force) = 0; virtual void on_session_created(uint64 unique_id, uint64 first_id) = 0; virtual void on_session_failed(Status status) = 0; diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 7ddf9f656..47d530dc4 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -459,8 +459,8 @@ static ActorOwn<> get_full_config(DcOption option, Promiseupdate_server_time_difference(diff); + void update_server_time_difference(double diff, bool force) final { + G()->update_server_time_difference(diff, force); } double get_server_time_difference() final { return G()->get_server_time_difference(); diff --git a/td/telegram/Global.cpp b/td/telegram/Global.cpp index 697fd618a..dacaaef86 100644 --- a/td/telegram/Global.cpp +++ b/td/telegram/Global.cpp @@ -185,8 +185,8 @@ int32 Global::to_unix_time(double server_time) const { return static_cast(server_time); } -void Global::update_server_time_difference(double diff) { - if (!server_time_difference_was_updated_ || server_time_difference_ < diff) { +void Global::update_server_time_difference(double diff, bool force) { + if (force || !server_time_difference_was_updated_ || server_time_difference_ < diff) { server_time_difference_ = diff; server_time_difference_was_updated_ = true; do_save_server_time_difference(); diff --git a/td/telegram/Global.h b/td/telegram/Global.h index 5f678a2c7..20319d7d5 100644 --- a/td/telegram/Global.h +++ b/td/telegram/Global.h @@ -159,7 +159,7 @@ class Global final : public ActorContext { return to_unix_time(server_time_cached()); } - void update_server_time_difference(double diff); + void update_server_time_difference(double diff, bool force); void save_server_time(); diff --git a/td/telegram/net/AuthDataShared.cpp b/td/telegram/net/AuthDataShared.cpp index df7251d70..fb9e30705 100644 --- a/td/telegram/net/AuthDataShared.cpp +++ b/td/telegram/net/AuthDataShared.cpp @@ -51,8 +51,8 @@ class AuthDataSharedImpl final : public AuthDataShared { } // TODO: extract it from G() - void update_server_time_difference(double diff) final { - G()->update_server_time_difference(diff); + void update_server_time_difference(double diff, bool force) final { + G()->update_server_time_difference(diff, force); } double get_server_time_difference() final { diff --git a/td/telegram/net/AuthDataShared.h b/td/telegram/net/AuthDataShared.h index c96d76f41..efcdd3e5f 100644 --- a/td/telegram/net/AuthDataShared.h +++ b/td/telegram/net/AuthDataShared.h @@ -37,7 +37,7 @@ class AuthDataShared { virtual const std::shared_ptr &public_rsa_key() = 0; virtual mtproto::AuthKey get_auth_key() = 0; virtual void set_auth_key(const mtproto::AuthKey &auth_key) = 0; - virtual void update_server_time_difference(double diff) = 0; + virtual void update_server_time_difference(double diff, bool force) = 0; virtual double get_server_time_difference() = 0; virtual void add_auth_key_listener(unique_ptr listener) = 0; diff --git a/td/telegram/net/Session.cpp b/td/telegram/net/Session.cpp index a4b6dd859..0f64d84a4 100644 --- a/td/telegram/net/Session.cpp +++ b/td/telegram/net/Session.cpp @@ -250,7 +250,7 @@ Session::Session(unique_ptr callback, std::shared_ptr auth_data_.set_use_pfs(use_pfs); auth_data_.set_main_auth_key(shared_auth_data_->get_auth_key()); // auth_data_.break_main_auth_key(); - auth_data_.set_server_time_difference(shared_auth_data_->get_server_time_difference()); + auth_data_.reset_server_time_difference(shared_auth_data_->get_server_time_difference()); auto now = Time::now(); auth_data_.set_future_salts(shared_auth_data_->get_future_salts(), now); if (use_pfs && !tmp_auth_key.empty()) { @@ -612,8 +612,8 @@ void Session::on_server_salt_updated() { shared_auth_data_->set_future_salts(auth_data_.get_future_salts()); } -void Session::on_server_time_difference_updated() { - shared_auth_data_->update_server_time_difference(auth_data_.get_server_time_difference()); +void Session::on_server_time_difference_updated(bool force) { + shared_auth_data_->update_server_time_difference(auth_data_.get_server_time_difference(), force); } void Session::on_closed(Status status) { @@ -1434,7 +1434,7 @@ void Session::on_handshake_ready(Result> r on_server_salt_updated(); } if (auth_data_.update_server_time_difference(handshake->get_server_time_diff())) { - on_server_time_difference_updated(); + on_server_time_difference_updated(true); } } } diff --git a/td/telegram/net/Session.h b/td/telegram/net/Session.h index d56e41eb8..b7f0f437b 100644 --- a/td/telegram/net/Session.h +++ b/td/telegram/net/Session.h @@ -207,7 +207,7 @@ class Session final void on_auth_key_updated() final; void on_tmp_auth_key_updated() final; void on_server_salt_updated() final; - void on_server_time_difference_updated() final; + void on_server_time_difference_updated(bool force) final; void on_session_created(uint64 unique_id, uint64 first_message_id) final; void on_session_failed(Status status) final; diff --git a/test/mtproto.cpp b/test/mtproto.cpp index 07975031b..614e153e6 100644 --- a/test/mtproto.cpp +++ b/test/mtproto.cpp @@ -604,7 +604,7 @@ class FastPingTestActor final : public td::Actor { if (iteration_ % 2 == 0) { auth_data = td::make_unique(); auth_data->set_tmp_auth_key(handshake_->get_auth_key()); - auth_data->set_server_time_difference(handshake_->get_server_time_diff()); + auth_data->reset_server_time_difference(handshake_->get_server_time_diff()); auth_data->set_server_salt(handshake_->get_server_salt(), td::Time::now()); auth_data->set_future_salts({td::mtproto::ServerSalt{0u, 1e20, 1e30}}, td::Time::now()); auth_data->set_use_pfs(true);