2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef __TRANSMISSION__
|
|
|
|
#error only libtransmission should #include this header.
|
|
|
|
#endif
|
|
|
|
|
2023-03-22 15:24:10 +00:00
|
|
|
#include <algorithm> // for std::binary_search()
|
|
|
|
#include <cstdint> // for uint64_t
|
|
|
|
#include <cstddef> // for size_t
|
2021-11-29 01:12:54 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-11-29 01:12:54 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/bitfield.h"
|
|
|
|
#include "libtransmission/tr-macros.h" // TR_CONSTEXPR20
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
struct tr_block_info;
|
2022-01-15 19:33:57 +00:00
|
|
|
struct tr_torrent_metainfo;
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
class tr_file_piece_map
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
struct index_span_t
|
|
|
|
{
|
|
|
|
T begin;
|
|
|
|
T end;
|
|
|
|
};
|
|
|
|
using file_span_t = index_span_t<tr_file_index_t>;
|
|
|
|
using piece_span_t = index_span_t<tr_piece_index_t>;
|
|
|
|
|
2021-12-24 06:39:55 +00:00
|
|
|
template<typename T>
|
|
|
|
struct offset_t
|
|
|
|
{
|
|
|
|
T index;
|
|
|
|
uint64_t offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
using file_offset_t = offset_t<tr_file_index_t>;
|
2023-12-06 04:06:27 +00:00
|
|
|
explicit tr_file_piece_map(tr_torrent_metainfo const& tm);
|
|
|
|
tr_file_piece_map(tr_block_info const& block_info, uint64_t const* file_sizes, size_t n_files);
|
2021-12-24 06:39:55 +00:00
|
|
|
|
2023-12-06 04:06:27 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 piece_span_t piece_span_for_file(tr_file_index_t const file) const noexcept
|
2023-03-06 21:50:19 +00:00
|
|
|
{
|
|
|
|
return file_pieces_[file];
|
|
|
|
}
|
|
|
|
|
2023-12-06 04:06:27 +00:00
|
|
|
[[nodiscard]] file_span_t file_span_for_piece(tr_piece_index_t piece) const;
|
2021-12-24 06:39:55 +00:00
|
|
|
|
2023-11-28 16:51:13 +00:00
|
|
|
[[nodiscard]] file_offset_t file_offset(uint64_t offset) const;
|
2021-12-24 06:39:55 +00:00
|
|
|
|
2023-12-06 04:06:27 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 size_t file_count() const
|
2021-11-29 01:12:54 +00:00
|
|
|
{
|
2021-12-24 06:39:55 +00:00
|
|
|
return std::size(file_pieces_);
|
2021-11-29 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 04:06:27 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 auto byte_span_for_file(tr_file_index_t const file) const
|
2021-12-26 18:43:27 +00:00
|
|
|
{
|
2023-11-09 22:49:18 +00:00
|
|
|
auto const& span = file_bytes_[file];
|
2021-12-26 18:43:27 +00:00
|
|
|
return tr_byte_span_t{ span.begin, span.end };
|
|
|
|
}
|
|
|
|
|
2023-12-06 04:06:27 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool is_edge_piece(tr_piece_index_t const piece) const
|
2023-03-22 15:24:10 +00:00
|
|
|
{
|
|
|
|
return std::binary_search(std::begin(edge_pieces_), std::end(edge_pieces_), piece);
|
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
private:
|
2023-12-06 04:06:27 +00:00
|
|
|
using byte_span_t = index_span_t<uint64_t>;
|
|
|
|
|
|
|
|
void reset(tr_torrent_metainfo const& tm);
|
2023-03-22 15:24:10 +00:00
|
|
|
void reset(tr_block_info const& block_info, uint64_t const* file_sizes, size_t n_files);
|
|
|
|
|
2021-12-24 06:39:55 +00:00
|
|
|
std::vector<byte_span_t> file_bytes_;
|
|
|
|
std::vector<piece_span_t> file_pieces_;
|
2023-03-22 15:24:10 +00:00
|
|
|
std::vector<tr_piece_index_t> edge_pieces_;
|
2021-11-29 01:12:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class tr_file_priorities
|
|
|
|
{
|
|
|
|
public:
|
2023-03-22 15:24:10 +00:00
|
|
|
TR_CONSTEXPR20 explicit tr_file_priorities(tr_file_piece_map const* fpm) noexcept
|
2022-09-23 05:51:15 +00:00
|
|
|
: fpm_{ fpm }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-29 01:12:54 +00:00
|
|
|
void set(tr_file_index_t file, tr_priority_t priority);
|
|
|
|
void set(tr_file_index_t const* files, size_t n, tr_priority_t priority);
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] tr_priority_t file_priority(tr_file_index_t file) const;
|
|
|
|
[[nodiscard]] tr_priority_t piece_priority(tr_piece_index_t piece) const;
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
tr_file_piece_map const* fpm_;
|
|
|
|
std::vector<tr_priority_t> priorities_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class tr_files_wanted
|
|
|
|
{
|
|
|
|
public:
|
2023-12-06 04:06:27 +00:00
|
|
|
explicit tr_files_wanted(tr_file_piece_map const* fpm);
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
void set(tr_file_index_t file, bool wanted);
|
|
|
|
void set(tr_file_index_t const* files, size_t n, bool wanted);
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool file_wanted(tr_file_index_t file) const
|
2022-08-31 04:17:23 +00:00
|
|
|
{
|
|
|
|
return wanted_.test(file);
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] bool piece_wanted(tr_piece_index_t piece) const;
|
2021-11-29 01:12:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
tr_file_piece_map const* fpm_;
|
|
|
|
tr_bitfield wanted_;
|
|
|
|
};
|