diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index 9956c8e69..9d7a23a56 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -727,7 +727,7 @@ void AuthManager::log_out(uint64 query_id) { // TODO: send auth.cancelCode if state_ == State::WaitCode LOG(WARNING) << "Destroying auth keys by user request"; destroy_auth_keys(); - on_query_ok(); + on_current_query_ok(); } else { LOG(WARNING) << "Logging out by user request"; G()->td_db()->get_binlog_pmc()->set("auth", "logout"); @@ -812,8 +812,10 @@ void AuthManager::on_query_error(uint64 query_id, Status status) { send_closure(G()->td(), &Td::send_error, query_id, std::move(status)); } -void AuthManager::on_query_ok() { - CHECK(query_id_ != 0); +void AuthManager::on_current_query_ok() { + if (query_id_ == 0) { + return; + } auto id = query_id_; net_query_id_ = 0; net_query_type_ = NetQueryType::None; @@ -873,7 +875,7 @@ void AuthManager::on_sent_code(telegram_api::object_ptrtoken_.as_slice().str(); set_login_token_expires_at(Time::now() + td::max(token->expires_ - G()->server_time(), 1.0)); update_state(State::WaitQrCodeConfirmation, true); - if (query_id_ != 0) { - on_query_ok(); - } + on_current_query_ok(); break; } case telegram_api::auth_loginTokenMigrateTo::ID: { @@ -985,9 +985,7 @@ void AuthManager::on_get_login_token(tl_object_ptrdc_id_; return; } - if (query_id_ != 0) { - on_query_ok(); - } + on_current_query_ok(); imported_dc_id_ = token->dc_id_; start_net_query(NetQueryType::ImportQrCode, G()->net_query_creator().create_unauth( @@ -1082,9 +1080,7 @@ void AuthManager::on_get_password_result(NetQueryPtr &&net_query) { G()->net_query_creator().create_unauth(telegram_api::auth_checkPassword(std::move(hash)))); } else { update_state(State::WaitPassword); - if (query_id_ != 0) { - on_query_ok(); - } + on_current_query_ok(); } } @@ -1097,7 +1093,7 @@ void AuthManager::on_request_password_recovery_result(NetQueryPtr &&net_query) { CHECK(email_address_pattern->get_id() == telegram_api::auth_passwordRecovery::ID); wait_password_state_.email_address_pattern_ = std::move(email_address_pattern->email_pattern_); update_state(State::WaitPassword, true); - on_query_ok(); + on_current_query_ok(); } void AuthManager::on_check_password_recovery_code_result(NetQueryPtr &&net_query) { @@ -1108,7 +1104,7 @@ void AuthManager::on_check_password_recovery_code_result(NetQueryPtr &&net_query if (!r_success.ok()) { return on_current_query_error(Status::Error(400, "Invalid recovery code")); } - on_query_ok(); + on_current_query_ok(); } void AuthManager::on_request_firebase_sms_result(NetQueryPtr &&net_query) { @@ -1116,7 +1112,7 @@ void AuthManager::on_request_firebase_sms_result(NetQueryPtr &&net_query) { if (r_bool.is_error()) { return on_current_query_error(r_bool.move_as_error()); } - on_query_ok(); + on_current_query_ok(); } void AuthManager::on_authentication_result(NetQueryPtr &&net_query, bool is_from_current_query) { @@ -1143,9 +1139,7 @@ void AuthManager::on_log_out_result(NetQueryPtr &&net_query) { } // state_ will stay LoggingOut, so no queries will work. destroy_auth_keys(); - if (query_id_ != 0) { - on_query_ok(); - } + on_current_query_ok(); } void AuthManager::on_authorization_lost(string source) { if (state_ == State::LoggingOut && net_query_type_ == NetQueryType::LogOut) { @@ -1194,28 +1188,20 @@ void AuthManager::on_delete_account_result(NetQueryPtr &&net_query) { } destroy_auth_keys(); - if (query_id_ != 0) { - on_query_ok(); - } + on_current_query_ok(); } void AuthManager::on_get_authorization(tl_object_ptr auth_ptr) { if (state_ == State::Ok) { LOG(WARNING) << "Ignore duplicate auth.Authorization"; - if (query_id_ != 0) { - on_query_ok(); - } - return; + return on_current_query_ok(); } CHECK(auth_ptr != nullptr); if (auth_ptr->get_id() == telegram_api::auth_authorizationSignUpRequired::ID) { auto sign_up_required = telegram_api::move_object_as(auth_ptr); terms_of_service_ = TermsOfService(std::move(sign_up_required->terms_of_service_)); update_state(State::WaitRegistration); - if (query_id_ != 0) { - on_query_ok(); - } - return; + return on_current_query_ok(); } auto auth = telegram_api::move_object_as(auth_ptr); @@ -1277,9 +1263,7 @@ void AuthManager::on_get_authorization(tl_object_ptrset_is_bot_online(true); } send_closure(G()->config_manager(), &ConfigManager::request_config, false); - if (query_id_ != 0) { - on_query_ok(); - } + on_current_query_ok(); } void AuthManager::on_result(NetQueryPtr net_query) { diff --git a/td/telegram/AuthManager.h b/td/telegram/AuthManager.h index 61f8dd638..d525cc4a1 100644 --- a/td/telegram/AuthManager.h +++ b/td/telegram/AuthManager.h @@ -182,7 +182,7 @@ class AuthManager final : public NetActor { void on_new_query(uint64 query_id); void on_current_query_error(Status status); - void on_query_ok(); + void on_current_query_ok(); void start_net_query(NetQueryType net_query_type, NetQueryPtr net_query); static void on_update_login_token_static(void *td); diff --git a/td/telegram/PhoneNumberManager.cpp b/td/telegram/PhoneNumberManager.cpp index bc960fd26..112eb505c 100644 --- a/td/telegram/PhoneNumberManager.cpp +++ b/td/telegram/PhoneNumberManager.cpp @@ -16,7 +16,6 @@ #include "td/telegram/telegram_api.h" #include "td/utils/logging.h" -#include "td/utils/ScopeGuard.h" namespace td { @@ -124,7 +123,7 @@ void PhoneNumberManager::check_code(uint64 query_id, string code) { void PhoneNumberManager::on_new_query(uint64 query_id) { if (query_id_ != 0) { - on_query_error(Status::Error(400, "Another query has started")); + on_current_query_error(Status::Error(400, "Another query has started")); } net_query_id_ = 0; net_query_type_ = NetQueryType::None; @@ -132,8 +131,10 @@ void PhoneNumberManager::on_new_query(uint64 query_id) { // TODO: cancel older net_query } -void PhoneNumberManager::on_query_error(Status status) { - CHECK(query_id_ != 0); +void PhoneNumberManager::on_current_query_error(Status status) { + if (query_id_ == 0) { + return; + } auto id = query_id_; query_id_ = 0; net_query_id_ = 0; @@ -145,8 +146,10 @@ void PhoneNumberManager::on_query_error(uint64 id, Status status) { send_closure(G()->td(), &Td::send_error, id, std::move(status)); } -void PhoneNumberManager::on_query_ok() { - CHECK(query_id_ != 0); +void PhoneNumberManager::on_current_query_ok() { + if (query_id_ == 0) { + return; + } auto id = query_id_; net_query_id_ = 0; net_query_type_ = NetQueryType::None; @@ -163,30 +166,30 @@ void PhoneNumberManager::start_net_query(NetQueryType net_query_type, NetQueryPt void PhoneNumberManager::process_check_code_result(Result> &&result) { if (result.is_error()) { - return on_query_error(result.move_as_error()); + return on_current_query_error(result.move_as_error()); } send_closure(G()->contacts_manager(), &ContactsManager::on_get_user, result.move_as_ok(), "process_check_code_result"); state_ = State::Ok; - on_query_ok(); + on_current_query_ok(); } void PhoneNumberManager::process_check_code_result(Result &&result) { if (result.is_error()) { - return on_query_error(result.move_as_error()); + return on_current_query_error(result.move_as_error()); } state_ = State::Ok; - on_query_ok(); + on_current_query_ok(); } void PhoneNumberManager::on_check_code_result(NetQueryPtr &&net_query) { switch (type_) { case Type::ChangePhone: - return process_check_code_result(fetch_result(net_query->ok())); + return process_check_code_result(fetch_result(std::move(net_query))); case Type::VerifyPhone: - return process_check_code_result(fetch_result(net_query->ok())); + return process_check_code_result(fetch_result(std::move(net_query))); case Type::ConfirmPhone: - return process_check_code_result(fetch_result(net_query->ok())); + return process_check_code_result(fetch_result(std::move(net_query))); default: UNREACHABLE(); } @@ -196,24 +199,24 @@ void PhoneNumberManager::on_send_code_result(NetQueryPtr &&net_query) { auto r_sent_code = [&] { switch (type_) { case Type::ChangePhone: - return fetch_result(net_query->ok()); + return fetch_result(std::move(net_query)); case Type::VerifyPhone: - return fetch_result(net_query->ok()); + return fetch_result(std::move(net_query)); case Type::ConfirmPhone: - return fetch_result(net_query->ok()); + return fetch_result(std::move(net_query)); default: UNREACHABLE(); - return fetch_result(net_query->ok()); + return fetch_result(std::move(net_query)); } }(); if (r_sent_code.is_error()) { - return on_query_error(r_sent_code.move_as_error()); + return on_current_query_error(r_sent_code.move_as_error()); } auto sent_code_ptr = r_sent_code.move_as_ok(); auto sent_code_id = sent_code_ptr->get_id(); if (sent_code_id != telegram_api::auth_sentCode::ID) { CHECK(sent_code_id == telegram_api::auth_sentCodeSuccess::ID); - return on_query_error(Status::Error(500, "Receive invalid response")); + return on_current_query_error(Status::Error(500, "Receive invalid response")); } auto sent_code = telegram_api::move_object_as(sent_code_ptr); @@ -222,7 +225,7 @@ void PhoneNumberManager::on_send_code_result(NetQueryPtr &&net_query) { switch (sent_code->type_->get_id()) { case telegram_api::auth_sentCodeTypeSetUpEmailRequired::ID: case telegram_api::auth_sentCodeTypeEmailCode::ID: - return on_query_error(Status::Error(500, "Receive incorrect response")); + return on_current_query_error(Status::Error(500, "Receive incorrect response")); default: break; } @@ -230,28 +233,19 @@ void PhoneNumberManager::on_send_code_result(NetQueryPtr &&net_query) { send_code_helper_.on_sent_code(std::move(sent_code)); state_ = State::WaitCode; - on_query_ok(); + on_current_query_ok(); } void PhoneNumberManager::on_result(NetQueryPtr net_query) { - SCOPE_EXIT { - net_query->clear(); - }; NetQueryType type = NetQueryType::None; if (net_query->id() == net_query_id_) { net_query_id_ = 0; type = net_query_type_; net_query_type_ = NetQueryType::None; - if (net_query->is_error()) { - if (query_id_ != 0) { - on_query_error(net_query->move_as_error()); - } - return; - } } switch (type) { case NetQueryType::None: - net_query->ignore(); + net_query->clear(); break; case NetQueryType::SendCode: on_send_code_result(std::move(net_query)); diff --git a/td/telegram/PhoneNumberManager.h b/td/telegram/PhoneNumberManager.h index 3b429fd46..3257496e6 100644 --- a/td/telegram/PhoneNumberManager.h +++ b/td/telegram/PhoneNumberManager.h @@ -48,9 +48,9 @@ class PhoneNumberManager final : public NetActor { void on_new_query(uint64 query_id); - void on_query_ok(); + void on_current_query_ok(); - void on_query_error(Status status); + void on_current_query_error(Status status); static void on_query_error(uint64 id, Status status); diff --git a/td/telegram/files/FileDownloader.cpp b/td/telegram/files/FileDownloader.cpp index 3b2af8e87..ef5ce3d2c 100644 --- a/td/telegram/files/FileDownloader.cpp +++ b/td/telegram/files/FileDownloader.cpp @@ -166,7 +166,7 @@ Result FileDownloader::should_restart_part(Part part, NetQueryPtr &net_que switch (narrow_cast(UniqueId::extract_key(net_query->id()))) { case QueryType::Default: { if (net_query->ok_tl_constructor() == telegram_api::upload_fileCdnRedirect::ID) { - TRY_RESULT(file_base, fetch_result(net_query->ok())); + TRY_RESULT(file_base, fetch_result(std::move(net_query))); CHECK(file_base->get_id() == telegram_api::upload_fileCdnRedirect::ID); auto file = move_tl_object_as(file_base); LOG(DEBUG) << "Downloading of part " << part.id << " was redirected to " << oneline(to_string(file)); @@ -193,14 +193,14 @@ Result FileDownloader::should_restart_part(Part part, NetQueryPtr &net_que return false; } case QueryType::ReuploadCDN: { - TRY_RESULT(file_hashes, fetch_result(net_query->ok())); + TRY_RESULT(file_hashes, fetch_result(std::move(net_query))); add_hash_info(file_hashes); LOG(DEBUG) << "Part " << part.id << " was reuplaoded to CDN"; return true; } case QueryType::CDN: { if (net_query->ok_tl_constructor() == telegram_api::upload_cdnFileReuploadNeeded::ID) { - TRY_RESULT(file_base, fetch_result(net_query->ok())); + TRY_RESULT(file_base, fetch_result(std::move(net_query))); CHECK(file_base->get_id() == telegram_api::upload_cdnFileReuploadNeeded::ID); auto file = move_tl_object_as(file_base); LOG(DEBUG) << "Part " << part.id << " must be reuplaoded to " << oneline(to_string(file)); @@ -306,10 +306,10 @@ Result FileDownloader::process_part(Part part, NetQueryPtr net_query) { switch (query_type) { case QueryType::Default: { if (remote_.is_web()) { - TRY_RESULT(file, fetch_result(net_query->ok())); + TRY_RESULT(file, fetch_result(std::move(net_query))); bytes = std::move(file->bytes_); } else { - TRY_RESULT(file_base, fetch_result(net_query->ok())); + TRY_RESULT(file_base, fetch_result(std::move(net_query))); CHECK(file_base->get_id() == telegram_api::upload_file::ID); auto file = move_tl_object_as(file_base); LOG(DEBUG) << "Receive part " << part.id << ": " << to_string(file); @@ -318,7 +318,7 @@ Result FileDownloader::process_part(Part part, NetQueryPtr net_query) { break; } case QueryType::CDN: { - TRY_RESULT(file_base, fetch_result(net_query->ok())); + TRY_RESULT(file_base, fetch_result(std::move(net_query))); CHECK(file_base->get_id() == telegram_api::upload_cdnFile::ID); auto file = move_tl_object_as(file_base); LOG(DEBUG) << "Receive part " << part.id << " from CDN: " << to_string(file); diff --git a/td/telegram/files/FileHashUploader.cpp b/td/telegram/files/FileHashUploader.cpp index 5357bfaf3..949040612 100644 --- a/td/telegram/files/FileHashUploader.cpp +++ b/td/telegram/files/FileHashUploader.cpp @@ -123,10 +123,7 @@ void FileHashUploader::on_result(NetQueryPtr net_query) { } Status FileHashUploader::on_result_impl(NetQueryPtr net_query) { - if (net_query->is_error()) { - return net_query->move_as_error(); - } - TRY_RESULT(res, fetch_result(net_query->ok())); + TRY_RESULT(res, fetch_result(std::move(net_query))); switch (res->get_id()) { case telegram_api::documentEmpty::ID: diff --git a/td/telegram/files/FileUploader.cpp b/td/telegram/files/FileUploader.cpp index 0e78a434f..918286c8a 100644 --- a/td/telegram/files/FileUploader.cpp +++ b/td/telegram/files/FileUploader.cpp @@ -295,14 +295,11 @@ Result> FileUploader::start_part(Part part, int32 p } Result FileUploader::process_part(Part part, NetQueryPtr net_query) { - if (net_query->is_error()) { - return net_query->move_as_error(); - } Result result = [&] { if (big_flag_) { - return fetch_result(net_query->ok()); + return fetch_result(std::move(net_query)); } else { - return fetch_result(net_query->ok()); + return fetch_result(std::move(net_query)); } }(); if (result.is_error()) { diff --git a/td/telegram/net/DcAuthManager.cpp b/td/telegram/net/DcAuthManager.cpp index a3d785fef..43735a861 100644 --- a/td/telegram/net/DcAuthManager.cpp +++ b/td/telegram/net/DcAuthManager.cpp @@ -109,14 +109,9 @@ void DcAuthManager::on_result(NetQueryPtr net_query) { dc.wait_id = std::numeric_limits::max(); switch (dc.state) { case DcInfo::State::Import: { - if (net_query->is_error()) { - LOG(WARNING) << "DC auth_exportAuthorization error: " << net_query->error(); - dc.state = DcInfo::State::Export; - break; - } - auto r_result_auth_exported = fetch_result(net_query->ok()); + auto r_result_auth_exported = fetch_result(std::move(net_query)); if (r_result_auth_exported.is_error()) { - LOG(WARNING) << "Failed to parse result to auth_exportAuthorization: " << r_result_auth_exported.error(); + LOG(WARNING) << "Receive error for auth.exportAuthorization: " << r_result_auth_exported.error(); dc.state = DcInfo::State::Export; break; } @@ -126,14 +121,9 @@ void DcAuthManager::on_result(NetQueryPtr net_query) { break; } case DcInfo::State::BeforeOk: { - if (net_query->is_error()) { - LOG(WARNING) << "DC authImport error: " << net_query->error(); - dc.state = DcInfo::State::Export; - break; - } - auto result_auth = fetch_result(net_query->ok()); + auto result_auth = fetch_result(std::move(net_query)); if (result_auth.is_error()) { - LOG(WARNING) << "Failed to parse result to auth_importAuthorization: " << result_auth.error(); + LOG(WARNING) << "Receive error for auth.importAuthorization: " << result_auth.error(); dc.state = DcInfo::State::Export; break; } @@ -143,7 +133,6 @@ void DcAuthManager::on_result(NetQueryPtr net_query) { default: UNREACHABLE(); } - net_query->clear(); loop(); } diff --git a/td/telegram/net/NetQuery.h b/td/telegram/net/NetQuery.h index 56aa86d6b..355f2b8c9 100644 --- a/td/telegram/net/NetQuery.h +++ b/td/telegram/net/NetQuery.h @@ -172,10 +172,6 @@ class NetQuery final : public TsListNode { return tl_magic(answer_); } - void ignore() const { - status_.ignore(); - } - uint64 session_id() const { return session_id_.load(std::memory_order_relaxed); }