From 093ba9c9d4f63a8cc8445c06f44dc605837ee396 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 25 Aug 2020 18:58:37 +0300 Subject: [PATCH] Change offset and limit simultaneously. GitOrigin-RevId: eb07ec70a9048c4b5a5a5e0f1e17431b8528e6de --- td/telegram/files/FileLoadManager.cpp | 20 ++++---------------- td/telegram/files/FileLoadManager.h | 3 +-- td/telegram/files/FileLoader.cpp | 15 +++++---------- td/telegram/files/FileLoader.h | 3 +-- td/telegram/files/FileLoaderActor.h | 4 +--- td/telegram/files/FileManager.cpp | 24 +++++++++++++++++------- td/telegram/files/PartsManager.cpp | 6 +++--- td/telegram/files/PartsManager.h | 2 +- 8 files changed, 33 insertions(+), 44 deletions(-) diff --git a/td/telegram/files/FileLoadManager.cpp b/td/telegram/files/FileLoadManager.cpp index 59ed04db2..6cf56069e 100644 --- a/td/telegram/files/FileLoadManager.cpp +++ b/td/telegram/files/FileLoadManager.cpp @@ -158,7 +158,8 @@ void FileLoadManager::update_local_file_location(QueryId id, const LocalFileLoca } send_closure(node->loader_, &FileLoaderActor::update_local_file_location, local); } -void FileLoadManager::update_download_offset(QueryId id, int64 offset) { + +void FileLoadManager::update_downloaded_part(QueryId id, int64 offset, int64 limit) { if (stop_flag_) { return; } @@ -170,22 +171,9 @@ void FileLoadManager::update_download_offset(QueryId id, int64 offset) { if (node == nullptr) { return; } - send_closure(node->loader_, &FileLoaderActor::update_download_offset, offset); -} -void FileLoadManager::update_download_limit(QueryId id, int64 limit) { - if (stop_flag_) { - return; - } - auto it = query_id_to_node_id_.find(id); - if (it == query_id_to_node_id_.end()) { - return; - } - auto node = nodes_container_.get(it->second); - if (node == nullptr) { - return; - } - send_closure(node->loader_, &FileLoaderActor::update_download_limit, limit); + send_closure(node->loader_, &FileLoaderActor::update_downloaded_part, offset, limit); } + void FileLoadManager::hangup() { nodes_container_.for_each([](auto id, auto &node) { node.loader_.reset(); }); stop_flag_ = true; diff --git a/td/telegram/files/FileLoadManager.h b/td/telegram/files/FileLoadManager.h index 0aa2b58fa..8519b14ab 100644 --- a/td/telegram/files/FileLoadManager.h +++ b/td/telegram/files/FileLoadManager.h @@ -58,8 +58,7 @@ class FileLoadManager final : public Actor { void from_bytes(QueryId id, FileType type, BufferSlice bytes, string name); void cancel(QueryId id); void update_local_file_location(QueryId id, const LocalFileLocation &local); - void update_download_offset(QueryId id, int64 offset); - void update_download_limit(QueryId id, int64 limit); + void update_downloaded_part(QueryId id, int64 offset, int64 limit); void get_content(const FullLocalFileLocation &local_location, Promise promise); private: diff --git a/td/telegram/files/FileLoader.cpp b/td/telegram/files/FileLoader.cpp index a5dab7658..e656badf0 100644 --- a/td/telegram/files/FileLoader.cpp +++ b/td/telegram/files/FileLoader.cpp @@ -65,9 +65,9 @@ void FileLoader::update_local_file_location(const LocalFileLocation &local) { loop(); } -void FileLoader::update_download_offset(int64 offset) { +void FileLoader::update_downloaded_part(int64 offset, int64 limit) { if (parts_manager_.get_streaming_offset() != offset) { - auto begin_part_id = parts_manager_.set_streaming_offset(offset); + auto begin_part_id = parts_manager_.set_streaming_offset(offset, limit); auto end_part_id = begin_part_id + static_cast(part_map_.size()) * 2; VLOG(files) << "Protect parts " << begin_part_id << " ... " << end_part_id; for (auto &it : part_map_) { @@ -76,17 +76,13 @@ void FileLoader::update_download_offset(int64 offset) { it.second.second.reset(); // cancel_query(it.second.second); } } + } else { + parts_manager_.set_streaming_limit(limit); } update_estimated_limit(); loop(); } -void FileLoader::update_download_limit(int64 limit) { - parts_manager_.set_streaming_limit(limit); - update_estimated_limit(); - loop(); -} - void FileLoader::start_up() { auto r_file_info = init(); if (r_file_info.is_error()) { @@ -124,8 +120,7 @@ void FileLoader::start_up() { if (file_info.only_check) { parts_manager_.set_checked_prefix_size(0); } - parts_manager_.set_streaming_offset(file_info.offset); - parts_manager_.set_streaming_limit(file_info.limit); + parts_manager_.set_streaming_offset(file_info.offset, file_info.limit); if (ordered_flag_) { ordered_parts_ = OrderedEventsProcessor>(parts_manager_.get_ready_prefix_count()); } diff --git a/td/telegram/files/FileLoader.h b/td/telegram/files/FileLoader.h index 82a1697b3..7ea60512b 100644 --- a/td/telegram/files/FileLoader.h +++ b/td/telegram/files/FileLoader.h @@ -39,8 +39,7 @@ class FileLoader : public FileLoaderActor { void update_resources(const ResourceState &other) override; void update_local_file_location(const LocalFileLocation &local) override; - void update_download_offset(int64 offset) override; - void update_download_limit(int64 limit) override; + void update_downloaded_part(int64 offset, int64 limit) override; protected: void set_ordered_flag(bool flag); diff --git a/td/telegram/files/FileLoaderActor.h b/td/telegram/files/FileLoaderActor.h index a38f5f192..3164e133b 100644 --- a/td/telegram/files/FileLoaderActor.h +++ b/td/telegram/files/FileLoaderActor.h @@ -26,9 +26,7 @@ class FileLoaderActor : public NetQueryCallback { // TODO: existence of these three functions is a dirty hack. Refactoring is highly appreciated virtual void update_local_file_location(const LocalFileLocation &local) { } - virtual void update_download_offset(int64 offset) { - } - virtual void update_download_limit(int64 limit) { + virtual void update_downloaded_part(int64 offset, int64 limit) { } }; diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index a9bfb2828..64c124936 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -2231,13 +2231,17 @@ void FileManager::run_download(FileNodePtr node) { LOG(INFO) << "Update download offset and limits of file " << node->main_file_id_; CHECK(node->download_id_ != 0); send_closure(file_load_manager_, &FileLoadManager::update_priority, node->download_id_, priority); - if (need_update_limit) { + if (need_update_limit || need_update_offset) { + auto download_offset = node->download_offset_; auto download_limit = node->download_limit_; - send_closure(file_load_manager_, &FileLoadManager::update_download_limit, node->download_id_, download_limit); - } - if (need_update_offset) { - auto download_offset = file_view.is_encrypted_any() ? 0 : node->download_offset_; - send_closure(file_load_manager_, &FileLoadManager::update_download_offset, node->download_id_, download_offset); + if (file_view.is_encrypted_any()) { + CHECK(download_offset <= MAX_FILE_SIZE); + CHECK(download_limit <= std::numeric_limits::max()); + download_limit += download_offset; + download_offset = 0; + } + send_closure(file_load_manager_, &FileLoadManager::update_downloaded_part, node->download_id_, download_offset, + download_limit); } return; } @@ -2297,8 +2301,14 @@ void FileManager::run_download(FileNodePtr node) { LOG(INFO) << "Run download of file " << file_id << " of size " << node->size_ << " from " << node->remote_.full.value() << " with suggested name " << node->suggested_name() << " and encyption key " << node->encryption_key_; - auto download_offset = file_view.is_encrypted_any() ? 0 : node->download_offset_; + auto download_offset = node->download_offset_; auto download_limit = node->download_limit_; + if (file_view.is_encrypted_any()) { + CHECK(download_offset <= MAX_FILE_SIZE); + CHECK(download_limit <= std::numeric_limits::max()); + download_limit += download_offset; + download_offset = 0; + } send_closure(file_load_manager_, &FileLoadManager::download, id, node->remote_.full.value(), node->local_, node->size_, node->suggested_name(), node->encryption_key_, node->can_search_locally_, download_offset, download_limit, priority); diff --git a/td/telegram/files/PartsManager.cpp b/td/telegram/files/PartsManager.cpp index dbea55d9e..fab3b5557 100644 --- a/td/telegram/files/PartsManager.cpp +++ b/td/telegram/files/PartsManager.cpp @@ -29,9 +29,9 @@ Status PartsManager::init_known_prefix(int64 known_prefix, size_t part_size, con return init_no_size(part_size, ready_parts); } -int32 PartsManager::set_streaming_offset(int64 offset) { +int32 PartsManager::set_streaming_offset(int64 offset, int64 limit) { auto finish = [&] { - set_streaming_limit(streaming_limit_); + set_streaming_limit(limit); update_first_not_ready_part(); return first_streaming_not_ready_part_; }; @@ -502,7 +502,7 @@ Status PartsManager::init_common(const std::vector &ready_parts) { void PartsManager::set_need_check() { need_check_ = true; - set_streaming_offset(0); + set_streaming_offset(0, 0); } void PartsManager::set_checked_prefix_size(int64 size) { diff --git a/td/telegram/files/PartsManager.h b/td/telegram/files/PartsManager.h index 50c62607d..2cc99912c 100644 --- a/td/telegram/files/PartsManager.h +++ b/td/telegram/files/PartsManager.h @@ -35,7 +35,7 @@ class PartsManager { Status set_known_prefix(size_t size, bool is_ready); void set_need_check(); void set_checked_prefix_size(int64 size); - int32 set_streaming_offset(int64 offset); + int32 set_streaming_offset(int64 offset, int64 limit); void set_streaming_limit(int64 limit); int64 get_checked_prefix_size() const;