refactor: remove unused native libtorrent C++ bridge

This commit is contained in:
Chubby Granny Chaser
2026-03-24 14:39:25 +00:00
parent aa771f6f34
commit eec4f5ec80
8 changed files with 0 additions and 1003 deletions
-115
View File
@@ -1,115 +0,0 @@
#include "bridge_state.h"
#include <algorithm>
#include <exception>
#include <limits>
#include <libtorrent/session_handle.hpp>
#include <libtorrent/settings_pack.hpp>
namespace hydra::libtorrent_bridge::detail {
namespace {
constexpr std::uint16_t kDefaultListenPortStart = 5881;
constexpr std::uint16_t kDefaultListenPortEnd = 5892;
std::string build_listen_interfaces(std::uint16_t listen_port) {
if (listen_port == 0) {
listen_port = kDefaultListenPortStart;
}
return std::string("0.0.0.0:") + std::to_string(listen_port) + ",[::]:" +
std::to_string(listen_port);
}
} // namespace
std::mutex g_mutex;
std::unique_ptr<lt::session> g_session;
std::unordered_map<std::string, DownloadState> g_downloads;
std::uint16_t g_listen_port_start = kDefaultListenPortStart;
std::uint16_t g_listen_port_end = kDefaultListenPortEnd;
std::pair<std::uint16_t, std::uint16_t> normalize_listen_port_range(
std::uint16_t listen_port_start,
std::uint16_t listen_port_end) {
if (listen_port_start == 0 && listen_port_end == 0) {
listen_port_start = kDefaultListenPortStart;
listen_port_end = kDefaultListenPortEnd;
} else if (listen_port_start == 0) {
listen_port_start = listen_port_end;
} else if (listen_port_end == 0) {
listen_port_end = listen_port_start;
}
if (listen_port_start > listen_port_end) {
std::swap(listen_port_start, listen_port_end);
}
return std::make_pair(listen_port_start, listen_port_end);
}
std::string ensure_session_locked() {
if (g_session) {
return "";
}
auto const normalized_port_range =
normalize_listen_port_range(g_listen_port_start, g_listen_port_end);
std::string last_error = "internal_error";
for (std::uint32_t port = normalized_port_range.first;
port <= normalized_port_range.second;
++port) {
auto const listen_port = static_cast<std::uint16_t>(port);
try {
lt::settings_pack settings;
settings.set_str(
lt::settings_pack::listen_interfaces,
build_listen_interfaces(listen_port));
settings.set_bool(lt::settings_pack::enable_upnp, true);
settings.set_bool(lt::settings_pack::enable_natpmp, true);
settings.set_bool(lt::settings_pack::enable_dht, true);
g_session = std::make_unique<lt::session>(settings);
return "";
} catch (std::exception const& e) {
last_error = e.what();
} catch (...) {
last_error = "internal_error";
}
if (port == std::numeric_limits<std::uint16_t>::max()) {
break;
}
}
return last_error;
}
std::string ensure_session() {
std::lock_guard<std::mutex> guard(g_mutex);
return ensure_session_locked();
}
void cleanup_torrent_handle(lt::torrent_handle const& handle) {
if (!handle.is_valid()) {
return;
}
std::lock_guard<std::mutex> guard(g_mutex);
if (!g_session) {
return;
}
try {
handle.pause();
} catch (...) {
}
g_session->remove_torrent(handle, lt::session_handle::delete_partfile);
}
} // namespace hydra::libtorrent_bridge::detail
-36
View File
@@ -1,36 +0,0 @@
#pragma once
#include <cstdint>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
#include <libtorrent/session.hpp>
#include <libtorrent/torrent_handle.hpp>
namespace hydra::libtorrent_bridge::detail {
struct DownloadState {
lt::torrent_handle handle;
std::optional<std::int64_t> selected_size_bytes;
};
extern std::mutex g_mutex;
extern std::unique_ptr<lt::session> g_session;
extern std::unordered_map<std::string, DownloadState> g_downloads;
extern std::uint16_t g_listen_port_start;
extern std::uint16_t g_listen_port_end;
std::pair<std::uint16_t, std::uint16_t> normalize_listen_port_range(
std::uint16_t listen_port_start,
std::uint16_t listen_port_end);
std::string ensure_session_locked();
std::string ensure_session();
void cleanup_torrent_handle(lt::torrent_handle const& handle);
} // namespace hydra::libtorrent_bridge::detail
-104
View File
@@ -1,104 +0,0 @@
#include "bridge_utils.h"
#include <algorithm>
namespace hydra::libtorrent_bridge::detail {
namespace {
constexpr std::uint32_t kHydraStatusCheckingFiles = 1;
constexpr std::uint32_t kHydraStatusDownloadingMetadata = 2;
constexpr std::uint32_t kHydraStatusDownloading = 3;
constexpr std::uint32_t kHydraStatusFinished = 4;
constexpr std::uint32_t kHydraStatusSeeding = 5;
} // namespace
std::uint32_t map_torrent_state(lt::torrent_status::state_t state) {
#if TORRENT_ABI_VERSION == 1
auto const state_value = static_cast<int>(state);
if (state_value == 0) {
return kHydraStatusCheckingFiles;
}
if (state_value == 6) {
return kHydraStatusDownloading;
}
#endif
switch (state) {
case lt::torrent_status::checking_files:
case lt::torrent_status::checking_resume_data:
return kHydraStatusCheckingFiles;
case lt::torrent_status::downloading_metadata:
return kHydraStatusDownloadingMetadata;
case lt::torrent_status::downloading:
return kHydraStatusDownloading;
case lt::torrent_status::finished:
return kHydraStatusFinished;
case lt::torrent_status::seeding:
return kHydraStatusSeeding;
default:
return kHydraStatusDownloading;
}
}
BridgeTorrentStatusResult empty_status_result() {
BridgeTorrentStatusResult result;
result.present = false;
result.progress = 0.0;
result.num_peers = 0;
result.num_seeds = 0;
result.download_speed = 0;
result.upload_speed = 0;
result.bytes_downloaded = 0;
result.file_size = 0;
result.folder_name = "";
result.status = 0;
return result;
}
BridgeTorrentFilesResult empty_files_result() {
BridgeTorrentFilesResult result;
result.ok = false;
result.error = "internal_error";
result.name = "";
result.total_size = 0;
return result;
}
rust::String to_rust_string(std::string const& value) {
return rust::String(value);
}
std::string normalize_save_path(rust::Str save_path) {
std::string path(save_path);
if (!path.empty()) {
return path;
}
return std::string();
}
std::string map_error_code(lt::error_code const& ec) {
if (!ec) {
return "";
}
std::string message = ec.message();
if (message.find("magnet") != std::string::npos ||
message.find("invalid") != std::string::npos) {
return "invalid_magnet";
}
return message.empty() ? std::string("internal_error") : message;
}
std::vector<std::string> to_std_trackers(rust::Vec<rust::String> const& trackers) {
std::vector<std::string> output;
output.reserve(trackers.size());
for (auto const& tracker : trackers) {
output.emplace_back(static_cast<std::string>(tracker));
}
return output;
}
} // namespace hydra::libtorrent_bridge::detail
-27
View File
@@ -1,27 +0,0 @@
#pragma once
#include "rust/cxx.h"
#include "hydra-native/src/libtorrent_bridge.rs.h"
#include <cstdint>
#include <string>
#include <vector>
#include <libtorrent/error_code.hpp>
#include <libtorrent/torrent_status.hpp>
namespace hydra::libtorrent_bridge::detail {
std::uint32_t map_torrent_state(lt::torrent_status::state_t state);
BridgeTorrentStatusResult empty_status_result();
BridgeTorrentFilesResult empty_files_result();
rust::String to_rust_string(std::string const& value);
std::string normalize_save_path(rust::Str save_path);
std::string map_error_code(lt::error_code const& ec);
std::vector<std::string> to_std_trackers(rust::Vec<rust::String> const& trackers);
} // namespace hydra::libtorrent_bridge::detail
@@ -1,464 +0,0 @@
#include "cpp/libtorrent_bridge.h"
#include "hydra-native/src/libtorrent_bridge.rs.h"
#include "bridge_state.h"
#include "bridge_utils.h"
#include "torrent_helpers.h"
#include <algorithm>
#include <cstdint>
#include <limits>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <tuple>
#include <iostream>
#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/error_code.hpp>
#include <libtorrent/file_storage.hpp>
#include <libtorrent/session_handle.hpp>
#include <libtorrent/settings_pack.hpp>
#include <libtorrent/torrent_flags.hpp>
#include <libtorrent/torrent_handle.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/torrent_status.hpp>
namespace hydra::libtorrent_bridge {
rust::String init_session(std::uint16_t listen_port_start, std::uint16_t listen_port_end) {
{
std::lock_guard<std::mutex> guard(detail::g_mutex);
auto const normalized_port_range =
detail::normalize_listen_port_range(listen_port_start, listen_port_end);
detail::g_listen_port_start = normalized_port_range.first;
detail::g_listen_port_end = normalized_port_range.second;
}
return detail::to_rust_string(detail::ensure_session());
}
rust::String set_download_limit(std::int64_t max_download_speed_bytes_per_second) {
std::lock_guard<std::mutex> guard(detail::g_mutex);
auto err = detail::ensure_session_locked();
if (!err.empty()) {
return detail::to_rust_string(err);
}
try {
int limit = 0;
if (max_download_speed_bytes_per_second > 0) {
auto const max_int64 = static_cast<std::int64_t>(std::numeric_limits<int>::max());
auto const bounded = std::min(max_download_speed_bytes_per_second, max_int64);
limit = static_cast<int>(bounded);
}
#if TORRENT_ABI_VERSION >= 3
lt::settings_pack settings;
settings.set_int(lt::settings_pack::download_rate_limit, limit);
detail::g_session->apply_settings(settings);
#else
detail::g_session->set_download_rate_limit(limit);
#endif
} catch (std::exception const& e) {
return detail::to_rust_string(e.what());
} catch (...) {
return detail::to_rust_string("internal_error");
}
return "";
}
rust::String start_torrent(
rust::Str game_id,
rust::Str magnet,
rust::Str save_path,
rust::Vec<rust::String> const& trackers,
rust::Vec<std::uint32_t> const& file_indices,
bool selective,
bool upload_mode,
std::uint32_t timeout_ms) {
std::string game_id_value(game_id);
std::string save_path_value = detail::normalize_save_path(save_path);
if (save_path_value.empty()) {
return "invalid_save_path";
}
{
std::lock_guard<std::mutex> guard(detail::g_mutex);
auto err = detail::ensure_session_locked();
if (!err.empty()) {
return detail::to_rust_string(err);
}
}
lt::torrent_handle existing_handle;
{
std::lock_guard<std::mutex> guard(detail::g_mutex);
auto it = detail::g_downloads.find(game_id_value);
if (it != detail::g_downloads.end()) {
existing_handle = it->second.handle;
}
}
if (existing_handle.is_valid() && !selective) {
try {
existing_handle.set_flags(lt::torrent_flags::auto_managed);
existing_handle.resume();
return "";
} catch (std::exception const& e) {
return detail::to_rust_string(e.what());
} catch (...) {
return "internal_error";
}
}
if (existing_handle.is_valid() && selective) {
std::ignore = cancel_torrent(game_id);
}
std::string params_error;
lt::add_torrent_params params = detail::build_add_torrent_params(
std::string(magnet),
save_path_value,
detail::to_std_trackers(trackers),
selective,
upload_mode,
&params_error);
if (!params_error.empty()) {
return detail::to_rust_string(params_error);
}
lt::torrent_handle handle;
{
std::lock_guard<std::mutex> guard(detail::g_mutex);
if (!detail::g_session) {
return "internal_error";
}
lt::error_code ec;
handle = detail::g_session->add_torrent(params, ec);
if (ec) {
return detail::to_rust_string(detail::map_error_code(ec));
}
detail::g_downloads[game_id_value] = detail::DownloadState{handle, std::nullopt};
}
if (selective) {
std::optional<std::int64_t> selected_size_bytes;
auto selective_error =
detail::apply_selective_file_priorities(handle, file_indices, timeout_ms, &selected_size_bytes);
if (!selective_error.empty()) {
std::ignore = cancel_torrent(game_id);
return detail::to_rust_string(selective_error);
}
std::lock_guard<std::mutex> guard(detail::g_mutex);
auto it = detail::g_downloads.find(game_id_value);
if (it != detail::g_downloads.end()) {
it->second.selected_size_bytes = selected_size_bytes;
}
}
try {
handle.set_flags(lt::torrent_flags::auto_managed);
handle.resume();
} catch (std::exception const& e) {
std::ignore = cancel_torrent(game_id);
return detail::to_rust_string(e.what());
} catch (...) {
std::ignore = cancel_torrent(game_id);
return "internal_error";
}
return "";
}
rust::String pause_torrent(rust::Str game_id) {
lt::torrent_handle handle;
{
std::lock_guard<std::mutex> guard(detail::g_mutex);
auto it = detail::g_downloads.find(std::string(game_id));
if (it == detail::g_downloads.end()) {
return "";
}
handle = it->second.handle;
}
if (!handle.is_valid()) {
return "";
}
try {
handle.pause();
handle.unset_flags(lt::torrent_flags::auto_managed);
} catch (std::exception const& e) {
return detail::to_rust_string(e.what());
} catch (...) {
return "internal_error";
}
return "";
}
rust::String cancel_torrent(rust::Str game_id) {
lt::torrent_handle handle;
{
std::lock_guard<std::mutex> guard(detail::g_mutex);
auto err = detail::ensure_session_locked();
if (!err.empty()) {
return detail::to_rust_string(err);
}
auto it = detail::g_downloads.find(std::string(game_id));
if (it == detail::g_downloads.end()) {
return "";
}
handle = it->second.handle;
detail::g_downloads.erase(it);
}
if (!handle.is_valid()) {
return "";
}
try {
handle.pause();
} catch (...) {
}
try {
std::lock_guard<std::mutex> guard(detail::g_mutex);
if (!detail::g_session) {
return "";
}
detail::g_session->remove_torrent(handle, lt::session_handle::delete_partfile);
} catch (std::exception const& e) {
return detail::to_rust_string(e.what());
} catch (...) {
return "internal_error";
}
return "";
}
BridgeTorrentStatusResult get_torrent_status(rust::Str game_id) {
BridgeTorrentStatusResult result = detail::empty_status_result();
try {
std::string game_id_value(game_id);
lt::torrent_handle handle;
std::optional<std::int64_t> selected_size_bytes;
{
std::lock_guard<std::mutex> guard(detail::g_mutex);
auto it = detail::g_downloads.find(game_id_value);
if (it == detail::g_downloads.end()) {
return result;
}
handle = it->second.handle;
selected_size_bytes = it->second.selected_size_bytes;
}
if (!handle.is_valid()) {
return result;
}
lt::torrent_status status;
try {
status = handle.status();
} catch (...) {
return result;
}
std::shared_ptr<const lt::torrent_info> info;
if (status.has_metadata) {
try {
info = handle.torrent_file();
} catch (...) {
info.reset();
}
}
auto total_wanted = static_cast<std::int64_t>(status.total_wanted);
std::int64_t file_size = 0;
std::string file_size_source = "status.total_wanted";
if (total_wanted > 0) {
file_size = total_wanted;
} else if (selected_size_bytes.has_value() && selected_size_bytes.value() > 0) {
file_size = selected_size_bytes.value();
file_size_source = "selected_size_bytes";
} else if (info) {
file_size = static_cast<std::int64_t>(info->total_size());
file_size_source = "torrent_info.total_size";
} else {
file_size_source = "unknown";
}
auto total_wanted_done = static_cast<std::int64_t>(status.total_wanted_done);
std::int64_t bytes_downloaded = 0;
if (total_wanted_done >= 0) {
bytes_downloaded = total_wanted_done;
} else if (file_size > 0) {
bytes_downloaded =
static_cast<std::int64_t>(status.progress * static_cast<double>(file_size));
} else {
bytes_downloaded = static_cast<std::int64_t>(status.all_time_download);
}
if (bytes_downloaded < 0) {
bytes_downloaded = 0;
}
double progress = 0.0;
if (file_size <= 0) {
progress = static_cast<double>(status.progress);
} else {
progress = std::clamp(
static_cast<double>(bytes_downloaded) / static_cast<double>(file_size),
0.0,
1.0);
}
auto mapped_status = detail::map_torrent_state(status.state);
std::cerr << "[hydra-native][get_torrent_status] game_id=" << game_id_value
<< " mapped_status=" << mapped_status << " total_wanted=" << total_wanted
<< " total_wanted_done=" << total_wanted_done
<< " all_time_download=" << static_cast<std::int64_t>(status.all_time_download)
<< " file_size=" << file_size << " file_size_source=" << file_size_source
<< std::endl;
result.present = true;
result.progress = progress;
result.num_peers = static_cast<std::uint32_t>(std::max(status.num_peers, 0));
result.num_seeds = static_cast<std::uint32_t>(std::max(status.num_seeds, 0));
result.download_speed = static_cast<std::int64_t>(status.download_rate);
result.upload_speed = static_cast<std::int64_t>(status.upload_rate);
result.bytes_downloaded = bytes_downloaded;
result.file_size = file_size;
result.folder_name = info ? detail::to_rust_string(info->name()) : "";
result.status = mapped_status;
} catch (...) {
return detail::empty_status_result();
}
return result;
}
BridgeTorrentFilesResult get_torrent_files(
rust::Str magnet,
rust::Str save_path,
rust::Vec<rust::String> const& trackers,
std::uint32_t timeout_ms,
std::uint32_t max_files) {
BridgeTorrentFilesResult result = detail::empty_files_result();
auto session_error = detail::ensure_session();
if (!session_error.empty()) {
result.error = detail::to_rust_string(session_error);
return result;
}
std::string params_error;
lt::add_torrent_params params = detail::build_add_torrent_params(
std::string(magnet),
detail::normalize_save_path(save_path),
detail::to_std_trackers(trackers),
false,
true,
&params_error);
if (!params_error.empty()) {
result.error = detail::to_rust_string(params_error);
return result;
}
lt::torrent_handle handle;
{
std::lock_guard<std::mutex> guard(detail::g_mutex);
if (!detail::g_session) {
result.error = "internal_error";
return result;
}
lt::error_code ec;
handle = detail::g_session->add_torrent(params, ec);
if (ec) {
result.error = detail::to_rust_string(detail::map_error_code(ec));
return result;
}
}
auto cleanup = [&handle]() { detail::cleanup_torrent_handle(handle); };
try {
handle.set_flags(lt::torrent_flags::auto_managed);
handle.resume();
} catch (...) {
cleanup();
result.error = "internal_error";
return result;
}
if (!detail::wait_for_metadata(handle, timeout_ms)) {
cleanup();
result.error = "metadata_timeout";
return result;
}
std::shared_ptr<const lt::torrent_info> info;
try {
info = handle.torrent_file();
} catch (...) {
cleanup();
result.error = "metadata_incomplete";
return result;
}
if (!info) {
cleanup();
result.error = "metadata_incomplete";
return result;
}
auto const& files_storage = info->files();
auto const file_count = static_cast<std::size_t>(files_storage.num_files());
if (file_count > static_cast<std::size_t>(max_files)) {
cleanup();
result.error = "too_many_files";
return result;
}
result.ok = true;
result.error = "";
result.name = detail::to_rust_string(info->name());
result.total_size = static_cast<std::int64_t>(info->total_size());
result.files.reserve(file_count);
for (std::size_t index = 0; index < file_count; ++index) {
BridgeTorrentFileEntry entry;
entry.index = static_cast<std::uint32_t>(index);
entry.path = detail::to_rust_string(files_storage.file_path(lt::file_index_t(static_cast<int>(index))));
entry.length = static_cast<std::int64_t>(
files_storage.file_size(lt::file_index_t(static_cast<int>(index))));
result.files.push_back(entry);
}
cleanup();
return result;
}
} // namespace hydra::libtorrent_bridge
@@ -1,40 +0,0 @@
#pragma once
#include "rust/cxx.h"
#include <cstdint>
namespace hydra::libtorrent_bridge {
struct BridgeTorrentFileEntry;
struct BridgeTorrentFilesResult;
struct BridgeTorrentStatusResult;
rust::String init_session(std::uint16_t listen_port_start, std::uint16_t listen_port_end);
rust::String set_download_limit(std::int64_t max_download_speed_bytes_per_second);
rust::String start_torrent(
rust::Str game_id,
rust::Str magnet,
rust::Str save_path,
rust::Vec<rust::String> const& trackers,
rust::Vec<std::uint32_t> const& file_indices,
bool selective,
bool upload_mode,
std::uint32_t timeout_ms);
rust::String pause_torrent(rust::Str game_id);
rust::String cancel_torrent(rust::Str game_id);
BridgeTorrentStatusResult get_torrent_status(rust::Str game_id);
BridgeTorrentFilesResult get_torrent_files(
rust::Str magnet,
rust::Str save_path,
rust::Vec<rust::String> const& trackers,
std::uint32_t timeout_ms,
std::uint32_t max_files);
} // namespace hydra::libtorrent_bridge
-179
View File
@@ -1,179 +0,0 @@
#include "torrent_helpers.h"
#include <algorithm>
#include <memory>
#include <thread>
#include <libtorrent/download_priority.hpp>
#include <libtorrent/error_code.hpp>
#include <libtorrent/file_storage.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/torrent_flags.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/torrent_status.hpp>
namespace hydra::libtorrent_bridge::detail {
bool wait_for_metadata(lt::torrent_handle const& handle, std::uint32_t timeout_ms) {
if (!handle.is_valid()) {
return false;
}
auto timeout = std::chrono::milliseconds(std::max<std::uint32_t>(timeout_ms, 1000));
auto deadline = std::chrono::steady_clock::now() + timeout;
auto poll_interval = std::chrono::milliseconds(250);
while (std::chrono::steady_clock::now() < deadline) {
if (!handle.is_valid()) {
return false;
}
lt::torrent_status status;
try {
status = handle.status();
} catch (...) {
return false;
}
if (status.has_metadata) {
return true;
}
std::this_thread::sleep_for(poll_interval);
}
return false;
}
std::vector<std::size_t> sanitize_file_indices(
rust::Vec<std::uint32_t> const& raw_indices,
std::size_t file_count,
std::string* error) {
std::vector<std::size_t> indices;
indices.reserve(raw_indices.size());
for (auto index : raw_indices) {
if (file_count == 0 || static_cast<std::size_t>(index) >= file_count) {
if (error != nullptr) {
*error = "invalid_file_indices";
}
return {};
}
indices.push_back(static_cast<std::size_t>(index));
}
std::sort(indices.begin(), indices.end());
indices.erase(std::unique(indices.begin(), indices.end()), indices.end());
if (indices.empty()) {
if (error != nullptr) {
*error = "empty_selection";
}
return {};
}
return indices;
}
lt::add_torrent_params build_add_torrent_params(
std::string const& magnet,
std::string const& save_path,
std::vector<std::string> const& trackers,
bool selective,
bool upload_mode,
std::string* error) {
lt::error_code ec;
lt::add_torrent_params params = lt::parse_magnet_uri(magnet, ec);
if (ec) {
if (error != nullptr) {
*error = "invalid_magnet";
}
return lt::add_torrent_params();
}
params.save_path = save_path;
params.trackers = trackers;
params.flags |= lt::torrent_flags::paused;
params.flags |= lt::torrent_flags::auto_managed;
if (selective) {
params.flags |= lt::torrent_flags::default_dont_download;
}
if (upload_mode) {
params.flags |= lt::torrent_flags::upload_mode;
}
return params;
}
std::string apply_selective_file_priorities(
lt::torrent_handle const& handle,
rust::Vec<std::uint32_t> const& file_indices,
std::uint32_t timeout_ms,
std::optional<std::int64_t>* selected_size_bytes) {
handle.set_flags(lt::torrent_flags::auto_managed);
handle.resume();
if (!wait_for_metadata(handle, timeout_ms)) {
return "metadata_timeout";
}
std::shared_ptr<const lt::torrent_info> info;
try {
info = handle.torrent_file();
} catch (...) {
return "metadata_incomplete";
}
if (!info) {
return "metadata_incomplete";
}
auto const& files_storage = info->files();
auto const file_count = static_cast<std::size_t>(files_storage.num_files());
std::string sanitize_error;
auto indices = sanitize_file_indices(file_indices, file_count, &sanitize_error);
if (!sanitize_error.empty()) {
return sanitize_error;
}
handle.pause();
handle.unset_flags(lt::torrent_flags::auto_managed);
std::vector<lt::download_priority_t> priorities(file_count, lt::dont_download);
std::int64_t size_sum = 0;
for (auto const index : indices) {
priorities[index] = lt::default_priority;
size_sum += static_cast<std::int64_t>(
files_storage.file_size(lt::file_index_t(static_cast<int>(index))));
}
handle.prioritize_files(priorities);
auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(3);
while (std::chrono::steady_clock::now() < deadline) {
std::vector<lt::download_priority_t> current_priorities;
try {
current_priorities = handle.get_file_priorities();
} catch (...) {
break;
}
if (current_priorities == priorities) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
*selected_size_bytes = size_sum;
return "";
}
} // namespace hydra::libtorrent_bridge::detail
-38
View File
@@ -1,38 +0,0 @@
#pragma once
#include "rust/cxx.h"
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/torrent_handle.hpp>
namespace hydra::libtorrent_bridge::detail {
bool wait_for_metadata(lt::torrent_handle const& handle, std::uint32_t timeout_ms);
std::vector<std::size_t> sanitize_file_indices(
rust::Vec<std::uint32_t> const& raw_indices,
std::size_t file_count,
std::string* error);
lt::add_torrent_params build_add_torrent_params(
std::string const& magnet,
std::string const& save_path,
std::vector<std::string> const& trackers,
bool selective,
bool upload_mode,
std::string* error);
std::string apply_selective_file_priorities(
lt::torrent_handle const& handle,
rust::Vec<std::uint32_t> const& file_indices,
std::uint32_t timeout_ms,
std::optional<std::int64_t>* selected_size_bytes);
} // namespace hydra::libtorrent_bridge::detail