1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-03 05:25:52 +00:00
transmission/libtransmission/peer-mgr-wishlist.h
Yat Ho 7e4b4f10a1
refactor: faster wishlist (#7027)
* chore: housekeeping

* perf: short circuit peer has block check

* refactor: track active requests in each respective peer

* refactor: swap `ActiveRequests` with new request tracking method

* refactor: use bitfield to store active requests per peer

* perf: check active request numbers first

* refactor: initialise candidate values in constructor

* refactor: better naming

* refactor: use `find_by_block()` more

* refactor: store wishlist mediator in swarm object

* test: make it compile

* test: update endgame test

* test: new test for choke event

* test: remove redundant lines

* test: new test for request event

* test: new test for reject event

* refactor: cache block have state in wishlist

* test: fix `gotBlockResortsPiece`

* fixup! refactor: track active requests in each respective peer

* fixup! test: fix `gotBlockResortsPiece`

* fix: count webseeds when calculating active requests

* build: update xcode project

* fix: add missing `candidates_dirty_` checks

* chore: remove old `depends-on` comments

* fixup! refactor: use bitfield to store active requests per peer

* refactor: extract block peer event to separate function

* perf: reorder conditions by overhead

* perf: check for completed block instead of completed piece

* chore: remove duplicated "unrequested piece" check

* refactor: merge similar block size sanity check

* refactor: use map to store number of requests in wishlist

* refactor: add asserts

* refactor: flush write buffer as soon as there is new data

* refactor: more accurate function naming

* fix: account for corrupt pieces in wishlist

* fix: account for unaligned blocks in wishlist

* Revert "fix: account for unaligned blocks in wishlist"

This reverts commit c3fce93cbae49c11d62e26caccedf55c1987aa95.

* fixup! refactor: use map to store number of requests in wishlist

* fix: account for unaligned blocks in wishlist v2

* chore: add `[[nodiscard]]`

* fixup! fix: account for unaligned blocks in wishlist v2

* fix: crash when handshake finishes in the middle of function
2024-11-11 19:30:00 -06:00

89 lines
4.4 KiB
C++

// This file Copyright © Mnemosyne LLC.
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#pragma once
#ifndef LIBTRANSMISSION_PEER_MODULE
#error only the libtransmission peer module should #include this header.
#endif
#include <cstddef> // size_t
#include <functional>
#include <memory>
#include <vector>
#include "libtransmission/transmission.h"
#include "libtransmission/observable.h"
#include "libtransmission/utils.h"
class tr_bitfield;
struct tr_peer;
/**
* Figures out what blocks we want to request next.
*/
class Wishlist
{
public:
static auto constexpr EndgameMaxPeers = size_t{ 2U };
static auto constexpr NormalMaxPeers = size_t{ 1U };
struct Mediator
{
[[nodiscard]] virtual bool client_has_block(tr_block_index_t block) const = 0;
[[nodiscard]] virtual bool client_has_piece(tr_piece_index_t piece) const = 0;
[[nodiscard]] virtual bool client_wants_piece(tr_piece_index_t piece) const = 0;
[[nodiscard]] virtual bool is_sequential_download() const = 0;
[[nodiscard]] virtual uint8_t count_active_requests(tr_block_index_t block) const = 0;
[[nodiscard]] virtual size_t count_piece_replication(tr_piece_index_t piece) const = 0;
[[nodiscard]] virtual tr_block_span_t block_span(tr_piece_index_t piece) const = 0;
[[nodiscard]] virtual tr_piece_index_t piece_count() const = 0;
[[nodiscard]] virtual tr_priority_t priority(tr_piece_index_t piece) const = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_peer_disconnect(
libtransmission::SimpleObservable<tr_torrent*, tr_bitfield const&, tr_bitfield const&>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_got_bad_piece(
libtransmission::SimpleObservable<tr_torrent*, tr_piece_index_t>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_got_bitfield(
libtransmission::SimpleObservable<tr_torrent*, tr_bitfield const&>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_got_block(
libtransmission::SimpleObservable<tr_torrent*, tr_block_index_t>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_got_choke(
libtransmission::SimpleObservable<tr_torrent*, tr_bitfield const&>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_got_have(
libtransmission::SimpleObservable<tr_torrent*, tr_piece_index_t>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_got_have_all(
libtransmission::SimpleObservable<tr_torrent*>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_got_reject(
libtransmission::SimpleObservable<tr_torrent*, tr_peer*, tr_block_index_t>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_piece_completed(
libtransmission::SimpleObservable<tr_torrent*, tr_piece_index_t>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_priority_changed(
libtransmission::SimpleObservable<tr_torrent*, tr_file_index_t const*, tr_file_index_t, tr_priority_t>::Observer
observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_sent_cancel(
libtransmission::SimpleObservable<tr_torrent*, tr_peer*, tr_block_index_t>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_sent_request(
libtransmission::SimpleObservable<tr_torrent*, tr_peer*, tr_block_span_t>::Observer observer) = 0;
[[nodiscard]] virtual libtransmission::ObserverTag observe_sequential_download_changed(
libtransmission::SimpleObservable<tr_torrent*, bool>::Observer observer) = 0;
virtual ~Mediator() = default;
};
explicit Wishlist(Mediator& mediator_in);
~Wishlist();
// the next blocks that we should request from a peer
[[nodiscard]] std::vector<tr_block_span_t> next(
size_t n_wanted_blocks,
std::function<bool(tr_piece_index_t)> const& peer_has_piece,
std::function<bool(tr_block_index_t)> const& has_active_pending_to_peer);
private:
class Impl;
std::unique_ptr<Impl> impl_;
};