Always call fetch_result on the whole NetQueryPtr.

This commit is contained in:
levlam 2023-09-02 22:39:26 +03:00
parent ce2e7192d7
commit 049d84beed
9 changed files with 59 additions and 102 deletions

View File

@ -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_ptr<telegram_api::auth_SentC
send_code_helper_.on_sent_code(std::move(sent_code));
update_state(State::WaitCode, true);
}
on_query_ok();
on_current_query_ok();
}
void AuthManager::on_send_code_result(NetQueryPtr &&net_query) {
@ -899,7 +901,7 @@ void AuthManager::on_send_email_code_result(NetQueryPtr &&net_query) {
}
update_state(State::WaitEmailCode, true);
on_query_ok();
on_current_query_ok();
}
void AuthManager::on_verify_email_address_result(NetQueryPtr &&net_query) {
@ -974,9 +976,7 @@ void AuthManager::on_get_login_token(tl_object_ptr<telegram_api::auth_LoginToken
login_token_ = token->token_.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_ptr<telegram_api::auth_LoginToken
LOG(ERROR) << "Receive wrong DC " << token->dc_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<telegram_api::auth_Authorization> 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<telegram_api::auth_authorizationSignUpRequired>(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<telegram_api::auth_authorization>(auth_ptr);
@ -1277,9 +1263,7 @@ void AuthManager::on_get_authorization(tl_object_ptr<telegram_api::auth_Authoriz
td_->set_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) {

View File

@ -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);

View File

@ -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<tl_object_ptr<telegram_api::User>> &&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<bool> &&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<telegram_api::account_changePhone>(net_query->ok()));
return process_check_code_result(fetch_result<telegram_api::account_changePhone>(std::move(net_query)));
case Type::VerifyPhone:
return process_check_code_result(fetch_result<telegram_api::account_verifyPhone>(net_query->ok()));
return process_check_code_result(fetch_result<telegram_api::account_verifyPhone>(std::move(net_query)));
case Type::ConfirmPhone:
return process_check_code_result(fetch_result<telegram_api::account_confirmPhone>(net_query->ok()));
return process_check_code_result(fetch_result<telegram_api::account_confirmPhone>(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<telegram_api::account_sendChangePhoneCode>(net_query->ok());
return fetch_result<telegram_api::account_sendChangePhoneCode>(std::move(net_query));
case Type::VerifyPhone:
return fetch_result<telegram_api::account_sendVerifyPhoneCode>(net_query->ok());
return fetch_result<telegram_api::account_sendVerifyPhoneCode>(std::move(net_query));
case Type::ConfirmPhone:
return fetch_result<telegram_api::account_sendConfirmPhoneCode>(net_query->ok());
return fetch_result<telegram_api::account_sendConfirmPhoneCode>(std::move(net_query));
default:
UNREACHABLE();
return fetch_result<telegram_api::account_sendChangePhoneCode>(net_query->ok());
return fetch_result<telegram_api::account_sendChangePhoneCode>(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<telegram_api::auth_sentCode>(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));

View File

@ -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);

View File

@ -166,7 +166,7 @@ Result<bool> FileDownloader::should_restart_part(Part part, NetQueryPtr &net_que
switch (narrow_cast<QueryType>(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<telegram_api::upload_getFile>(net_query->ok()));
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getFile>(std::move(net_query)));
CHECK(file_base->get_id() == telegram_api::upload_fileCdnRedirect::ID);
auto file = move_tl_object_as<telegram_api::upload_fileCdnRedirect>(file_base);
LOG(DEBUG) << "Downloading of part " << part.id << " was redirected to " << oneline(to_string(file));
@ -193,14 +193,14 @@ Result<bool> FileDownloader::should_restart_part(Part part, NetQueryPtr &net_que
return false;
}
case QueryType::ReuploadCDN: {
TRY_RESULT(file_hashes, fetch_result<telegram_api::upload_reuploadCdnFile>(net_query->ok()));
TRY_RESULT(file_hashes, fetch_result<telegram_api::upload_reuploadCdnFile>(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<telegram_api::upload_getCdnFile>(net_query->ok()));
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getCdnFile>(std::move(net_query)));
CHECK(file_base->get_id() == telegram_api::upload_cdnFileReuploadNeeded::ID);
auto file = move_tl_object_as<telegram_api::upload_cdnFileReuploadNeeded>(file_base);
LOG(DEBUG) << "Part " << part.id << " must be reuplaoded to " << oneline(to_string(file));
@ -306,10 +306,10 @@ Result<size_t> FileDownloader::process_part(Part part, NetQueryPtr net_query) {
switch (query_type) {
case QueryType::Default: {
if (remote_.is_web()) {
TRY_RESULT(file, fetch_result<telegram_api::upload_getWebFile>(net_query->ok()));
TRY_RESULT(file, fetch_result<telegram_api::upload_getWebFile>(std::move(net_query)));
bytes = std::move(file->bytes_);
} else {
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getFile>(net_query->ok()));
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getFile>(std::move(net_query)));
CHECK(file_base->get_id() == telegram_api::upload_file::ID);
auto file = move_tl_object_as<telegram_api::upload_file>(file_base);
LOG(DEBUG) << "Receive part " << part.id << ": " << to_string(file);
@ -318,7 +318,7 @@ Result<size_t> FileDownloader::process_part(Part part, NetQueryPtr net_query) {
break;
}
case QueryType::CDN: {
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getCdnFile>(net_query->ok()));
TRY_RESULT(file_base, fetch_result<telegram_api::upload_getCdnFile>(std::move(net_query)));
CHECK(file_base->get_id() == telegram_api::upload_cdnFile::ID);
auto file = move_tl_object_as<telegram_api::upload_cdnFile>(file_base);
LOG(DEBUG) << "Receive part " << part.id << " from CDN: " << to_string(file);

View File

@ -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<telegram_api::messages_getDocumentByHash>(net_query->ok()));
TRY_RESULT(res, fetch_result<telegram_api::messages_getDocumentByHash>(std::move(net_query)));
switch (res->get_id()) {
case telegram_api::documentEmpty::ID:

View File

@ -295,14 +295,11 @@ Result<std::pair<NetQueryPtr, bool>> FileUploader::start_part(Part part, int32 p
}
Result<size_t> FileUploader::process_part(Part part, NetQueryPtr net_query) {
if (net_query->is_error()) {
return net_query->move_as_error();
}
Result<bool> result = [&] {
if (big_flag_) {
return fetch_result<telegram_api::upload_saveBigFilePart>(net_query->ok());
return fetch_result<telegram_api::upload_saveBigFilePart>(std::move(net_query));
} else {
return fetch_result<telegram_api::upload_saveFilePart>(net_query->ok());
return fetch_result<telegram_api::upload_saveFilePart>(std::move(net_query));
}
}();
if (result.is_error()) {

View File

@ -109,14 +109,9 @@ void DcAuthManager::on_result(NetQueryPtr net_query) {
dc.wait_id = std::numeric_limits<decltype(dc.wait_id)>::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<telegram_api::auth_exportAuthorization>(net_query->ok());
auto r_result_auth_exported = fetch_result<telegram_api::auth_exportAuthorization>(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<telegram_api::auth_importAuthorization>(net_query->ok());
auto result_auth = fetch_result<telegram_api::auth_importAuthorization>(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();
}

View File

@ -172,10 +172,6 @@ class NetQuery final : public TsListNode<NetQueryDebug> {
return tl_magic(answer_);
}
void ignore() const {
status_.ignore();
}
uint64 session_id() const {
return session_id_.load(std::memory_order_relaxed);
}