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.
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2009-11-24 02:16:31 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2009-11-24 02:16:31 +00:00
|
|
|
#endif
|
|
|
|
|
2021-12-16 09:43:51 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-08-12 00:59:58 +00:00
|
|
|
#include <cstdint> // int64_t
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <ctime> // time_t
|
2023-07-15 20:38:38 +00:00
|
|
|
#include <deque>
|
2024-01-03 03:04:17 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
2022-08-12 00:59:58 +00:00
|
|
|
#include <optional>
|
2024-01-03 03:04:17 +00:00
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2022-08-12 00:59:58 +00:00
|
|
|
#include <vector>
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-07-12 12:36:16 +00:00
|
|
|
#include <small/vector.hpp>
|
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
#include "libtransmission/tr-macros.h"
|
2021-12-16 09:43:51 +00:00
|
|
|
|
2021-09-16 16:22:33 +00:00
|
|
|
// defined by BEP #9
|
2024-01-03 03:04:17 +00:00
|
|
|
inline constexpr auto MetadataPieceSize = 1024 * 16;
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2023-07-17 13:56:57 +00:00
|
|
|
using tr_metadata_piece = small::max_size_vector<std::byte, MetadataPieceSize>;
|
2023-07-12 12:36:16 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
class tr_metadata_download
|
2023-07-15 20:38:38 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
public:
|
|
|
|
tr_metadata_download(std::string_view log_name, int64_t size);
|
|
|
|
|
|
|
|
[[nodiscard]] static constexpr auto is_valid_metadata_size(int64_t const size) noexcept
|
2023-07-15 20:38:38 +00:00
|
|
|
{
|
2024-01-03 03:04:17 +00:00
|
|
|
return size > 0 && size <= std::numeric_limits<int>::max();
|
|
|
|
}
|
2023-07-15 20:38:38 +00:00
|
|
|
|
2024-05-24 20:50:01 +00:00
|
|
|
[[nodiscard]] auto is_complete() const noexcept
|
|
|
|
{
|
|
|
|
return std::empty(pieces_needed_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_metadata_piece(int64_t piece, void const* data, size_t len);
|
2023-07-15 20:38:38 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] std::optional<int64_t> get_next_metadata_request(time_t now) noexcept;
|
2023-07-15 20:38:38 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] double get_metadata_percent() const noexcept;
|
2023-07-15 20:38:38 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] constexpr auto const& get_metadata() const noexcept
|
|
|
|
{
|
|
|
|
return metadata_;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 std::string_view log_name() const noexcept
|
|
|
|
{
|
|
|
|
return log_name_;
|
|
|
|
}
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
private:
|
|
|
|
struct metadata_node
|
|
|
|
{
|
|
|
|
time_t requested_at = {};
|
|
|
|
int64_t piece = {};
|
|
|
|
};
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
[[nodiscard]] constexpr size_t get_piece_length(int64_t const piece) const noexcept
|
|
|
|
{
|
|
|
|
return piece + 1 == piece_count_ ? // last piece
|
|
|
|
std::size(metadata_) - (piece * MetadataPieceSize) :
|
|
|
|
MetadataPieceSize;
|
|
|
|
}
|
2009-11-24 17:10:40 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
void create_all_needed(int64_t n_pieces) noexcept;
|
2022-06-21 21:47:57 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
std::vector<char> metadata_;
|
|
|
|
std::deque<metadata_node> pieces_needed_;
|
|
|
|
int64_t piece_count_ = {};
|
2023-02-13 18:33:33 +00:00
|
|
|
|
2024-01-03 03:04:17 +00:00
|
|
|
std::string log_name_;
|
|
|
|
};
|