2009-01-07 06:53:29 +00:00
|
|
|
/*
|
2021-11-25 18:26:51 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
2009-01-07 06:53:29 +00:00
|
|
|
*/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
#include <algorithm>
|
2021-11-09 03:30:03 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2021-11-25 18:26:51 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#include "completion.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2007-11-12 22:26:10 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::leftUntilDone() const
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const size_when_done = sizeWhenDone();
|
|
|
|
auto const has_total = hasTotal();
|
|
|
|
return size_when_done - has_total;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::computeHasValid() const
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t size = 0;
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
for (tr_piece_index_t piece = 0, n = block_info_->n_pieces; piece < n; ++piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (hasPiece(piece))
|
|
|
|
{
|
|
|
|
size += block_info_->countBytesInPiece(piece);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return size;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::hasValid() const
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (!has_valid_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
has_valid_ = computeHasValid();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return *has_valid_;
|
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::computeSizeWhenDone() const
|
|
|
|
{
|
|
|
|
if (hasAll())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return block_info_->total_size;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
// count bytes that we want or that we already have
|
|
|
|
auto size = size_t{ 0 };
|
|
|
|
for (tr_piece_index_t piece = 0; piece < block_info_->n_pieces; ++piece)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (!tor_->pieceIsDnd(piece))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
size += block_info_->countBytesInPiece(piece);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size += countHasBytesInSpan(block_info_->blockSpanForPiece(piece));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-28 15:43:34 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return size;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::sizeWhenDone() const
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (!size_when_done_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
size_when_done_ = computeSizeWhenDone();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
|
|
|
|
return *size_when_done_;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void tr_completion::amountDone(float* tab, size_t n_tabs) const
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (n_tabs < 1)
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const blocks_per_tab = std::size(blocks_) / n_tabs;
|
|
|
|
for (size_t i = 0; i < n_tabs; ++i)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const begin = i * n_tabs;
|
|
|
|
auto const end = std::min(begin + blocks_per_tab, std::size(blocks_));
|
|
|
|
auto const numerator = blocks_.count(begin, end);
|
|
|
|
tab[i] = (double)numerator / (end - begin);
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2007-09-26 01:55:04 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
size_t tr_completion::countMissingBlocksInPiece(tr_piece_index_t piece) const
|
2008-11-05 04:50:03 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const [begin, end] = block_info_->blockSpanForPiece(piece);
|
|
|
|
return (end - begin) - blocks_.count(begin, end);
|
2008-11-05 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
size_t tr_completion::countMissingBytesInPiece(tr_piece_index_t piece) const
|
2011-09-26 06:18:48 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return block_info_->countBytesInPiece(piece) - countHasBytesInSpan(block_info_->blockSpanForPiece(piece));
|
2011-09-26 06:18:48 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_completeness tr_completion::status() const
|
2008-04-24 15:25:01 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (!hasMetainfo())
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return TR_LEECH;
|
2011-03-05 03:51:57 +00:00
|
|
|
}
|
2009-01-02 17:01:55 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
if (hasAll())
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return TR_SEED;
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
if (size_now_ == sizeWhenDone())
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return TR_PARTIAL_SEED;
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return TR_LEECH;
|
|
|
|
}
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
std::vector<uint8_t> tr_completion::createPieceBitfield() const
|
|
|
|
{
|
|
|
|
size_t const n = block_info_->n_pieces;
|
|
|
|
auto pieces = tr_bitfield{ n };
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
bool* const flags = new bool[n];
|
|
|
|
for (tr_piece_index_t piece = 0; piece < n; ++piece)
|
2021-10-14 19:26:38 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
flags[piece] = hasPiece(piece);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
pieces.setFromBools(flags, n);
|
|
|
|
delete[] flags;
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return pieces.raw();
|
2009-01-02 17:01:55 +00:00
|
|
|
}
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
/// mutators
|
|
|
|
|
|
|
|
void tr_completion::addBlock(tr_block_index_t block)
|
2009-01-12 21:59:53 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (hasBlock(block))
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
return; // already had it
|
2012-12-15 04:28:19 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
blocks_.set(block);
|
|
|
|
size_now_ += block_info_->countBytesInBlock(block);
|
|
|
|
|
|
|
|
has_valid_.reset();
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void tr_completion::setBlocks(tr_bitfield blocks)
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
TR_ASSERT(std::size(blocks_) == std::size(blocks));
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
blocks_ = std::move(blocks);
|
|
|
|
size_now_ = countHasBytesInSpan({ 0, tr_block_index_t(std::size(blocks_)) });
|
|
|
|
size_when_done_.reset();
|
|
|
|
has_valid_.reset();
|
|
|
|
}
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void tr_completion::addPiece(tr_piece_index_t piece)
|
|
|
|
{
|
|
|
|
auto const [begin, end] = block_info_->blockSpanForPiece(piece);
|
2012-12-15 04:28:19 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
for (tr_block_index_t block = begin; block < end; ++block)
|
2012-12-15 04:28:19 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
addBlock(block);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
2009-01-12 21:59:53 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
void tr_completion::removePiece(tr_piece_index_t piece)
|
|
|
|
{
|
|
|
|
auto const [begin, end] = block_info_->blockSpanForPiece(piece);
|
|
|
|
size_now_ -= countHasBytesInSpan(block_info_->blockSpanForPiece(piece));
|
|
|
|
has_valid_.reset();
|
|
|
|
blocks_.unsetSpan(begin, end);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2011-01-31 16:43:37 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
uint64_t tr_completion::countHasBytesInSpan(tr_block_span_t span) const
|
2011-03-28 16:31:05 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
auto const [begin, end] = span;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
auto n = blocks_.count(begin, end);
|
|
|
|
n *= block_info_->block_size;
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
if (end == block_info_->n_blocks && blocks_.test(end - 1))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
n -= block_info_->block_size - block_info_->final_block_size;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 19:26:38 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return n;
|
2009-01-12 21:59:53 +00:00
|
|
|
}
|