2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 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.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <cstddef> // size_t
|
2021-11-25 18:26:51 +00:00
|
|
|
#include <optional>
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "transmission.h"
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
#include "block-info.h"
|
2011-03-28 16:31:05 +00:00
|
|
|
#include "bitfield.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
/**
|
|
|
|
* @brief knows which blocks and pieces we have
|
|
|
|
*/
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_completion
|
2009-01-02 17:01:55 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
struct torrent_view
|
|
|
|
{
|
2021-11-29 01:12:54 +00:00
|
|
|
virtual bool pieceIsWanted(tr_piece_index_t piece) const = 0;
|
2021-11-28 01:58:35 +00:00
|
|
|
|
|
|
|
virtual ~torrent_view() = default;
|
2021-11-25 18:26:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit tr_completion(torrent_view const* tor, tr_block_info const* block_info)
|
|
|
|
: tor_{ tor }
|
|
|
|
, block_info_{ block_info }
|
2022-04-14 01:22:59 +00:00
|
|
|
, blocks_{ block_info_->blockCount() }
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
|
|
|
blocks_.setHasNone();
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr tr_bitfield const& blocks() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
|
|
|
return blocks_;
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr bool hasAll() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
|
|
|
return hasMetainfo() && blocks_.hasAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool hasBlock(tr_block_index_t block) const
|
|
|
|
{
|
|
|
|
return blocks_.test(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool hasBlocks(tr_block_span_t span) const
|
|
|
|
{
|
|
|
|
return blocks_.count(span.begin, span.end) == span.end - span.begin;
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] constexpr bool hasNone() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
|
|
|
return !hasMetainfo() || blocks_.hasNone();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool hasPiece(tr_piece_index_t piece) const
|
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return block_info_->pieceSize() != 0 && countMissingBlocksInPiece(piece) == 0;
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr uint64_t hasTotal() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
|
|
|
return size_now_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] uint64_t hasValid() const;
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
[[nodiscard]] auto leftUntilDone() const
|
|
|
|
{
|
|
|
|
return sizeWhenDone() - hasTotal();
|
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
[[nodiscard]] constexpr double percentComplete() const
|
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
auto const denom = block_info_->totalSize();
|
2021-11-25 18:26:51 +00:00
|
|
|
return denom ? std::clamp(double(size_now_) / denom, 0.0, 1.0) : 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] double percentDone() const
|
|
|
|
{
|
|
|
|
auto const denom = sizeWhenDone();
|
|
|
|
return denom ? std::clamp(double(size_now_) / denom, 0.0, 1.0) : 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] uint64_t sizeWhenDone() const;
|
|
|
|
|
2022-08-31 04:17:23 +00:00
|
|
|
[[nodiscard]] tr_completeness status() const
|
|
|
|
{
|
|
|
|
if (!hasMetainfo())
|
|
|
|
{
|
|
|
|
return TR_LEECH;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasAll())
|
|
|
|
{
|
|
|
|
return TR_SEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size_now_ == sizeWhenDone())
|
|
|
|
{
|
|
|
|
return TR_PARTIAL_SEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TR_LEECH;
|
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
[[nodiscard]] std::vector<uint8_t> createPieceBitfield() const;
|
|
|
|
|
|
|
|
[[nodiscard]] size_t countMissingBlocksInPiece(tr_piece_index_t) const;
|
|
|
|
[[nodiscard]] size_t countMissingBytesInPiece(tr_piece_index_t) const;
|
|
|
|
|
|
|
|
void amountDone(float* tab, size_t n_tabs) const;
|
|
|
|
|
2022-08-03 06:15:37 +00:00
|
|
|
void addBlock(tr_block_index_t block);
|
|
|
|
void addPiece(tr_piece_index_t piece);
|
|
|
|
void removePiece(tr_piece_index_t piece);
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
void setHasPiece(tr_piece_index_t i, bool has)
|
|
|
|
{
|
|
|
|
if (has)
|
|
|
|
{
|
|
|
|
addPiece(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removePiece(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:51:56 +00:00
|
|
|
void setHasAll() noexcept;
|
2022-02-14 19:17:51 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void setBlocks(tr_bitfield blocks);
|
|
|
|
|
|
|
|
void invalidateSizeWhenDone()
|
|
|
|
{
|
|
|
|
size_when_done_.reset();
|
|
|
|
}
|
|
|
|
|
2021-12-26 18:43:27 +00:00
|
|
|
[[nodiscard]] uint64_t countHasBytesInSpan(tr_byte_span_t) const;
|
|
|
|
|
2022-03-29 04:29:35 +00:00
|
|
|
[[nodiscard]] constexpr bool hasMetainfo() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
|
|
|
return !std::empty(blocks_);
|
|
|
|
}
|
|
|
|
|
2022-03-29 04:29:35 +00:00
|
|
|
private:
|
2021-11-25 18:26:51 +00:00
|
|
|
[[nodiscard]] uint64_t computeHasValid() const;
|
|
|
|
[[nodiscard]] uint64_t computeSizeWhenDone() const;
|
2022-07-04 16:48:54 +00:00
|
|
|
|
|
|
|
[[nodiscard]] uint64_t countHasBytesInPiece(tr_piece_index_t piece) const
|
|
|
|
{
|
|
|
|
return countHasBytesInSpan(block_info_->byteSpanForPiece(piece));
|
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
torrent_view const* tor_;
|
|
|
|
tr_block_info const* block_info_;
|
|
|
|
|
|
|
|
tr_bitfield blocks_{ 0 };
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-12-15 15:53:20 +00:00
|
|
|
// Number of bytes we'll have when done downloading. [0..totalSize]
|
2021-11-25 18:26:51 +00:00
|
|
|
// Mutable because lazy-calculated
|
|
|
|
mutable std::optional<uint64_t> size_when_done_;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2021-12-15 15:53:20 +00:00
|
|
|
// Number of verified bytes we have right now. [0..totalSize]
|
2021-11-25 18:26:51 +00:00
|
|
|
// Mutable because lazy-calculated
|
|
|
|
mutable std::optional<uint64_t> has_valid_;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
// Number of bytes we have now. [0..sizeWhenDone]
|
|
|
|
uint64_t size_now_ = 0;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|