From 2baff56b7a6bac487a8c70be2336e101d3d40e78 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 13 Mar 2023 16:30:11 +0300 Subject: [PATCH] Don't store TdParameters in Td. --- td/telegram/Td.cpp | 51 ++++++++++++++++++++++---------------------- td/telegram/Td.h | 4 +--- td/telegram/TdDb.cpp | 3 +-- td/telegram/TdDb.h | 3 +-- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index e53d71c6b..15d29fbe8 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -2955,9 +2955,9 @@ void Td::run_request(uint64 id, tl_object_ptr function) { case td_api::setTdlibParameters::ID: { auto parameters = move_tl_object_as(function); auto database_encryption_key = as_db_key(std::move(parameters->database_encryption_key_)); - auto status = set_parameters(std::move(parameters)); - if (status.is_error()) { - return send_closure(actor_id(this), &Td::send_error, id, std::move(status)); + auto r_parameters = set_parameters(std::move(parameters)); + if (r_parameters.is_error()) { + return send_closure(actor_id(this), &Td::send_error, id, r_parameters.move_as_error()); } VLOG(td_init) << "Begin to open database"; @@ -2967,7 +2967,7 @@ void Td::run_request(uint64 id, tl_object_ptr function) { PromiseCreator::lambda([actor_id = actor_id(this)](Result r_opened_database) { send_closure(actor_id, &Td::init, std::move(r_opened_database)); }); - return TdDb::open(get_database_scheduler_id(), parameters_, std::move(database_encryption_key), + return TdDb::open(get_database_scheduler_id(), r_parameters.move_as_ok(), std::move(database_encryption_key), std::move(promise)); } default: @@ -3549,14 +3549,14 @@ void Td::init(Result r_opened_database) { } auto events = r_opened_database.move_as_ok(); - parameters_.database_directory = std::move(events.database_directory); - parameters_.files_directory = std::move(events.files_directory); - - LOG(INFO) << "Successfully inited database in " << tag("database_directory", parameters_.database_directory) - << " and " << tag("files_directory", parameters_.files_directory); + LOG(INFO) << "Successfully inited database in " << tag("database_directory", events.parameters.database_directory) + << " and " << tag("files_directory", events.parameters.files_directory); VLOG(td_init) << "Successfully inited database"; - G()->init(parameters_, actor_id(this), std::move(events.database)).ensure(); + auto api_id = events.parameters.api_id; + auto api_hash = events.parameters.api_hash; + + G()->init(std::move(events.parameters), actor_id(this), std::move(events.database)).ensure(); init_options_and_network(); @@ -3600,7 +3600,7 @@ void Td::init(Result r_opened_database) { }); VLOG(td_init) << "Create AuthManager"; - auth_manager_ = td::make_unique(parameters_.api_id, parameters_.api_hash, create_reference()); + auth_manager_ = td::make_unique(api_id, api_hash, create_reference()); auth_manager_actor_ = register_actor("AuthManager", auth_manager_.get()); G()->set_auth_manager(auth_manager_actor_.get()); @@ -4095,7 +4095,7 @@ Status Td::fix_parameters(TdParameters ¶meters) { return Status::OK(); } -Status Td::set_parameters(td_api::object_ptr parameters) { +Result Td::set_parameters(td_api::object_ptr parameters) { VLOG(td_init) << "Begin to set TDLib parameters"; if (!clean_input_string(parameters->api_hash_) || !clean_input_string(parameters->system_language_code_) || !clean_input_string(parameters->device_model_) || !clean_input_string(parameters->system_version_) || @@ -4104,19 +4104,20 @@ Status Td::set_parameters(td_api::object_ptr paramet return Status::Error(400, "Strings must be encoded in UTF-8"); } - parameters_.database_directory = parameters->database_directory_; - parameters_.files_directory = parameters->files_directory_; - parameters_.api_id = parameters->api_id_; - parameters_.api_hash = parameters->api_hash_; - parameters_.use_test_dc = parameters->use_test_dc_; - parameters_.use_file_db = parameters->use_file_database_; - parameters_.use_chat_info_db = parameters->use_chat_info_database_; - parameters_.use_message_db = parameters->use_message_database_; - parameters_.use_secret_chats = parameters->use_secret_chats_; - parameters_.enable_storage_optimizer = parameters->enable_storage_optimizer_; - parameters_.ignore_file_names = parameters->ignore_file_names_; + TdParameters result; + result.database_directory = parameters->database_directory_; + result.files_directory = parameters->files_directory_; + result.api_id = parameters->api_id_; + result.api_hash = parameters->api_hash_; + result.use_test_dc = parameters->use_test_dc_; + result.use_file_db = parameters->use_file_database_; + result.use_chat_info_db = parameters->use_chat_info_database_; + result.use_message_db = parameters->use_message_database_; + result.use_secret_chats = parameters->use_secret_chats_; + result.enable_storage_optimizer = parameters->enable_storage_optimizer_; + result.ignore_file_names = parameters->ignore_file_names_; - TRY_STATUS(fix_parameters(parameters_)); + TRY_STATUS(fix_parameters(result)); VLOG(td_init) << "Create MtprotoHeader::Options"; options_.api_id = parameters->api_id_; @@ -4149,7 +4150,7 @@ Status Td::set_parameters(td_api::object_ptr paramet options_.is_emulator = false; options_.proxy = Proxy(); - return Status::OK(); + return std::move(result); } void Td::on_request(uint64 id, const td_api::setTdlibParameters &request) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index d50b59188..d59fa43b6 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -290,8 +290,6 @@ class Td final : public Actor { MtprotoHeader::Options options_; - TdParameters parameters_; - ConnectionState connection_state_ = ConnectionState::Empty; std::unordered_multimap request_set_; @@ -1610,7 +1608,7 @@ class Td final : public Actor { static Status fix_parameters(TdParameters ¶meters) TD_WARN_UNUSED_RESULT; - Status set_parameters(td_api::object_ptr parameters) TD_WARN_UNUSED_RESULT; + Result set_parameters(td_api::object_ptr parameters) TD_WARN_UNUSED_RESULT; static td_api::object_ptr make_error(int32 code, CSlice error) { return td_api::make_object(code, error.str()); diff --git a/td/telegram/TdDb.cpp b/td/telegram/TdDb.cpp index 93b798f87..afe8d9aaf 100644 --- a/td/telegram/TdDb.cpp +++ b/td/telegram/TdDb.cpp @@ -444,8 +444,6 @@ void TdDb::open_impl(TdParameters parameters, DbKey key, Promise TRY_STATUS_PROMISE(promise, check_parameters(parameters)); OpenedDatabase result; - result.database_directory = parameters.database_directory; - result.files_directory = parameters.files_directory; // Init pmc Binlog *binlog_ptr = nullptr; @@ -538,6 +536,7 @@ void TdDb::open_impl(TdParameters parameters, DbKey key, Promise db->config_pmc_ = std::move(concurrent_config_pmc); db->binlog_ = std::move(concurrent_binlog); + result.parameters = std::move(parameters); result.database = std::move(db); promise.set_value(std::move(result)); diff --git a/td/telegram/TdDb.h b/td/telegram/TdDb.h index ee487c0a5..e1f4df5b8 100644 --- a/td/telegram/TdDb.h +++ b/td/telegram/TdDb.h @@ -51,8 +51,7 @@ class TdDb { ~TdDb(); struct OpenedDatabase { - string database_directory; - string files_directory; + TdParameters parameters; unique_ptr database;