2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-08-08 18:05:39 +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
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifndef LIBTRANSMISSION_PEER_MODULE
|
|
|
|
#error only the libtransmission peer module should #include this header.
|
|
|
|
#endif
|
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <cstddef> // size_t
|
2024-02-05 06:14:34 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "libtransmission/transmission.h"
|
2021-11-19 18:37:38 +00:00
|
|
|
|
2024-02-05 06:14:34 +00:00
|
|
|
#include "libtransmission/observable.h"
|
|
|
|
#include "libtransmission/utils.h"
|
|
|
|
|
|
|
|
class tr_bitfield;
|
|
|
|
|
2021-11-19 18:37:38 +00:00
|
|
|
/**
|
|
|
|
* Figures out what blocks we want to request next.
|
|
|
|
*/
|
|
|
|
class Wishlist
|
|
|
|
{
|
|
|
|
public:
|
2023-06-25 17:33:32 +00:00
|
|
|
static auto constexpr EndgameMaxPeers = size_t{ 2U };
|
2024-02-05 06:14:34 +00:00
|
|
|
static auto constexpr NormalMaxPeers = size_t{ 1U };
|
2023-06-25 17:33:32 +00:00
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
struct Mediator
|
2021-11-19 18:37:38 +00:00
|
|
|
{
|
2024-02-05 06:14:34 +00:00
|
|
|
[[nodiscard]] virtual bool client_has_block(tr_block_index_t block) const = 0;
|
|
|
|
[[nodiscard]] virtual bool client_wants_piece(tr_piece_index_t piece) const = 0;
|
|
|
|
[[nodiscard]] virtual bool is_endgame() const = 0;
|
|
|
|
[[nodiscard]] virtual bool is_sequential_download() const = 0;
|
|
|
|
[[nodiscard]] virtual size_t count_active_requests(tr_block_index_t block) const = 0;
|
|
|
|
[[nodiscard]] virtual size_t count_missing_blocks(tr_piece_index_t piece) 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&>::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_piece_index_t, tr_block_index_t>::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_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_sequential_download_changed(
|
|
|
|
libtransmission::SimpleObservable<tr_torrent*, bool>::Observer observer) = 0;
|
|
|
|
|
2022-02-22 15:09:24 +00:00
|
|
|
virtual ~Mediator() = default;
|
2021-11-19 18:37:38 +00:00
|
|
|
};
|
|
|
|
|
2024-02-05 06:14:34 +00:00
|
|
|
explicit Wishlist(std::unique_ptr<Mediator> mediator_in);
|
2024-02-12 03:13:59 +00:00
|
|
|
~Wishlist();
|
2023-03-22 15:24:10 +00:00
|
|
|
|
|
|
|
// the next blocks that we should request from a peer
|
2024-02-05 06:14:34 +00:00
|
|
|
[[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);
|
|
|
|
|
2023-03-22 15:24:10 +00:00
|
|
|
private:
|
2024-02-12 03:13:59 +00:00
|
|
|
class Impl;
|
|
|
|
std::unique_ptr<Impl> impl_;
|
2021-11-19 18:37:38 +00:00
|
|
|
};
|