From 559b20840a069aa00d6de1ddecd8ea03cd48a136 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 15 Sep 2018 15:24:25 +0300 Subject: [PATCH] tg_cli: support for proper closing. GitOrigin-RevId: 904b5a87c64bdd2d097a8a81f7c39bd31063097b --- td/telegram/cli.cpp | 115 +++++++++++++++++--------- td/telegram/net/TempAuthKeyWatchdog.h | 3 + 2 files changed, 81 insertions(+), 37 deletions(-) diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 7d0399f7..a40a7f8f 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -393,6 +393,41 @@ class CliClient final : public Actor { } } + void on_update_autorization_state(const td_api::AuthorizationState &state) { + switch (state.get_id()) { + case td_api::authorizationStateWaitTdlibParameters::ID: { + auto parameters = td_api::make_object(); + parameters->use_test_dc_ = use_test_dc_; + parameters->use_message_database_ = true; + parameters->use_secret_chats_ = true; + parameters->api_id_ = api_id_; + parameters->api_hash_ = api_hash_; + parameters->system_language_code_ = "en"; + parameters->device_model_ = "Desktop"; + parameters->system_version_ = "Unknown"; + parameters->application_version_ = "tg_cli"; + send_request(td_api::make_object(std::move(parameters))); + break; + } + case td_api::authorizationStateWaitEncryptionKey::ID: + send_request(td_api::make_object()); + break; + case td_api::authorizationStateReady::ID: + LOG(INFO) << "Logged in"; + break; + case td_api::authorizationStateClosed::ID: + LOG(WARNING) << "TD closed"; + // TODO only if count of created matches count of closed + td_.reset(); + if (!close_flag_) { + create_td("ClientActor3"); + } + break; + default: + break; + } + } + int64 as_chat_id(Slice str) const { str = trim(str); if (str[0] == '@') { @@ -513,13 +548,13 @@ class CliClient final : public Actor { return transform(full_split(ids_string, delimiter), to_integer); } - void on_result(uint64 id, tl_object_ptr result) { + void on_result(uint64 generation, uint64 id, tl_object_ptr result) { if (id > 0 && GET_VERBOSITY_LEVEL() < VERBOSITY_NAME(td_requests)) { - LOG(ERROR) << "on_result [id=" << id << "] " << to_string(result); + LOG(ERROR) << "on_result [" << generation << "][id=" << id << "] " << to_string(result); } auto as_json_str = json_encode(ToJson(result)); - // LOG(INFO) << "on_result [id=" << id << "] " << as_json_str; + // LOG(INFO) << "on_result [" << generation << "][id=" << id << "] " << as_json_str; auto copy_as_json_str = as_json_str; auto as_json_value = json_decode(copy_as_json_str).move_as_ok(); td_api::object_ptr object; @@ -527,7 +562,11 @@ class CliClient final : public Actor { CHECK(object != nullptr); auto as_json_str2 = json_encode(ToJson(object)); CHECK(as_json_str == as_json_str2) << "\n" << tag("a", as_json_str) << "\n" << tag("b", as_json_str2); - // LOG(INFO) << "on_result [id=" << id << "] " << as_json_str; + // LOG(INFO) << "on_result [" << generation << "][id=" << id << "] " << as_json_str; + + if (generation != generation_) { + return; + } int32 result_id = result == nullptr ? 0 : result->get_id(); @@ -593,6 +632,10 @@ class CliClient final : public Actor { case td_api::updateFileGenerationStart::ID: on_file_generation_start(*static_cast(result.get())); break; + case td_api::updateAuthorizationState::ID: + on_update_autorization_state( + *(static_cast(result.get())->authorization_state_)); + break; case td_api::updateChatLastMessage::ID: { auto message = static_cast(result.get())->last_message_.get(); if (message != nullptr && message->content_->get_id() == td_api::messageText::ID) { @@ -609,18 +652,21 @@ class CliClient final : public Actor { } } - void on_error(uint64 id, tl_object_ptr error) { + void on_error(uint64 generation, uint64 id, tl_object_ptr error) { if (id > 0 && GET_VERBOSITY_LEVEL() < VERBOSITY_NAME(td_requests)) { - LOG(ERROR) << "on_error [id=" << id << "] " << to_string(error); + LOG(ERROR) << "on_error [" << generation << "][id=" << id << "] " << to_string(error); } } - void on_closed() { - LOG(INFO) << "on_closed"; - ready_to_stop_ = true; - if (close_flag_) { - yield(); - return; + void on_closed(uint64 generation) { + LOG(WARNING) << "on_closed " << generation; + closed_td_++; + if (closed_td_ == generation_) { + LOG(WARNING) << "Ready to stop"; + ready_to_stop_ = true; + if (close_flag_) { + yield(); + } } } @@ -662,40 +708,48 @@ class CliClient final : public Actor { } #endif - unique_ptr make_td_callback() { + uint64 generation_ = 0; + uint64 closed_td_ = 0; + void create_td(Slice name) { + if (ready_to_stop_) { + return; + } + class TdCallbackImpl : public TdCallback { public: - explicit TdCallbackImpl(CliClient *client) : client_(client) { + TdCallbackImpl(CliClient *client, uint64 generation) : client_(client), generation_(generation) { + LOG(WARNING) << "Creating new TD with generation " << generation; } void on_result(uint64 id, tl_object_ptr result) override { - client_->on_result(id, std::move(result)); + client_->on_result(generation_, id, std::move(result)); } void on_error(uint64 id, tl_object_ptr error) override { - client_->on_error(id, std::move(error)); + client_->on_error(generation_, id, std::move(error)); } ~TdCallbackImpl() override { - client_->on_closed(); + client_->on_closed(generation_); } private: CliClient *client_; + uint64 generation_; }; - return make_unique(this); + + td_ = create_actor(name, make_unique(this, ++generation_)); } void init_td() { close_flag_ = false; ready_to_stop_ = false; + generation_ = 0; + closed_td_ = 0; + + create_td("ClientActor1"); bool test_init = false; - if (test_init) { - td_ = create_actor("ClientActor1", make_td_callback()); - } - td_ = create_actor("ClientActor2", make_td_callback()); - ready_to_stop_ = false; + create_td("ClientActor2"); - if (test_init) { for (int i = 0; i < 4; i++) { send_closure_later(td_, &ClientActor::request, std::numeric_limits::max(), td_api::make_object(0.001 + 1000 * (i / 2))); @@ -722,19 +776,6 @@ class CliClient final : public Actor { bad_parameters->api_hash_ = api_hash_; send_request(td_api::make_object(std::move(bad_parameters))); } - - auto parameters = td_api::make_object(); - parameters->use_test_dc_ = use_test_dc_; - parameters->use_message_database_ = true; - parameters->use_secret_chats_ = true; - parameters->api_id_ = api_id_; - parameters->api_hash_ = api_hash_; - parameters->system_language_code_ = "en"; - parameters->device_model_ = "Desktop"; - parameters->system_version_ = "Unknown"; - parameters->application_version_ = "tg_cli"; - send_request(td_api::make_object(std::move(parameters))); - send_request(td_api::make_object()); } void init() { diff --git a/td/telegram/net/TempAuthKeyWatchdog.h b/td/telegram/net/TempAuthKeyWatchdog.h index 84be95b9..48c7c811 100644 --- a/td/telegram/net/TempAuthKeyWatchdog.h +++ b/td/telegram/net/TempAuthKeyWatchdog.h @@ -117,6 +117,9 @@ class TempAuthKeyWatchdog : public NetQueryCallback { void on_result(NetQueryPtr query) final { run_sync_ = false; if (query->is_error()) { + if (G()->close_flag()) { + return; + } LOG(ERROR) << "auth_dropTempAuthKeys failed " << query->error(); need_sync_ = true; } else {