2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
2003-10-23 01:00:57 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifndef TORRENT_PIECE_PICKER_HPP_INCLUDED
|
|
|
|
#define TORRENT_PIECE_PICKER_HPP_INCLUDED
|
|
|
|
|
2015-04-18 04:33:39 +02:00
|
|
|
// heavy weight reference counting invariant checks
|
|
|
|
//#define TORRENT_DEBUG_REFCOUNTS
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
2007-03-15 23:03:56 +01:00
|
|
|
#include <utility>
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint>
|
2016-06-20 17:32:06 +02:00
|
|
|
#include <tuple>
|
2013-01-06 05:02:29 +01:00
|
|
|
#include <set>
|
2018-08-19 11:08:09 +02:00
|
|
|
#include <unordered_map>
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-04-18 04:33:39 +02:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
#include "libtorrent/time.hpp"
|
2016-09-24 17:46:56 +02:00
|
|
|
#include "libtorrent/piece_block.hpp"
|
2016-12-22 16:42:33 +01:00
|
|
|
#include "libtorrent/aux_/vector.hpp"
|
2018-03-24 13:02:42 +01:00
|
|
|
#include "libtorrent/aux_/array.hpp"
|
2018-11-05 01:23:13 +01:00
|
|
|
#include "libtorrent/span.hpp"
|
2017-07-23 09:55:40 +02:00
|
|
|
#include "libtorrent/alert_types.hpp" // for picker_flags_t
|
2017-10-29 00:44:40 +02:00
|
|
|
#include "libtorrent/download_priority.hpp"
|
2018-01-28 14:39:43 +01:00
|
|
|
#include "libtorrent/flags.hpp"
|
2018-03-24 13:02:42 +01:00
|
|
|
#include "libtorrent/units.hpp"
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
class torrent;
|
|
|
|
class peer_connection;
|
2016-12-22 16:42:33 +01:00
|
|
|
template <typename Index>
|
|
|
|
struct typed_bitfield;
|
2014-07-06 21:18:00 +02:00
|
|
|
struct counters;
|
2015-08-18 10:25:13 +02:00
|
|
|
struct torrent_peer;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2018-03-22 23:28:55 +01:00
|
|
|
using prio_index_t = aux::strong_typedef<int, struct prio_index_tag_t>;
|
|
|
|
using picker_options_t = flags::bitfield_flag<std::uint16_t, struct picker_options_tag>;
|
2018-03-24 13:02:42 +01:00
|
|
|
using download_queue_t = aux::strong_typedef<std::uint8_t, struct dl_queue_tag>;
|
2019-08-05 01:21:02 +02:00
|
|
|
using piece_extent_t = aux::strong_typedef<int, struct piece_extent_tag>;
|
2018-01-28 14:39:43 +01:00
|
|
|
|
2018-08-07 22:23:13 +02:00
|
|
|
struct piece_count
|
|
|
|
{
|
|
|
|
// the number of pieces included in the "set"
|
|
|
|
int num_pieces;
|
|
|
|
// the number of blocks, out of those pieces, that are pad
|
|
|
|
// blocks (i.e. entirely part of pad files)
|
|
|
|
int pad_blocks;
|
|
|
|
// true if the last piece is part of the set
|
|
|
|
bool last_piece;
|
|
|
|
};
|
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT piece_picker
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2018-09-04 13:46:09 +02:00
|
|
|
// only defined when TORRENT_PICKER_LOG is defined, used for debugging
|
|
|
|
// unit tests
|
|
|
|
friend void print_pieces(piece_picker const& p);
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
public:
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// the number of priority levels
|
|
|
|
priority_levels = 8,
|
|
|
|
// priority factor
|
2019-11-24 15:08:29 +01:00
|
|
|
prio_factor = 3,
|
|
|
|
// max blocks per piece
|
|
|
|
// there are counters in downloading_piece that only have 15 bits to
|
|
|
|
// count blocks per piece, that's restricting this
|
|
|
|
max_blocks_per_piece = (1 << 15) - 1
|
2008-09-06 23:04:57 +02:00
|
|
|
};
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
struct block_info
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
block_info(): num_peers(0), state(state_none) {}
|
2003-11-02 22:06:50 +01:00
|
|
|
// the peer this block was requested or
|
2015-08-18 10:25:13 +02:00
|
|
|
// downloaded from.
|
2016-12-22 16:42:33 +01:00
|
|
|
torrent_peer* peer = nullptr;
|
2007-07-06 19:15:35 +02:00
|
|
|
// the number of peers that has this block in their
|
|
|
|
// download or request queues
|
|
|
|
unsigned num_peers:14;
|
|
|
|
// the state of this block
|
2007-06-10 22:46:09 +02:00
|
|
|
enum { state_none, state_requested, state_writing, state_finished };
|
|
|
|
unsigned state:2;
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2012-04-12 07:00:20 +02:00
|
|
|
// to allow verifying the invariant of blocks belonging to the right piece
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t piece_index{-1};
|
2015-08-18 10:25:13 +02:00
|
|
|
std::set<torrent_peer*> peers;
|
2012-04-12 07:00:20 +02:00
|
|
|
#endif
|
2003-11-02 22:06:50 +01:00
|
|
|
};
|
|
|
|
|
2018-01-28 14:39:43 +01:00
|
|
|
// pick rarest first
|
|
|
|
static constexpr picker_options_t rarest_first = 0_bit;
|
|
|
|
|
|
|
|
// pick the most common first, or the last pieces if sequential
|
|
|
|
static constexpr picker_options_t reverse = 1_bit;
|
|
|
|
|
|
|
|
// only pick pieces exclusively requested from this peer
|
|
|
|
static constexpr picker_options_t on_parole = 2_bit;
|
|
|
|
|
|
|
|
// always pick partial pieces before any other piece
|
|
|
|
static constexpr picker_options_t prioritize_partials = 3_bit;
|
|
|
|
|
|
|
|
// pick pieces in sequential order
|
|
|
|
static constexpr picker_options_t sequential = 4_bit;
|
|
|
|
|
|
|
|
// treat pieces with priority 6 and below as filtered
|
|
|
|
// to trigger end-game mode until all prio 7 pieces are
|
|
|
|
// completed
|
|
|
|
static constexpr picker_options_t time_critical_mode = 5_bit;
|
|
|
|
|
|
|
|
// only expands pieces (when prefer contiguous blocks is set)
|
|
|
|
// within properly aligned ranges, not the largest possible
|
|
|
|
// range of pieces.
|
|
|
|
static constexpr picker_options_t align_expanded_pieces = 6_bit;
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2019-08-05 01:21:02 +02:00
|
|
|
// this will create an affinity to pick pieces in extents of 4 MiB, in an
|
|
|
|
// attempt to improve disk I/O by picking ranges of pieces (if pieces are
|
|
|
|
// small)
|
|
|
|
static constexpr picker_options_t piece_extent_affinity = 7_bit;
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
struct downloading_piece
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
downloading_piece()
|
|
|
|
: finished(0)
|
2015-02-08 22:17:00 +01:00
|
|
|
, passed_hash_check(0)
|
|
|
|
, writing(0)
|
|
|
|
, locked(0)
|
|
|
|
, requested(0)
|
|
|
|
, outstanding_hash_check(0) {}
|
2011-02-21 06:24:41 +01:00
|
|
|
|
2011-08-16 11:22:41 +02:00
|
|
|
bool operator<(downloading_piece const& rhs) const { return index < rhs.index; }
|
|
|
|
|
2013-10-03 17:39:19 +02:00
|
|
|
// the index of the piece
|
2017-11-12 13:24:27 +01:00
|
|
|
piece_index_t index{(std::numeric_limits<std::int32_t>::max)()};
|
2015-02-14 22:32:41 +01:00
|
|
|
|
|
|
|
// info about each block in this piece. this is an index into the
|
|
|
|
// m_block_info array, when multiplied by m_blocks_per_piece.
|
|
|
|
// The m_blocks_per_piece following entries contain information about
|
|
|
|
// all blocks in this piece.
|
2017-11-12 13:24:27 +01:00
|
|
|
std::uint16_t info_idx{(std::numeric_limits<std::uint16_t>::max)()};
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
// the number of blocks in the finished state
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint16_t finished:15;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// set to true when the hash check job
|
|
|
|
// returns with a valid hash for this piece.
|
|
|
|
// we might not 'have' the piece yet though,
|
|
|
|
// since it might not have been written to
|
|
|
|
// disk. This is not set of locked is
|
|
|
|
// set.
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint16_t passed_hash_check:1;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-02-08 22:17:00 +01:00
|
|
|
// the number of blocks in the writing state
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint16_t writing:15;
|
2015-02-08 22:17:00 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// when this is set, blocks from this piece may
|
|
|
|
// not be picked. This is used when the hash check
|
|
|
|
// fails or writing to the disk fails, while waiting
|
|
|
|
// to synchronize the disk thread and clear out any
|
|
|
|
// remaining state. Once this synchronization is
|
|
|
|
// done, restore_piece() is called to clear the
|
|
|
|
// locked flag.
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint16_t locked:1;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
// the number of blocks in the requested state
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint16_t requested:15;
|
2013-10-03 17:39:19 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// set to true while there is an outstanding
|
|
|
|
// hash check for this piece
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint16_t outstanding_hash_check:1;
|
2003-11-02 22:06:50 +01:00
|
|
|
};
|
2015-06-30 02:40:32 +02:00
|
|
|
|
2017-10-25 22:31:49 +02:00
|
|
|
piece_picker(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces);
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void get_availability(aux::vector<int, piece_index_t>& avail) const;
|
|
|
|
int get_availability(piece_index_t piece) const;
|
2007-05-30 08:52:59 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// increases the peer count for the given piece
|
2008-01-31 18:52:29 +01:00
|
|
|
// (is used when a HAVE message is received)
|
2016-12-22 16:42:33 +01:00
|
|
|
void inc_refcount(piece_index_t index, const torrent_peer* peer);
|
|
|
|
void dec_refcount(piece_index_t index, const torrent_peer* peer);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
// increases the peer count for the given piece
|
|
|
|
// (is used when a BITFIELD message is received)
|
2016-12-22 16:42:33 +01:00
|
|
|
void inc_refcount(typed_bitfield<piece_index_t> const& bitmask
|
|
|
|
, const torrent_peer* peer);
|
2003-12-18 04:30:41 +01:00
|
|
|
// decreases the peer count for the given piece
|
|
|
|
// (used when a peer disconnects)
|
2016-12-22 16:42:33 +01:00
|
|
|
void dec_refcount(typed_bitfield<piece_index_t> const& bitmask
|
|
|
|
, const torrent_peer* peer);
|
2015-06-30 02:40:32 +02:00
|
|
|
|
2007-04-15 04:14:02 +02:00
|
|
|
// these will increase and decrease the peer count
|
|
|
|
// of all pieces. They are used when seeds join
|
|
|
|
// or leave the swarm.
|
2015-08-18 10:25:13 +02:00
|
|
|
void inc_refcount_all(const torrent_peer* peer);
|
|
|
|
void dec_refcount_all(const torrent_peer* peer);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2020-01-30 14:24:05 +01:00
|
|
|
// we have every piece. This is used when creating a piece picker for a
|
|
|
|
// seed
|
|
|
|
void we_have_all();
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// This indicates that we just received this piece
|
|
|
|
// it means that the refcounter will indicate that
|
|
|
|
// we are not interested in this piece anymore
|
|
|
|
// (i.e. we don't have to maintain a refcount)
|
2016-12-22 16:42:33 +01:00
|
|
|
void we_have(piece_index_t index);
|
|
|
|
void we_dont_have(piece_index_t index);
|
2008-06-07 04:58:28 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// the lowest piece index we do not have
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t cursor() const { return m_cursor; }
|
2015-06-30 02:40:32 +02:00
|
|
|
|
2015-02-15 03:17:57 +01:00
|
|
|
// one past the last piece we do not have.
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t reverse_cursor() const { return m_reverse_cursor; }
|
2015-02-15 03:17:57 +01:00
|
|
|
|
2008-08-26 00:32:50 +02:00
|
|
|
// sets all pieces to dont-have
|
2017-10-25 22:31:49 +02:00
|
|
|
void resize(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces);
|
2008-06-07 04:58:28 +02:00
|
|
|
int num_pieces() const { return int(m_piece_map.size()); }
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
bool have_piece(piece_index_t index) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
bool is_downloading(piece_index_t index) const
|
2012-09-11 07:27:14 +02:00
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_ASSERT(index >= piece_index_t(0));
|
|
|
|
TORRENT_ASSERT(index < m_piece_map.end_index());
|
2012-09-11 07:27:14 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_pos const& p = m_piece_map[index];
|
2014-07-06 21:18:00 +02:00
|
|
|
return p.downloading();
|
2012-09-11 07:27:14 +02:00
|
|
|
}
|
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// sets the priority of a piece.
|
2007-08-10 23:13:35 +02:00
|
|
|
// returns true if the priority was changed from 0 to non-0
|
|
|
|
// or vice versa
|
2017-10-29 00:44:40 +02:00
|
|
|
bool set_piece_priority(piece_index_t index, download_priority_t prio);
|
2005-05-25 12:01:01 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// returns the priority for the piece at 'index'
|
2017-10-29 00:44:40 +02:00
|
|
|
download_priority_t piece_priority(piece_index_t index) const;
|
2005-05-25 12:01:01 +02:00
|
|
|
|
2007-03-20 02:59:00 +01:00
|
|
|
// returns the current piece priorities for all pieces
|
2017-10-29 00:44:40 +02:00
|
|
|
void piece_priorities(std::vector<download_priority_t>& pieces) const;
|
2007-03-20 02:59:00 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// pieces should be the vector that represents the pieces a
|
|
|
|
// client has. It returns a list of all pieces that this client
|
|
|
|
// has and that are interesting to download. It returns them in
|
|
|
|
// priority order. It doesn't care about the download flag.
|
|
|
|
// The user of this function must lookup if any piece is
|
|
|
|
// marked as being downloaded. If the user of this function
|
|
|
|
// decides to download a piece, it must mark it as being downloaded
|
|
|
|
// itself, by using the mark_as_downloading() member function.
|
2004-11-30 12:17:32 +01:00
|
|
|
// THIS IS DONE BY THE peer_connection::send_request() MEMBER FUNCTION!
|
2014-07-06 21:18:00 +02:00
|
|
|
// The last argument is the torrent_peer pointer for the peer that
|
2007-07-04 04:16:49 +02:00
|
|
|
// we'll download from.
|
2015-01-20 03:34:55 +01:00
|
|
|
// prefer_contiguous_blocks indicates how many blocks we would like
|
|
|
|
// to request contiguously. The blocks are not merged by the piece
|
|
|
|
// picker, but may be coalesced later by the peer_connection.
|
|
|
|
// this feature is used by web_peer_connection to request larger blocks
|
|
|
|
// at a time to mitigate limited pipelining and lack of keep-alive
|
|
|
|
// (i.e. higher overhead per request).
|
2017-07-23 09:55:40 +02:00
|
|
|
picker_flags_t pick_pieces(typed_bitfield<piece_index_t> const& pieces
|
2008-09-06 23:04:57 +02:00
|
|
|
, std::vector<piece_block>& interesting_blocks, int num_blocks
|
2015-08-18 10:25:13 +02:00
|
|
|
, int prefer_contiguous_blocks, torrent_peer* peer
|
2018-01-28 14:39:43 +01:00
|
|
|
, picker_options_t options, std::vector<piece_index_t> const& suggested_pieces
|
2014-07-06 21:18:00 +02:00
|
|
|
, int num_peers
|
|
|
|
, counters& pc
|
|
|
|
) const;
|
2007-07-04 04:16:49 +02:00
|
|
|
|
2007-08-14 19:47:48 +02:00
|
|
|
// picks blocks from each of the pieces in the piece_list
|
|
|
|
// vector that is also in the piece bitmask. The blocks
|
|
|
|
// are added to interesting_blocks, and busy blocks are
|
|
|
|
// added to backup_blocks. num blocks is the number of
|
2007-09-05 23:21:11 +02:00
|
|
|
// blocks to be picked. Blocks are not picked from pieces
|
|
|
|
// that are being downloaded
|
2016-12-22 16:42:33 +01:00
|
|
|
int add_blocks(piece_index_t piece, typed_bitfield<piece_index_t> const& pieces
|
2007-08-14 19:47:48 +02:00
|
|
|
, std::vector<piece_block>& interesting_blocks
|
2008-09-06 23:04:57 +02:00
|
|
|
, std::vector<piece_block>& backup_blocks
|
|
|
|
, std::vector<piece_block>& backup_blocks2
|
2015-01-20 03:34:55 +01:00
|
|
|
, int num_blocks, int prefer_contiguous_blocks
|
2016-12-22 16:42:33 +01:00
|
|
|
, torrent_peer* peer, std::vector<piece_index_t> const& ignore
|
2018-01-28 14:39:43 +01:00
|
|
|
, picker_options_t options) const;
|
2007-09-05 23:21:11 +02:00
|
|
|
|
|
|
|
// picks blocks only from downloading pieces
|
2008-09-06 23:04:57 +02:00
|
|
|
int add_blocks_downloading(downloading_piece const& dp
|
2016-12-22 16:42:33 +01:00
|
|
|
, typed_bitfield<piece_index_t> const& pieces
|
2007-09-05 23:21:11 +02:00
|
|
|
, std::vector<piece_block>& interesting_blocks
|
|
|
|
, std::vector<piece_block>& backup_blocks
|
2008-09-06 23:04:57 +02:00
|
|
|
, std::vector<piece_block>& backup_blocks2
|
2015-01-20 03:34:55 +01:00
|
|
|
, int num_blocks, int prefer_contiguous_blocks
|
2015-08-18 10:25:13 +02:00
|
|
|
, torrent_peer* peer
|
2018-01-28 14:39:43 +01:00
|
|
|
, picker_options_t options) const;
|
2007-08-14 19:47:48 +02:00
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
// clears the peer pointer in all downloading pieces with this
|
|
|
|
// peer pointer
|
2015-08-18 10:25:13 +02:00
|
|
|
void clear_peer(torrent_peer* peer);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
// this is an invariant check
|
|
|
|
void check_peers();
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// returns true if any client is currently downloading this
|
|
|
|
// piece-block, or if it's queued for downloading by some client
|
2005-08-15 00:04:58 +02:00
|
|
|
// or if it already has been successfully downloaded
|
2007-06-10 22:46:09 +02:00
|
|
|
bool is_requested(piece_block block) const;
|
|
|
|
// returns true if the block has been downloaded
|
|
|
|
bool is_downloaded(piece_block block) const;
|
|
|
|
// returns true if the block has been downloaded and written to disk
|
2003-11-05 00:27:06 +01:00
|
|
|
bool is_finished(piece_block block) const;
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// marks this piece-block as queued for downloading
|
2015-01-26 03:04:58 +01:00
|
|
|
// options are flags from options_t.
|
2015-08-18 10:25:13 +02:00
|
|
|
bool mark_as_downloading(piece_block block, torrent_peer* peer
|
2018-01-28 14:39:43 +01:00
|
|
|
, picker_options_t options = {});
|
2015-01-26 03:04:58 +01:00
|
|
|
|
2010-05-01 19:47:28 +02:00
|
|
|
// returns true if the block was marked as writing,
|
|
|
|
// and false if the block is already finished or writing
|
2015-08-18 10:25:13 +02:00
|
|
|
bool mark_as_writing(piece_block block, torrent_peer* peer);
|
2010-05-01 19:47:28 +02:00
|
|
|
|
2015-08-18 10:25:13 +02:00
|
|
|
void mark_as_canceled(piece_block block, torrent_peer* peer);
|
|
|
|
void mark_as_finished(piece_block block, torrent_peer* peer);
|
2016-08-07 22:05:20 +02:00
|
|
|
|
2018-01-23 01:44:29 +01:00
|
|
|
void mark_as_pad(piece_block block);
|
2018-01-29 10:20:45 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// prevent blocks from being picked from this piece.
|
|
|
|
// to unlock the piece, call restore_piece() on it
|
2016-12-22 16:42:33 +01:00
|
|
|
void lock_piece(piece_index_t piece);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
void write_failed(piece_block block);
|
2007-07-06 19:15:35 +02:00
|
|
|
int num_peers(piece_block block) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void piece_passed(piece_index_t index);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2007-08-03 08:13:26 +02:00
|
|
|
// returns information about the given piece
|
2016-12-22 16:42:33 +01:00
|
|
|
void piece_info(piece_index_t index, piece_picker::downloading_piece& st) const;
|
2010-09-05 18:01:36 +02:00
|
|
|
|
2015-02-08 23:15:59 +01:00
|
|
|
struct piece_stats_t
|
2010-09-05 18:01:36 +02:00
|
|
|
{
|
2015-02-08 23:15:59 +01:00
|
|
|
int peer_count;
|
|
|
|
int priority;
|
|
|
|
bool have;
|
|
|
|
bool downloading;
|
|
|
|
};
|
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_stats_t piece_stats(piece_index_t index) const;
|
2010-09-05 18:01:36 +02:00
|
|
|
|
2005-02-23 17:56:32 +01:00
|
|
|
// if a piece had a hash-failure, it must be restored and
|
2003-11-02 22:06:50 +01:00
|
|
|
// made available for redownloading
|
2016-12-22 16:42:33 +01:00
|
|
|
void restore_piece(piece_index_t index);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// clears the given piece's download flag
|
|
|
|
// this means that this piece-block can be picked again
|
2017-09-28 10:11:20 +02:00
|
|
|
void abort_download(piece_block block, torrent_peer* peer = nullptr);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// returns true if all blocks in this piece are finished
|
|
|
|
// or if we have the piece
|
2016-12-22 16:42:33 +01:00
|
|
|
bool is_piece_finished(piece_index_t index) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// returns true if we have the piece or if the piece
|
|
|
|
// has passed the hash check
|
2016-12-22 16:42:33 +01:00
|
|
|
bool has_piece_passed(piece_index_t index) const;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2005-02-23 17:56:32 +01:00
|
|
|
// returns the number of blocks there is in the given piece
|
2016-12-22 16:42:33 +01:00
|
|
|
int blocks_in_piece(piece_index_t index) const;
|
2003-11-02 22:06:50 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// return the peer pointers to all peers that participated in
|
|
|
|
// this piece
|
2016-12-22 16:42:33 +01:00
|
|
|
void get_downloaders(std::vector<torrent_peer*>& d, piece_index_t index) const;
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<piece_picker::downloading_piece> get_download_queue() const;
|
|
|
|
int get_download_queue_size() const;
|
2003-11-02 22:06:50 +01:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
void get_download_queue_sizes(int* partial
|
|
|
|
, int* full, int* finished, int* zero_prio) const;
|
2012-06-24 22:57:31 +02:00
|
|
|
|
2015-08-18 10:25:13 +02:00
|
|
|
torrent_peer* get_downloader(piece_block block) const;
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2005-05-30 19:43:03 +02:00
|
|
|
|
2018-08-07 22:23:13 +02:00
|
|
|
// piece states
|
|
|
|
//
|
|
|
|
// have: -----------
|
|
|
|
// pieces: # # # # # # # # # # #
|
|
|
|
// filtered: -------
|
|
|
|
// pads blk: ^ ^ ^
|
|
|
|
//
|
|
|
|
// want-have: * * * *
|
|
|
|
// want: * * * * * * *
|
|
|
|
// total-have: * * * * * *
|
|
|
|
//
|
|
|
|
// we only care about:
|
|
|
|
// 1. pieces we have (less pad blocks we have)
|
|
|
|
// 2. pieces we have AND want (less pad blocks we have and want)
|
|
|
|
// 3. pieces we want (less pad blocks we want)
|
|
|
|
|
|
|
|
// number of pieces not filtered, as well as the number of
|
|
|
|
// blocks out of those pieces that are pad blocks.
|
|
|
|
// ``last_piece`` is set if the last piece is one of the
|
|
|
|
// pieces.
|
|
|
|
piece_count want() const;
|
|
|
|
|
|
|
|
// number of pieces we have out of the ones we have not filtered
|
|
|
|
piece_count have_want() const;
|
|
|
|
|
|
|
|
// number of pieces we have (regardless of whether they are filtered)
|
|
|
|
piece_count have() const;
|
2007-04-12 00:27:58 +02:00
|
|
|
|
2018-08-07 22:23:13 +02:00
|
|
|
piece_count all_pieces() const;
|
|
|
|
|
|
|
|
int pad_blocks_in_piece(piece_index_t const index) const;
|
2008-06-07 04:58:28 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// number of pieces whose hash has passed (but haven't necessarily
|
|
|
|
// been flushed to disk yet)
|
|
|
|
int num_passed() const { return m_num_passed; }
|
|
|
|
|
2019-09-04 09:55:50 +02:00
|
|
|
// return true if all the pieces we want have passed the hash check (but
|
|
|
|
// may not have been written to disk yet)
|
|
|
|
bool is_finished() const
|
|
|
|
{
|
|
|
|
// this expression warrants some explanation:
|
|
|
|
// if the number of pieces we *want* to download
|
|
|
|
// is less than or (more likely) equal to the number of pieces that
|
|
|
|
// have passed the hash check (discounting the pieces that have passed
|
|
|
|
// the check but then had their priority set to 0). Then we're
|
|
|
|
// finished. Note that any piece we *have* implies it's both passed the
|
|
|
|
// hash check *and* been written to disk.
|
|
|
|
// num_pieces() - m_num_filtered - m_num_have_filtered
|
|
|
|
// <= (num_passed() - m_num_have_filtered)
|
|
|
|
// this can be simplified. Note how m_num_have_filtered appears on both
|
|
|
|
// side of the equation.
|
|
|
|
//
|
|
|
|
return num_pieces() - m_num_filtered <= num_passed();
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2018-08-09 00:55:02 +02:00
|
|
|
bool is_seeding() const { return m_num_have == num_pieces(); }
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2012-07-02 05:27:32 +02:00
|
|
|
// the number of pieces we want and don't have
|
2014-07-06 21:18:00 +02:00
|
|
|
int num_want_left() const { return num_pieces() - m_num_have - m_num_filtered + m_num_have_filtered; }
|
2012-07-02 05:27:32 +02:00
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2014-07-06 21:18:00 +02:00
|
|
|
void check_piece_state() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
// used in debug mode
|
2016-12-26 17:25:50 +01:00
|
|
|
void verify_priority(prio_index_t start, prio_index_t end, int prio) const;
|
2007-09-10 01:46:28 +02:00
|
|
|
void verify_pick(std::vector<piece_block> const& picked
|
2016-12-22 16:42:33 +01:00
|
|
|
, typed_bitfield<piece_index_t> const& bits) const;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void check_peer_invariant(typed_bitfield<piece_index_t> const& have
|
|
|
|
, torrent_peer const* p) const;
|
2017-09-28 21:20:44 +02:00
|
|
|
void check_invariant(const torrent* t = nullptr) const;
|
2013-02-25 05:13:46 +01:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-03 03:10:11 +01:00
|
|
|
// functor that compares indices on downloading_pieces
|
|
|
|
struct has_index
|
|
|
|
{
|
2016-12-22 16:42:33 +01:00
|
|
|
explicit has_index(piece_index_t const i) : index(i)
|
|
|
|
{ TORRENT_ASSERT(i >= piece_index_t(0)); }
|
2016-11-27 14:46:53 +01:00
|
|
|
bool operator()(downloading_piece const& p) const
|
2004-01-03 03:10:11 +01:00
|
|
|
{ return p.index == index; }
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t const index;
|
2004-01-03 03:10:11 +01:00
|
|
|
};
|
|
|
|
|
2004-01-15 17:45:34 +01:00
|
|
|
int blocks_in_last_piece() const
|
|
|
|
{ return m_blocks_in_last_piece; }
|
|
|
|
|
2009-07-19 06:59:27 +02:00
|
|
|
std::pair<int, int> distributed_copies() const;
|
2004-08-05 15:56:26 +02:00
|
|
|
|
2015-02-14 22:32:41 +01:00
|
|
|
// return the array of block_info objects for a given downloading_piece.
|
|
|
|
// this array has m_blocks_per_piece elements in it
|
2018-11-05 01:23:13 +01:00
|
|
|
span<block_info const> blocks_for_piece(downloading_piece const& dp) const;
|
2015-02-14 22:32:41 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
private:
|
|
|
|
|
2019-08-05 01:21:02 +02:00
|
|
|
piece_extent_t extent_for(piece_index_t) const;
|
|
|
|
index_range<piece_index_t> extent_for(piece_extent_t) const;
|
|
|
|
|
|
|
|
void record_downloading_piece(piece_index_t const p);
|
|
|
|
|
2018-08-19 11:08:09 +02:00
|
|
|
int num_pad_blocks() const { return m_num_pad_blocks; }
|
2018-08-07 22:23:13 +02:00
|
|
|
|
2018-11-05 01:23:13 +01:00
|
|
|
span<block_info> mutable_blocks_for_piece(downloading_piece const& dp);
|
2017-03-05 05:45:07 +01:00
|
|
|
|
2016-06-20 17:32:06 +02:00
|
|
|
std::tuple<bool, bool, int, int> requested_from(
|
2015-02-14 22:32:41 +01:00
|
|
|
piece_picker::downloading_piece const& p
|
2015-08-18 10:25:13 +02:00
|
|
|
, int num_blocks_in_piece, torrent_peer* peer) const;
|
2015-02-14 22:32:41 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
bool can_pick(piece_index_t piece, typed_bitfield<piece_index_t> const& bitmask) const;
|
|
|
|
bool is_piece_free(piece_index_t piece, typed_bitfield<piece_index_t> const& bitmask) const;
|
|
|
|
std::pair<piece_index_t, piece_index_t>
|
|
|
|
expand_piece(piece_index_t piece, int whole_pieces
|
|
|
|
, typed_bitfield<piece_index_t> const& have
|
2018-01-28 14:39:43 +01:00
|
|
|
, picker_options_t options) const;
|
2007-09-03 23:16:24 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
struct piece_pos
|
|
|
|
{
|
|
|
|
piece_pos() {}
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_pos(int const peer_count_, int const index_)
|
2016-11-29 06:55:41 +01:00
|
|
|
: peer_count(static_cast<std::uint16_t>(peer_count_))
|
2018-03-24 13:02:42 +01:00
|
|
|
, download_state(static_cast<uint8_t>(piece_pos::piece_open))
|
2017-10-29 00:44:40 +02:00
|
|
|
, piece_priority(static_cast<std::uint8_t>(default_priority))
|
2016-11-29 06:55:41 +01:00
|
|
|
, index(index_)
|
2004-01-26 01:21:12 +01:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(peer_count_ >= 0);
|
2017-11-12 13:24:27 +01:00
|
|
|
TORRENT_ASSERT(peer_count_ < (std::numeric_limits<std::uint16_t>::max)());
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index_ >= 0);
|
2004-01-26 01:21:12 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2018-03-24 13:02:42 +01:00
|
|
|
// the piece is partially downloaded or requested
|
|
|
|
static constexpr download_queue_t piece_downloading{0};
|
|
|
|
|
|
|
|
// partial pieces where all blocks in the piece have been requested
|
|
|
|
static constexpr download_queue_t piece_full{1};
|
|
|
|
// partial pieces where all blocks in the piece have been received
|
|
|
|
// and are either finished or writing
|
|
|
|
static constexpr download_queue_t piece_finished{2};
|
|
|
|
// partial pieces whose priority is 0
|
|
|
|
static constexpr download_queue_t piece_zero_prio{3};
|
|
|
|
|
|
|
|
// the states up to this point indicate the piece is being
|
|
|
|
// downloaded (or at least has a partially downloaded piece
|
|
|
|
// in one of the m_downloads buckets).
|
|
|
|
static constexpr download_queue_t num_download_categories{4};
|
|
|
|
|
|
|
|
// the piece is open to be picked
|
|
|
|
static constexpr download_queue_t piece_open{4};
|
|
|
|
|
|
|
|
// this is not a new download category/download list bucket.
|
|
|
|
// it still goes into the piece_downloading bucket. However,
|
|
|
|
// it indicates that this piece only has outstanding requests
|
|
|
|
// from reverse peers. This is to de-prioritize it somewhat
|
|
|
|
static constexpr download_queue_t piece_downloading_reverse{5};
|
|
|
|
static constexpr download_queue_t piece_full_reverse{6};
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
// returns one of the valid download categories of state_t or
|
|
|
|
// piece_open if this piece is not being downloaded
|
2018-03-24 13:02:42 +01:00
|
|
|
download_queue_t download_queue() const
|
2015-01-26 03:04:58 +01:00
|
|
|
{
|
2018-03-24 13:02:42 +01:00
|
|
|
if (state() == piece_downloading_reverse)
|
2015-01-26 03:04:58 +01:00
|
|
|
return piece_downloading;
|
2018-03-24 13:02:42 +01:00
|
|
|
if (state() == piece_full_reverse)
|
2015-01-26 03:04:58 +01:00
|
|
|
return piece_full;
|
2018-03-24 13:02:42 +01:00
|
|
|
return state();
|
2015-01-26 03:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool reverse() const
|
|
|
|
{
|
2018-03-24 13:02:42 +01:00
|
|
|
return state() == piece_downloading_reverse
|
|
|
|
|| state() == piece_full_reverse;
|
2015-01-26 03:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void unreverse()
|
|
|
|
{
|
2018-03-24 13:02:42 +01:00
|
|
|
if (state() == piece_downloading_reverse)
|
|
|
|
state(piece_downloading);
|
|
|
|
else if (state() == piece_full_reverse)
|
|
|
|
state(piece_full);
|
2015-01-26 03:04:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void make_reverse()
|
|
|
|
{
|
2018-03-24 13:02:42 +01:00
|
|
|
if (state() == piece_downloading)
|
|
|
|
state(piece_downloading_reverse);
|
|
|
|
else if (state() == piece_full)
|
|
|
|
state(piece_full_reverse);
|
2015-01-26 03:04:58 +01:00
|
|
|
}
|
|
|
|
|
2007-07-15 17:41:55 +02:00
|
|
|
// the number of peers that has this piece
|
|
|
|
// (availability)
|
2016-11-29 23:14:14 +01:00
|
|
|
std::uint32_t peer_count : 26;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2020-01-30 14:24:05 +01:00
|
|
|
// one of the download_queue_t values. This indicates whether this piece
|
2015-01-26 03:04:58 +01:00
|
|
|
// is currently being downloaded or not, and what state it's in if
|
|
|
|
// it is. Specifically, as an optimization, pieces that have all blocks
|
|
|
|
// requested from them are separated out into separate lists to make
|
|
|
|
// lookups quicker. The main oddity is that whether a downloading piece
|
|
|
|
// has only been requested from peers that are reverse, that's
|
|
|
|
// recorded as piece_downloading_reverse, which really means the same
|
|
|
|
// as piece_downloading, it just saves space to also indicate that it
|
|
|
|
// has a bit lower priority. The reverse bit is only relevant if the
|
2016-04-06 22:40:42 +02:00
|
|
|
// state is piece_downloading.
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t download_state : 3;
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
// TODO: 2 having 8 priority levels is probably excessive. It should
|
|
|
|
// probably be changed to 3 levels + dont-download
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// is 0 if the piece is filtered (not to be downloaded)
|
2015-01-26 03:04:58 +01:00
|
|
|
// 1 is low priority
|
|
|
|
// 2 is low priority
|
|
|
|
// 3 is mid priority
|
|
|
|
// 4 is default priority
|
|
|
|
// 5 is mid priority
|
|
|
|
// 6 is high priority
|
|
|
|
// 7 is high priority
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint32_t piece_priority : 3;
|
2015-01-26 03:04:58 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// index in to the piece_info vector
|
2016-12-26 17:25:50 +01:00
|
|
|
prio_index_t index;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
// all the peers that have this piece
|
2015-08-18 10:25:13 +02:00
|
|
|
std::set<const torrent_peer*> have_peers;
|
2013-01-06 05:02:29 +01:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2016-12-26 17:25:50 +01:00
|
|
|
// index is set to this to indicate that we have the
|
|
|
|
// piece. There is no entry for the piece in the
|
|
|
|
// buckets if this is the case.
|
2017-08-03 12:57:15 +02:00
|
|
|
static constexpr prio_index_t we_have_index{-1};
|
2016-12-26 17:25:50 +01:00
|
|
|
|
2017-08-03 12:57:15 +02:00
|
|
|
// the priority value that means the piece is filtered
|
|
|
|
static constexpr std::uint32_t filter_priority = 0;
|
|
|
|
|
|
|
|
// the max number the peer count can hold
|
|
|
|
static constexpr std::uint32_t max_peer_count = 0xffff;
|
2015-08-18 10:25:13 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
bool have() const { return index == we_have_index; }
|
2007-10-05 02:30:00 +02:00
|
|
|
void set_have() { index = we_have_index; TORRENT_ASSERT(have()); }
|
2016-12-26 17:25:50 +01:00
|
|
|
void set_not_have() { index = prio_index_t(0); TORRENT_ASSERT(!have()); }
|
2018-03-24 13:02:42 +01:00
|
|
|
bool downloading() const { return state() != piece_open; }
|
2015-08-18 10:25:13 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
bool filtered() const { return piece_priority == filter_priority; }
|
2013-05-10 06:47:56 +02:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
// this function returns the effective priority of the piece. It's
|
|
|
|
// actually the sort order of this piece compared to other pieces. A
|
|
|
|
// lower index means it will be picked before a piece with a higher
|
2015-08-18 10:25:13 +02:00
|
|
|
// index.
|
2015-01-26 03:04:58 +01:00
|
|
|
// The availability of the piece (the number of peers that have this
|
|
|
|
// piece) is fundamentally controlling the priority. It's multiplied
|
|
|
|
// by 3 to form 3 levels of priority for each availability.
|
|
|
|
//
|
|
|
|
// downloading pieces (not reverse)
|
|
|
|
// | open pieces (not downloading)
|
|
|
|
// | | downloading pieces (reverse peers)
|
|
|
|
// | | |
|
|
|
|
// +---+---+---+
|
|
|
|
// | 0 | 1 | 2 |
|
|
|
|
// +---+---+---+
|
|
|
|
// this '3' is called prio_factor
|
2008-09-06 23:04:57 +02:00
|
|
|
//
|
2016-04-06 22:40:42 +02:00
|
|
|
// the manually set priority takes precedence over the availability
|
2015-01-26 03:04:58 +01:00
|
|
|
// by multiplying availability by priority.
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int priority(piece_picker const* picker) const
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// filtered pieces (prio = 0), pieces we have or pieces with
|
|
|
|
// availability = 0 should not be present in the piece list
|
|
|
|
// returning -1 indicates that they shouldn't.
|
2014-07-06 21:18:00 +02:00
|
|
|
if (filtered() || have() || peer_count + picker->m_seeds == 0
|
2018-03-24 13:02:42 +01:00
|
|
|
|| state() == piece_full
|
|
|
|
|| state() == piece_finished)
|
2008-01-31 18:52:29 +01:00
|
|
|
return -1;
|
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
TORRENT_ASSERT(piece_priority > 0);
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
// this is to keep downloading pieces at higher priority than
|
|
|
|
// pieces that are not being downloaded, and to make reverse
|
|
|
|
// downloading pieces to be lower priority
|
|
|
|
int adjustment = -2;
|
|
|
|
if (reverse()) adjustment = -1;
|
2018-03-24 13:02:42 +01:00
|
|
|
else if (state() != piece_open) adjustment = -3;
|
2015-01-26 03:04:58 +01:00
|
|
|
|
|
|
|
// the + 1 here is because peer_count count be 0, it m_seeds
|
|
|
|
// is > 0. We don't actually care about seeds (except for the
|
|
|
|
// first one) since the order of the pieces is unaffected.
|
|
|
|
int availability = int(peer_count) + 1;
|
|
|
|
TORRENT_ASSERT(availability > 0);
|
|
|
|
TORRENT_ASSERT(int(priority_levels - piece_priority) > 0);
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
return availability * int(priority_levels - piece_priority)
|
|
|
|
* prio_factor + adjustment;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2017-06-10 08:18:32 +02:00
|
|
|
bool operator!=(piece_pos const& p) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{ return index != p.index || peer_count != p.peer_count; }
|
|
|
|
|
2017-06-10 08:18:32 +02:00
|
|
|
bool operator==(piece_pos const& p) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{ return index == p.index && peer_count == p.peer_count; }
|
2018-03-24 13:02:42 +01:00
|
|
|
|
|
|
|
download_queue_t state() const { return download_queue_t(download_state); }
|
|
|
|
void state(download_queue_t q) { download_state = static_cast<std::uint8_t>(q); }
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifndef TORRENT_DEBUG_REFCOUNTS
|
2016-05-23 14:15:39 +02:00
|
|
|
static_assert(sizeof(piece_pos) == sizeof(char) * 8, "unexpected struct size");
|
2012-04-01 21:14:30 +02:00
|
|
|
#endif
|
2003-11-02 22:06:50 +01:00
|
|
|
|
2015-02-08 19:01:29 +01:00
|
|
|
bool partial_compare_rarest_first(downloading_piece const* lhs
|
2016-11-27 14:46:53 +01:00
|
|
|
, downloading_piece const* rhs) const;
|
2015-02-08 19:01:29 +01:00
|
|
|
|
2012-05-14 06:48:23 +02:00
|
|
|
void break_one_seed();
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
void update_pieces() const;
|
|
|
|
|
2016-12-26 17:25:50 +01:00
|
|
|
prio_index_t priority_begin(int prio) const;
|
|
|
|
prio_index_t priority_end(int prio) const;
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
// fills in the range [start, end) of pieces in
|
|
|
|
// m_pieces that have priority 'prio'
|
2016-12-26 17:25:50 +01:00
|
|
|
std::pair<prio_index_t, prio_index_t> priority_range(int prio) const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
// adds the piece 'index' to m_pieces
|
2016-12-22 16:42:33 +01:00
|
|
|
void add(piece_index_t index);
|
2008-01-31 18:52:29 +01:00
|
|
|
// removes the piece with the given priority and the
|
|
|
|
// elem_index in the m_pieces vector
|
2016-12-26 17:25:50 +01:00
|
|
|
void remove(int priority, prio_index_t elem_index);
|
2008-01-31 18:52:29 +01:00
|
|
|
// updates the position of the piece with the given
|
|
|
|
// priority and the elem_index in the m_pieces vector
|
2016-12-26 17:25:50 +01:00
|
|
|
void update(int priority, prio_index_t elem_index);
|
2008-01-31 18:52:29 +01:00
|
|
|
// shuffles the given piece inside it's priority range
|
2016-12-26 17:25:50 +01:00
|
|
|
void shuffle(int priority, prio_index_t elem_index);
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
std::vector<downloading_piece>::iterator add_download_piece(piece_index_t index);
|
2016-11-27 14:46:53 +01:00
|
|
|
void erase_download_piece(std::vector<downloading_piece>::iterator i);
|
2007-05-09 02:49:13 +02:00
|
|
|
|
2018-03-24 13:02:42 +01:00
|
|
|
std::vector<downloading_piece>::const_iterator find_dl_piece(download_queue_t, piece_index_t) const;
|
|
|
|
std::vector<downloading_piece>::iterator find_dl_piece(download_queue_t, piece_index_t);
|
2011-08-15 06:16:43 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// returns an iterator to the downloading piece, whichever
|
|
|
|
// download list it may live in now
|
2015-01-26 03:04:58 +01:00
|
|
|
std::vector<downloading_piece>::iterator update_piece_state(
|
|
|
|
std::vector<downloading_piece>::iterator dp);
|
2011-08-16 08:30:53 +02:00
|
|
|
|
2009-11-28 23:41:21 +01:00
|
|
|
private:
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2018-11-09 18:48:33 +01:00
|
|
|
#if TORRENT_USE_ASSERTS || TORRENT_USE_INVARIANT_CHECKS
|
2018-10-04 23:41:03 +02:00
|
|
|
index_range<download_queue_t> categories() const
|
|
|
|
{ return {{}, piece_picker::piece_pos::num_download_categories}; }
|
2018-11-09 18:48:33 +01:00
|
|
|
#endif
|
2018-10-04 23:41:03 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
// the following vectors are mutable because they sometimes may
|
|
|
|
// be updated lazily, triggered by const functions
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this maps indices to number of peers that has this piece and
|
|
|
|
// index into the m_piece_info vectors.
|
|
|
|
// piece_pos::we_have_index means that we have the piece, so it
|
|
|
|
// doesn't exist in the piece_info buckets
|
|
|
|
// pieces with the filtered flag set doesn't have entries in
|
|
|
|
// the m_piece_info buckets either
|
|
|
|
// TODO: should this be allocated lazily?
|
2016-12-22 16:42:33 +01:00
|
|
|
mutable aux::vector<piece_pos, piece_index_t> m_piece_map;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2018-08-19 11:08:09 +02:00
|
|
|
// this indicates whether a block has been marked as a pad
|
|
|
|
// block or not. It's indexed by block index, i.e. piece_index
|
|
|
|
// * blocks_per_piece + block. These blocks should not be
|
|
|
|
// picked and are considered to be had
|
2018-02-18 00:36:57 +01:00
|
|
|
// TODO: this could be a much more efficient data structure
|
2018-08-19 11:08:09 +02:00
|
|
|
bitfield m_pad_blocks;
|
|
|
|
|
|
|
|
// tracks the number of blocks in a specific piece that are pad blocks
|
|
|
|
std::unordered_map<piece_index_t, int> m_pads_in_piece;
|
|
|
|
|
2019-08-05 01:21:02 +02:00
|
|
|
// when the adjecent_piece affinity is enabled, this contains the most
|
|
|
|
// recent "extents" of adjecent pieces that have been requested from
|
|
|
|
// this is mutable because it's updated by functions to pick pieces, which
|
|
|
|
// are const. That's an efficient place to update it, since it's being
|
|
|
|
// traversed already.
|
|
|
|
mutable std::vector<piece_extent_t> m_recent_extents;
|
|
|
|
|
2018-08-19 11:08:09 +02:00
|
|
|
// the number of bits set in the m_pad_blocks bitfield, i.e.
|
|
|
|
// the number of blocks marked as pads
|
|
|
|
int m_num_pad_blocks = 0;
|
2018-01-23 01:44:29 +01:00
|
|
|
|
2018-08-07 22:23:13 +02:00
|
|
|
// the number of pad blocks that we already have
|
|
|
|
int m_have_pad_blocks = 0;
|
|
|
|
|
|
|
|
// the number of pad blocks part of filtered pieces we don't have
|
|
|
|
int m_filtered_pad_blocks = 0;
|
|
|
|
|
|
|
|
// the number of pad blocks we have that are also filtered
|
|
|
|
int m_have_filtered_pad_blocks = 0;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the number of seeds. These are not added to
|
|
|
|
// the availability counters of the pieces
|
2016-10-27 02:41:57 +02:00
|
|
|
int m_seeds = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
// the number of pieces that have passed the hash check
|
2016-10-27 02:41:57 +02:00
|
|
|
int m_num_passed = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
// this vector contains all piece indices that are pickable
|
|
|
|
// sorted by priority. Pieces are in random random order
|
|
|
|
// among pieces with the same priority
|
2016-12-26 17:25:50 +01:00
|
|
|
mutable aux::vector<piece_index_t, prio_index_t> m_pieces;
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2016-08-02 13:20:13 +02:00
|
|
|
// these are indices to the priority boundaries inside
|
2008-01-31 18:52:29 +01:00
|
|
|
// the m_pieces vector. priority 0 always start at
|
2016-08-02 13:20:13 +02:00
|
|
|
// 0, priority 1 starts at m_priority_boundaries[0] etc.
|
2017-01-17 03:51:49 +01:00
|
|
|
mutable aux::vector<prio_index_t> m_priority_boundaries;
|
2003-10-23 18:55:52 +02:00
|
|
|
|
2015-01-26 03:04:58 +01:00
|
|
|
// each piece that's currently being downloaded has an entry in this list
|
2016-04-06 22:40:42 +02:00
|
|
|
// with block allocations. i.e. it says which parts of the piece that is
|
2015-01-26 03:04:58 +01:00
|
|
|
// being downloaded. This list is ordered by piece index to make lookups
|
|
|
|
// efficient there are as many buckets as there are piece states. See
|
|
|
|
// piece_pos::state_t. The only download state that does not have a
|
|
|
|
// corresponding downloading_piece vector is piece_open and
|
|
|
|
// piece_downloading_reverse (the latter uses the same as
|
|
|
|
// piece_downloading).
|
2018-03-24 13:02:42 +01:00
|
|
|
aux::array<aux::vector<downloading_piece>
|
|
|
|
, static_cast<std::uint8_t>(piece_pos::num_download_categories)
|
|
|
|
, download_queue_t> m_downloads;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2015-02-14 22:32:41 +01:00
|
|
|
// this holds the information of the blocks in partially downloaded
|
|
|
|
// pieces. the downloading_piece::info index point into this vector for
|
|
|
|
// its storage
|
2017-01-17 03:51:49 +01:00
|
|
|
aux::vector<block_info> m_block_info;
|
2007-05-09 02:49:13 +02:00
|
|
|
|
2015-02-14 22:32:41 +01:00
|
|
|
// these are block ranges in m_block_info that are free. The numbers
|
|
|
|
// in here, when multiplied by m_blocks_per_piece is the index to the
|
|
|
|
// first block in the range that's free to use by a new downloading_piece.
|
|
|
|
// this is a free-list.
|
2016-06-18 20:01:38 +02:00
|
|
|
std::vector<std::uint16_t> m_free_block_infos;
|
2015-02-14 22:32:41 +01:00
|
|
|
|
2016-10-27 02:41:57 +02:00
|
|
|
std::uint16_t m_blocks_per_piece = 0;
|
|
|
|
std::uint16_t m_blocks_in_last_piece = 0;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-05-30 19:43:03 +02:00
|
|
|
// the number of filtered pieces that we don't already
|
|
|
|
// have. total_number_of_pieces - number_of_pieces_we_have
|
|
|
|
// - num_filtered is supposed to the number of pieces
|
|
|
|
// we still want to download
|
2018-08-07 22:23:13 +02:00
|
|
|
// TODO: it would be more intuitive to account "wanted" pieces
|
|
|
|
// instead of filtered
|
2016-10-27 02:41:57 +02:00
|
|
|
int m_num_filtered = 0;
|
2005-05-30 19:43:03 +02:00
|
|
|
|
|
|
|
// the number of pieces we have that also are filtered
|
2016-10-27 02:41:57 +02:00
|
|
|
int m_num_have_filtered = 0;
|
2015-06-30 02:40:32 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// we have all pieces in the range [0, m_cursor)
|
|
|
|
// m_cursor is the first piece we don't have
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t m_cursor{0};
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
// we have all pieces in the range [m_reverse_cursor, end)
|
|
|
|
// m_reverse_cursor is the first piece where we also have
|
|
|
|
// all the subsequent pieces
|
2016-12-22 16:42:33 +01:00
|
|
|
piece_index_t m_reverse_cursor{0};
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2015-02-12 04:16:53 +01:00
|
|
|
// the number of pieces we have (i.e. passed + flushed).
|
|
|
|
// This includes pieces that we have filtered but still have
|
2016-10-27 02:41:57 +02:00
|
|
|
int m_num_have = 0;
|
2015-06-30 02:40:32 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
// if this is set to true, it means update_pieces()
|
|
|
|
// has to be called before accessing m_pieces.
|
2016-10-27 02:41:57 +02:00
|
|
|
mutable bool m_dirty = false;
|
2008-06-19 13:28:34 +02:00
|
|
|
public:
|
|
|
|
|
2016-05-17 15:24:06 +02:00
|
|
|
enum { max_pieces = (std::numeric_limits<int>::max)() - 1 };
|
2008-06-19 13:28:34 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_PIECE_PICKER_HPP_INCLUDED
|