2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2012-10-02 05:16:33 +02:00
|
|
|
Copyright (c) 2003-2012, 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
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
#include <bitset>
|
2007-03-15 23:03:56 +01:00
|
|
|
#include <utility>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
#include <boost/static_assert.hpp>
|
2009-11-26 22:05:57 +01:00
|
|
|
#include <boost/cstdint.hpp>
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2004-01-25 19:18:36 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
#include "libtorrent/peer_id.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2010-02-18 18:26:21 +01:00
|
|
|
#include "libtorrent/time.hpp"
|
2003-11-02 22:06:50 +01:00
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
// #define TORRENT_DEBUG_REFCOUNTS
|
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
#include <set>
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
|
|
|
class torrent;
|
|
|
|
class peer_connection;
|
2009-04-26 23:21:31 +02:00
|
|
|
struct bitfield;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
struct TORRENT_EXTRA_EXPORT piece_block
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2010-05-13 08:29:33 +02:00
|
|
|
const static piece_block invalid;
|
|
|
|
|
2009-10-20 18:44:11 +02:00
|
|
|
piece_block() {}
|
2010-05-13 08:29:33 +02:00
|
|
|
piece_block(boost::uint32_t p_index, boost::uint16_t b_index)
|
2003-10-23 01:00:57 +02:00
|
|
|
: piece_index(p_index)
|
|
|
|
, block_index(b_index)
|
2010-05-13 08:29:33 +02:00
|
|
|
{
|
2012-04-04 06:23:28 +02:00
|
|
|
TORRENT_ASSERT(p_index < (1 << 19));
|
|
|
|
TORRENT_ASSERT(b_index < (1 << 13));
|
2010-05-13 08:29:33 +02:00
|
|
|
}
|
2012-04-04 06:23:28 +02:00
|
|
|
boost::uint32_t piece_index:19;
|
|
|
|
boost::uint32_t block_index:13;
|
2004-09-16 03:14:16 +02:00
|
|
|
|
|
|
|
bool operator<(piece_block const& b) const
|
|
|
|
{
|
|
|
|
if (piece_index < b.piece_index) return true;
|
|
|
|
if (piece_index == b.piece_index) return block_index < b.block_index;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(piece_block const& b) const
|
2003-11-05 00:27:06 +01:00
|
|
|
{ return piece_index == b.piece_index && block_index == b.block_index; }
|
|
|
|
|
2004-09-16 03:14:16 +02:00
|
|
|
bool operator!=(piece_block const& b) const
|
2003-11-05 00:27:06 +01:00
|
|
|
{ return piece_index != b.piece_index || block_index != b.block_index; }
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
|
2012-03-19 00:31:04 +01:00
|
|
|
class TORRENT_EXTRA_EXPORT piece_picker
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
struct piece_pos;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// the number of priority levels
|
|
|
|
priority_levels = 8,
|
|
|
|
// priority factor
|
|
|
|
prio_factor = priority_levels - 4
|
|
|
|
};
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
struct block_info
|
|
|
|
{
|
2007-07-06 19:15:35 +02:00
|
|
|
block_info(): peer(0), num_peers(0), state(state_none) {}
|
2003-11-02 22:06:50 +01:00
|
|
|
// the peer this block was requested or
|
2007-07-04 04:16:49 +02:00
|
|
|
// downloaded from. This is a pointer to
|
|
|
|
// a policy::peer object
|
|
|
|
void* peer;
|
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;
|
2012-04-12 07:00:20 +02:00
|
|
|
#if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS
|
|
|
|
// to allow verifying the invariant of blocks belonging to the right piece
|
|
|
|
int piece_index;
|
|
|
|
#endif
|
2003-11-02 22:06:50 +01:00
|
|
|
};
|
|
|
|
|
2007-04-27 02:27:37 +02:00
|
|
|
// the peers that are downloading this piece
|
|
|
|
// are considered fast peers or slow peers.
|
|
|
|
// none is set if the blocks were downloaded
|
|
|
|
// in a previous session
|
|
|
|
enum piece_state_t
|
|
|
|
{ none, slow, medium, fast };
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
enum options_t
|
|
|
|
{
|
|
|
|
// pick rarest first
|
|
|
|
rarest_first = 1,
|
|
|
|
// pick the most common first, or the last pieces if sequential
|
|
|
|
reverse = 2,
|
|
|
|
// only pick pieces exclusively requested from this peer
|
|
|
|
on_parole = 4,
|
|
|
|
// always pick partial pieces before any other piece
|
|
|
|
prioritize_partials = 8,
|
|
|
|
// pick pieces in sequential order
|
2009-01-14 10:07:27 +01:00
|
|
|
sequential = 16,
|
|
|
|
// have affinity to pieces with the same speed category
|
|
|
|
speed_affinity = 32,
|
|
|
|
// ignore the prefer_whole_pieces parameter
|
|
|
|
ignore_whole_pieces = 64
|
2008-09-06 23:04:57 +02:00
|
|
|
};
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
struct downloading_piece
|
|
|
|
{
|
2013-10-03 17:39:19 +02:00
|
|
|
downloading_piece(): info(NULL), index(-1)
|
|
|
|
, finished(0), writing(0), requested(0)
|
|
|
|
, state(none) {}
|
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; }
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
// info about each block
|
2007-05-09 02:49:13 +02:00
|
|
|
// this is a pointer into the m_block_info
|
|
|
|
// vector owned by the piece_picker
|
|
|
|
block_info* info;
|
2013-10-03 17:39:19 +02:00
|
|
|
// the index of the piece
|
|
|
|
int index;
|
2007-06-10 22:46:09 +02:00
|
|
|
// the number of blocks in the finished state
|
|
|
|
boost::int16_t finished;
|
|
|
|
// the number of blocks in the writing state
|
|
|
|
boost::int16_t writing;
|
|
|
|
// the number of blocks in the requested state
|
|
|
|
boost::int16_t requested;
|
2013-10-03 17:39:19 +02:00
|
|
|
|
|
|
|
// one of piece_state_t values
|
|
|
|
// really just needs 2 bits
|
|
|
|
boost::uint16_t state;
|
2003-11-02 22:06:50 +01:00
|
|
|
};
|
2010-09-05 18:01:36 +02:00
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
piece_picker();
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2007-05-30 08:52:59 +02:00
|
|
|
void get_availability(std::vector<int>& avail) const;
|
|
|
|
|
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)
|
2013-01-06 05:02:29 +01:00
|
|
|
void inc_refcount(int index, const void* peer);
|
|
|
|
void dec_refcount(int index, const void* 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)
|
2013-01-06 05:02:29 +01:00
|
|
|
void inc_refcount(bitfield const& bitmask, const void* peer);
|
2003-12-18 04:30:41 +01:00
|
|
|
// decreases the peer count for the given piece
|
|
|
|
// (used when a peer disconnects)
|
2013-01-06 05:02:29 +01:00
|
|
|
void dec_refcount(bitfield const& bitmask, const void* peer);
|
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.
|
2013-01-06 05:02:29 +01:00
|
|
|
void inc_refcount_all(const void* peer);
|
|
|
|
void dec_refcount_all(const void* peer);
|
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)
|
|
|
|
void we_have(int index);
|
2008-06-07 04:58:28 +02:00
|
|
|
void we_dont_have(int index);
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
int cursor() const { return m_cursor; }
|
|
|
|
int reverse_cursor() const { return m_reverse_cursor; }
|
2009-01-05 02:08:09 +01:00
|
|
|
int sparse_regions() const { return m_sparse_regions; }
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2008-08-26 00:32:50 +02:00
|
|
|
// sets all pieces to dont-have
|
2010-02-23 17:26:24 +01:00
|
|
|
void init(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()); }
|
|
|
|
|
|
|
|
bool have_piece(int index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < int(m_piece_map.size()));
|
|
|
|
return m_piece_map[index].index == piece_pos::we_have_index;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2012-09-11 07:27:14 +02:00
|
|
|
bool is_downloading(int index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < int(m_piece_map.size()));
|
|
|
|
|
|
|
|
piece_pos const& p = m_piece_map[index];
|
|
|
|
return p.downloading;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
bool set_piece_priority(int index, int 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'
|
|
|
|
int piece_priority(int 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
|
|
|
|
void piece_priorities(std::vector<int>& pieces) const;
|
|
|
|
|
|
|
|
// ========== start deprecation ==============
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
// fills the bitmask with 1's for pieces that are filtered
|
|
|
|
void filtered_pieces(std::vector<bool>& mask) const;
|
|
|
|
|
2007-03-20 02:59:00 +01:00
|
|
|
// ========== end deprecation ==============
|
|
|
|
|
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!
|
2007-07-04 04:16:49 +02:00
|
|
|
// The last argument is the policy::peer pointer for the peer that
|
|
|
|
// we'll download from.
|
2008-05-28 04:35:02 +02:00
|
|
|
void pick_pieces(bitfield const& pieces
|
2008-09-06 23:04:57 +02:00
|
|
|
, std::vector<piece_block>& interesting_blocks, int num_blocks
|
|
|
|
, int prefer_whole_pieces, void* peer, piece_state_t speed
|
2011-08-15 01:16:12 +02:00
|
|
|
, int options, std::vector<int> const& suggested_pieces
|
|
|
|
, int num_peers) 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
|
2008-09-06 23:04:57 +02:00
|
|
|
int add_blocks(int piece, bitfield 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
|
2007-09-03 23:16:24 +02:00
|
|
|
, int num_blocks, int prefer_whole_pieces
|
2008-09-06 23:04:57 +02:00
|
|
|
, void* peer, std::vector<int> const& ignore
|
|
|
|
, piece_state_t speed, int 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
|
|
|
|
, bitfield 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
|
2007-09-05 23:21:11 +02:00
|
|
|
, int num_blocks, int prefer_whole_pieces
|
2007-08-14 19:47:48 +02:00
|
|
|
, void* peer, piece_state_t speed
|
2008-09-06 23:04:57 +02:00
|
|
|
, int 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
|
|
|
|
void clear_peer(void* peer);
|
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
|
2007-09-15 22:20:07 +02:00
|
|
|
bool mark_as_downloading(piece_block block, void* peer
|
2007-04-27 02:27:37 +02:00
|
|
|
, piece_state_t s);
|
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
|
|
|
|
bool mark_as_writing(piece_block block, void* peer);
|
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
void mark_as_finished(piece_block block, void* peer);
|
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
|
|
|
|
2007-08-03 08:13:26 +02:00
|
|
|
// returns information about the given piece
|
|
|
|
void piece_info(int index, piece_picker::downloading_piece& st) const;
|
2010-09-05 18:01:36 +02:00
|
|
|
|
|
|
|
piece_pos const& piece_stats(int index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0 && index < int(m_piece_map.size()));
|
|
|
|
return m_piece_map[index];
|
|
|
|
}
|
|
|
|
|
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
|
2003-10-23 01:00:57 +02:00
|
|
|
void restore_piece(int index);
|
|
|
|
|
|
|
|
// clears the given piece's download flag
|
|
|
|
// this means that this piece-block can be picked again
|
2010-10-09 04:58:56 +02:00
|
|
|
void abort_download(piece_block block, void* peer = 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
bool is_piece_finished(int index) const;
|
|
|
|
|
2005-02-23 17:56:32 +01:00
|
|
|
// returns the number of blocks there is in the given piece
|
2003-10-30 00:28:09 +01:00
|
|
|
int blocks_in_piece(int index) const;
|
2003-11-02 22:06:50 +01:00
|
|
|
|
|
|
|
// the number of downloaded blocks that hasn't passed
|
|
|
|
// the hash-check yet
|
2003-10-30 00:28:09 +01:00
|
|
|
int unverified_blocks() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
void get_downloaders(std::vector<void*>& d, int index) const;
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2006-12-21 03:44:00 +01:00
|
|
|
std::vector<downloading_piece> const& get_download_queue() const
|
2003-11-02 22:06:50 +01:00
|
|
|
{ return m_downloads; }
|
|
|
|
|
2012-06-24 22:57:31 +02:00
|
|
|
int num_downloading_pieces() const { return int(m_downloads.size()); }
|
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
void* get_downloader(piece_block block) const;
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2005-05-30 19:43:03 +02:00
|
|
|
// the number of filtered pieces we don't have
|
|
|
|
int num_filtered() const { return m_num_filtered; }
|
|
|
|
|
|
|
|
// the number of filtered pieces we already have
|
|
|
|
int num_have_filtered() const { return m_num_have_filtered; }
|
2007-04-12 00:27:58 +02:00
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
int num_have() const { return m_num_have; }
|
|
|
|
|
2012-07-02 05:27:32 +02:00
|
|
|
// the number of pieces we want and don't have
|
|
|
|
int num_want_left() const { return num_pieces() - m_num_have - m_num_filtered; }
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2003-10-23 01:00:57 +02:00
|
|
|
// used in debug mode
|
2008-01-31 18:52:29 +01:00
|
|
|
void verify_priority(int start, int end, int prio) const;
|
2007-09-10 01:46:28 +02:00
|
|
|
void verify_pick(std::vector<piece_block> const& picked
|
2008-05-28 04:35:02 +02:00
|
|
|
, bitfield const& bits) const;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2013-02-25 05:13:46 +01:00
|
|
|
#if defined TORRENT_DEBUG && !defined TORRENT_DISABLE_INVARIANT_CHECKS
|
|
|
|
void check_invariant(const torrent* t = 0) const;
|
|
|
|
#endif
|
2009-04-04 23:50:36 +02:00
|
|
|
#if defined TORRENT_PICKER_LOG
|
2008-01-31 18:52:29 +01:00
|
|
|
void print_pieces() const;
|
2003-10-23 01:00:57 +02:00
|
|
|
#endif
|
|
|
|
|
2004-01-03 03:10:11 +01:00
|
|
|
// functor that compares indices on downloading_pieces
|
|
|
|
struct has_index
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
has_index(int i): index(i) { TORRENT_ASSERT(i >= 0); }
|
2004-01-03 03:10:11 +01:00
|
|
|
bool operator()(const downloading_piece& p) const
|
|
|
|
{ return p.index == index; }
|
|
|
|
int index;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
private:
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
friend struct piece_pos;
|
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
bool can_pick(int piece, bitfield const& bitmask) const;
|
2008-09-06 23:04:57 +02:00
|
|
|
bool is_piece_free(int piece, bitfield const& bitmask) const;
|
2007-09-03 23:16:24 +02:00
|
|
|
std::pair<int, int> expand_piece(int piece, int whole_pieces
|
2008-05-28 04:35:02 +02:00
|
|
|
, bitfield const& have) const;
|
2007-09-03 23:16:24 +02:00
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
public:
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
struct piece_pos
|
|
|
|
{
|
|
|
|
piece_pos() {}
|
|
|
|
piece_pos(int peer_count_, int index_)
|
|
|
|
: peer_count(peer_count_)
|
|
|
|
, downloading(0)
|
2011-08-16 08:30:53 +02:00
|
|
|
, full(0)
|
2007-03-15 23:03:56 +01:00
|
|
|
, piece_priority(1)
|
2003-10-23 01:00:57 +02:00
|
|
|
, index(index_)
|
2004-01-26 01:21:12 +01:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(peer_count_ >= 0);
|
|
|
|
TORRENT_ASSERT(index_ >= 0);
|
2004-01-26 01:21:12 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-07-15 17:41:55 +02:00
|
|
|
// the number of peers that has this piece
|
|
|
|
// (availability)
|
2012-04-01 21:14:30 +02:00
|
|
|
#if TORRENT_COMPACT_PICKER
|
|
|
|
boost::uint32_t peer_count : 9;
|
|
|
|
#else
|
|
|
|
boost::uint32_t peer_count : 16;
|
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
// is 1 if the piece is marked as being downloaded
|
2012-04-01 21:14:30 +02:00
|
|
|
boost::uint32_t downloading : 1;
|
2011-08-16 08:30:53 +02:00
|
|
|
// set when downloading, but no free blocks to request left
|
2012-04-01 21:14:30 +02:00
|
|
|
boost::uint32_t full : 1;
|
2007-03-15 23:03:56 +01:00
|
|
|
// is 0 if the piece is filtered (not to be downloaded)
|
|
|
|
// 1 is normal priority (default)
|
2007-03-20 02:59:00 +01:00
|
|
|
// 2 is higher priority than pieces at the same availability level
|
|
|
|
// 3 is same priority as partial pieces
|
|
|
|
// 4 is higher priority than partial pieces
|
|
|
|
// 5 and 6 same priority as availability 1 (ignores availability)
|
|
|
|
// 7 is maximum priority (ignores availability)
|
2012-04-01 21:14:30 +02:00
|
|
|
boost::uint32_t piece_priority : 3;
|
2003-10-23 01:00:57 +02:00
|
|
|
// index in to the piece_info vector
|
2012-04-01 21:14:30 +02:00
|
|
|
#if TORRENT_COMPACT_PICKER
|
|
|
|
boost::uint32_t index : 18;
|
|
|
|
#else
|
|
|
|
boost::uint32_t index;
|
|
|
|
#endif
|
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
|
|
|
|
std::set<const void*> have_peers;
|
|
|
|
#endif
|
2007-03-15 23:03:56 +01:00
|
|
|
enum
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2007-03-15 23:03:56 +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.
|
2012-04-01 21:14:30 +02:00
|
|
|
#if TORRENT_COMPACT_PICKER
|
2007-03-20 02:59:00 +01:00
|
|
|
we_have_index = 0x3ffff,
|
2012-04-01 21:14:30 +02:00
|
|
|
#else
|
|
|
|
we_have_index = 0xffffffff,
|
|
|
|
#endif
|
2007-03-15 23:03:56 +01:00
|
|
|
// the priority value that means the piece is filtered
|
|
|
|
filter_priority = 0,
|
|
|
|
// the max number the peer count can hold
|
2012-04-01 21:14:30 +02:00
|
|
|
#if TORRENT_COMPACT_PICKER
|
2011-08-16 08:30:53 +02:00
|
|
|
max_peer_count = 0x1ff
|
2012-04-01 21:14:30 +02:00
|
|
|
#else
|
|
|
|
max_peer_count = 0xffff
|
|
|
|
#endif
|
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()); }
|
2008-06-07 04:58:28 +02:00
|
|
|
void set_not_have() { index = 0; TORRENT_ASSERT(!have()); }
|
2006-04-25 23:04:48 +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
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// prio 7 is always top priority
|
|
|
|
// prio 0 is always -1 (don't pick)
|
|
|
|
// downloading pieces are always on an even prio_factor priority
|
|
|
|
//
|
|
|
|
// availability x, downloading
|
|
|
|
// | availability x, prio 3; availability 2x, prio 6
|
|
|
|
// | | availability x, prio 2; availability 2x, prio 5
|
|
|
|
// | | | availability x, prio 1; availability 2x, prio 4
|
|
|
|
// | | | |
|
|
|
|
// +---+---+---+---+
|
|
|
|
// | 0 | 1 | 2 | 3 |
|
|
|
|
// +---+---+---+---+
|
|
|
|
|
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.
|
|
|
|
if (filtered() || have() || peer_count + picker->m_seeds == 0)
|
2008-01-31 18:52:29 +01:00
|
|
|
return -1;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// prio 7 disregards availability
|
|
|
|
if (piece_priority == priority_levels - 1) return 1 - downloading;
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// prio 4,5,6 halves the availability of a piece
|
|
|
|
int availability = peer_count;
|
2010-03-06 08:16:39 +01:00
|
|
|
int p = piece_priority;
|
2008-09-06 23:04:57 +02:00
|
|
|
if (piece_priority >= priority_levels / 2)
|
|
|
|
{
|
|
|
|
availability /= 2;
|
2010-03-06 08:16:39 +01:00
|
|
|
p -= (priority_levels - 2) / 2;
|
2008-09-06 23:04:57 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (downloading) return availability * prio_factor;
|
2010-03-06 08:16:39 +01:00
|
|
|
return availability * prio_factor + (priority_levels / 2) - p;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
bool operator!=(piece_pos p) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{ return index != p.index || peer_count != p.peer_count; }
|
|
|
|
|
2006-04-25 23:04:48 +02:00
|
|
|
bool operator==(piece_pos p) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{ return index == p.index && peer_count == p.peer_count; }
|
|
|
|
};
|
|
|
|
|
2013-09-14 12:08:31 +02:00
|
|
|
void set_num_pad_files(int n) { m_num_pad_files = n; }
|
|
|
|
|
2010-09-05 18:01:36 +02:00
|
|
|
private:
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifndef TORRENT_DEBUG_REFCOUNTS
|
2012-04-01 21:14:30 +02:00
|
|
|
#if TORRENT_COMPACT_PICKER
|
2007-03-15 23:03:56 +01:00
|
|
|
BOOST_STATIC_ASSERT(sizeof(piece_pos) == sizeof(char) * 4);
|
2012-04-01 21:14:30 +02:00
|
|
|
#else
|
|
|
|
BOOST_STATIC_ASSERT(sizeof(piece_pos) == sizeof(char) * 8);
|
2013-01-06 05:02:29 +01:00
|
|
|
#endif
|
2012-04-01 21:14:30 +02:00
|
|
|
#endif
|
2003-11-02 22:06:50 +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;
|
|
|
|
|
|
|
|
// fills in the range [start, end) of pieces in
|
|
|
|
// m_pieces that have priority 'prio'
|
|
|
|
void priority_range(int prio, int* start, int* end);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
// adds the piece 'index' to m_pieces
|
2007-03-15 23:03:56 +01:00
|
|
|
void add(int 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
|
|
|
|
void remove(int priority, int elem_index);
|
|
|
|
// updates the position of the piece with the given
|
|
|
|
// priority and the elem_index in the m_pieces vector
|
|
|
|
void update(int priority, int elem_index);
|
|
|
|
// shuffles the given piece inside it's priority range
|
|
|
|
void shuffle(int priority, int elem_index);
|
|
|
|
|
2011-08-15 06:16:43 +02:00
|
|
|
// void sort_piece(std::vector<downloading_piece>::iterator dp);
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2011-08-15 06:16:43 +02:00
|
|
|
downloading_piece& add_download_piece(int index);
|
2007-05-09 02:49:13 +02:00
|
|
|
void erase_download_piece(std::vector<downloading_piece>::iterator i);
|
|
|
|
|
2011-08-15 06:16:43 +02:00
|
|
|
std::vector<downloading_piece>::const_iterator find_dl_piece(int index) const;
|
|
|
|
std::vector<downloading_piece>::iterator find_dl_piece(int index);
|
|
|
|
|
2011-08-16 08:30:53 +02:00
|
|
|
void update_full(downloading_piece& dp);
|
|
|
|
|
2009-11-28 23:41:21 +01:00
|
|
|
// some compilers (e.g. gcc 2.95, does not inherit access
|
|
|
|
// privileges to nested classes)
|
|
|
|
public:
|
2008-01-31 18:52:29 +01:00
|
|
|
// the number of seeds. These are not added to
|
|
|
|
// the availability counters of the pieces
|
|
|
|
int m_seeds;
|
2009-11-28 23:41:21 +01:00
|
|
|
private:
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
// the following vectors are mutable because they sometimes may
|
|
|
|
// be updated lazily, triggered by const functions
|
|
|
|
|
|
|
|
// this vector contains all piece indices that are pickable
|
|
|
|
// sorted by priority. Pieces are in random random order
|
|
|
|
// among pieces with the same priority
|
|
|
|
mutable std::vector<int> m_pieces;
|
|
|
|
|
|
|
|
// these are indices to the priority boundries inside
|
|
|
|
// the m_pieces vector. priority 0 always start at
|
|
|
|
// 0, priority 1 starts at m_priority_boundries[0] etc.
|
|
|
|
mutable std::vector<int> m_priority_boundries;
|
2003-10-23 18:55:52 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
// this maps indices to number of peers that has this piece and
|
|
|
|
// index into the m_piece_info vectors.
|
2005-05-25 12:01:01 +02:00
|
|
|
// piece_pos::we_have_index means that we have the piece, so it
|
|
|
|
// doesn't exist in the piece_info buckets
|
2005-09-01 23:04:21 +02:00
|
|
|
// pieces with the filtered flag set doesn't have entries in
|
|
|
|
// the m_piece_info buckets either
|
2008-01-31 18:52:29 +01:00
|
|
|
mutable std::vector<piece_pos> m_piece_map;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
// each piece that's currently being downloaded
|
|
|
|
// has an entry in this list with block allocations.
|
|
|
|
// i.e. it says wich parts of the piece that
|
2011-08-15 06:16:43 +02:00
|
|
|
// is being downloaded. This list is ordered
|
|
|
|
// by piece index to make lookups efficient
|
2003-10-23 01:00:57 +02:00
|
|
|
std::vector<downloading_piece> m_downloads;
|
|
|
|
|
2007-05-09 02:49:13 +02:00
|
|
|
// this holds the information of the
|
|
|
|
// blocks in partially downloaded pieces.
|
|
|
|
// the first m_blocks_per_piece entries
|
|
|
|
// in the vector belongs to the first
|
|
|
|
// entry in m_downloads, the second
|
|
|
|
// m_blocks_per_piece entries to the
|
|
|
|
// second entry in m_downloads and so on.
|
|
|
|
std::vector<block_info> m_block_info;
|
|
|
|
|
2013-09-14 12:08:31 +02:00
|
|
|
boost::uint16_t m_blocks_per_piece;
|
|
|
|
boost::uint16_t m_blocks_in_last_piece;
|
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
|
|
|
|
int m_num_filtered;
|
|
|
|
|
|
|
|
// the number of pieces we have that also are filtered
|
|
|
|
int m_num_have_filtered;
|
2007-05-14 12:54:18 +02:00
|
|
|
|
|
|
|
// the number of pieces we have
|
|
|
|
int m_num_have;
|
2006-04-25 23:04:48 +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
|
|
|
|
int m_cursor;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
int m_reverse_cursor;
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2009-01-05 02:08:09 +01:00
|
|
|
// the number of regions of pieces we don't have.
|
|
|
|
int m_sparse_regions;
|
|
|
|
|
2013-09-14 12:08:31 +02:00
|
|
|
// this is the number of partial download pieces
|
|
|
|
// that may be caused by pad files. We raise the limit
|
|
|
|
// of number of partial pieces by this amount, to not
|
|
|
|
// prioritize pieces that intersect pad files for no
|
|
|
|
// apparent reason
|
|
|
|
int m_num_pad_files;
|
|
|
|
|
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.
|
|
|
|
mutable bool m_dirty;
|
2008-06-19 13:28:34 +02:00
|
|
|
public:
|
|
|
|
|
2012-04-02 05:32:26 +02:00
|
|
|
#if TORRENT_COMPACT_PICKER
|
2008-06-19 13:28:34 +02:00
|
|
|
enum { max_pieces = piece_pos::we_have_index - 1 };
|
2012-04-02 05:32:26 +02:00
|
|
|
#else
|
2012-04-04 06:23:28 +02:00
|
|
|
// still limited by piece_block
|
|
|
|
enum { max_pieces = (1 << 19) - 2 };
|
2012-04-02 05:32:26 +02:00
|
|
|
#endif
|
2008-06-19 13:28:34 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_PIECE_PICKER_HPP_INCLUDED
|
2005-11-01 19:30:39 +01:00
|
|
|
|