2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2020-08-26 23:52:01 +02:00
|
|
|
#include "td/telegram/DelayDispatcher.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/files/FileLoaderActor.h"
|
|
|
|
#include "td/telegram/files/FileLocation.h"
|
|
|
|
#include "td/telegram/files/PartsManager.h"
|
|
|
|
#include "td/telegram/files/ResourceManager.h"
|
|
|
|
#include "td/telegram/files/ResourceState.h"
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/OrderedEventsProcessor.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class FileLoader : public FileLoaderActor {
|
|
|
|
public:
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
virtual ~Callback() = default;
|
|
|
|
};
|
2021-07-03 22:51:36 +02:00
|
|
|
void set_resource_manager(ActorShared<ResourceManager> resource_manager) final;
|
|
|
|
void update_priority(int8 priority) final;
|
|
|
|
void update_resources(const ResourceState &other) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void update_local_file_location(const LocalFileLocation &local) final;
|
2022-05-20 15:11:31 +02:00
|
|
|
void update_downloaded_part(int64 offset, int64 limit, int64 max_resource_limit) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void set_ordered_flag(bool flag);
|
|
|
|
size_t get_part_size() const;
|
|
|
|
|
|
|
|
struct PrefixInfo {
|
|
|
|
int64 size = -1;
|
|
|
|
bool is_ready = false;
|
|
|
|
};
|
|
|
|
struct FileInfo {
|
2021-11-11 15:39:09 +01:00
|
|
|
int64 size{0};
|
2018-11-11 12:38:04 +01:00
|
|
|
int64 expected_size{0};
|
2021-11-11 15:39:09 +01:00
|
|
|
bool is_size_final{false};
|
|
|
|
int32 part_size{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<int> ready_parts;
|
2021-11-11 15:39:09 +01:00
|
|
|
bool use_part_count_limit{true};
|
|
|
|
bool only_check{false};
|
|
|
|
bool need_delay{false};
|
2018-11-11 12:38:04 +01:00
|
|
|
int64 offset{0};
|
2019-02-18 20:08:05 +01:00
|
|
|
int64 limit{0};
|
2019-07-31 17:04:38 +02:00
|
|
|
bool is_upload{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
virtual Result<FileInfo> init() TD_WARN_UNUSED_RESULT = 0;
|
|
|
|
virtual Status on_ok(int64 size) TD_WARN_UNUSED_RESULT = 0;
|
|
|
|
virtual void on_error(Status status) = 0;
|
|
|
|
virtual Status before_start_parts() {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2020-01-08 18:15:49 +01:00
|
|
|
virtual Result<std::pair<NetQueryPtr, bool>> start_part(Part part, int part_count,
|
|
|
|
int64 streaming_offset) TD_WARN_UNUSED_RESULT = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual void after_start_parts() {
|
|
|
|
}
|
|
|
|
virtual Result<size_t> process_part(Part part, NetQueryPtr net_query) TD_WARN_UNUSED_RESULT = 0;
|
2018-12-26 22:42:26 +01:00
|
|
|
struct Progress {
|
|
|
|
int32 part_count{0};
|
|
|
|
int32 part_size{0};
|
|
|
|
int32 ready_part_count{0};
|
|
|
|
string ready_bitmask;
|
|
|
|
bool is_ready{false};
|
|
|
|
int64 ready_size{0};
|
|
|
|
int64 size{0};
|
|
|
|
};
|
|
|
|
virtual void on_progress(Progress progress) = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
virtual Callback *get_callback() = 0;
|
2018-12-26 17:11:15 +01:00
|
|
|
virtual Result<PrefixInfo> on_update_local_location(const LocalFileLocation &location,
|
|
|
|
int64 file_size) TD_WARN_UNUSED_RESULT {
|
2019-08-26 03:53:22 +02:00
|
|
|
return Status::Error("Unsupported");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
virtual Result<bool> should_restart_part(Part part, NetQueryPtr &net_query) TD_WARN_UNUSED_RESULT {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Status process_check_query(NetQueryPtr net_query) {
|
2019-08-26 03:53:22 +02:00
|
|
|
return Status::Error("Unsupported");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
struct CheckInfo {
|
|
|
|
bool need_check{false};
|
2018-02-26 11:05:14 +01:00
|
|
|
bool changed{false};
|
2018-12-31 20:04:05 +01:00
|
|
|
int64 checked_prefix_size{0};
|
|
|
|
std::vector<NetQueryPtr> queries;
|
|
|
|
};
|
|
|
|
virtual Result<CheckInfo> check_loop(int64 checked_prefix_size, int64 ready_prefix_size, bool is_ready) {
|
|
|
|
return CheckInfo{};
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void keep_fd_flag(bool keep_fd) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-19 15:08:30 +02:00
|
|
|
static constexpr uint8 COMMON_QUERY_KEY = 2;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool stop_flag_ = false;
|
|
|
|
ActorShared<ResourceManager> resource_manager_;
|
|
|
|
ResourceState resource_state_;
|
|
|
|
PartsManager parts_manager_;
|
|
|
|
uint64 blocking_id_{0};
|
|
|
|
std::map<uint64, std::pair<Part, ActorShared<>>> part_map_;
|
|
|
|
bool ordered_flag_ = false;
|
|
|
|
OrderedEventsProcessor<std::pair<Part, NetQueryPtr>> ordered_parts_;
|
2018-03-16 10:31:23 +01:00
|
|
|
ActorOwn<DelayDispatcher> delay_dispatcher_;
|
2018-03-17 18:06:16 +01:00
|
|
|
double next_delay_ = 0;
|
2018-03-16 10:31:23 +01:00
|
|
|
|
|
|
|
uint32 debug_total_parts_ = 0;
|
|
|
|
uint32 debug_bad_part_order_ = 0;
|
2018-03-17 18:06:16 +01:00
|
|
|
std::vector<int32> debug_bad_parts_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
|
|
|
void loop() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status do_loop();
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final;
|
|
|
|
void hangup_shared() final;
|
|
|
|
void tear_down() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void update_estimated_limit();
|
2018-12-26 22:42:26 +01:00
|
|
|
void on_progress_impl();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(NetQueryPtr query) final;
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_part_query(Part part, NetQueryPtr query);
|
|
|
|
void on_common_query(NetQueryPtr query);
|
|
|
|
Status try_on_part_query(Part part, NetQueryPtr query);
|
|
|
|
};
|
2018-12-26 17:11:15 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|