2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// 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 }
|
|
|
|
, blocks_{ block_info_->n_blocks }
|
|
|
|
{
|
|
|
|
blocks_.setHasNone();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr tr_bitfield const& blocks() const
|
|
|
|
{
|
|
|
|
return blocks_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool hasAll() const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr bool hasNone() const
|
|
|
|
{
|
|
|
|
return !hasMetainfo() || blocks_.hasNone();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] bool hasPiece(tr_piece_index_t piece) const
|
|
|
|
{
|
|
|
|
return block_info_->piece_size != 0 && countMissingBlocksInPiece(piece) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr uint64_t hasTotal() const
|
|
|
|
{
|
|
|
|
return size_now_;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] uint64_t hasValid() const;
|
|
|
|
|
|
|
|
[[nodiscard]] uint64_t leftUntilDone() const;
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr double percentComplete() const
|
|
|
|
{
|
|
|
|
auto const denom = block_info_->total_size;
|
|
|
|
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;
|
|
|
|
|
|
|
|
[[nodiscard]] tr_completeness status() const;
|
|
|
|
|
|
|
|
[[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;
|
|
|
|
|
|
|
|
void addBlock(tr_block_index_t i);
|
|
|
|
void addPiece(tr_piece_index_t i);
|
|
|
|
void removePiece(tr_piece_index_t i);
|
|
|
|
|
|
|
|
void setHasPiece(tr_piece_index_t i, bool has)
|
|
|
|
{
|
|
|
|
if (has)
|
|
|
|
{
|
|
|
|
addPiece(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removePiece(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 19:17:51 +00:00
|
|
|
void setHasAll();
|
|
|
|
|
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;
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
private:
|
|
|
|
[[nodiscard]] constexpr bool hasMetainfo() const
|
|
|
|
{
|
|
|
|
return !std::empty(blocks_);
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] uint64_t computeHasValid() const;
|
|
|
|
[[nodiscard]] uint64_t computeSizeWhenDone() const;
|
2021-12-26 18:43:27 +00:00
|
|
|
[[nodiscard]] uint64_t countHasBytesInBlocks(tr_block_span_t) const;
|
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
|
|
|
};
|