2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2021-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +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.
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
2022-06-20 04:08:58 +00:00
|
|
|
#include <set>
|
2021-11-19 18:37:38 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define LIBTRANSMISSION_PEER_MODULE
|
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/crypto-utils.h" // for tr_salt_shaker
|
|
|
|
#include "libtransmission/peer-mgr-wishlist.h"
|
|
|
|
#include "libtransmission/tr-assert.h"
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2023-04-14 16:47:54 +00:00
|
|
|
using SaltType = tr_piece_index_t;
|
2021-11-19 18:37:38 +00:00
|
|
|
|
|
|
|
struct Candidate
|
|
|
|
{
|
|
|
|
tr_piece_index_t piece;
|
|
|
|
size_t n_blocks_missing;
|
|
|
|
tr_priority_t priority;
|
2023-04-14 16:47:54 +00:00
|
|
|
SaltType salt;
|
2021-11-19 18:37:38 +00:00
|
|
|
|
2023-04-14 16:47:54 +00:00
|
|
|
Candidate(tr_piece_index_t piece_in, size_t missing_in, tr_priority_t priority_in, SaltType salt_in)
|
2021-11-19 18:37:38 +00:00
|
|
|
: piece{ piece_in }
|
|
|
|
, n_blocks_missing{ missing_in }
|
|
|
|
, priority{ priority_in }
|
|
|
|
, salt{ salt_in }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-23 22:47:41 +00:00
|
|
|
[[nodiscard]] int compare(Candidate const& that) const // <=>
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
// prefer pieces closer to completion
|
|
|
|
if (n_blocks_missing != that.n_blocks_missing)
|
|
|
|
{
|
|
|
|
return n_blocks_missing < that.n_blocks_missing ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefer higher priority
|
|
|
|
if (priority != that.priority)
|
|
|
|
{
|
|
|
|
return priority > that.priority ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (salt != that.salt)
|
|
|
|
{
|
|
|
|
return salt < that.salt ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(Candidate const& that) const // less than
|
|
|
|
{
|
|
|
|
return compare(that) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
std::vector<Candidate> getCandidates(Wishlist::Mediator const& mediator)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
// count up the pieces that we still want
|
|
|
|
auto wanted_pieces = std::vector<std::pair<tr_piece_index_t, size_t>>{};
|
2022-02-22 15:09:24 +00:00
|
|
|
auto const n_pieces = mediator.countAllPieces();
|
2021-11-19 18:37:38 +00:00
|
|
|
wanted_pieces.reserve(n_pieces);
|
|
|
|
for (tr_piece_index_t i = 0; i < n_pieces; ++i)
|
|
|
|
{
|
2022-02-22 15:09:24 +00:00
|
|
|
if (!mediator.clientCanRequestPiece(i))
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
size_t const n_missing = mediator.countMissingBlocks(i);
|
2021-11-19 18:37:38 +00:00
|
|
|
if (n_missing == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
wanted_pieces.emplace_back(i, n_missing);
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform them into candidates
|
2023-04-14 16:47:54 +00:00
|
|
|
auto salter = tr_salt_shaker<SaltType>{};
|
2021-11-19 18:37:38 +00:00
|
|
|
auto const n = std::size(wanted_pieces);
|
|
|
|
auto candidates = std::vector<Candidate>{};
|
2023-04-14 16:47:54 +00:00
|
|
|
auto const is_sequential = mediator.isSequentialDownload();
|
2021-11-19 18:37:38 +00:00
|
|
|
candidates.reserve(n);
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
{
|
|
|
|
auto const [piece, n_missing] = wanted_pieces[i];
|
2023-04-14 16:47:54 +00:00
|
|
|
auto const salt = is_sequential ? piece : salter();
|
|
|
|
candidates.emplace_back(piece, n_missing, mediator.priority(piece), salt);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return candidates;
|
|
|
|
}
|
|
|
|
|
2021-11-28 01:58:35 +00:00
|
|
|
std::vector<tr_block_span_t> makeSpans(tr_block_index_t const* sorted_blocks, size_t n_blocks)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
if (n_blocks == 0)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
auto spans = std::vector<tr_block_span_t>{};
|
|
|
|
auto cur = tr_block_span_t{ sorted_blocks[0], sorted_blocks[0] + 1 };
|
2021-11-19 18:37:38 +00:00
|
|
|
for (size_t i = 1; i < n_blocks; ++i)
|
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
if (cur.end == sorted_blocks[i])
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
++cur.end;
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
spans.push_back(cur);
|
|
|
|
cur = tr_block_span_t{ sorted_blocks[i], sorted_blocks[i] + 1 };
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-25 18:26:51 +00:00
|
|
|
spans.push_back(cur);
|
2021-11-19 18:37:38 +00:00
|
|
|
|
2021-11-25 18:26:51 +00:00
|
|
|
return spans;
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-03-22 15:24:10 +00:00
|
|
|
std::vector<tr_block_span_t> Wishlist::next(size_t n_wanted_blocks)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2022-02-12 22:52:40 +00:00
|
|
|
if (n_wanted_blocks == 0)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-03-22 15:24:10 +00:00
|
|
|
auto candidates = getCandidates(mediator_);
|
2023-04-14 16:47:54 +00:00
|
|
|
|
|
|
|
// We usually won't need all the candidates to be sorted until endgame, so don't
|
|
|
|
// waste cycles sorting all of them here. partial sort is enough.
|
2021-11-19 18:37:38 +00:00
|
|
|
auto constexpr MaxSortedPieces = size_t{ 30 };
|
|
|
|
auto const middle = std::min(std::size(candidates), MaxSortedPieces);
|
|
|
|
std::partial_sort(std::begin(candidates), std::begin(candidates) + middle, std::end(candidates));
|
|
|
|
|
2022-06-20 04:08:58 +00:00
|
|
|
auto blocks = std::set<tr_block_index_t>{};
|
2021-11-19 18:37:38 +00:00
|
|
|
for (auto const& candidate : candidates)
|
|
|
|
{
|
|
|
|
// do we have enough?
|
2022-06-20 04:08:58 +00:00
|
|
|
if (std::size(blocks) >= n_wanted_blocks)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// walk the blocks in this piece
|
2023-03-22 15:24:10 +00:00
|
|
|
auto const [begin, end] = mediator_.blockSpan(candidate.piece);
|
2022-06-20 04:08:58 +00:00
|
|
|
for (tr_block_index_t block = begin; block < end && std::size(blocks) < n_wanted_blocks; ++block)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
// don't request blocks we've already got
|
2023-03-22 15:24:10 +00:00
|
|
|
if (!mediator_.clientCanRequestBlock(block))
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't request from too many peers
|
2023-03-22 15:24:10 +00:00
|
|
|
size_t const n_peers = mediator_.countActiveRequests(block);
|
|
|
|
if (size_t const max_peers = mediator_.isEndgame() ? 2 : 1; n_peers >= max_peers)
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-20 04:08:58 +00:00
|
|
|
blocks.insert(block);
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 04:08:58 +00:00
|
|
|
auto const blocks_v = std::vector<tr_block_index_t>{ std::begin(blocks), std::end(blocks) };
|
|
|
|
return makeSpans(std::data(blocks_v), std::size(blocks_v));
|
2021-11-19 18:37:38 +00:00
|
|
|
}
|