mirror of
https://github.com/transmission/transmission
synced 2025-01-04 22:11:23 +00:00
f6f0db75e1
* fix: use-init-statement Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX1f6EvHJiycnfA7gfrG\&open\=AX1f6EvHJiycnfA7gfrG * fix replace-use-of-new warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX1ZNs41ZmlvCveKTzon\&open\=AX1ZNs41ZmlvCveKTzon * fix has-virtual-functions-but-non-virtual-destructor warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX1ZNs72ZmlvCveKTzo6\&open\=AX1ZNs72ZmlvCveKTzo6 * fix make-variable-const warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX0_a_OVNJn7rAzml_7B\&open\=AX0_a_OVNJn7rAzml_7B * fix remove-redundant-static-specifiers Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX06St-81usi2gyYkPTb\&open\=AX06St-81usi2gyYkPTb * fix function-should-be-declared-const Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX06St-81usi2gyYkPTd\&open\=AX06St-81usi2gyYkPTd * fix use-init-statement warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX06St-81usi2gyYkPTc\&open\=AX06St-81usi2gyYkPTc * fix class-has-virtual-functions-but-non-virtual-destructor warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX06Stz41usi2gyYkPTS\&open\=AX06Stz41usi2gyYkPTS * fix remove-commented-out-code warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX06St241usi2gyYkPTT\&open\=AX06St241usi2gyYkPTT * fix remove-commented-out-code warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX06St241usi2gyYkPTV\&open\=AX06St241usi2gyYkPTV * fix has-virtual-functions-but-non-virtual-destructor warning https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX06St241usi2gyYkPTW\&open\=AX06St241usi2gyYkPTW * fix remove-commented-out-code warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX06SuCA1usi2gyYkPTh\&open\=AX06SuCA1usi2gyYkPTh * fix use-init-statement warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX0rAQvnfJ-O-YIDS9xF\&open\=AX0rAQvnfJ-O-YIDS9xF * fix use-init-statement warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX0rAQvnfJ-O-YIDS9xG\&open\=AX0rAQvnfJ-O-YIDS9xG * fix remove-redundant-access-specifier warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX1ZNs5tZmlvCveKTzor\&open\=AX1ZNs5tZmlvCveKTzor * fix use-init-statement warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX01Itl7f_SST5i7BN1l\&open\=AX01Itl7f_SST5i7BN1l * fix use-init-statement warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX0wDijI2l89lDvp1C9P\&open\=AX0wDijI2l89lDvp1C9P * fix use-automatically-managed-memory-instead-of-new warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX1f6E6HJiycnfA7gfrI\&open\=AX1f6E6HJiycnfA7gfrI * fix use-init-statement warning Xref: https://sonarcloud.io/project/issues\?id\=transmission_transmission\&issues\=AX0l8vknEafnvRiIHUEv\&open\=AX0l8vknEafnvRiIHUEv * fixup! fix has-virtual-functions-but-non-virtual-destructor warning
160 lines
3.8 KiB
C++
160 lines
3.8 KiB
C++
/*
|
|
* This file Copyright (C) 2009-2014 Mnemosyne LLC
|
|
*
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
*
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#ifndef __TRANSMISSION__
|
|
#error only libtransmission should #include this header.
|
|
#endif
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "block-info.h"
|
|
#include "bitfield.h"
|
|
|
|
/**
|
|
* @brief knows which blocks and pieces we have
|
|
*/
|
|
struct tr_completion
|
|
{
|
|
struct torrent_view
|
|
{
|
|
virtual bool pieceIsDnd(tr_piece_index_t piece) const = 0;
|
|
|
|
virtual ~torrent_view() = default;
|
|
};
|
|
|
|
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]] bool isDone() const
|
|
{
|
|
return hasMetainfo() && leftUntilDone() == 0;
|
|
}
|
|
|
|
[[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);
|
|
}
|
|
}
|
|
|
|
void setBlocks(tr_bitfield blocks);
|
|
|
|
void invalidateSizeWhenDone()
|
|
{
|
|
size_when_done_.reset();
|
|
}
|
|
|
|
private:
|
|
[[nodiscard]] constexpr bool hasMetainfo() const
|
|
{
|
|
return !std::empty(blocks_);
|
|
}
|
|
|
|
[[nodiscard]] uint64_t computeHasValid() const;
|
|
[[nodiscard]] uint64_t computeSizeWhenDone() const;
|
|
[[nodiscard]] uint64_t countHasBytesInSpan(tr_block_span_t) const;
|
|
|
|
torrent_view const* tor_;
|
|
tr_block_info const* block_info_;
|
|
|
|
tr_bitfield blocks_{ 0 };
|
|
|
|
// Number of bytes we'll have when done downloading. [0..info.totalSize]
|
|
// Mutable because lazy-calculated
|
|
mutable std::optional<uint64_t> size_when_done_;
|
|
|
|
// Number of verified bytes we have right now. [0..info.totalSize]
|
|
// Mutable because lazy-calculated
|
|
mutable std::optional<uint64_t> has_valid_;
|
|
|
|
// Number of bytes we have now. [0..sizeWhenDone]
|
|
uint64_t size_now_ = 0;
|
|
};
|