2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
2014-02-23 20:12:25 +01:00
|
|
|
Copyright (c) 2003-2014, 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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <cmath>
|
2003-12-01 06:01:40 +01:00
|
|
|
#include <algorithm>
|
2003-12-17 20:03:23 +01:00
|
|
|
#include <numeric>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2011-11-02 05:50:04 +01:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/piece_picker.hpp"
|
2008-05-28 04:35:02 +02:00
|
|
|
#include "libtorrent/bitfield.hpp"
|
2011-02-26 08:55:51 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/alloca.hpp"
|
|
|
|
#include "libtorrent/performance_counters.hpp" // for counters
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2003-10-23 01:00:57 +02:00
|
|
|
#include "libtorrent/peer_connection.hpp"
|
2006-01-11 02:32:26 +01:00
|
|
|
#include "libtorrent/torrent.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/torrent_peer.hpp"
|
2003-10-23 01:00:57 +02:00
|
|
|
#endif
|
|
|
|
|
2013-10-03 08:47:28 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
2011-11-02 06:28:25 +01:00
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
#define TORRENT_PIECE_PICKER_INVARIANT_CHECK INVARIANT_CHECK
|
2008-02-18 04:07:14 +01:00
|
|
|
//#define TORRENT_NO_EXPENSIVE_INVARIANT_CHECK
|
2008-09-06 23:04:57 +02:00
|
|
|
//#define TORRENT_PIECE_PICKER_INVARIANT_CHECK
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2012-04-04 06:23:28 +02:00
|
|
|
const piece_block piece_block::invalid(0x7FFFF, 0x1FFF);
|
2010-05-13 08:29:33 +02:00
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
piece_picker::piece_picker()
|
2008-01-31 18:52:29 +01:00
|
|
|
: m_seeds(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_num_passed(0)
|
2008-01-31 18:52:29 +01:00
|
|
|
, m_priority_boundries(1, int(m_pieces.size()))
|
2008-09-06 23:04:57 +02:00
|
|
|
, m_blocks_per_piece(0)
|
|
|
|
, m_blocks_in_last_piece(0)
|
2005-05-30 19:43:03 +02:00
|
|
|
, m_num_filtered(0)
|
|
|
|
, m_num_have_filtered(0)
|
2008-09-06 23:04:57 +02:00
|
|
|
, m_cursor(0)
|
|
|
|
, m_reverse_cursor(0)
|
2009-01-05 02:08:09 +01:00
|
|
|
, m_sparse_regions(1)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_num_have(0)
|
2013-09-14 12:08:31 +02:00
|
|
|
, m_num_pad_files(0)
|
2008-01-31 18:52:29 +01:00
|
|
|
, m_dirty(false)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "new piece_picker" << std::endl;
|
2008-01-31 18:52:29 +01:00
|
|
|
#endif
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2008-06-07 04:58:28 +02:00
|
|
|
check_invariant();
|
2007-03-17 18:28:59 +01:00
|
|
|
#endif
|
2008-06-07 04:58:28 +02:00
|
|
|
}
|
|
|
|
|
2010-02-23 17:26:24 +01:00
|
|
|
void piece_picker::init(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces)
|
2008-06-07 04:58:28 +02:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(blocks_per_piece > 0);
|
2010-02-23 17:26:24 +01:00
|
|
|
TORRENT_ASSERT(total_num_pieces > 0);
|
2008-06-07 04:58:28 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "piece_picker::init()" << std::endl;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2008-06-07 04:58:28 +02:00
|
|
|
// allocate the piece_map to cover all pieces
|
|
|
|
// and make them invalid (as if we don't have a single piece)
|
2010-02-23 17:26:24 +01:00
|
|
|
m_piece_map.resize(total_num_pieces, piece_pos(0, 0));
|
2008-09-06 23:04:57 +02:00
|
|
|
m_reverse_cursor = int(m_piece_map.size());
|
|
|
|
m_cursor = 0;
|
2004-01-25 23:41:55 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int i = 0; i < num_download_categories; ++i)
|
|
|
|
m_downloads[i].clear();
|
2009-05-25 22:31:20 +02:00
|
|
|
m_block_info.clear();
|
|
|
|
|
2008-08-26 00:32:50 +02:00
|
|
|
m_num_filtered += m_num_have_filtered;
|
|
|
|
m_num_have_filtered = 0;
|
|
|
|
m_num_have = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
m_num_passed = 0;
|
2008-08-26 00:32:50 +02:00
|
|
|
m_dirty = true;
|
|
|
|
for (std::vector<piece_pos>::iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
i->peer_count = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
i->state = piece_pos::piece_open;
|
2008-08-26 00:32:50 +02:00
|
|
|
i->index = 0;
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
i->have_peers.clear();
|
|
|
|
#endif
|
2008-08-26 00:32:50 +02:00
|
|
|
}
|
|
|
|
|
2009-08-30 09:38:52 +02:00
|
|
|
for (std::vector<piece_pos>::iterator i = m_piece_map.begin() + m_cursor
|
2008-09-06 23:04:57 +02:00
|
|
|
, end(m_piece_map.end()); i != end && (i->have() || i->filtered());
|
|
|
|
++i, ++m_cursor);
|
2009-08-30 09:38:52 +02:00
|
|
|
for (std::vector<piece_pos>::reverse_iterator i = m_piece_map.rend()
|
2008-09-15 18:21:03 +02:00
|
|
|
- m_reverse_cursor; m_reverse_cursor > 0 && (i->have() || i->filtered());
|
|
|
|
++i, --m_reverse_cursor);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2005-03-19 13:22:40 +01:00
|
|
|
// the piece index is stored in 20 bits, which limits the allowed
|
|
|
|
// number of pieces somewhat
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(m_piece_map.size() < piece_pos::we_have_index);
|
2005-03-19 13:22:40 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
m_blocks_per_piece = blocks_per_piece;
|
2010-02-23 17:26:24 +01:00
|
|
|
m_blocks_in_last_piece = blocks_in_last_piece;
|
2003-10-23 01:00:57 +02:00
|
|
|
if (m_blocks_in_last_piece == 0) m_blocks_in_last_piece = blocks_per_piece;
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_blocks_in_last_piece <= m_blocks_per_piece);
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
|
|
|
|
2007-08-03 08:13:26 +02:00
|
|
|
void piece_picker::piece_info(int index, piece_picker::downloading_piece& st) const
|
|
|
|
{
|
2008-10-10 07:25:55 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2013-10-20 09:10:09 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-10-10 07:25:55 +02:00
|
|
|
#endif
|
2007-08-03 08:13:26 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < int(m_piece_map.size()));
|
2007-08-03 08:13:26 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int state = m_piece_map[index].state;
|
|
|
|
if (state > piece_pos::piece_open)
|
2007-08-03 08:13:26 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::const_iterator piece = find_dl_piece(state - 1, index);
|
|
|
|
TORRENT_ASSERT(piece != m_downloads[state - 1].end());
|
2007-08-03 08:13:26 +02:00
|
|
|
st = *piece;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
st.info = 0;
|
|
|
|
st.index = index;
|
|
|
|
st.writing = 0;
|
|
|
|
st.requested = 0;
|
|
|
|
if (m_piece_map[index].have())
|
|
|
|
{
|
|
|
|
st.finished = blocks_in_piece(index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
st.finished = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
piece_picker::dlpiece_iter piece_picker::add_download_piece(int piece)
|
2007-05-09 02:49:13 +02:00
|
|
|
{
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(piece >= 0);
|
|
|
|
TORRENT_ASSERT(piece < int(m_piece_map.size()));
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int num_downloads = 0;
|
|
|
|
for (int k = 0; k < num_download_categories; ++k) num_downloads += m_downloads[k].size();
|
|
|
|
|
2007-05-09 02:49:13 +02:00
|
|
|
int block_index = num_downloads * m_blocks_per_piece;
|
|
|
|
if (int(m_block_info.size()) < block_index + m_blocks_per_piece)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
block_info* base = NULL;
|
2007-08-03 08:13:26 +02:00
|
|
|
if (!m_block_info.empty()) base = &m_block_info[0];
|
2007-05-09 02:49:13 +02:00
|
|
|
m_block_info.resize(block_index + m_blocks_per_piece);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (base != NULL && &m_block_info[0] != base)
|
2007-05-09 02:49:13 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int k = 0; k < num_download_categories; ++k)
|
|
|
|
{
|
|
|
|
// this means the memory was reallocated, update the pointers
|
|
|
|
for (int i = 0; i < int(m_downloads[k].size()); ++i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_downloads[k][i].info >= base);
|
|
|
|
TORRENT_ASSERT(m_downloads[k][i].info < base + m_block_info.size());
|
|
|
|
m_downloads[k][i].info = &m_block_info[m_downloads[k][i].info - base];
|
|
|
|
}
|
|
|
|
}
|
2007-05-09 02:49:13 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
// always insert into bucket 0 (piece_downloading)
|
2013-10-02 23:51:30 +02:00
|
|
|
downloading_piece ret;
|
2011-08-15 06:16:43 +02:00
|
|
|
ret.index = piece;
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::iterator i = std::lower_bound(m_downloads[0].begin()
|
|
|
|
, m_downloads[0].end(), ret);
|
|
|
|
TORRENT_ASSERT(i == m_downloads[0].end() || i->index != piece);
|
2007-05-09 02:49:13 +02:00
|
|
|
ret.info = &m_block_info[block_index];
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(ret.info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(ret.info < &m_block_info[0] + m_block_info.size());
|
2013-10-02 23:51:30 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
|
|
|
VALGRIND_CHECK_VALUE_IS_DEFINED(piece);
|
|
|
|
VALGRIND_CHECK_VALUE_IS_DEFINED(block_index);
|
|
|
|
#endif
|
2007-05-09 02:49:13 +02:00
|
|
|
for (int i = 0; i < m_blocks_per_piece; ++i)
|
|
|
|
{
|
2007-07-06 19:15:35 +02:00
|
|
|
ret.info[i].num_peers = 0;
|
2007-06-10 22:46:09 +02:00
|
|
|
ret.info[i].state = block_info::state_none;
|
2007-07-04 04:16:49 +02:00
|
|
|
ret.info[i].peer = 0;
|
2013-10-02 23:51:30 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
|
|
|
VALGRIND_CHECK_VALUE_IS_DEFINED(ret.info[i].peer);
|
|
|
|
#endif
|
2014-01-19 20:45:50 +01:00
|
|
|
#if TORRENT_USE_ASSERTS
|
2012-04-12 07:00:20 +02:00
|
|
|
ret.info[i].piece_index = piece;
|
2014-07-06 21:18:00 +02:00
|
|
|
ret.info[i].peers.clear();
|
2012-04-12 07:00:20 +02:00
|
|
|
#endif
|
2007-05-09 02:49:13 +02:00
|
|
|
}
|
2013-10-02 23:51:30 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
2014-01-25 02:53:33 +01:00
|
|
|
VALGRIND_CHECK_VALUE_IS_DEFINED(ret.info);
|
|
|
|
VALGRIND_CHECK_VALUE_IS_DEFINED(ret.index);
|
2013-10-02 23:51:30 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
i = m_downloads[0].insert(i, ret);
|
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
return i;
|
2007-05-09 02:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void piece_picker::erase_download_piece(std::vector<downloading_piece>::iterator i)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int state = m_piece_map[i->index].state;
|
|
|
|
TORRENT_ASSERT(state > piece_pos::piece_open);
|
|
|
|
int queue = state - 1;
|
|
|
|
TORRENT_ASSERT(find_dl_piece(queue, i->index) == i);
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
int prev_size = m_downloads[queue].size();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int total_downloading_pieces = 0;
|
|
|
|
for (int k = 0; k < num_download_categories; ++k) total_downloading_pieces += m_downloads[k].size();
|
|
|
|
|
|
|
|
std::vector<downloading_piece>::iterator other;
|
|
|
|
bool found = false;
|
|
|
|
for (int k = 0; k < num_download_categories; ++k)
|
|
|
|
{
|
|
|
|
other = std::find_if(
|
|
|
|
m_downloads[k].begin(), m_downloads[k].end()
|
|
|
|
, boost::bind(&downloading_piece::info, _1)
|
|
|
|
== &m_block_info[(total_downloading_pieces - 1) * m_blocks_per_piece]);
|
|
|
|
if (other != m_downloads[k].end())
|
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TORRENT_ASSERT(found);
|
2007-07-15 17:41:55 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (found && i->index != other->index)
|
2007-05-09 02:49:13 +02:00
|
|
|
{
|
2007-07-15 17:41:55 +02:00
|
|
|
std::copy(other->info, other->info + m_blocks_per_piece, i->info);
|
|
|
|
other->info = i->info;
|
2007-05-09 02:49:13 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
m_piece_map[i->index].state = piece_pos::piece_open;
|
|
|
|
m_downloads[queue].erase(i);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(prev_size == m_downloads[queue].size() + 1);
|
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<piece_picker::downloading_piece> piece_picker::get_download_queue() const
|
|
|
|
{
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<downloading_piece> ret;
|
|
|
|
for (int k = 0; k < num_download_categories; ++k)
|
|
|
|
ret.insert(ret.end(), m_downloads[k].begin(), m_downloads[k].end());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int piece_picker::get_download_queue_size() const
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
for (int k = 0; k < num_download_categories; ++k)
|
|
|
|
ret += m_downloads[k].size();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void piece_picker::get_download_queue_sizes(int* partial, int* full, int* finished, int* zero_prio) const
|
|
|
|
{
|
|
|
|
*partial = m_downloads[0].size();
|
|
|
|
*full = m_downloads[1].size();
|
|
|
|
*finished = m_downloads[2].size();
|
|
|
|
*zero_prio = m_downloads[3].size();
|
2007-05-09 02:49:13 +02:00
|
|
|
}
|
2007-09-10 01:46:28 +02:00
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void piece_picker::check_piece_state() const
|
|
|
|
{
|
|
|
|
#ifndef TORRENT_DISABLE_INVARIANT_CHECKS
|
|
|
|
for (int k = 0; k < num_download_categories; ++k)
|
|
|
|
{
|
|
|
|
if (!m_downloads[k].empty())
|
|
|
|
{
|
|
|
|
for (std::vector<downloading_piece>::const_iterator i = m_downloads[k].begin();
|
|
|
|
i != m_downloads[k].end() - 1; ++i)
|
|
|
|
{
|
|
|
|
downloading_piece const& dp = *i;
|
|
|
|
downloading_piece const& next = *(i + 1);
|
|
|
|
// TORRENT_ASSERT(dp.finished + dp.writing >= next.finished + next.writing);
|
|
|
|
TORRENT_ASSERT(dp.index < next.index);
|
|
|
|
TORRENT_ASSERT(dp.info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(dp.info < &m_block_info[0] + m_block_info.size());
|
|
|
|
TORRENT_ASSERT((dp.info - &m_block_info[0]) % m_blocks_per_piece == 0);
|
|
|
|
for (int k = 0; k < m_blocks_per_piece; ++k)
|
|
|
|
{
|
|
|
|
if (dp.info[k].peer)
|
|
|
|
{
|
|
|
|
torrent_peer* p = (torrent_peer*)dp.info[k].peer;
|
|
|
|
TORRENT_ASSERT(p->in_use);
|
|
|
|
TORRENT_ASSERT(p->connection == NULL || static_cast<peer_connection*>(p->connection)->m_in_use);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-09-10 01:46:28 +02:00
|
|
|
void piece_picker::verify_pick(std::vector<piece_block> const& picked
|
2008-05-28 04:35:02 +02:00
|
|
|
, bitfield const& bits) const
|
2007-09-10 01:46:28 +02:00
|
|
|
{
|
2008-05-28 04:35:02 +02:00
|
|
|
TORRENT_ASSERT(bits.size() == m_piece_map.size());
|
2007-09-10 01:46:28 +02:00
|
|
|
for (std::vector<piece_block>::const_iterator i = picked.begin()
|
|
|
|
, end(picked.end()); i != end; ++i)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->piece_index >= 0);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(i->piece_index < bits.size());
|
2008-05-28 04:35:02 +02:00
|
|
|
TORRENT_ASSERT(bits[i->piece_index]);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_piece_map[i->piece_index].have());
|
2013-01-30 07:20:37 +01:00
|
|
|
TORRENT_ASSERT(!m_piece_map[i->piece_index].filtered());
|
2007-09-10 01:46:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
void piece_picker::verify_priority(int range_start, int range_end, int prio) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(range_start <= range_end);
|
|
|
|
TORRENT_ASSERT(range_end <= int(m_pieces.size()));
|
|
|
|
for (std::vector<int>::const_iterator i = m_pieces.begin() + range_start
|
|
|
|
, end(m_pieces.begin() + range_end); i != end; ++i)
|
|
|
|
{
|
|
|
|
int index = *i;
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < int(m_piece_map.size()));
|
|
|
|
int p = m_piece_map[index].priority(this);
|
|
|
|
TORRENT_ASSERT(p == prio);
|
|
|
|
}
|
|
|
|
}
|
2008-02-18 04:07:14 +01:00
|
|
|
|
2009-04-04 23:50:36 +02:00
|
|
|
#if defined TORRENT_PICKER_LOG
|
2008-01-31 18:52:29 +01:00
|
|
|
void piece_picker::print_pieces() const
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
int limit = 10;
|
|
|
|
std::cerr << "[" << this << "] ";
|
2008-01-31 18:52:29 +01:00
|
|
|
for (std::vector<int>::const_iterator i = m_priority_boundries.begin()
|
|
|
|
, end(m_priority_boundries.end()); i != end; ++i)
|
|
|
|
{
|
2008-06-07 04:58:28 +02:00
|
|
|
std::cerr << *i << " ";
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << std::endl;
|
2008-01-31 18:52:29 +01:00
|
|
|
int index = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] ";
|
2008-01-31 18:52:29 +01:00
|
|
|
std::vector<int>::const_iterator j = m_priority_boundries.begin();
|
|
|
|
for (std::vector<int>::const_iterator i = m_pieces.begin()
|
|
|
|
, end(m_pieces.end()); i != end; ++i, ++index)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (limit == 0) break;
|
2008-01-31 18:52:29 +01:00
|
|
|
if (*i == -1) break;
|
|
|
|
while (j != m_priority_boundries.end() && *j <= index)
|
|
|
|
{
|
2008-06-07 04:58:28 +02:00
|
|
|
std::cerr << "| ";
|
2008-01-31 18:52:29 +01:00
|
|
|
++j;
|
|
|
|
}
|
2008-06-07 04:58:28 +02:00
|
|
|
std::cerr << *i << "(" << m_piece_map[*i].index << ") ";
|
2014-07-06 21:18:00 +02:00
|
|
|
--limit;
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2008-06-07 04:58:28 +02:00
|
|
|
std::cerr << std::endl;
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2013-02-25 05:13:46 +01:00
|
|
|
#endif // TORRENT_PIECE_PICKER
|
2014-01-21 20:26:09 +01:00
|
|
|
#endif // TORRENT_USE_INVARIANT_CHECKS
|
2008-02-18 04:07:14 +01:00
|
|
|
|
2014-01-21 20:26:09 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2014-07-06 21:18:00 +02:00
|
|
|
void piece_picker::check_peer_invariant(bitfield const& have
|
|
|
|
, void const* p) const
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
int num_pieces = have.size();
|
|
|
|
for (int i = 0; i < num_pieces; ++i)
|
|
|
|
{
|
|
|
|
int h = have[i];
|
|
|
|
TORRENT_ASSERT(m_piece_map[i].have_peers.count(p) == h);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void piece_picker::check_invariant(torrent const* t) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifndef TORRENT_DEBUG_REFCOUNTS
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_OPTIMIZE_MEMORY_USAGE
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(sizeof(piece_pos) == 4);
|
2012-04-01 21:14:30 +02:00
|
|
|
#else
|
|
|
|
TORRENT_ASSERT(sizeof(piece_pos) == 8);
|
2013-01-06 05:02:29 +01:00
|
|
|
#endif
|
2012-04-01 21:14:30 +02:00
|
|
|
#endif
|
2007-12-16 02:35:42 +01:00
|
|
|
TORRENT_ASSERT(m_num_have >= 0);
|
|
|
|
TORRENT_ASSERT(m_num_have_filtered >= 0);
|
|
|
|
TORRENT_ASSERT(m_num_filtered >= 0);
|
2008-06-11 10:30:06 +02:00
|
|
|
TORRENT_ASSERT(m_seeds >= 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int k = 0; k < num_download_categories; ++k)
|
2007-07-15 17:41:55 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!m_downloads[k].empty())
|
2007-07-15 17:41:55 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<downloading_piece>::const_iterator i = m_downloads[k].begin();
|
|
|
|
i != m_downloads[k].end() - 1; ++i)
|
|
|
|
{
|
|
|
|
downloading_piece const& dp = *i;
|
|
|
|
downloading_piece const& next = *(i + 1);
|
|
|
|
// TORRENT_ASSERT(dp.finished + dp.writing >= next.finished + next.writing);
|
|
|
|
TORRENT_ASSERT(dp.index < next.index);
|
|
|
|
TORRENT_ASSERT(dp.info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(dp.info < &m_block_info[0] + m_block_info.size());
|
|
|
|
TORRENT_ASSERT((dp.info - &m_block_info[0]) % m_blocks_per_piece == 0);
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
for (int k = 0; k < m_blocks_per_piece; ++k)
|
|
|
|
{
|
|
|
|
if (dp.info[k].peer)
|
|
|
|
{
|
|
|
|
torrent_peer* p = (torrent_peer*)dp.info[k].peer;
|
|
|
|
TORRENT_ASSERT(p->in_use);
|
|
|
|
TORRENT_ASSERT(p->connection == NULL
|
|
|
|
|| static_cast<peer_connection*>(p->connection)->m_in_use);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2007-07-15 17:41:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
if (t != 0)
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((int)m_piece_map.size() == t->torrent_file().num_pieces());
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int j = 0; j < num_download_categories; ++j)
|
2007-04-27 02:27:37 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<downloading_piece>::const_iterator i = m_downloads[j].begin()
|
|
|
|
, end(m_downloads[j].end()); i != end; ++i)
|
2007-04-27 02:27:37 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(m_piece_map[i->index].state == j + 1);
|
|
|
|
bool blocks_requested = false;
|
|
|
|
int num_blocks = blocks_in_piece(i->index);
|
|
|
|
int num_requested = 0;
|
|
|
|
int num_finished = 0;
|
|
|
|
int num_writing = 0;
|
|
|
|
int num_open = 0;
|
|
|
|
for (int k = 0; k < num_blocks; ++k)
|
2007-04-27 02:27:37 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(i->info[k].piece_index == i->index);
|
|
|
|
TORRENT_ASSERT(i->info[k].peer == 0 || static_cast<torrent_peer*>(i->info[k].peer)->in_use);
|
|
|
|
if (i->info[k].state == block_info::state_finished)
|
|
|
|
{
|
|
|
|
++num_finished;
|
|
|
|
TORRENT_ASSERT(i->info[k].num_peers == 0);
|
|
|
|
}
|
|
|
|
else if (i->info[k].state == block_info::state_requested)
|
|
|
|
{
|
|
|
|
++num_requested;
|
|
|
|
blocks_requested = true;
|
|
|
|
TORRENT_ASSERT(i->info[k].num_peers > 0);
|
|
|
|
}
|
|
|
|
else if (i->info[k].state == block_info::state_writing)
|
|
|
|
{
|
|
|
|
++num_writing;
|
|
|
|
TORRENT_ASSERT(i->info[k].num_peers == 0);
|
|
|
|
}
|
|
|
|
else if (i->info[k].state == block_info::state_none)
|
|
|
|
{
|
|
|
|
++num_open;
|
|
|
|
TORRENT_ASSERT(i->info[k].num_peers == 0);
|
|
|
|
}
|
2007-04-27 02:27:37 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
switch(j + 1)
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
case piece_pos::piece_downloading:
|
|
|
|
TORRENT_ASSERT(!m_piece_map[i->index].filtered());
|
|
|
|
TORRENT_ASSERT(num_open > 0);
|
|
|
|
break;
|
|
|
|
case piece_pos::piece_full:
|
|
|
|
TORRENT_ASSERT(!m_piece_map[i->index].filtered());
|
|
|
|
TORRENT_ASSERT(num_open == 0);
|
|
|
|
// if requested == 0, the piece should be in the finished state
|
|
|
|
TORRENT_ASSERT(num_requested > 0);
|
|
|
|
break;
|
|
|
|
case piece_pos::piece_finished:
|
|
|
|
TORRENT_ASSERT(!m_piece_map[i->index].filtered());
|
|
|
|
TORRENT_ASSERT(num_open == 0);
|
|
|
|
TORRENT_ASSERT(num_requested == 0);
|
|
|
|
TORRENT_ASSERT(num_finished + num_writing == num_blocks);
|
|
|
|
break;
|
|
|
|
case piece_pos::piece_zero_prio:
|
|
|
|
TORRENT_ASSERT(m_piece_map[i->index].filtered());
|
|
|
|
break;
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(blocks_requested == (i->state != none));
|
|
|
|
TORRENT_ASSERT(num_requested == i->requested);
|
|
|
|
TORRENT_ASSERT(num_writing == i->writing);
|
|
|
|
TORRENT_ASSERT(num_finished == i->finished);
|
|
|
|
if (m_piece_map[i->index].state > piece_pos::piece_downloading
|
|
|
|
&& m_piece_map[i->index].state < piece_pos::piece_zero_prio)
|
|
|
|
TORRENT_ASSERT(num_finished + num_writing + num_requested == num_blocks);
|
2007-04-27 02:27:37 +02:00
|
|
|
}
|
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
int num_pieces = int(m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(m_cursor >= 0);
|
|
|
|
TORRENT_ASSERT(m_cursor <= num_pieces);
|
|
|
|
TORRENT_ASSERT(m_reverse_cursor <= num_pieces);
|
|
|
|
TORRENT_ASSERT(m_reverse_cursor >= 0);
|
|
|
|
TORRENT_ASSERT(m_reverse_cursor > m_cursor
|
|
|
|
|| (m_cursor == num_pieces && m_reverse_cursor == 0));
|
|
|
|
|
2008-02-18 04:07:14 +01:00
|
|
|
#ifdef TORRENT_NO_EXPENSIVE_INVARIANT_CHECK
|
|
|
|
return;
|
|
|
|
#endif
|
2007-04-27 02:27:37 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (!m_dirty)
|
2008-02-18 04:07:14 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!m_priority_boundries.empty());
|
|
|
|
int prio = 0;
|
|
|
|
int start = 0;
|
|
|
|
for (std::vector<int>::const_iterator i = m_priority_boundries.begin()
|
|
|
|
, end(m_priority_boundries.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
verify_priority(start, *i, prio);
|
|
|
|
++prio;
|
|
|
|
start = *i;
|
|
|
|
}
|
|
|
|
TORRENT_ASSERT(m_priority_boundries.back() == int(m_pieces.size()));
|
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
int index = 0;
|
|
|
|
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end && (i->have() || i->filtered());
|
|
|
|
++i, ++index);
|
|
|
|
TORRENT_ASSERT(m_cursor == index);
|
|
|
|
index = num_pieces;
|
|
|
|
if (num_pieces > 0)
|
2008-02-18 04:07:14 +01:00
|
|
|
{
|
2009-08-30 09:38:52 +02:00
|
|
|
for (std::vector<piece_pos>::reverse_iterator i = m_piece_map.rend()
|
2008-09-15 18:21:03 +02:00
|
|
|
- index; index > 0 && (i->have() || i->filtered()); ++i, --index);
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(index == num_pieces
|
|
|
|
|| m_piece_map[index].have()
|
|
|
|
|| m_piece_map[index].filtered());
|
|
|
|
TORRENT_ASSERT(m_reverse_cursor == index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_reverse_cursor == 0);
|
2008-02-18 04:07:14 +01:00
|
|
|
}
|
2007-04-27 02:27:37 +02:00
|
|
|
|
2005-05-30 19:43:03 +02:00
|
|
|
int num_filtered = 0;
|
|
|
|
int num_have_filtered = 0;
|
2007-05-14 12:54:18 +02:00
|
|
|
int num_have = 0;
|
2003-10-23 01:00:57 +02:00
|
|
|
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin();
|
2005-05-13 02:39:39 +02:00
|
|
|
i != m_piece_map.end(); ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-25 19:18:36 +01:00
|
|
|
int index = static_cast<int>(i - m_piece_map.begin());
|
2008-01-31 18:52:29 +01:00
|
|
|
piece_pos const& p = *i;
|
|
|
|
|
|
|
|
if (p.filtered())
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
if (p.index != piece_pos::we_have_index)
|
2005-05-30 19:43:03 +02:00
|
|
|
++num_filtered;
|
|
|
|
else
|
|
|
|
++num_have_filtered;
|
|
|
|
}
|
2013-01-06 05:02:29 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
TORRENT_ASSERT(p.have_peers.size() == p.peer_count + m_seeds);
|
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
if (p.index == piece_pos::we_have_index)
|
2007-05-14 12:54:18 +02:00
|
|
|
++num_have;
|
|
|
|
|
2007-03-29 02:52:16 +02:00
|
|
|
#if 0
|
2003-10-23 01:00:57 +02:00
|
|
|
if (t != 0)
|
|
|
|
{
|
|
|
|
int actual_peer_count = 0;
|
2004-01-13 04:08:59 +01:00
|
|
|
for (torrent::const_peer_iterator peer = t->begin();
|
2005-05-13 02:39:39 +02:00
|
|
|
peer != t->end(); ++peer)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-01-13 04:08:59 +01:00
|
|
|
if (peer->second->has_piece(index)) actual_peer_count++;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((int)i->peer_count == actual_peer_count);
|
2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
int num_downloaders = 0;
|
|
|
|
for (std::vector<peer_connection*>::const_iterator peer = t->begin();
|
|
|
|
peer != t->end();
|
|
|
|
++peer)
|
|
|
|
{
|
|
|
|
const std::vector<piece_block>& queue = (*peer)->download_queue();
|
|
|
|
if (std::find_if(queue.begin(), queue.end(), has_index(index)) == queue.end()) continue;
|
|
|
|
|
|
|
|
++num_downloaders;
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (i->downloading())
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(num_downloaders == 1);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(num_downloaders == 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
2007-03-29 02:52:16 +02:00
|
|
|
#endif
|
2007-04-15 04:14:02 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (p.index == piece_pos::we_have_index)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(t == 0 || t->have_piece(index));
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(p.downloading() == false);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (t != 0)
|
|
|
|
TORRENT_ASSERT(!t->have_piece(index));
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
int prio = p.priority(this);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(prio == -1 || p.downloading() == (prio % piece_picker::prio_factor == 0));
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
if (!m_dirty)
|
2008-01-31 18:52:29 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(prio < int(m_priority_boundries.size())
|
|
|
|
|| m_dirty);
|
|
|
|
if (prio >= 0)
|
2006-07-16 02:08:50 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(p.index < m_pieces.size());
|
|
|
|
TORRENT_ASSERT(m_pieces[p.index] == index);
|
2006-07-16 02:08:50 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
else
|
2007-03-21 03:09:50 +01:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(prio == -1);
|
|
|
|
// make sure there's no entry
|
|
|
|
// with this index. (there shouldn't
|
|
|
|
// be since the priority is -1)
|
|
|
|
TORRENT_ASSERT(std::find(m_pieces.begin(), m_pieces.end(), index)
|
|
|
|
== m_pieces.end());
|
2007-03-21 03:09:50 +01:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2007-04-15 04:14:02 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int count_downloading = std::count_if(m_downloads[0].begin(), m_downloads[0].end()
|
2007-03-29 02:52:16 +02:00
|
|
|
, has_index(index));
|
2014-07-06 21:18:00 +02:00
|
|
|
int count_full = std::count_if(m_downloads[1].begin(), m_downloads[1].end()
|
|
|
|
, has_index(index));
|
|
|
|
int count_finished = std::count_if(m_downloads[2].begin(), m_downloads[2].end()
|
|
|
|
, has_index(index));
|
|
|
|
TORRENT_ASSERT(i->state == piece_pos::piece_open
|
|
|
|
|| i->state == piece_pos::piece_zero_prio
|
|
|
|
|| count_downloading + count_full + count_finished == 1);
|
|
|
|
|
|
|
|
switch(i->state)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
case piece_pos::piece_open:
|
|
|
|
TORRENT_ASSERT(count_downloading + count_full + count_finished == 0);
|
|
|
|
break;
|
|
|
|
case piece_pos::piece_downloading:
|
|
|
|
TORRENT_ASSERT(count_downloading == 1);
|
|
|
|
break;
|
|
|
|
case piece_pos::piece_full:
|
|
|
|
TORRENT_ASSERT(count_full == 1);
|
|
|
|
break;
|
|
|
|
case piece_pos::piece_finished:
|
|
|
|
TORRENT_ASSERT(count_finished == 1);
|
|
|
|
break;
|
|
|
|
};
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(num_have == m_num_have);
|
|
|
|
TORRENT_ASSERT(num_filtered == m_num_filtered);
|
|
|
|
TORRENT_ASSERT(num_have_filtered == m_num_have_filtered);
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
if (!m_dirty)
|
|
|
|
{
|
|
|
|
for (std::vector<int>::const_iterator i = m_pieces.begin()
|
|
|
|
, end(m_pieces.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_piece_map[*i].priority(this) >= 0);
|
|
|
|
}
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2004-09-08 01:16:11 +02:00
|
|
|
#endif
|
|
|
|
|
2009-07-19 06:59:27 +02:00
|
|
|
std::pair<int, int> piece_picker::distributed_copies() const
|
2004-08-05 15:56:26 +02:00
|
|
|
{
|
2008-06-11 10:30:06 +02:00
|
|
|
TORRENT_ASSERT(m_seeds >= 0);
|
2009-07-19 06:59:27 +02:00
|
|
|
const int num_pieces = m_piece_map.size();
|
2004-09-08 01:16:11 +02:00
|
|
|
|
2010-02-22 02:51:25 +01:00
|
|
|
if (num_pieces == 0) return std::make_pair(1, 0);
|
2007-04-11 22:33:28 +02:00
|
|
|
int min_availability = piece_pos::max_peer_count;
|
|
|
|
// find the lowest availability count
|
|
|
|
// count the number of pieces that have that availability
|
|
|
|
// and also the number of pieces that have more than that.
|
|
|
|
int integer_part = 0;
|
|
|
|
int fraction_part = 0;
|
|
|
|
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i)
|
2004-08-05 15:56:26 +02:00
|
|
|
{
|
2007-04-11 22:33:28 +02:00
|
|
|
int peer_count = int(i->peer_count);
|
|
|
|
// take ourself into account
|
|
|
|
if (i->have()) ++peer_count;
|
|
|
|
if (min_availability > peer_count)
|
|
|
|
{
|
2007-08-27 07:13:09 +02:00
|
|
|
min_availability = peer_count;
|
2007-04-11 22:33:28 +02:00
|
|
|
fraction_part += integer_part;
|
|
|
|
integer_part = 1;
|
|
|
|
}
|
|
|
|
else if (peer_count == min_availability)
|
|
|
|
{
|
|
|
|
++integer_part;
|
|
|
|
}
|
|
|
|
else
|
2004-09-08 01:16:11 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(peer_count > min_availability);
|
2007-04-11 22:33:28 +02:00
|
|
|
++fraction_part;
|
2004-08-05 15:56:26 +02:00
|
|
|
}
|
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(integer_part + fraction_part == num_pieces);
|
2009-07-19 06:59:27 +02:00
|
|
|
return std::make_pair(min_availability + m_seeds, fraction_part * 1000 / num_pieces);
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void piece_picker::priority_range(int prio, int* start, int* end)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(prio >= 0);
|
|
|
|
TORRENT_ASSERT(prio < int(m_priority_boundries.size())
|
|
|
|
|| m_dirty);
|
|
|
|
if (prio == 0) *start = 0;
|
|
|
|
else *start = m_priority_boundries[prio - 1];
|
|
|
|
*end = m_priority_boundries[prio];
|
|
|
|
TORRENT_ASSERT(*start <= *end);
|
2004-08-05 15:56:26 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-09-01 23:04:21 +02:00
|
|
|
void piece_picker::add(int index)
|
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(!m_dirty);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < int(m_piece_map.size()));
|
2005-09-01 23:04:21 +02:00
|
|
|
piece_pos& p = m_piece_map[index];
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!p.filtered());
|
|
|
|
TORRENT_ASSERT(!p.have());
|
2005-09-01 23:04:21 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int priority = p.priority(this);
|
|
|
|
TORRENT_ASSERT(priority >= 0);
|
|
|
|
if (int(m_priority_boundries.size()) <= priority)
|
|
|
|
m_priority_boundries.resize(priority + 1, m_pieces.size());
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(int(m_priority_boundries.size()) >= priority);
|
2005-09-01 23:04:21 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int range_start, range_end;
|
|
|
|
priority_range(priority, &range_start, &range_end);
|
|
|
|
int new_index;
|
|
|
|
if (range_end == range_start) new_index = range_start;
|
2011-02-26 08:55:51 +01:00
|
|
|
else new_index = random() % (range_end - range_start + 1) + range_start;
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "add " << index << " (" << priority << ")" << std::endl;
|
|
|
|
std::cerr << "[" << this << "] " << " p: state: " << p.state
|
|
|
|
<< " peer_count: " << p.peer_count
|
|
|
|
<< " prio: " << p.piece_priority
|
|
|
|
<< " index: " << p.index << std::endl;
|
2008-01-31 18:52:29 +01:00
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
m_pieces.push_back(-1);
|
|
|
|
|
|
|
|
for (;;)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(new_index < int(m_pieces.size()));
|
|
|
|
int temp = m_pieces[new_index];
|
|
|
|
m_pieces[new_index] = index;
|
|
|
|
m_piece_map[index].index = new_index;
|
|
|
|
index = temp;
|
|
|
|
do
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
temp = m_priority_boundries[priority]++;
|
|
|
|
++priority;
|
|
|
|
} while (temp == new_index && priority < int(m_priority_boundries.size()));
|
|
|
|
new_index = temp;
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << " index: " << index
|
2008-01-31 18:52:29 +01:00
|
|
|
<< " prio: " << priority
|
|
|
|
<< " new_index: " << new_index
|
|
|
|
<< std::endl;
|
|
|
|
#endif
|
|
|
|
if (priority >= int(m_priority_boundries.size())) break;
|
|
|
|
TORRENT_ASSERT(temp >= 0);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
if (index != -1)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(new_index == int(m_pieces.size() - 1));
|
|
|
|
m_pieces[new_index] = index;
|
|
|
|
m_piece_map[index].index = new_index;
|
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
|
|
|
// shuffle(priority, new_index);
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
// print_pieces();
|
2008-01-31 18:52:29 +01:00
|
|
|
#endif
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2006-04-25 23:04:48 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
void piece_picker::remove(int priority, int elem_index)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(!m_dirty);
|
|
|
|
TORRENT_ASSERT(priority >= 0);
|
2012-04-02 07:30:15 +02:00
|
|
|
TORRENT_ASSERT(elem_index < int(m_pieces.size()));
|
|
|
|
TORRENT_ASSERT(elem_index >= 0);
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "remove " << m_pieces[elem_index] << " (" << priority << ")" << std::endl;
|
2008-01-31 18:52:29 +01:00
|
|
|
#endif
|
|
|
|
int next_index = elem_index;
|
|
|
|
TORRENT_ASSERT(m_piece_map[m_pieces[elem_index]].priority(this) == -1);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(elem_index < int(m_pieces.size()));
|
|
|
|
int temp;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
temp = --m_priority_boundries[priority];
|
|
|
|
++priority;
|
|
|
|
} while (next_index == temp && priority < int(m_priority_boundries.size()));
|
|
|
|
if (next_index == temp) break;
|
|
|
|
next_index = temp;
|
|
|
|
|
|
|
|
int piece = m_pieces[next_index];
|
|
|
|
m_pieces[elem_index] = piece;
|
|
|
|
m_piece_map[piece].index = elem_index;
|
|
|
|
TORRENT_ASSERT(m_piece_map[piece].priority(this) == priority - 1);
|
|
|
|
TORRENT_ASSERT(elem_index < int(m_pieces.size() - 1));
|
|
|
|
elem_index = next_index;
|
|
|
|
|
|
|
|
if (priority == int(m_priority_boundries.size()))
|
|
|
|
break;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
m_pieces.pop_back();
|
|
|
|
TORRENT_ASSERT(next_index == int(m_pieces.size()));
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
2005-09-01 23:04:21 +02:00
|
|
|
}
|
2005-05-25 12:01:01 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// will update the piece with the given properties (priority, elem_index)
|
2008-01-31 18:52:29 +01:00
|
|
|
// to place it at the correct position
|
|
|
|
void piece_picker::update(int priority, int elem_index)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(!m_dirty);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(elem_index >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(elem_index < int(m_piece_map.size()));
|
|
|
|
TORRENT_ASSERT(priority >= 0);
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(int(m_priority_boundries.size()) > priority);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// make sure the passed in elem_index actually lives in the specified
|
|
|
|
// priority bucket. If it doesn't, it means this piece changed
|
|
|
|
// state without updating the corresponding entry in the pieces list
|
|
|
|
TORRENT_ASSERT(m_priority_boundries[priority] >= elem_index);
|
|
|
|
TORRENT_ASSERT(priority == 0 || m_priority_boundries[priority-1] <= elem_index);
|
|
|
|
TORRENT_ASSERT(priority + 1 == m_priority_boundries.size() || m_priority_boundries[priority+1] > elem_index);
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int index = m_pieces[elem_index];
|
2003-10-23 01:00:57 +02:00
|
|
|
// update the piece_map
|
|
|
|
piece_pos& p = m_piece_map[index];
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(int(p.index) == elem_index || p.have());
|
2007-03-21 03:09:50 +01:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int new_priority = p.priority(this);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
if (new_priority == priority) return;
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (new_priority == -1)
|
2006-07-16 02:08:50 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
remove(priority, elem_index);
|
|
|
|
return;
|
2006-07-16 02:08:50 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (int(m_priority_boundries.size()) <= new_priority)
|
|
|
|
m_priority_boundries.resize(new_priority + 1, m_pieces.size());
|
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "update " << index << " (" << priority << "->" << new_priority << ")" << std::endl;
|
2008-01-31 18:52:29 +01:00
|
|
|
#endif
|
|
|
|
if (priority > new_priority)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
int new_index;
|
|
|
|
int temp = index;
|
|
|
|
for (;;)
|
2006-04-25 23:04:48 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(priority > 0);
|
2008-01-31 18:52:29 +01:00
|
|
|
--priority;
|
|
|
|
new_index = m_priority_boundries[priority]++;
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(new_index >= 0);
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(new_index < int(m_pieces.size()));
|
|
|
|
if (temp != m_pieces[new_index])
|
|
|
|
{
|
|
|
|
temp = m_pieces[new_index];
|
|
|
|
m_pieces[elem_index] = temp;
|
|
|
|
m_piece_map[temp].index = elem_index;
|
|
|
|
TORRENT_ASSERT(elem_index < int(m_pieces.size()));
|
|
|
|
}
|
|
|
|
elem_index = new_index;
|
|
|
|
if (priority == new_priority) break;
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
m_pieces[elem_index] = index;
|
|
|
|
m_piece_map[index].index = elem_index;
|
|
|
|
TORRENT_ASSERT(elem_index < int(m_pieces.size()));
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
shuffle(priority, elem_index);
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_piece_map[index].priority(this) == priority);
|
2006-04-25 23:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
int new_index;
|
|
|
|
int temp = index;
|
|
|
|
for (;;)
|
2006-07-16 02:08:50 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(priority >= 0);
|
|
|
|
TORRENT_ASSERT(priority < int(m_priority_boundries.size()));
|
2008-01-31 18:52:29 +01:00
|
|
|
new_index = --m_priority_boundries[priority];
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(new_index >= 0);
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(new_index < int(m_pieces.size()));
|
|
|
|
if (temp != m_pieces[new_index])
|
|
|
|
{
|
|
|
|
temp = m_pieces[new_index];
|
|
|
|
m_pieces[elem_index] = temp;
|
|
|
|
m_piece_map[temp].index = elem_index;
|
|
|
|
TORRENT_ASSERT(elem_index < int(m_pieces.size()));
|
|
|
|
}
|
|
|
|
elem_index = new_index;
|
|
|
|
++priority;
|
|
|
|
if (priority == new_priority) break;
|
2006-07-16 02:08:50 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
m_pieces[elem_index] = index;
|
|
|
|
m_piece_map[index].index = elem_index;
|
|
|
|
TORRENT_ASSERT(elem_index < int(m_pieces.size()));
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
shuffle(priority, elem_index);
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(m_piece_map[index].priority(this) == priority);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
void piece_picker::shuffle(int priority, int elem_index)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "shuffle()" << std::endl;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(!m_dirty);
|
|
|
|
TORRENT_ASSERT(priority >= 0);
|
|
|
|
TORRENT_ASSERT(elem_index >= 0);
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(elem_index < int(m_pieces.size()));
|
|
|
|
TORRENT_ASSERT(m_piece_map[m_pieces[elem_index]].priority(this) == priority);
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
int range_start, range_end;
|
|
|
|
priority_range(priority, &range_start, &range_end);
|
|
|
|
TORRENT_ASSERT(range_start < range_end);
|
2011-02-26 08:55:51 +01:00
|
|
|
int other_index = random() % (range_end - range_start) + range_start;
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
if (other_index == elem_index) return;
|
|
|
|
|
|
|
|
// swap other_index with elem_index
|
|
|
|
piece_pos& p1 = m_piece_map[m_pieces[other_index]];
|
|
|
|
piece_pos& p2 = m_piece_map[m_pieces[elem_index]];
|
|
|
|
|
|
|
|
int temp = p1.index;
|
|
|
|
p1.index = p2.index;
|
|
|
|
p2.index = temp;
|
|
|
|
std::swap(m_pieces[other_index], m_pieces[elem_index]);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2009-09-08 04:38:53 +02:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
void piece_picker::restore_piece(int index)
|
|
|
|
{
|
2006-07-16 02:08:50 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "restore_piece(" << index << ")" << std::endl;
|
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < (int)m_piece_map.size());
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int state = m_piece_map[index].state;
|
|
|
|
TORRENT_ASSERT(state != piece_pos::piece_open);
|
|
|
|
if (state == piece_pos::piece_open) return;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(state - 1, index);
|
2008-02-18 04:07:14 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
2014-01-22 10:53:47 +01:00
|
|
|
TORRENT_ASSERT(i->info >= &m_block_info[0]
|
|
|
|
&& i->info < &m_block_info[0] + m_block_info.size());
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
i->locked = false;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
piece_pos& p = m_piece_map[index];
|
2008-01-31 18:52:29 +01:00
|
|
|
int prev_priority = p.priority(this);
|
2009-06-10 10:30:55 +02:00
|
|
|
erase_download_piece(i);
|
2008-01-31 18:52:29 +01:00
|
|
|
int new_priority = p.priority(this);
|
2007-03-20 20:50:15 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
2007-03-20 20:50:15 +01:00
|
|
|
if (new_priority == prev_priority) return;
|
2008-01-31 18:52:29 +01:00
|
|
|
if (m_dirty) return;
|
2011-11-07 05:31:48 +01:00
|
|
|
if (prev_priority == -1) add(index);
|
|
|
|
else update(prev_priority, p.index);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
void piece_picker::inc_refcount_all(const void* peer)
|
2007-04-15 04:14:02 +02:00
|
|
|
{
|
2010-09-01 05:00:15 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2007-04-15 04:14:02 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2010-09-01 05:00:15 +02:00
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
++m_seeds;
|
|
|
|
if (m_seeds == 1)
|
2007-04-15 04:14:02 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
// when m_seeds is increased from 0 to 1
|
|
|
|
// we may have to add pieces that previously
|
|
|
|
// didn't have any peers
|
|
|
|
m_dirty = true;
|
2007-04-15 04:14:02 +02:00
|
|
|
}
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
for (std::vector<piece_pos>::iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(i->have_peers.count(peer) == 0);
|
|
|
|
i->have_peers.insert(peer);
|
|
|
|
}
|
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2007-04-17 02:20:39 +02:00
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
void piece_picker::dec_refcount_all(const void* peer)
|
2008-01-31 18:52:29 +01:00
|
|
|
{
|
2010-09-01 05:00:15 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2010-09-01 05:00:15 +02:00
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
if (m_seeds > 0)
|
2007-04-17 02:20:39 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
--m_seeds;
|
|
|
|
if (m_seeds == 0)
|
2007-04-17 02:20:39 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
// when m_seeds is decreased from 1 to 0
|
|
|
|
// we may have to remove pieces that previously
|
|
|
|
// didn't have any peers
|
|
|
|
m_dirty = true;
|
2007-04-17 02:20:39 +02:00
|
|
|
}
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
for (std::vector<piece_pos>::iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(i->have_peers.count(peer) == 1);
|
|
|
|
i->have_peers.erase(peer);
|
|
|
|
}
|
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
return;
|
2007-04-17 02:20:39 +02:00
|
|
|
}
|
2008-06-11 10:30:06 +02:00
|
|
|
TORRENT_ASSERT(m_seeds == 0);
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2007-04-15 04:14:02 +02:00
|
|
|
for (std::vector<piece_pos>::iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i)
|
|
|
|
{
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
TORRENT_ASSERT(i->have_peers.count(peer) == 1);
|
|
|
|
i->have_peers.erase(peer);
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(i->peer_count > 0);
|
|
|
|
--i->peer_count;
|
2007-04-15 04:14:02 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
m_dirty = true;
|
2007-04-15 04:14:02 +02:00
|
|
|
}
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
void piece_picker::inc_refcount(int index, const void* peer)
|
2007-04-15 04:14:02 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2007-04-15 04:14:02 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "inc_refcount(" << index << ")" << std::endl;
|
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
piece_pos& p = m_piece_map[index];
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
TORRENT_ASSERT(p.have_peers.count(peer) == 0);
|
|
|
|
p.have_peers.insert(peer);
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int prev_priority = p.priority(this);
|
|
|
|
++p.peer_count;
|
|
|
|
if (m_dirty) return;
|
|
|
|
int new_priority = p.priority(this);
|
|
|
|
if (prev_priority == new_priority) return;
|
|
|
|
if (prev_priority == -1)
|
|
|
|
add(index);
|
|
|
|
else
|
|
|
|
update(prev_priority, p.index);
|
|
|
|
}
|
|
|
|
|
2012-05-14 06:48:23 +02:00
|
|
|
// this function decrements the m_seeds counter
|
|
|
|
// and increments the peer counter on every piece
|
|
|
|
// instead. Sometimes of we connect to a seed that
|
|
|
|
// later sends us a dont-have message, we'll need to
|
|
|
|
// turn that m_seed into counts on the pieces since
|
|
|
|
// they can't be negative
|
|
|
|
void piece_picker::break_one_seed()
|
|
|
|
{
|
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(m_seeds > 0);
|
|
|
|
--m_seeds;
|
|
|
|
|
|
|
|
for (std::vector<piece_pos>::iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
++i->peer_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_dirty = true;
|
|
|
|
}
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
void piece_picker::dec_refcount(int index, const void* peer)
|
2008-01-31 18:52:29 +01:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2007-04-17 02:20:39 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "dec_refcount(" << index << ")" << std::endl;
|
2013-01-06 05:02:29 +01:00
|
|
|
#endif
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
piece_pos& p = m_piece_map[index];
|
|
|
|
|
2012-05-14 06:48:23 +02:00
|
|
|
if (p.peer_count == 0)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_seeds > 0);
|
|
|
|
// this is the case where we have one or more
|
|
|
|
// seeds, and one of them saying: I don't have this
|
|
|
|
// piece anymore. we need to break up one of the seed
|
|
|
|
// counters into actual peer counters on the pieces
|
|
|
|
break_one_seed();
|
|
|
|
}
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int prev_priority = p.priority(this);
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
TORRENT_ASSERT(p.have_peers.count(peer) == 1);
|
|
|
|
p.have_peers.erase(peer);
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(p.peer_count > 0);
|
|
|
|
--p.peer_count;
|
|
|
|
if (m_dirty) return;
|
|
|
|
if (prev_priority >= 0) update(prev_priority, p.index);
|
|
|
|
}
|
2007-04-15 04:14:02 +02:00
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
void piece_picker::inc_refcount(bitfield const& bitmask, const void* peer)
|
2008-01-31 18:52:29 +01:00
|
|
|
{
|
2009-05-22 05:36:05 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2009-05-22 05:36:05 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "inc_refcount(bitfield)" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// nothing set, nothing to do here
|
|
|
|
if (bitmask.none_set()) return;
|
|
|
|
|
|
|
|
if (bitmask.all_set() && bitmask.size() == m_piece_map.size())
|
|
|
|
{
|
|
|
|
inc_refcount_all(peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int size = (std::min)(50, int(bitmask.size()/2));
|
|
|
|
|
|
|
|
// this is an optimization where if just a few
|
|
|
|
// pieces end up changing, instead of making
|
|
|
|
// the piece list dirty, just update those pieces
|
|
|
|
// instead
|
|
|
|
int* incremented = TORRENT_ALLOCA(int, size);
|
|
|
|
int num_inc = 0;
|
|
|
|
|
|
|
|
if (!m_dirty)
|
|
|
|
{
|
|
|
|
// first count how many pieces we're updating. If it's few (less than half)
|
|
|
|
// we'll just update them one at a time. Othewise we'll just update the counters
|
|
|
|
// and mark the picker as dirty, so we'll rebuild it next time we need it.
|
|
|
|
// this only matters if we're not already dirty, in which case the fasted
|
|
|
|
// thing to do is to just update the counters and be done
|
|
|
|
int index = 0;
|
|
|
|
for (bitfield::const_iterator i = bitmask.begin()
|
|
|
|
, end(bitmask.end()); i != end; ++i, ++index)
|
|
|
|
{
|
|
|
|
if (!*i) continue;
|
|
|
|
if (num_inc < size) incremented[num_inc] = index;
|
|
|
|
++num_inc;
|
|
|
|
if (num_inc >= size) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_inc < size)
|
|
|
|
{
|
|
|
|
// not that many pieces were updated
|
|
|
|
// just update those individually instead of
|
|
|
|
// rebuilding the whole piece list
|
|
|
|
for (int i = 0; i < num_inc; ++i)
|
|
|
|
{
|
|
|
|
int piece = incremented[i];
|
|
|
|
piece_pos& p = m_piece_map[piece];
|
|
|
|
int prev_priority = p.priority(this);
|
|
|
|
++p.peer_count;
|
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
TORRENT_ASSERT(p.have_peers.count(peer) == 0);
|
|
|
|
p.have_peers.insert(peer);
|
|
|
|
#endif
|
|
|
|
int new_priority = p.priority(this);
|
|
|
|
if (prev_priority == new_priority) continue;
|
|
|
|
else if (prev_priority >= 0) update(prev_priority, p.index);
|
|
|
|
else add(piece);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-04-15 04:14:02 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int index = 0;
|
|
|
|
bool updated = false;
|
2008-05-28 04:35:02 +02:00
|
|
|
for (bitfield::const_iterator i = bitmask.begin()
|
2008-01-31 18:52:29 +01:00
|
|
|
, end(bitmask.end()); i != end; ++i, ++index)
|
|
|
|
{
|
|
|
|
if (*i)
|
2007-04-17 02:20:39 +02:00
|
|
|
{
|
2013-01-06 05:02:29 +01:00
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
TORRENT_ASSERT(m_piece_map[index].have_peers.count(peer) == 0);
|
|
|
|
m_piece_map[index].have_peers.insert(peer);
|
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
++m_piece_map[index].peer_count;
|
|
|
|
updated = true;
|
2007-04-17 02:20:39 +02:00
|
|
|
}
|
2007-04-15 04:14:02 +02:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// if we're already dirty, no point in doing anything more
|
|
|
|
if (m_dirty) return;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (updated) m_dirty = true;
|
2007-04-15 04:14:02 +02:00
|
|
|
}
|
|
|
|
|
2013-01-06 05:02:29 +01:00
|
|
|
void piece_picker::dec_refcount(bitfield const& bitmask, const void* peer)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2009-05-22 05:36:05 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2009-05-22 05:36:05 +02:00
|
|
|
#endif
|
2012-10-21 22:24:14 +02:00
|
|
|
TORRENT_ASSERT(bitmask.size() <= m_piece_map.size());
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "dec_refcount(bitfield)" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// nothing set, nothing to do here
|
|
|
|
if (bitmask.none_set()) return;
|
|
|
|
|
|
|
|
if (bitmask.all_set() && bitmask.size() == m_piece_map.size())
|
|
|
|
{
|
|
|
|
dec_refcount_all(peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int size = (std::min)(50, int(bitmask.size()/2));
|
|
|
|
|
|
|
|
// this is an optimization where if just a few
|
|
|
|
// pieces end up changing, instead of making
|
|
|
|
// the piece list dirty, just update those pieces
|
|
|
|
// instead
|
|
|
|
int* decremented = TORRENT_ALLOCA(int, size);
|
|
|
|
int num_dec = 0;
|
|
|
|
|
|
|
|
if (!m_dirty)
|
|
|
|
{
|
|
|
|
// first count how many pieces we're updating. If it's few (less than half)
|
|
|
|
// we'll just update them one at a time. Othewise we'll just update the counters
|
|
|
|
// and mark the picker as dirty, so we'll rebuild it next time we need it.
|
|
|
|
// this only matters if we're not already dirty, in which case the fasted
|
|
|
|
// thing to do is to just update the counters and be done
|
|
|
|
int index = 0;
|
|
|
|
for (bitfield::const_iterator i = bitmask.begin()
|
|
|
|
, end(bitmask.end()); i != end; ++i, ++index)
|
|
|
|
{
|
|
|
|
if (!*i) continue;
|
|
|
|
if (num_dec < size) decremented[num_dec] = index;
|
|
|
|
++num_dec;
|
|
|
|
if (num_dec >= size) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_dec < size)
|
|
|
|
{
|
|
|
|
// not that many pieces were updated
|
|
|
|
// just update those individually instead of
|
|
|
|
// rebuilding the whole piece list
|
|
|
|
for (int i = 0; i < num_dec; ++i)
|
|
|
|
{
|
|
|
|
int piece = decremented[i];
|
|
|
|
piece_pos& p = m_piece_map[piece];
|
|
|
|
int prev_priority = p.priority(this);
|
|
|
|
|
|
|
|
if (p.peer_count == 0)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_seeds > 0);
|
|
|
|
// this is the case where we have one or more
|
|
|
|
// seeds, and one of them saying: I don't have this
|
|
|
|
// piece anymore. we need to break up one of the seed
|
|
|
|
// counters into actual peer counters on the pieces
|
|
|
|
break_one_seed();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
TORRENT_ASSERT(p.have_peers.count(peer) == 1);
|
|
|
|
p.have_peers.erase(peer);
|
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(p.peer_count > 0);
|
|
|
|
--p.peer_count;
|
|
|
|
if (!m_dirty && prev_priority >= 0) update(prev_priority, p.index);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int index = 0;
|
|
|
|
bool updated = false;
|
2008-05-28 04:35:02 +02:00
|
|
|
for (bitfield::const_iterator i = bitmask.begin()
|
2008-01-31 18:52:29 +01:00
|
|
|
, end(bitmask.end()); i != end; ++i, ++index)
|
|
|
|
{
|
|
|
|
if (*i)
|
|
|
|
{
|
2012-05-14 06:48:23 +02:00
|
|
|
piece_pos& p = m_piece_map[index];
|
|
|
|
if (p.peer_count == 0)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_seeds > 0);
|
|
|
|
// this is the case where we have one or more
|
|
|
|
// seeds, and one of them saying: I don't have this
|
|
|
|
// piece anymore. we need to break up one of the seed
|
|
|
|
// counters into actual peer counters on the pieces
|
|
|
|
break_one_seed();
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_DEBUG_REFCOUNTS
|
|
|
|
TORRENT_ASSERT(p.have_peers.count(peer) == 1);
|
|
|
|
p.have_peers.erase(peer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TORRENT_ASSERT(p.peer_count > 0);
|
2012-05-14 06:48:23 +02:00
|
|
|
--p.peer_count;
|
2008-01-31 18:52:29 +01:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
2005-09-01 23:04:21 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// if we're already dirty, no point in doing anything more
|
|
|
|
if (m_dirty) return;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (updated) m_dirty = true;
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2005-09-01 23:04:21 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
void piece_picker::update_pieces() const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(m_dirty);
|
|
|
|
if (m_priority_boundries.empty()) m_priority_boundries.resize(1, 0);
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "update_pieces" << std::endl;
|
2008-01-31 18:52:29 +01:00
|
|
|
#endif
|
|
|
|
std::fill(m_priority_boundries.begin(), m_priority_boundries.end(), 0);
|
|
|
|
for (std::vector<piece_pos>::iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
int prio = i->priority(this);
|
|
|
|
if (prio == -1) continue;
|
|
|
|
if (prio >= int(m_priority_boundries.size()))
|
|
|
|
m_priority_boundries.resize(prio + 1, 0);
|
|
|
|
i->index = m_priority_boundries[prio];
|
|
|
|
++m_priority_boundries[prio];
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
for (std::vector<int>::iterator i = m_priority_boundries.begin()
|
|
|
|
, end(m_priority_boundries.end()); i != end; ++i)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
*i += index;
|
|
|
|
index = *i;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
m_pieces.resize(index, 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
2003-10-23 01:00:57 +02:00
|
|
|
#endif
|
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
index = 0;
|
|
|
|
for (std::vector<piece_pos>::iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i, ++index)
|
|
|
|
{
|
|
|
|
piece_pos& p = *i;
|
|
|
|
int prio = p.priority(this);
|
|
|
|
if (prio == -1) continue;
|
|
|
|
int new_index = (prio == 0 ? 0 : m_priority_boundries[prio - 1]) + p.index;
|
|
|
|
m_pieces[new_index] = index;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int start = 0;
|
|
|
|
for (std::vector<int>::iterator i = m_priority_boundries.begin()
|
|
|
|
, end(m_priority_boundries.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (start == *i) continue;
|
|
|
|
std::random_shuffle(&m_pieces[0] + start, &m_pieces[0] + *i);
|
|
|
|
start = *i;
|
|
|
|
}
|
2005-09-01 23:04:21 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
index = 0;
|
|
|
|
for (std::vector<int>::const_iterator i = m_pieces.begin()
|
|
|
|
, end(m_pieces.end()); i != end; ++i, ++index)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(*i >= 0 && *i < int(m_piece_map.size()));
|
|
|
|
m_piece_map[*i].index = index;
|
|
|
|
}
|
2005-09-01 23:04:21 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
m_dirty = false;
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
print_pieces();
|
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
void piece_picker::piece_passed(int index)
|
|
|
|
{
|
|
|
|
piece_pos& p = m_piece_map[index];
|
|
|
|
int state = p.state;
|
|
|
|
if (state == piece_pos::piece_open) return;
|
|
|
|
|
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(state - 1, index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
|
|
|
|
|
|
|
TORRENT_ASSERT(i->locked == false);
|
|
|
|
if (i->locked) return;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(!i->passed_hash_check);
|
|
|
|
i->passed_hash_check = true;
|
|
|
|
++m_num_passed;
|
|
|
|
|
|
|
|
if (i->finished < blocks_in_piece(index)) return;
|
|
|
|
|
|
|
|
we_have(index);
|
|
|
|
}
|
|
|
|
|
2008-06-07 04:58:28 +02:00
|
|
|
void piece_picker::we_dont_have(int index)
|
|
|
|
{
|
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < (int)m_piece_map.size());
|
|
|
|
|
|
|
|
piece_pos& p = m_piece_map[index];
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(p.downloading() == false);
|
2008-06-07 04:58:28 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "piece_picker::we_dont_have(" << index << ")" << std::endl;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!p.have())
|
|
|
|
{
|
|
|
|
// even though we don't have the piece, it
|
|
|
|
// might still have passed hash check
|
|
|
|
int state = p.state;
|
|
|
|
if (state == piece_pos::piece_open) return;
|
|
|
|
|
|
|
|
std::vector<downloading_piece>::iterator i
|
|
|
|
= find_dl_piece(state - 1, index);
|
|
|
|
if (i->passed_hash_check)
|
|
|
|
{
|
|
|
|
i->passed_hash_check = false;
|
|
|
|
TORRENT_ASSERT(m_num_passed > 0);
|
|
|
|
--m_num_passed;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-06-07 04:58:28 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(m_num_passed > 0);
|
|
|
|
--m_num_passed;
|
2008-06-07 04:58:28 +02:00
|
|
|
if (p.filtered())
|
|
|
|
{
|
|
|
|
++m_num_filtered;
|
|
|
|
--m_num_have_filtered;
|
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// update cursors
|
|
|
|
if (index < m_cursor)
|
|
|
|
m_cursor = index;
|
|
|
|
if (index >= m_reverse_cursor)
|
|
|
|
m_reverse_cursor = index + 1;
|
|
|
|
if (m_reverse_cursor == m_cursor)
|
|
|
|
{
|
|
|
|
m_reverse_cursor = 0;
|
|
|
|
m_cursor = num_pieces();
|
|
|
|
}
|
|
|
|
}
|
2008-06-07 04:58:28 +02:00
|
|
|
|
|
|
|
--m_num_have;
|
|
|
|
p.set_not_have();
|
|
|
|
|
|
|
|
if (m_dirty) return;
|
2008-06-12 17:40:50 +02:00
|
|
|
if (p.priority(this) >= 0) add(index);
|
2008-06-07 04:58:28 +02:00
|
|
|
}
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
// this is used to indicate that we succesfully have
|
|
|
|
// downloaded a piece, and that no further attempts
|
|
|
|
// to pick that piece should be made. The piece will
|
|
|
|
// be removed from the available piece list.
|
2003-10-23 01:00:57 +02:00
|
|
|
void piece_picker::we_have(int index)
|
|
|
|
{
|
2008-10-06 01:28:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2006-07-16 02:08:50 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-10-06 01:28:57 +02:00
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < (int)m_piece_map.size());
|
2004-01-26 01:21:12 +01:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
2014-07-06 21:18:00 +02:00
|
|
|
std::cerr << "[" << this << "] " << "piece_picker::we_have(" << index << ")" << std::endl;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2005-05-25 12:01:01 +02:00
|
|
|
piece_pos& p = m_piece_map[index];
|
2007-03-15 23:03:56 +01:00
|
|
|
int info_index = p.index;
|
2008-01-31 18:52:29 +01:00
|
|
|
int priority = p.priority(this);
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(priority < int(m_priority_boundries.size()) || m_dirty);
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (p.state != piece_pos::piece_open)
|
2007-11-04 20:10:58 +01:00
|
|
|
{
|
|
|
|
std::vector<downloading_piece>::iterator i
|
2014-07-06 21:18:00 +02:00
|
|
|
= find_dl_piece(p.state - 1, index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[p.state - 1].end());
|
|
|
|
// decrement num_passed here to compensate
|
|
|
|
// for the unconditional increment further down
|
|
|
|
if (i->passed_hash_check) --m_num_passed;
|
2007-11-04 20:10:58 +01:00
|
|
|
erase_download_piece(i);
|
|
|
|
}
|
2007-03-30 05:09:28 +02:00
|
|
|
|
2007-05-14 12:54:18 +02:00
|
|
|
if (p.have()) return;
|
2009-01-05 02:08:09 +01:00
|
|
|
|
|
|
|
// maintain sparse_regions
|
|
|
|
if (index == 0)
|
|
|
|
{
|
2011-02-15 11:05:25 +01:00
|
|
|
if (index == int(m_piece_map.size()) - 1
|
2009-01-05 02:08:09 +01:00
|
|
|
|| m_piece_map[index + 1].have())
|
|
|
|
--m_sparse_regions;
|
|
|
|
}
|
|
|
|
else if (index == int(m_piece_map.size() - 1))
|
|
|
|
{
|
|
|
|
if (index == 0
|
|
|
|
|| m_piece_map[index - 1].have())
|
|
|
|
--m_sparse_regions;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool have_before = m_piece_map[index-1].have();
|
|
|
|
bool have_after = m_piece_map[index+1].have();
|
|
|
|
if (have_after && have_before) --m_sparse_regions;
|
|
|
|
else if (!have_after && !have_before) ++m_sparse_regions;
|
|
|
|
}
|
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
if (p.filtered())
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
|
|
|
--m_num_filtered;
|
|
|
|
++m_num_have_filtered;
|
|
|
|
}
|
2007-05-14 12:54:18 +02:00
|
|
|
++m_num_have;
|
2014-07-06 21:18:00 +02:00
|
|
|
++m_num_passed;
|
2007-03-15 23:03:56 +01:00
|
|
|
p.set_have();
|
2008-09-06 23:04:57 +02:00
|
|
|
if (m_cursor == m_reverse_cursor - 1 &&
|
|
|
|
m_cursor == index)
|
|
|
|
{
|
|
|
|
m_cursor = int(m_piece_map.size());
|
|
|
|
m_reverse_cursor = 0;
|
|
|
|
TORRENT_ASSERT(num_pieces() > 0);
|
|
|
|
}
|
|
|
|
else if (m_cursor == index)
|
|
|
|
{
|
|
|
|
++m_cursor;
|
|
|
|
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin() + m_cursor
|
|
|
|
, end(m_piece_map.end()); i != end && (i->have() || i->filtered());
|
|
|
|
++i, ++m_cursor);
|
|
|
|
}
|
|
|
|
else if (m_reverse_cursor - 1 == index)
|
|
|
|
{
|
|
|
|
--m_reverse_cursor;
|
|
|
|
TORRENT_ASSERT(m_piece_map[m_reverse_cursor].have()
|
|
|
|
|| m_piece_map[m_reverse_cursor].filtered());
|
|
|
|
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin()
|
|
|
|
+ m_reverse_cursor - 1; m_reverse_cursor > 0 && (i->have() || i->filtered());
|
|
|
|
--i, --m_reverse_cursor);
|
|
|
|
TORRENT_ASSERT(m_piece_map[m_reverse_cursor].have()
|
|
|
|
|| m_piece_map[m_reverse_cursor].filtered());
|
|
|
|
}
|
|
|
|
TORRENT_ASSERT(m_reverse_cursor > m_cursor
|
|
|
|
|| (m_cursor == num_pieces() && m_reverse_cursor == 0));
|
2008-01-31 18:52:29 +01:00
|
|
|
if (priority == -1) return;
|
|
|
|
if (m_dirty) return;
|
|
|
|
remove(priority, info_index);
|
|
|
|
TORRENT_ASSERT(p.priority(this) == -1);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2007-08-10 23:13:35 +02:00
|
|
|
bool piece_picker::set_piece_priority(int index, int new_piece_priority)
|
2005-05-25 12:01:01 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2006-07-16 02:08:50 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "set_piece_priority(" << index << ", " << new_piece_priority << ")" << std::endl;
|
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(new_piece_priority >= 0);
|
|
|
|
TORRENT_ASSERT(new_piece_priority <= 7);
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < (int)m_piece_map.size());
|
2005-05-29 19:25:13 +02:00
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
piece_pos& p = m_piece_map[index];
|
2007-03-21 03:09:50 +01:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
// if the priority isn't changed, don't do anything
|
2007-08-10 23:13:35 +02:00
|
|
|
if (new_piece_priority == int(p.piece_priority)) return false;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int prev_priority = p.priority(this);
|
2008-10-19 00:29:56 +02:00
|
|
|
TORRENT_ASSERT(m_dirty || prev_priority < int(m_priority_boundries.size()));
|
2007-03-21 03:09:50 +01:00
|
|
|
|
2007-08-10 23:13:35 +02:00
|
|
|
bool ret = false;
|
2007-03-15 23:03:56 +01:00
|
|
|
if (new_piece_priority == piece_pos::filter_priority
|
|
|
|
&& p.piece_priority != piece_pos::filter_priority)
|
2005-05-30 19:43:03 +02:00
|
|
|
{
|
2007-03-15 23:03:56 +01:00
|
|
|
// the piece just got filtered
|
2008-09-06 23:04:57 +02:00
|
|
|
if (p.have())
|
|
|
|
{
|
|
|
|
++m_num_have_filtered;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++m_num_filtered;
|
|
|
|
|
|
|
|
// update m_cursor
|
|
|
|
if (m_cursor == m_reverse_cursor - 1 && m_cursor == index)
|
|
|
|
{
|
|
|
|
m_cursor = int(m_piece_map.size());
|
|
|
|
m_reverse_cursor = 0;
|
|
|
|
}
|
|
|
|
else if (m_cursor == index)
|
|
|
|
{
|
|
|
|
++m_cursor;
|
|
|
|
while (m_cursor < int(m_piece_map.size())
|
|
|
|
&& (m_piece_map[m_cursor].have()
|
|
|
|
|| m_piece_map[m_cursor].filtered()))
|
|
|
|
++m_cursor;
|
|
|
|
}
|
|
|
|
else if (m_reverse_cursor == index + 1)
|
|
|
|
{
|
|
|
|
--m_reverse_cursor;
|
|
|
|
while (m_reverse_cursor > 0
|
|
|
|
&& (m_piece_map[m_reverse_cursor-1].have()
|
|
|
|
|| m_piece_map[m_reverse_cursor-1].filtered()))
|
|
|
|
--m_reverse_cursor;
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 23:13:35 +02:00
|
|
|
ret = true;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
|
|
|
else if (new_piece_priority != piece_pos::filter_priority
|
|
|
|
&& p.piece_priority == piece_pos::filter_priority)
|
|
|
|
{
|
|
|
|
// the piece just got unfiltered
|
2008-09-06 23:04:57 +02:00
|
|
|
if (p.have())
|
|
|
|
{
|
|
|
|
--m_num_have_filtered;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
--m_num_filtered;
|
|
|
|
// update cursors
|
|
|
|
if (index < m_cursor)
|
|
|
|
m_cursor = index;
|
|
|
|
if (index >= m_reverse_cursor)
|
|
|
|
m_reverse_cursor = index + 1;
|
|
|
|
if (m_reverse_cursor == m_cursor)
|
|
|
|
{
|
|
|
|
m_reverse_cursor = 0;
|
|
|
|
m_cursor = num_pieces();
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 23:13:35 +02:00
|
|
|
ret = true;
|
2007-03-15 23:03:56 +01:00
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(m_num_filtered >= 0);
|
|
|
|
TORRENT_ASSERT(m_num_have_filtered >= 0);
|
2007-03-15 23:03:56 +01:00
|
|
|
|
|
|
|
p.piece_priority = new_piece_priority;
|
2008-01-31 18:52:29 +01:00
|
|
|
int new_priority = p.priority(this);
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (p.state > 0)
|
|
|
|
{
|
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(p.state - 1, index);
|
|
|
|
if (i != m_downloads[p.state - 1].end())
|
|
|
|
update_piece_state(i);
|
|
|
|
}
|
|
|
|
|
2008-08-19 09:11:17 +02:00
|
|
|
if (prev_priority == new_priority) return ret;
|
2008-01-31 18:52:29 +01:00
|
|
|
|
|
|
|
if (m_dirty) return ret;
|
|
|
|
if (prev_priority == -1)
|
2007-03-15 23:03:56 +01:00
|
|
|
{
|
|
|
|
add(index);
|
2005-05-30 19:43:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
update(prev_priority, p.index);
|
2005-05-30 19:43:03 +02:00
|
|
|
}
|
2007-08-10 23:13:35 +02:00
|
|
|
return ret;
|
2005-05-25 12:01:01 +02:00
|
|
|
}
|
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
int piece_picker::piece_priority(int index) const
|
2005-05-25 12:01:01 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
TORRENT_ASSERT(index < (int)m_piece_map.size());
|
2005-05-25 12:01:01 +02:00
|
|
|
|
2007-03-15 23:03:56 +01:00
|
|
|
return m_piece_map[index].piece_priority;
|
2005-05-25 12:01:01 +02:00
|
|
|
}
|
|
|
|
|
2007-03-20 02:59:00 +01:00
|
|
|
void piece_picker::piece_priorities(std::vector<int>& pieces) const
|
|
|
|
{
|
|
|
|
pieces.resize(m_piece_map.size());
|
|
|
|
std::vector<int>::iterator j = pieces.begin();
|
|
|
|
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin(),
|
|
|
|
end(m_piece_map.end()); i != end; ++i, ++j)
|
|
|
|
{
|
|
|
|
*j = i->piece_priority;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============ start deprecation ==============
|
|
|
|
|
2005-05-25 12:01:01 +02:00
|
|
|
void piece_picker::filtered_pieces(std::vector<bool>& mask) const
|
|
|
|
{
|
|
|
|
mask.resize(m_piece_map.size());
|
|
|
|
std::vector<bool>::iterator j = mask.begin();
|
|
|
|
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin(),
|
|
|
|
end(m_piece_map.end()); i != end; ++i, ++j)
|
|
|
|
{
|
2007-03-15 23:03:56 +01:00
|
|
|
*j = i->filtered();
|
2005-05-25 12:01:01 +02:00
|
|
|
}
|
|
|
|
}
|
2007-03-20 02:59:00 +01:00
|
|
|
|
|
|
|
// ============ end deprecation ==============
|
2007-04-27 02:27:37 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
int append_blocks(std::vector<piece_block>& dst, std::vector<piece_block>& src
|
|
|
|
, int num_blocks)
|
|
|
|
{
|
|
|
|
if (src.empty()) return num_blocks;
|
|
|
|
int to_copy;
|
|
|
|
// if (prefer_whole_pieces == 0)
|
|
|
|
to_copy = (std::min)(int(src.size()), num_blocks);
|
|
|
|
// else
|
|
|
|
// to_copy = int(src.size());
|
|
|
|
|
|
|
|
dst.insert(dst.end()
|
|
|
|
, src.begin(), src.begin() + to_copy);
|
|
|
|
src.clear();
|
|
|
|
return num_blocks - to_copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-27 02:27:37 +02:00
|
|
|
// pieces describes which pieces the peer we're requesting from
|
|
|
|
// has.
|
|
|
|
// interesting_blocks is an out parameter, and will be filled
|
|
|
|
// with (up to) num_blocks of interesting blocks that the peer has.
|
|
|
|
// prefer_whole_pieces can be set if this peer should download
|
|
|
|
// whole pieces rather than trying to download blocks from the
|
|
|
|
// same piece as other peers.
|
2014-07-06 21:18:00 +02:00
|
|
|
// the void* is the pointer to the torrent_peer of the peer we're
|
2007-07-04 04:16:49 +02:00
|
|
|
// picking pieces from. This is used when downloading whole pieces,
|
|
|
|
// to only pick from the same piece the same peer is downloading
|
|
|
|
// from. state is supposed to be set to fast if the peer is downloading
|
2007-04-27 02:27:37 +02:00
|
|
|
// relatively fast, by some notion. Slow peers will prefer not
|
|
|
|
// to pick blocks from the same pieces as fast peers, and vice
|
|
|
|
// versa. Downloading pieces are marked as being fast, medium
|
|
|
|
// or slow once they're started.
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
// options are:
|
|
|
|
// * rarest_first
|
|
|
|
// pick the rarest pieces first
|
|
|
|
// * reverse
|
|
|
|
// reverse the piece picking. Pick the most common
|
|
|
|
// pieces first or the last pieces (if picking sequential)
|
|
|
|
// * sequential
|
|
|
|
// download pieces in-order
|
|
|
|
// * on_parole
|
|
|
|
// the peer is on parole, only pick whole pieces which
|
|
|
|
// has only been downloaded and requested from the same
|
|
|
|
// peer
|
|
|
|
// * prioritize_partials
|
|
|
|
// pick blocks from downloading pieces first
|
2009-01-14 10:07:27 +01:00
|
|
|
// * speed_affinity
|
|
|
|
// have an affinity to pick pieces in the same speed
|
|
|
|
// category.
|
|
|
|
// * ignore_whole_pieces
|
|
|
|
// ignores the prefer_whole_pieces parameter (as if
|
|
|
|
// it was 0)
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
// only one of rarest_first, sequential can be set
|
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
void piece_picker::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
|
2014-07-06 21:18:00 +02:00
|
|
|
, int num_peers
|
|
|
|
, counters& pc
|
|
|
|
) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(peer == 0 || static_cast<torrent_peer*>(peer)->in_use);
|
2012-04-12 19:10:22 +02:00
|
|
|
|
2008-09-22 02:41:36 +02:00
|
|
|
// prevent the number of partial pieces to grow indefinitely
|
2011-08-15 01:16:12 +02:00
|
|
|
// make this scale by the number of peers we have. For large
|
|
|
|
// scale clients, we would have more peers, and allow a higher
|
|
|
|
// threshold for the number of partials
|
2014-07-06 21:18:00 +02:00
|
|
|
if (int(m_downloads[0].size()) > m_num_pad_files + num_peers * 3 / 2)
|
|
|
|
{
|
|
|
|
// if we have too many partial pieces, prioritize completing
|
|
|
|
// them. In order for this to have an affect, also disable
|
|
|
|
// prefer whole pieces (otherwise partial pieces would be de-prioritized)
|
|
|
|
options |= prioritize_partials;
|
|
|
|
prefer_whole_pieces = 0;
|
|
|
|
}
|
2008-09-22 02:41:36 +02:00
|
|
|
|
2009-01-14 10:07:27 +01:00
|
|
|
if (options & ignore_whole_pieces) prefer_whole_pieces = 0;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// only one of rarest_first and sequential can be set.
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(((options & rarest_first) ? 1 : 0)
|
|
|
|
+ ((options & sequential) ? 1 : 0) <= 1);
|
2008-10-10 07:25:55 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2006-07-16 02:08:50 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-10-10 07:25:55 +02:00
|
|
|
#endif
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(num_blocks > 0);
|
|
|
|
TORRENT_ASSERT(pieces.size() == m_piece_map.size());
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(!m_priority_boundries.empty()
|
|
|
|
|| m_dirty);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-04-27 02:27:37 +02:00
|
|
|
// this will be filled with blocks that we should not request
|
|
|
|
// unless we can't find num_blocks among the other ones.
|
|
|
|
// blocks that belong to pieces with a mismatching speed
|
|
|
|
// category for instance, or if we prefer whole pieces,
|
|
|
|
// blocks belonging to a piece that others have
|
|
|
|
// downloaded to
|
2005-08-15 00:04:58 +02:00
|
|
|
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
|
|
|
const std::vector<int> empty_vector;
|
2007-07-15 17:41:55 +02:00
|
|
|
|
2007-08-03 08:13:26 +02:00
|
|
|
// When prefer_whole_pieces is set (usually set when downloading from
|
|
|
|
// fast peers) the partial pieces will not be prioritized, but actually
|
|
|
|
// ignored as long as possible. All blocks found in downloading
|
|
|
|
// pieces are regarded as backup blocks
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (options & prioritize_partials)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<downloading_piece>::const_iterator i = m_downloads[0].begin()
|
|
|
|
, end(m_downloads[0].end()); i != end; ++i)
|
2008-09-06 23:04:57 +02:00
|
|
|
{
|
2014-04-22 06:21:14 +02:00
|
|
|
// in time critical mode, only pick prio 7 pieces
|
|
|
|
if ((options & time_critical_mode) && piece_priority(i->index) != 7)
|
|
|
|
continue;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
pc.inc_stats_counter(counters::piece_picker_partial_loops);
|
2013-01-30 07:20:37 +01:00
|
|
|
if (!is_piece_free(i->index, pieces)) continue;
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(m_piece_map[i->index].state == piece_pos::piece_downloading);
|
|
|
|
if (int(backup_blocks.size()) >= num_blocks
|
2014-05-10 05:23:05 +02:00
|
|
|
&& int(backup_blocks2.size()) >= num_blocks)
|
2011-08-16 08:30:53 +02:00
|
|
|
continue;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
num_blocks = add_blocks_downloading(*i, pieces
|
|
|
|
, interesting_blocks, backup_blocks, backup_blocks2
|
|
|
|
, num_blocks, prefer_whole_pieces, peer, speed, options);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
num_blocks = append_blocks(interesting_blocks, backup_blocks
|
|
|
|
, num_blocks);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
|
|
|
|
num_blocks = append_blocks(interesting_blocks, backup_blocks2
|
|
|
|
, num_blocks);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
if (!suggested_pieces.empty())
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
for (std::vector<int>::const_iterator i = suggested_pieces.begin();
|
|
|
|
i != suggested_pieces.end(); ++i)
|
|
|
|
{
|
2014-04-22 06:21:14 +02:00
|
|
|
// in time critical mode, only pick prio 7 pieces
|
|
|
|
if ((options & time_critical_mode) && piece_priority(*i) != 7)
|
|
|
|
continue;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
pc.inc_stats_counter(counters::piece_picker_suggest_loops);
|
2008-09-06 23:04:57 +02:00
|
|
|
if (!is_piece_free(*i, pieces)) continue;
|
|
|
|
num_blocks = add_blocks(*i, pieces
|
|
|
|
, interesting_blocks, backup_blocks
|
|
|
|
, backup_blocks2, num_blocks
|
|
|
|
, prefer_whole_pieces, peer, empty_vector
|
|
|
|
, speed, options);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (options & sequential)
|
2008-01-31 18:52:29 +01:00
|
|
|
{
|
2014-04-03 04:03:14 +02:00
|
|
|
if (m_dirty) update_pieces();
|
|
|
|
TORRENT_ASSERT(!m_dirty);
|
|
|
|
|
|
|
|
for (std::vector<int>::const_iterator i = m_pieces.begin();
|
|
|
|
i != m_pieces.end() && piece_priority(*i) == 7; ++i)
|
|
|
|
{
|
|
|
|
if (!is_piece_free(*i, pieces)) continue;
|
|
|
|
num_blocks = add_blocks(*i, pieces
|
|
|
|
, interesting_blocks, backup_blocks
|
|
|
|
, backup_blocks2, num_blocks
|
|
|
|
, prefer_whole_pieces, peer, suggested_pieces
|
|
|
|
, speed, options);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
|
|
|
|
2014-04-22 06:21:14 +02:00
|
|
|
// in time critical mode, only pick prio 7 pieces
|
|
|
|
if ((options & time_critical_mode) == 0)
|
2007-09-03 09:10:09 +02:00
|
|
|
{
|
2014-04-22 06:21:14 +02:00
|
|
|
if (options & reverse)
|
|
|
|
{
|
|
|
|
for (int i = m_reverse_cursor - 1; i >= m_cursor; --i)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
pc.inc_stats_counter(counters::piece_picker_sequential_loops);
|
2014-04-22 06:21:14 +02:00
|
|
|
if (!is_piece_free(i, pieces)) continue;
|
|
|
|
// we've already added prio 7 pieces
|
|
|
|
if (piece_priority(i) == 7) continue;
|
|
|
|
num_blocks = add_blocks(i, pieces
|
|
|
|
, interesting_blocks, backup_blocks
|
|
|
|
, backup_blocks2, num_blocks
|
|
|
|
, prefer_whole_pieces, peer, suggested_pieces
|
|
|
|
, speed, options);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
}
|
2014-04-22 06:21:14 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = m_cursor; i < m_reverse_cursor; ++i)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
|
|
|
pc.inc_stats_counter(counters::piece_picker_sequential_loops);
|
2014-04-22 06:21:14 +02:00
|
|
|
if (!is_piece_free(i, pieces)) continue;
|
|
|
|
// we've already added prio 7 pieces
|
|
|
|
if (piece_priority(i) == 7) continue;
|
|
|
|
num_blocks = add_blocks(i, pieces
|
|
|
|
, interesting_blocks, backup_blocks
|
|
|
|
, backup_blocks2, num_blocks
|
|
|
|
, prefer_whole_pieces, peer, suggested_pieces
|
|
|
|
, speed, options);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
2007-09-05 23:21:11 +02:00
|
|
|
}
|
2007-09-03 09:10:09 +02:00
|
|
|
}
|
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
else if (options & rarest_first)
|
2008-01-31 18:52:29 +01:00
|
|
|
{
|
|
|
|
if (m_dirty) update_pieces();
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(!m_dirty);
|
|
|
|
|
2014-04-22 06:21:14 +02:00
|
|
|
// in time critical mode, we're only allowed to pick prio 7
|
|
|
|
// pieces. This is why reverse mode is disabled when we're in
|
|
|
|
// time-critical mode, because all prio 7 pieces are at the front
|
|
|
|
// of the list
|
|
|
|
if ((options & reverse) && (options & time_critical_mode) == 0)
|
2008-09-06 23:04:57 +02:00
|
|
|
{
|
|
|
|
// it's a bit complicated in order to always prioritize
|
|
|
|
// partial pieces, and respect priorities. Every chunk
|
|
|
|
// of 4 priority levels are traversed in forward order, but otherwise
|
|
|
|
// they are traversed in reverse order
|
|
|
|
// round up to an even 4 priority boundry, to make it simpler
|
|
|
|
// to do the akward reverse traversing
|
|
|
|
#define div_round_up(n, d) (((n) + (d) - 1) / (d))
|
|
|
|
m_priority_boundries.resize(div_round_up(m_priority_boundries.size()
|
|
|
|
, prio_factor) * prio_factor, m_priority_boundries.back());
|
|
|
|
for (int i = m_priority_boundries.size() - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
int prio = (i / prio_factor) * prio_factor
|
|
|
|
+ prio_factor - 1 - (i % prio_factor);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(prio >= 0);
|
|
|
|
TORRENT_ASSERT(prio < int(m_priority_boundries.size()));
|
|
|
|
int start = prio == 0 ? 0 : m_priority_boundries[prio - 1];
|
|
|
|
for (int p = start; p < m_priority_boundries[prio]; ++p)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
pc.inc_stats_counter(counters::piece_picker_reverse_rare_loops);
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (!is_piece_free(m_pieces[p], pieces)) continue;
|
|
|
|
num_blocks = add_blocks(m_pieces[p], pieces
|
|
|
|
, interesting_blocks, backup_blocks
|
|
|
|
, backup_blocks2, num_blocks
|
|
|
|
, prefer_whole_pieces, peer, suggested_pieces
|
|
|
|
, speed, options);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef div_round_up
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (std::vector<int>::const_iterator i = m_pieces.begin();
|
|
|
|
i != m_pieces.end(); ++i)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
pc.inc_stats_counter(counters::piece_picker_rare_loops);
|
|
|
|
|
2014-04-22 06:21:14 +02:00
|
|
|
// in time critical mode, only pick prio 7 pieces
|
|
|
|
// it's safe to break here because in this mode we
|
|
|
|
// pick pieces in priority order. Once we hit a lower priority
|
|
|
|
// piece, we won't encounter any more prio 7 ones
|
|
|
|
if ((options & time_critical_mode) && piece_priority(*i) != 7)
|
|
|
|
break;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (!is_piece_free(*i, pieces)) continue;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
num_blocks = add_blocks(*i, pieces
|
|
|
|
, interesting_blocks, backup_blocks
|
|
|
|
, backup_blocks2, num_blocks
|
|
|
|
, prefer_whole_pieces, peer, suggested_pieces
|
|
|
|
, speed, options);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
|
|
|
}
|
2008-01-31 18:52:29 +01:00
|
|
|
}
|
2014-04-22 06:21:14 +02:00
|
|
|
else if (options & time_critical_mode)
|
|
|
|
{
|
|
|
|
// if we're in time-critical mode, we are only allowed to pick
|
|
|
|
// prio 7 pieces.
|
|
|
|
for (std::vector<int>::const_iterator i = m_pieces.begin();
|
|
|
|
i != m_pieces.end() && piece_priority(*i) == 7; ++i)
|
|
|
|
{
|
|
|
|
if (!is_piece_free(*i, pieces)) continue;
|
|
|
|
num_blocks = add_blocks(*i, pieces
|
|
|
|
, interesting_blocks, backup_blocks
|
|
|
|
, backup_blocks2, num_blocks
|
|
|
|
, prefer_whole_pieces, peer, suggested_pieces
|
|
|
|
, speed, options);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
}
|
|
|
|
}
|
2007-09-03 09:10:09 +02:00
|
|
|
else
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2014-04-22 06:21:14 +02:00
|
|
|
|
2007-07-07 03:26:30 +02:00
|
|
|
// we're not using rarest first (only for the first
|
|
|
|
// bucket, since that's where the currently downloading
|
|
|
|
// pieces are)
|
2011-02-26 08:55:51 +01:00
|
|
|
int start_piece = random() % m_piece_map.size();
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2007-08-27 07:13:09 +02:00
|
|
|
int piece = start_piece;
|
2007-07-07 03:26:30 +02:00
|
|
|
while (num_blocks > 0)
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
bool done = false;
|
|
|
|
// skip pieces we can't pick, and suggested pieces
|
|
|
|
// since we've already picked those
|
|
|
|
while (!can_pick(piece, pieces)
|
2009-06-28 02:32:14 +02:00
|
|
|
|| std::find(suggested_pieces.begin()
|
2014-07-06 21:18:00 +02:00
|
|
|
, suggested_pieces.end(), piece)
|
|
|
|
!= suggested_pieces.end())
|
2007-07-07 03:26:30 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
pc.inc_stats_counter(counters::piece_picker_rand_start_loops);
|
2007-07-07 03:26:30 +02:00
|
|
|
++piece;
|
2007-07-15 07:32:56 +02:00
|
|
|
if (piece == int(m_piece_map.size())) piece = 0;
|
2007-07-07 03:26:30 +02:00
|
|
|
// could not find any more pieces
|
2008-09-06 23:04:57 +02:00
|
|
|
if (piece == start_piece) { done = true; break; }
|
2007-07-07 03:26:30 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
if (done) break;
|
2007-07-07 03:26:30 +02:00
|
|
|
|
2009-06-28 02:32:14 +02:00
|
|
|
TORRENT_ASSERT(can_pick(piece, pieces));
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(m_piece_map[piece].downloading() == false);
|
2009-06-28 02:32:14 +02:00
|
|
|
|
2007-09-03 23:16:24 +02:00
|
|
|
int start, end;
|
2014-07-06 21:18:00 +02:00
|
|
|
boost::tie(start, end) = expand_piece(piece, prefer_whole_pieces, pieces, options);
|
2007-09-03 23:16:24 +02:00
|
|
|
for (int k = start; k < end; ++k)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(m_piece_map[k].downloading() == false);
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(m_piece_map[k].priority(this) >= 0);
|
2007-09-03 23:16:24 +02:00
|
|
|
int num_blocks_in_piece = blocks_in_piece(k);
|
|
|
|
if (prefer_whole_pieces == 0 && num_blocks_in_piece > num_blocks)
|
|
|
|
num_blocks_in_piece = num_blocks;
|
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
pc.inc_stats_counter(counters::piece_picker_rand_loops);
|
2013-01-30 07:20:37 +01:00
|
|
|
TORRENT_ASSERT(is_piece_free(k, pieces));
|
2007-09-03 23:16:24 +02:00
|
|
|
interesting_blocks.push_back(piece_block(k, j));
|
|
|
|
--num_blocks;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
piece = end;
|
2007-08-27 07:13:09 +02:00
|
|
|
if (piece == int(m_piece_map.size())) piece = 0;
|
|
|
|
// could not find any more pieces
|
2008-09-06 23:04:57 +02:00
|
|
|
if (piece == start_piece) break;
|
2007-07-07 03:26:30 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2007-09-03 09:10:09 +02:00
|
|
|
if (num_blocks <= 0) return;
|
2005-08-15 00:04:58 +02:00
|
|
|
|
2014-03-17 04:41:35 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2008-09-06 23:04:57 +02:00
|
|
|
verify_pick(interesting_blocks, pieces);
|
|
|
|
verify_pick(backup_blocks, pieces);
|
|
|
|
verify_pick(backup_blocks2, pieces);
|
|
|
|
#endif
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
num_blocks = append_blocks(interesting_blocks, backup_blocks
|
|
|
|
, num_blocks);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
|
|
|
|
num_blocks = append_blocks(interesting_blocks, backup_blocks2, num_blocks);
|
|
|
|
if (num_blocks <= 0) return;
|
|
|
|
|
|
|
|
// ===== THIS IS FOR END-GAME MODE =====
|
|
|
|
|
|
|
|
// don't double-pick anything if the peer is on parole
|
|
|
|
if (options & on_parole) return;
|
|
|
|
|
|
|
|
// in end game mode we pick a single block
|
|
|
|
// that has already been requested from someone
|
|
|
|
// all pieces that are interesting are in
|
|
|
|
// m_downloads[0] and m_download[1] (i.e. partial and full pieces)
|
|
|
|
|
2011-02-09 03:56:00 +01:00
|
|
|
std::vector<piece_block> temp;
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// pick one random block from one random partial piece.
|
|
|
|
// only pick from non-downloaded blocks.
|
|
|
|
// first, create a temporary array of the partial pieces
|
|
|
|
// this peer has, and can pick from. Cap the stack allocation
|
|
|
|
// at 200 pieces.
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int partials_size = (std::min)(200, int(m_downloads[0].size()
|
|
|
|
+ m_downloads[1].size()));
|
|
|
|
if (partials_size == 0) return;
|
|
|
|
|
|
|
|
downloading_piece const** partials
|
|
|
|
= TORRENT_ALLOCA(downloading_piece const*, partials_size);
|
|
|
|
int c = 0;
|
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG
|
|
|
|
for (std::vector<downloading_piece>::const_iterator i
|
|
|
|
= m_downloads[0].begin(), end(m_downloads[0].end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
downloading_piece const& dp = *i;
|
|
|
|
|
|
|
|
if ((options & time_critical_mode) && piece_priority(dp.index) != 7)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// we either don't have this piece, or we've already requested from it
|
|
|
|
bool found = false;
|
|
|
|
for (std::vector<piece_block>::const_iterator i
|
|
|
|
= interesting_blocks.begin(), end(interesting_blocks.end());
|
|
|
|
i != end; ++i)
|
|
|
|
{
|
|
|
|
if (i->piece_index != dp.index) continue;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
TORRENT_ASSERT(!pieces[dp.index] || found || dp.locked);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (std::vector<downloading_piece>::const_iterator i
|
|
|
|
= m_downloads[1].begin(), end(m_downloads[1].end());
|
|
|
|
i != end; ++i)
|
|
|
|
{
|
|
|
|
if (c == partials_size) break;
|
|
|
|
|
|
|
|
downloading_piece const& dp = *i;
|
|
|
|
TORRENT_ASSERT(dp.requested > 0);
|
|
|
|
// this peer doesn't have this piece, try again
|
|
|
|
if (!pieces[dp.index]) continue;
|
|
|
|
// don't pick pieces with priority 0
|
|
|
|
TORRENT_ASSERT(piece_priority(dp.index) > 0);
|
|
|
|
|
|
|
|
if ((options & time_critical_mode) && piece_priority(dp.index) != 7)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
partials[c++] = &dp;
|
|
|
|
}
|
|
|
|
|
|
|
|
partials_size = c;
|
|
|
|
while (partials_size > 0)
|
|
|
|
{
|
|
|
|
pc.inc_stats_counter(counters::piece_picker_busy_loops);
|
|
|
|
int piece = random() % partials_size;
|
|
|
|
downloading_piece const* dp = partials[piece];
|
|
|
|
TORRENT_ASSERT(pieces[dp->index]);
|
|
|
|
TORRENT_ASSERT(piece_priority(dp->index) > 0);
|
|
|
|
// fill in with blocks requested from other peers
|
2008-09-06 23:04:57 +02:00
|
|
|
// as backups
|
2014-07-06 21:18:00 +02:00
|
|
|
int num_blocks_in_piece = blocks_in_piece(dp->index);
|
|
|
|
TORRENT_ASSERT(dp->requested > 0);
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
block_info const& info = dp->info[j];
|
|
|
|
TORRENT_ASSERT(info.peer == 0 || static_cast<torrent_peer*>(info.peer)->in_use);
|
|
|
|
TORRENT_ASSERT(info.piece_index == dp->index);
|
2008-09-06 23:04:57 +02:00
|
|
|
if (info.state != block_info::state_requested
|
|
|
|
|| info.peer == peer)
|
|
|
|
continue;
|
2014-07-06 21:18:00 +02:00
|
|
|
temp.push_back(piece_block(dp->index, j));
|
|
|
|
}
|
|
|
|
// are we done?
|
|
|
|
if (!temp.empty())
|
|
|
|
{
|
|
|
|
interesting_blocks.push_back(temp[random() % temp.size()]);
|
|
|
|
--num_blocks;
|
|
|
|
break;
|
2008-09-06 23:04:57 +02:00
|
|
|
}
|
2011-08-16 08:30:53 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the piece we picked only had blocks outstanding requested
|
|
|
|
// by ourself. Remove it and pick another one.
|
|
|
|
partials[piece] = partials[partials_size-1];
|
|
|
|
--partials_size;
|
|
|
|
}
|
2011-02-09 03:56:00 +01:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2008-09-06 23:04:57 +02:00
|
|
|
// make sure that we at this point have added requests to all unrequested blocks
|
|
|
|
// in all downloading pieces
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<downloading_piece>::const_iterator i = m_downloads[0].begin()
|
|
|
|
, end(m_downloads[0].end()); i != end; ++i)
|
2008-09-06 23:04:57 +02:00
|
|
|
{
|
|
|
|
if (!pieces[i->index]) continue;
|
2008-09-19 08:46:58 +02:00
|
|
|
if (piece_priority(i->index) == 0) continue;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (i->locked) continue;
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2014-04-22 06:21:14 +02:00
|
|
|
if ((options & time_critical_mode) && piece_priority(i->index) != 7)
|
|
|
|
continue;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
int num_blocks_in_piece = blocks_in_piece(i->index);
|
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
|
|
|
{
|
|
|
|
block_info const& info = i->info[j];
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == i->index);
|
2008-09-06 23:04:57 +02:00
|
|
|
if (info.state != block_info::state_none) continue;
|
|
|
|
std::vector<piece_block>::iterator k = std::find(
|
|
|
|
interesting_blocks.begin(), interesting_blocks.end()
|
|
|
|
, piece_block(i->index, j));
|
|
|
|
if (k != interesting_blocks.end()) continue;
|
2014-04-22 06:21:14 +02:00
|
|
|
|
2009-10-20 04:49:56 +02:00
|
|
|
fprintf(stderr, "interesting blocks:\n");
|
2008-09-06 23:04:57 +02:00
|
|
|
for (k = interesting_blocks.begin(); k != interesting_blocks.end(); ++k)
|
2009-10-20 04:49:56 +02:00
|
|
|
fprintf(stderr, "(%d, %d)", k->piece_index, k->block_index);
|
|
|
|
fprintf(stderr, "\nnum_blocks: %d\n", num_blocks);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<downloading_piece>::const_iterator l = m_downloads[0].begin()
|
|
|
|
, end(m_downloads[0].end()); l != end; ++l)
|
2008-09-06 23:04:57 +02:00
|
|
|
{
|
2009-10-20 04:49:56 +02:00
|
|
|
fprintf(stderr, "%d : ", l->index);
|
2008-09-06 23:04:57 +02:00
|
|
|
int num_blocks_in_piece = blocks_in_piece(l->index);
|
|
|
|
for (int m = 0; m < num_blocks_in_piece; ++m)
|
2009-10-20 04:49:56 +02:00
|
|
|
fprintf(stderr, "%d", l->info[m].state);
|
|
|
|
fprintf(stderr, "\n");
|
2008-09-06 23:04:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TORRENT_ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (interesting_blocks.empty())
|
2008-09-06 23:04:57 +02:00
|
|
|
{
|
|
|
|
// print_pieces();
|
|
|
|
for (int i = 0; i < num_pieces(); ++i)
|
|
|
|
{
|
|
|
|
if (!pieces[i]) continue;
|
2012-05-14 06:48:23 +02:00
|
|
|
if (m_piece_map[i].priority(this) <= 0) continue;
|
2008-09-06 23:04:57 +02:00
|
|
|
if (have_piece(i)) continue;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int state = m_piece_map[i].state;
|
|
|
|
if (state == 0) continue;
|
|
|
|
std::vector<downloading_piece>::const_iterator k = find_dl_piece(state - 1, i);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(k != m_downloads[state-1].end());
|
|
|
|
if (k == m_downloads[state-1].end()) continue;
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
// this assert is not valid for web_seeds
|
|
|
|
/*
|
|
|
|
int num_blocks_in_piece = blocks_in_piece(k->index);
|
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
|
|
|
{
|
|
|
|
block_info const& info = k->info[j];
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == k->index);
|
2008-09-06 23:04:57 +02:00
|
|
|
if (info.state == block_info::state_finished) continue;
|
|
|
|
TORRENT_ASSERT(info.peer != 0);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// have piece means that the piece passed hash check
|
|
|
|
// AND has been successfully written to disk
|
|
|
|
bool piece_picker::have_piece(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.index == piece_pos::we_have_index;
|
|
|
|
}
|
|
|
|
|
2011-02-09 03:56:00 +01:00
|
|
|
int piece_picker::blocks_in_piece(int index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
2013-02-19 09:14:16 +01:00
|
|
|
TORRENT_ASSERT(index < (int)m_piece_map.size() || m_piece_map.empty());
|
2011-02-09 03:56:00 +01:00
|
|
|
if (index+1 == (int)m_piece_map.size())
|
|
|
|
return m_blocks_in_last_piece;
|
|
|
|
else
|
|
|
|
return m_blocks_per_piece;
|
|
|
|
}
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
bool piece_picker::is_piece_free(int piece, bitfield const& bitmask) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(piece >= 0 && piece < int(m_piece_map.size()));
|
|
|
|
return bitmask[piece]
|
|
|
|
&& !m_piece_map[piece].have()
|
|
|
|
&& !m_piece_map[piece].filtered();
|
2005-08-15 00:04:58 +02:00
|
|
|
}
|
|
|
|
|
2008-05-28 04:35:02 +02:00
|
|
|
bool piece_picker::can_pick(int piece, bitfield const& bitmask) const
|
2007-09-10 01:46:28 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(piece >= 0 && piece < int(m_piece_map.size()));
|
2007-09-10 01:46:28 +02:00
|
|
|
return bitmask[piece]
|
|
|
|
&& !m_piece_map[piece].have()
|
2014-07-06 21:18:00 +02:00
|
|
|
// TODO: when expanding pieces for cache stripe reasons,
|
|
|
|
// the !downloading condition doesn't make much sense
|
|
|
|
&& !m_piece_map[piece].downloading()
|
2007-09-10 01:46:28 +02:00
|
|
|
&& !m_piece_map[piece].filtered();
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
void piece_picker::check_peers()
|
|
|
|
{
|
|
|
|
for (std::vector<block_info>::iterator i = m_block_info.begin()
|
|
|
|
, end(m_block_info.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(i->peer == 0 || static_cast<torrent_peer*>(i->peer)->in_use);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
void piece_picker::clear_peer(void* peer)
|
|
|
|
{
|
|
|
|
for (std::vector<block_info>::iterator i = m_block_info.begin()
|
|
|
|
, end(m_block_info.end()); i != end; ++i)
|
2012-04-12 19:10:22 +02:00
|
|
|
{
|
2007-07-04 04:16:49 +02:00
|
|
|
if (i->peer == peer) i->peer = 0;
|
2012-04-12 19:10:22 +02:00
|
|
|
}
|
2007-07-04 04:16:49 +02:00
|
|
|
}
|
|
|
|
|
2005-08-15 00:04:58 +02:00
|
|
|
namespace
|
|
|
|
{
|
2007-07-06 00:49:28 +02:00
|
|
|
// the first bool is true if this is the only peer that has requested and downloaded
|
|
|
|
// blocks from this piece.
|
|
|
|
// the second bool is true if this is the only active peer that is requesting
|
|
|
|
// and downloading blocks from this piece. Active means having a connection.
|
|
|
|
boost::tuple<bool, bool> requested_from(piece_picker::downloading_piece const& p
|
2007-07-04 04:16:49 +02:00
|
|
|
, int num_blocks_in_piece, void* peer)
|
2005-08-15 00:04:58 +02:00
|
|
|
{
|
2007-07-06 00:49:28 +02:00
|
|
|
bool exclusive = true;
|
|
|
|
bool exclusive_active = true;
|
2005-08-15 00:04:58 +02:00
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
|
|
|
{
|
2007-05-08 13:13:13 +02:00
|
|
|
piece_picker::block_info const& info = p.info[j];
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(info.peer == 0 || static_cast<torrent_peer*>(info.peer)->in_use);
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == p.index);
|
2007-06-10 22:46:09 +02:00
|
|
|
if (info.state != piece_picker::block_info::state_none
|
2007-07-06 00:49:28 +02:00
|
|
|
&& info.peer != peer)
|
2005-08-15 00:04:58 +02:00
|
|
|
{
|
2007-07-06 00:49:28 +02:00
|
|
|
exclusive = false;
|
2007-07-06 19:15:35 +02:00
|
|
|
if (info.state == piece_picker::block_info::state_requested
|
|
|
|
&& info.peer != 0)
|
2007-07-06 00:49:28 +02:00
|
|
|
{
|
|
|
|
exclusive_active = false;
|
|
|
|
return boost::make_tuple(exclusive, exclusive_active);
|
|
|
|
}
|
2005-08-15 00:04:58 +02:00
|
|
|
}
|
|
|
|
}
|
2007-07-06 00:49:28 +02:00
|
|
|
return boost::make_tuple(exclusive, exclusive_active);
|
2005-08-15 00:04:58 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
int piece_picker::add_blocks(int piece
|
2008-05-28 04:35:02 +02:00
|
|
|
, bitfield const& pieces
|
2005-08-15 00:04:58 +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
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(piece >= 0);
|
|
|
|
TORRENT_ASSERT(piece < (int)m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(is_piece_free(piece, pieces));
|
|
|
|
|
|
|
|
// std::cout << "add_blocks(" << piece << ")" << std::endl;
|
|
|
|
// std::cout << " num_blocks " << num_blocks << std::endl;
|
|
|
|
|
|
|
|
// ignore pieces found in the ignore list
|
|
|
|
if (std::find(ignore.begin(), ignore.end(), piece) != ignore.end()) return num_blocks;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (m_piece_map[piece].state > piece_pos::piece_downloading) return num_blocks;
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(m_piece_map[piece].priority(this) >= 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
if (m_piece_map[piece].state == piece_pos::piece_downloading)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// if we're prioritizing partials, we've already
|
|
|
|
// looked through the downloading pieces
|
|
|
|
if (options & prioritize_partials) return num_blocks;
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(0, piece);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[0].end());
|
2007-08-21 20:39:44 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// std::cout << "add_blocks_downloading(" << piece << ")" << std::endl;
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
return add_blocks_downloading(*i, pieces
|
|
|
|
, interesting_blocks, backup_blocks, backup_blocks2
|
|
|
|
, num_blocks, prefer_whole_pieces, peer, speed, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
int num_blocks_in_piece = blocks_in_piece(piece);
|
2007-03-15 23:03:56 +01:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// pick a new piece
|
|
|
|
if (prefer_whole_pieces == 0)
|
|
|
|
{
|
|
|
|
if (num_blocks_in_piece > num_blocks)
|
|
|
|
num_blocks_in_piece = num_blocks;
|
2013-01-30 07:20:37 +01:00
|
|
|
TORRENT_ASSERT(is_piece_free(piece, pieces));
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
|
|
|
interesting_blocks.push_back(piece_block(piece, j));
|
|
|
|
num_blocks -= num_blocks_in_piece;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int start, end;
|
2014-07-06 21:18:00 +02:00
|
|
|
boost::tie(start, end) = expand_piece(piece, prefer_whole_pieces, pieces, options);
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int k = start; k < end; ++k)
|
2007-09-05 23:21:11 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(m_piece_map[k].priority(this) > 0);
|
|
|
|
num_blocks_in_piece = blocks_in_piece(k);
|
2013-01-30 07:20:37 +01:00
|
|
|
TORRENT_ASSERT(is_piece_free(k, pieces));
|
2005-08-15 00:04:58 +02:00
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
2007-09-03 23:16:24 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
interesting_blocks.push_back(piece_block(k, j));
|
|
|
|
--num_blocks;
|
2007-09-03 23:16:24 +02:00
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-03-17 04:41:35 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2007-09-10 01:46:28 +02:00
|
|
|
verify_pick(interesting_blocks, pieces);
|
|
|
|
#endif
|
2008-09-06 23:04:57 +02:00
|
|
|
if (num_blocks <= 0) return 0;
|
2003-11-02 22:06:50 +01:00
|
|
|
return num_blocks;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
int piece_picker::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
|
2008-09-06 23:04:57 +02:00
|
|
|
, void* peer, piece_state_t speed, int options) const
|
2007-09-05 23:21:11 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
if (!pieces[dp.index]) return num_blocks;
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(!m_piece_map[dp.index].filtered());
|
|
|
|
|
|
|
|
// this piece failed to write. We're currently restoring
|
|
|
|
// it. It's not OK to send more requests to it right now.
|
|
|
|
if (dp.locked) return num_blocks;
|
2007-09-10 01:46:28 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
int num_blocks_in_piece = blocks_in_piece(dp.index);
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// is true if all the other pieces that are currently
|
|
|
|
// requested from this piece are from the same
|
|
|
|
// peer as 'peer'.
|
|
|
|
bool exclusive;
|
|
|
|
bool exclusive_active;
|
|
|
|
boost::tie(exclusive, exclusive_active)
|
|
|
|
= requested_from(dp, num_blocks_in_piece, peer);
|
|
|
|
|
|
|
|
// peers on parole are only allowed to pick blocks from
|
|
|
|
// pieces that only they have downloaded/requested from
|
|
|
|
if ((options & on_parole) && !exclusive) return num_blocks;
|
|
|
|
|
|
|
|
// we prefer whole blocks, but there are other peers
|
|
|
|
// downloading from this piece, add it as backups
|
|
|
|
if (prefer_whole_pieces > 0 && !exclusive_active)
|
|
|
|
{
|
2011-08-16 08:30:53 +02:00
|
|
|
if (int(backup_blocks2.size()) >= num_blocks)
|
2008-09-06 23:04:57 +02:00
|
|
|
return num_blocks;
|
2007-09-10 10:07:18 +02:00
|
|
|
|
2007-09-05 23:21:11 +02:00
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
|
|
|
{
|
|
|
|
// ignore completed blocks and already requested blocks
|
2008-09-06 23:04:57 +02:00
|
|
|
block_info const& info = dp.info[j];
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == dp.index);
|
2008-09-06 23:04:57 +02:00
|
|
|
if (info.state != block_info::state_none) continue;
|
|
|
|
backup_blocks2.push_back(piece_block(dp.index, j));
|
2007-09-05 23:21:11 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
return num_blocks;
|
2007-09-05 23:21:11 +02:00
|
|
|
}
|
2007-09-10 01:46:28 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
for (int j = 0; j < num_blocks_in_piece; ++j)
|
|
|
|
{
|
|
|
|
// ignore completed blocks and already requested blocks
|
|
|
|
block_info const& info = dp.info[j];
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == dp.index);
|
2008-09-06 23:04:57 +02:00
|
|
|
if (info.state != block_info::state_none) continue;
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
// if the piece is fast and the peer is slow, or vice versa,
|
|
|
|
// add the block as a backup.
|
|
|
|
// override this behavior if all the other blocks
|
|
|
|
// have been requested from the same peer or
|
|
|
|
// if the state of the piece is none (the
|
|
|
|
// piece will in that case change state).
|
|
|
|
if (dp.state != none && dp.state != speed
|
2009-01-14 10:07:27 +01:00
|
|
|
&& !exclusive_active && (options & speed_affinity))
|
2007-09-17 02:37:45 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
if (abs(dp.state - speed) == 1)
|
2007-09-17 02:37:45 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// don't pick too many back-up blocks
|
|
|
|
if (int(backup_blocks.size()) >= num_blocks) return num_blocks;
|
|
|
|
backup_blocks.push_back(piece_block(dp.index, j));
|
2007-09-17 02:37:45 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
else
|
2007-09-17 02:37:45 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
// don't pick too many back-up blocks
|
2011-08-16 08:30:53 +02:00
|
|
|
if (int(backup_blocks2.size()) >= num_blocks) return num_blocks;
|
2008-09-06 23:04:57 +02:00
|
|
|
backup_blocks2.push_back(piece_block(dp.index, j));
|
2007-09-17 02:37:45 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
continue;
|
2007-09-17 02:37:45 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
// this block is interesting (we don't have it
|
|
|
|
// yet).
|
|
|
|
interesting_blocks.push_back(piece_block(dp.index, j));
|
|
|
|
// we have found a block that's free to download
|
|
|
|
num_blocks--;
|
|
|
|
// if we prefer whole pieces, continue picking from this
|
|
|
|
// piece even though we have num_blocks
|
|
|
|
if (prefer_whole_pieces > 0) continue;
|
|
|
|
TORRENT_ASSERT(num_blocks >= 0);
|
|
|
|
if (num_blocks <= 0) return num_blocks;
|
2007-09-17 02:37:45 +02:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(num_blocks >= 0 || prefer_whole_pieces > 0);
|
2007-09-17 02:37:45 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (num_blocks <= 0) return 0;
|
|
|
|
if (options & on_parole) return num_blocks;
|
2007-09-10 01:46:28 +02:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
if (int(backup_blocks.size()) >= num_blocks) return num_blocks;
|
2007-09-05 23:21:11 +02:00
|
|
|
|
2014-03-17 04:41:35 +01:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2007-09-10 01:46:28 +02:00
|
|
|
verify_pick(backup_blocks, pieces);
|
|
|
|
#endif
|
2007-09-05 23:21:11 +02:00
|
|
|
return num_blocks;
|
|
|
|
}
|
|
|
|
|
2007-09-03 23:16:24 +02:00
|
|
|
std::pair<int, int> piece_picker::expand_piece(int piece, int whole_pieces
|
2014-07-06 21:18:00 +02:00
|
|
|
, bitfield const& have, int options) const
|
2007-09-03 23:16:24 +02:00
|
|
|
{
|
|
|
|
if (whole_pieces == 0) return std::make_pair(piece, piece + 1);
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int start = piece;
|
|
|
|
int lower_limit;
|
|
|
|
|
|
|
|
if (options & align_expanded_pieces)
|
|
|
|
{
|
|
|
|
lower_limit = piece - (piece % whole_pieces);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lower_limit = piece - whole_pieces + 1;
|
|
|
|
if (lower_limit < 0) lower_limit = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (start - 1 >= lower_limit
|
|
|
|
&& can_pick(start - 1, have))
|
2007-09-03 23:16:24 +02:00
|
|
|
--start;
|
2014-07-06 21:18:00 +02:00
|
|
|
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(start >= 0);
|
2007-09-03 23:16:24 +02:00
|
|
|
int end = piece + 1;
|
2014-07-06 21:18:00 +02:00
|
|
|
int upper_limit ;
|
|
|
|
if (options & align_expanded_pieces)
|
|
|
|
{
|
|
|
|
upper_limit = lower_limit + whole_pieces;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
upper_limit = start + whole_pieces;
|
|
|
|
}
|
2007-09-03 23:16:24 +02:00
|
|
|
if (upper_limit > int(m_piece_map.size())) upper_limit = int(m_piece_map.size());
|
|
|
|
while (end < upper_limit
|
2007-09-10 01:46:28 +02:00
|
|
|
&& can_pick(end, have))
|
2007-09-03 23:16:24 +02:00
|
|
|
++end;
|
|
|
|
return std::make_pair(start, end);
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
bool piece_picker::is_piece_finished(int index) const
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index < (int)m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
piece_pos const& p = m_piece_map[index];
|
|
|
|
if (p.index == piece_pos::we_have_index) return true;
|
|
|
|
|
|
|
|
int state = p.state;
|
|
|
|
if (state == piece_pos::piece_open)
|
2006-12-22 01:45:43 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(find_dl_piece(0, index) == m_downloads[0].end());
|
|
|
|
TORRENT_ASSERT(find_dl_piece(1, index) == m_downloads[1].end());
|
|
|
|
TORRENT_ASSERT(find_dl_piece(2, index) == m_downloads[2].end());
|
2006-12-22 01:45:43 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(state - 1,index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT((int)i->finished <= m_blocks_per_piece);
|
2003-10-30 00:28:09 +01:00
|
|
|
int max_blocks = blocks_in_piece(index);
|
2009-06-10 10:30:55 +02:00
|
|
|
if (int(i->finished) + int(i->writing) < max_blocks) return false;
|
|
|
|
TORRENT_ASSERT(int(i->finished) + int(i->writing) == max_blocks);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-06-10 22:46:09 +02:00
|
|
|
for (int k = 0; k < max_blocks; ++k)
|
|
|
|
{
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(i->info[k].piece_index == index);
|
2009-06-10 10:30:55 +02:00
|
|
|
TORRENT_ASSERT(i->info[k].state == block_info::state_finished
|
|
|
|
|| i->info[k].state == block_info::state_writing);
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
bool piece_picker::has_piece_passed(int index) const
|
2011-08-15 06:16:43 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(index < (int)m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(index >= 0);
|
|
|
|
|
|
|
|
piece_pos const& p = m_piece_map[index];
|
|
|
|
if (p.index == piece_pos::we_have_index) return true;
|
|
|
|
|
|
|
|
int state = p.state;
|
|
|
|
if (state == piece_pos::piece_open)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(find_dl_piece(0, index) == m_downloads[0].end());
|
|
|
|
TORRENT_ASSERT(find_dl_piece(1, index) == m_downloads[1].end());
|
|
|
|
TORRENT_ASSERT(find_dl_piece(2, index) == m_downloads[2].end());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(state - 1,index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
|
|
|
return i->passed_hash_check;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<piece_picker::downloading_piece>::iterator piece_picker::find_dl_piece(
|
|
|
|
int queue, int index)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(queue >= 0 && queue < num_download_categories);
|
|
|
|
// return std::find_if(m_downloads[queue].begin(), m_downloads[queue].end(), has_index(index));
|
2011-08-16 11:22:41 +02:00
|
|
|
downloading_piece cmp;
|
|
|
|
cmp.index = index;
|
2011-08-15 06:16:43 +02:00
|
|
|
std::vector<piece_picker::downloading_piece>::iterator i = std::lower_bound(
|
2014-07-06 21:18:00 +02:00
|
|
|
m_downloads[queue].begin(), m_downloads[queue].end(), cmp);
|
|
|
|
if (i == m_downloads[queue].end()) return i;
|
2011-08-15 06:16:43 +02:00
|
|
|
if (i->index == index) return i;
|
2014-07-06 21:18:00 +02:00
|
|
|
return m_downloads[queue].end();
|
2011-08-15 06:16:43 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<piece_picker::downloading_piece>::const_iterator piece_picker::find_dl_piece(
|
|
|
|
int queue, int index) const
|
2011-08-15 06:16:43 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(queue >= 0 && queue < num_download_categories);
|
|
|
|
// return std::find_if(m_downloads[queue].begin(), m_downloads[queue].end(), has_index(index));
|
2011-08-16 11:22:41 +02:00
|
|
|
downloading_piece cmp;
|
|
|
|
cmp.index = index;
|
2011-08-15 06:16:43 +02:00
|
|
|
std::vector<piece_picker::downloading_piece>::const_iterator i = std::lower_bound(
|
2014-07-06 21:18:00 +02:00
|
|
|
m_downloads[queue].begin(), m_downloads[queue].end(), cmp);
|
|
|
|
if (i == m_downloads[queue].end()) return i;
|
2011-08-15 06:16:43 +02:00
|
|
|
if (i->index == index) return i;
|
2014-07-06 21:18:00 +02:00
|
|
|
return m_downloads[queue].end();
|
2011-08-15 06:16:43 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<piece_picker::downloading_piece>::iterator piece_picker::update_piece_state(
|
|
|
|
std::vector<piece_picker::downloading_piece>::iterator dp)
|
2011-08-16 08:30:53 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "update_piece_state(" << dp->index << ")" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int num_blocks = blocks_in_piece(dp->index);
|
|
|
|
piece_pos& p = m_piece_map[dp->index];
|
|
|
|
int current_state = p.state;
|
|
|
|
TORRENT_ASSERT(current_state != piece_pos::piece_open);
|
|
|
|
if (current_state == piece_pos::piece_open)
|
|
|
|
return dp;
|
|
|
|
|
|
|
|
// this function is not allowed to create new downloading pieces
|
|
|
|
int new_state = 0;
|
|
|
|
if (p.filtered())
|
|
|
|
{
|
|
|
|
new_state = piece_pos::piece_zero_prio;
|
|
|
|
}
|
|
|
|
else if (dp->requested + dp->finished + dp->writing == 0)
|
|
|
|
{
|
|
|
|
new_state = piece_pos::piece_open;
|
|
|
|
}
|
|
|
|
else if (dp->requested + dp->finished + dp->writing < num_blocks)
|
|
|
|
{
|
|
|
|
new_state = piece_pos::piece_downloading;
|
|
|
|
}
|
|
|
|
else if (dp->requested > 0)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(dp->requested + dp->finished + dp->writing == num_blocks);
|
|
|
|
new_state = piece_pos::piece_full;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(dp->finished + dp->writing == num_blocks);
|
|
|
|
new_state = piece_pos::piece_finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << " new_state: " << new_state << " current_state: " << current_state << std::endl;
|
|
|
|
#endif
|
|
|
|
if (new_state == current_state) return dp;
|
|
|
|
if (new_state == piece_pos::piece_open) return dp;
|
|
|
|
|
|
|
|
// assert that the iterator that was passed-in in fact lives in
|
|
|
|
// the correct list
|
|
|
|
TORRENT_ASSERT(find_dl_piece(current_state - 1, dp->index) == dp);
|
|
|
|
|
|
|
|
// remove the download_piece from the list corresponding
|
|
|
|
// to the old state
|
|
|
|
downloading_piece dp_info = *dp;
|
|
|
|
m_downloads[current_state - 1].erase(dp);
|
|
|
|
|
|
|
|
// insert the download_piece in the list corresponding to
|
|
|
|
// the new state
|
|
|
|
downloading_piece cmp;
|
|
|
|
cmp.index = dp_info.index;
|
|
|
|
std::vector<downloading_piece>::iterator i = std::lower_bound(
|
|
|
|
m_downloads[new_state - 1].begin(), m_downloads[new_state - 1].end(), cmp);
|
|
|
|
TORRENT_ASSERT(i == m_downloads[new_state - 1].end() || i->index != dp_info.index);
|
|
|
|
i = m_downloads[new_state - 1].insert(i, dp_info);
|
|
|
|
|
|
|
|
int prio = p.priority(this);
|
|
|
|
p.state = new_state;
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << " " << dp_info.index << " state (" << current_state << " -> " << new_state << ")" << std::endl;
|
|
|
|
#endif
|
|
|
|
if (!m_dirty)
|
|
|
|
{
|
|
|
|
if (prio == -1 && p.priority(this) != -1) add(dp_info.index);
|
|
|
|
else if (prio != -1) update(prio, p.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int piece_picker::get_block_state(piece_block block) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
|
|
|
|
|
|
|
// if we have the piece, the block state is considered finished
|
|
|
|
if (m_piece_map[block.piece_index].index == piece_pos::we_have_index)
|
|
|
|
return block_info::state_finished;
|
|
|
|
|
|
|
|
int state = m_piece_map[block.piece_index].state;
|
|
|
|
if (state == piece_pos::piece_open) return block_info::state_none;
|
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(state - 1, block.piece_index);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
|
|
|
TORRENT_ASSERT(i->info[block.block_index].piece_index == block.piece_index);
|
|
|
|
return i->info[block.block_index].state;
|
2011-08-16 08:30:53 +02:00
|
|
|
}
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
bool piece_picker::is_requested(piece_block block) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2013-10-16 10:29:12 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
2013-10-16 18:55:09 +02:00
|
|
|
VALGRIND_CHECK_VALUE_IS_DEFINED(block);
|
2013-10-16 10:29:12 +02:00
|
|
|
#endif
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int state = m_piece_map[block.piece_index].state;
|
|
|
|
if (state == piece_pos::piece_open) return false;
|
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(state - 1, block.piece_index);
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
2014-01-22 10:53:47 +01:00
|
|
|
TORRENT_ASSERT(i->info >= &m_block_info[0]
|
|
|
|
&& i->info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(i->info[block.block_index].piece_index == block.piece_index);
|
2007-06-10 22:46:09 +02:00
|
|
|
return i->info[block.block_index].state == block_info::state_requested;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool piece_picker::is_downloaded(piece_block block) const
|
|
|
|
{
|
2013-10-16 10:29:12 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
2013-10-16 18:55:09 +02:00
|
|
|
VALGRIND_CHECK_VALUE_IS_DEFINED(block);
|
2013-10-16 10:29:12 +02:00
|
|
|
#endif
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
2007-06-10 22:46:09 +02:00
|
|
|
|
|
|
|
if (m_piece_map[block.piece_index].index == piece_pos::we_have_index) return true;
|
2014-07-06 21:18:00 +02:00
|
|
|
int state = m_piece_map[block.piece_index].state;
|
|
|
|
if (state == piece_pos::piece_open) return false;
|
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(state - 1, block.piece_index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[state-1].end());
|
2014-01-22 10:53:47 +01:00
|
|
|
TORRENT_ASSERT(i->info >= &m_block_info[0]
|
|
|
|
&& i->info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(i->info[block.block_index].piece_index == block.piece_index);
|
2007-06-10 22:46:09 +02:00
|
|
|
return i->info[block.block_index].state == block_info::state_finished
|
|
|
|
|| i->info[block.block_index].state == block_info::state_writing;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2003-11-05 00:27:06 +01:00
|
|
|
bool piece_picker::is_finished(piece_block block) const
|
|
|
|
{
|
2013-10-16 10:29:12 +02:00
|
|
|
#ifdef TORRENT_USE_VALGRIND
|
2013-10-16 18:55:09 +02:00
|
|
|
VALGRIND_CHECK_VALUE_IS_DEFINED(block);
|
2013-10-16 10:29:12 +02:00
|
|
|
#endif
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
2003-11-05 00:27:06 +01:00
|
|
|
|
2013-10-16 10:29:12 +02:00
|
|
|
piece_pos const& p = m_piece_map[block.piece_index];
|
|
|
|
if (p.index == piece_pos::we_have_index) return true;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (p.state == piece_pos::piece_open) return false;
|
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(p.state - 1, block.piece_index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[p.state - 1].end());
|
2014-01-22 10:53:47 +01:00
|
|
|
TORRENT_ASSERT(i->info >= &m_block_info[0]
|
|
|
|
&& i->info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(i->info[block.block_index].piece_index == block.piece_index);
|
2007-06-10 22:46:09 +02:00
|
|
|
return i->info[block.block_index].state == block_info::state_finished;
|
2003-11-05 00:27:06 +01:00
|
|
|
}
|
|
|
|
|
2007-09-15 22:20:07 +02:00
|
|
|
bool piece_picker::mark_as_downloading(piece_block block
|
2007-07-04 04:16:49 +02:00
|
|
|
, void* peer, piece_state_t state)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "mark_as_downloading( {" << block.piece_index << ", " << block.block_index << "} )" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TORRENT_ASSERT(peer == 0 || static_cast<torrent_peer*>(peer)->in_use);
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(state != piece_picker::none);
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(int(block.block_index) < blocks_in_piece(block.piece_index));
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(!m_piece_map[block.piece_index].have());
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
piece_pos& p = m_piece_map[block.piece_index];
|
2014-07-06 21:18:00 +02:00
|
|
|
if (p.state == piece_pos::piece_open)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2008-01-31 18:52:29 +01:00
|
|
|
int prio = p.priority(this);
|
|
|
|
TORRENT_ASSERT(prio < int(m_priority_boundries.size())
|
|
|
|
|| m_dirty);
|
2014-07-06 21:18:00 +02:00
|
|
|
p.state = piece_pos::piece_downloading;
|
2008-09-06 23:04:57 +02:00
|
|
|
if (prio >= 0 && !m_dirty) update(prio, p.index);
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
dlpiece_iter dp = add_download_piece(block.piece_index);
|
|
|
|
dp->state = state;
|
|
|
|
block_info& info = dp->info[block.block_index];
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2007-06-10 22:46:09 +02:00
|
|
|
info.state = block_info::state_requested;
|
2007-05-08 13:13:13 +02:00
|
|
|
info.peer = peer;
|
2007-07-06 19:15:35 +02:00
|
|
|
info.num_peers = 1;
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
TORRENT_ASSERT(info.peers.count(peer) == 0);
|
|
|
|
info.peers.insert(peer);
|
|
|
|
#endif
|
|
|
|
++dp->requested;
|
|
|
|
// update_full may move the downloading piece to
|
|
|
|
// a different vector, so 'dp' may be invalid after
|
|
|
|
// this call
|
|
|
|
update_piece_state(dp);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(p.state - 1, block.piece_index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[p.state - 1].end());
|
2007-05-08 13:13:13 +02:00
|
|
|
block_info& info = i->info[block.block_index];
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(&info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(&info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2007-09-15 22:20:07 +02:00
|
|
|
if (info.state == block_info::state_writing
|
|
|
|
|| info.state == block_info::state_finished)
|
2014-07-06 21:18:00 +02:00
|
|
|
{
|
2007-09-15 22:20:07 +02:00
|
|
|
return false;
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(info.state == block_info::state_none
|
2007-07-06 19:15:35 +02:00
|
|
|
|| (info.state == block_info::state_requested
|
|
|
|
&& (info.num_peers > 0)));
|
2007-05-08 13:13:13 +02:00
|
|
|
info.peer = peer;
|
2007-07-06 19:15:35 +02:00
|
|
|
if (info.state != block_info::state_requested)
|
|
|
|
{
|
|
|
|
info.state = block_info::state_requested;
|
|
|
|
++i->requested;
|
2014-07-06 21:18:00 +02:00
|
|
|
i = update_piece_state(i);
|
2007-07-06 19:15:35 +02:00
|
|
|
}
|
|
|
|
++info.num_peers;
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
TORRENT_ASSERT(info.peers.count(peer) == 0);
|
|
|
|
info.peers.insert(peer);
|
|
|
|
#endif
|
2007-04-27 02:27:37 +02:00
|
|
|
if (i->state == none) i->state = state;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2007-09-15 22:20:07 +02:00
|
|
|
return true;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2007-07-06 19:15:35 +02:00
|
|
|
int piece_picker::num_peers(piece_block block) const
|
|
|
|
{
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(int(block.block_index) < blocks_in_piece(block.piece_index));
|
2007-07-06 19:15:35 +02:00
|
|
|
|
|
|
|
piece_pos const& p = m_piece_map[block.piece_index];
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!p.downloading()) return 0;
|
2007-07-06 19:15:35 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(p.state - 1, block.piece_index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[p.state - 1].end());
|
2007-07-06 19:15:35 +02:00
|
|
|
|
|
|
|
block_info const& info = i->info[block.block_index];
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(&info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(&info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2007-07-06 19:15:35 +02:00
|
|
|
return info.num_peers;
|
|
|
|
}
|
2010-01-15 17:45:42 +01:00
|
|
|
|
2007-05-30 08:52:59 +02:00
|
|
|
void piece_picker::get_availability(std::vector<int>& avail) const
|
|
|
|
{
|
2008-06-11 10:30:06 +02:00
|
|
|
TORRENT_ASSERT(m_seeds >= 0);
|
2007-05-30 08:52:59 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
|
|
|
|
|
|
|
avail.resize(m_piece_map.size());
|
|
|
|
std::vector<int>::iterator j = avail.begin();
|
|
|
|
for (std::vector<piece_pos>::const_iterator i = m_piece_map.begin()
|
|
|
|
, end(m_piece_map.end()); i != end; ++i, ++j)
|
2008-01-31 18:52:29 +01:00
|
|
|
*j = i->peer_count + m_seeds;
|
2007-05-30 08:52:59 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int piece_picker::get_availability(int piece) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(piece >= 0 && piece < int(m_piece_map.size()));
|
|
|
|
return m_piece_map[piece].peer_count + m_seeds;
|
|
|
|
}
|
|
|
|
|
2010-05-01 19:47:28 +02:00
|
|
|
bool piece_picker::mark_as_writing(piece_block block, void* peer)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2006-07-16 02:08:50 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2006-07-16 02:08:50 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "mark_as_writing( {" << block.piece_index << ", " << block.block_index << "} )" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TORRENT_ASSERT(peer == 0 || static_cast<torrent_peer*>(peer)->in_use);
|
2012-04-12 19:10:22 +02:00
|
|
|
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(int(block.block_index) < blocks_in_piece(block.piece_index));
|
2014-07-06 21:18:00 +02:00
|
|
|
// this is not valid for web peers
|
|
|
|
// TORRENT_ASSERT(peer != 0);
|
2003-11-05 00:27:06 +01:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
piece_pos& p = m_piece_map[block.piece_index];
|
2014-07-06 21:18:00 +02:00
|
|
|
if (p.downloading() == 0)
|
2009-03-17 10:34:44 +01:00
|
|
|
{
|
2011-02-09 03:56:00 +01:00
|
|
|
// if we already have this piece, just ignore this
|
|
|
|
if (have_piece(block.piece_index)) return false;
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
|
|
|
#endif
|
|
|
|
int prio = p.priority(this);
|
|
|
|
TORRENT_ASSERT(prio < int(m_priority_boundries.size())
|
|
|
|
|| m_dirty);
|
2014-07-06 21:18:00 +02:00
|
|
|
p.state = piece_pos::piece_downloading;
|
2011-04-16 21:25:39 +02:00
|
|
|
// prio being -1 can happen if a block is requested before
|
|
|
|
// the piece priority was set to 0
|
2009-03-17 10:34:44 +01:00
|
|
|
if (prio >= 0 && !m_dirty) update(prio, p.index);
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
dlpiece_iter dp = add_download_piece(block.piece_index);
|
|
|
|
dp->state = none;
|
|
|
|
block_info& info = dp->info[block.block_index];
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(&info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(&info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2009-03-17 10:34:44 +01:00
|
|
|
info.state = block_info::state_writing;
|
|
|
|
info.peer = peer;
|
|
|
|
info.num_peers = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
info.peers.clear();
|
|
|
|
#endif
|
|
|
|
dp->writing = 1;
|
|
|
|
|
|
|
|
update_piece_state(dp);
|
2009-03-17 10:34:44 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(p.state - 1, block.piece_index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[p.state - 1].end());
|
2009-03-17 10:34:44 +01:00
|
|
|
block_info& info = i->info[block.block_index];
|
2008-06-23 15:02:41 +02:00
|
|
|
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(&info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(&info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
info.peer = peer;
|
|
|
|
if (info.state == block_info::state_requested) --i->requested;
|
|
|
|
TORRENT_ASSERT(i->requested >= 0);
|
2010-05-01 19:47:28 +02:00
|
|
|
if (info.state == block_info::state_writing
|
|
|
|
|| info.state == block_info::state_finished)
|
|
|
|
return false;
|
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
++i->writing;
|
|
|
|
info.state = block_info::state_writing;
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
// all other requests for this block should have been
|
|
|
|
// cancelled now
|
|
|
|
info.num_peers = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
info.peers.clear();
|
|
|
|
#endif
|
2007-07-07 03:26:30 +02:00
|
|
|
|
2009-03-17 10:34:44 +01:00
|
|
|
if (i->requested == 0)
|
|
|
|
{
|
|
|
|
// there are no blocks requested in this piece.
|
|
|
|
// remove the fast/slow state from it
|
|
|
|
i->state = none;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
update_piece_state(i);
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
2010-05-01 19:47:28 +02:00
|
|
|
return true;
|
2007-06-10 22:46:09 +02:00
|
|
|
}
|
2008-02-18 04:07:14 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// calling this function prevents this piece from being picked
|
|
|
|
// by the piece picker until the pieces is restored. This allow
|
|
|
|
// the disk thread to synchronize and flush any failed state
|
|
|
|
// (used for disk write failures and piece hash failures).
|
|
|
|
void piece_picker::lock_piece(int piece)
|
|
|
|
{
|
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "lock_piece(" << piece << ")" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int state = m_piece_map[piece].state;
|
|
|
|
if (state == piece_pos::piece_open) return;
|
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(state - 1, piece);
|
|
|
|
if (i == m_downloads[state - 1].end()) return;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(i->passed_hash_check == false);
|
|
|
|
if (i->passed_hash_check)
|
|
|
|
{
|
|
|
|
// it's not clear why this would happen,
|
|
|
|
// but it seems reasonable to not break the
|
|
|
|
// accounting over it.
|
|
|
|
i->passed_hash_check = false;
|
|
|
|
TORRENT_ASSERT(m_num_passed > 0);
|
|
|
|
--m_num_passed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prevent this piece from being picked until it's restored
|
|
|
|
i->locked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: 3 it would be nice if this could be folded into lock_piece()
|
|
|
|
// the main distinction is that this also maintains the m_num_passed
|
|
|
|
// counter and the passed_hash_check member
|
2009-06-10 10:30:55 +02:00
|
|
|
void piece_picker::write_failed(piece_block block)
|
2008-02-18 04:07:14 +01:00
|
|
|
{
|
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "write_failed( {" << block.piece_index << ", " << block.block_index << "} )" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int state = m_piece_map[block.piece_index].state;
|
|
|
|
if (state == piece_pos::piece_open) return;
|
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(state - 1, block.piece_index);
|
|
|
|
if (i == m_downloads[state - 1].end()) return;
|
2008-02-18 04:07:14 +01:00
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
block_info& info = i->info[block.block_index];
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(&info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(&info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2009-06-10 10:30:55 +02:00
|
|
|
TORRENT_ASSERT(info.state == block_info::state_writing);
|
|
|
|
TORRENT_ASSERT(info.num_peers == 0);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(i->writing > 0);
|
|
|
|
TORRENT_ASSERT(info.state == block_info::state_writing);
|
|
|
|
|
|
|
|
if (info.state == block_info::state_finished) return;
|
|
|
|
if (info.state == block_info::state_writing) --i->writing;
|
|
|
|
|
|
|
|
info.peer = 0;
|
|
|
|
info.state = block_info::state_none;
|
2014-07-06 21:18:00 +02:00
|
|
|
if (i->passed_hash_check)
|
|
|
|
{
|
|
|
|
// the hash was good, but we failed to write
|
|
|
|
// some of the blocks to disk, which means we
|
|
|
|
// can't consider the piece complete
|
|
|
|
i->passed_hash_check = false;
|
|
|
|
TORRENT_ASSERT(m_num_passed > 0);
|
|
|
|
--m_num_passed;
|
|
|
|
}
|
2009-06-10 10:30:55 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// prevent this hash job from actually completing
|
|
|
|
// this piece, by setting the failure state.
|
|
|
|
// the piece is unlocked in the call to restore_piece()
|
|
|
|
i->locked = true;
|
|
|
|
|
|
|
|
i = update_piece_state(i);
|
2011-08-16 08:30:53 +02:00
|
|
|
|
2009-06-10 10:30:55 +02:00
|
|
|
if (i->finished + i->writing + i->requested == 0)
|
|
|
|
{
|
|
|
|
piece_pos& p = m_piece_map[block.piece_index];
|
|
|
|
int prev_priority = p.priority(this);
|
|
|
|
erase_download_piece(i);
|
|
|
|
int new_priority = p.priority(this);
|
|
|
|
|
|
|
|
if (m_dirty) return;
|
|
|
|
if (new_priority == prev_priority) return;
|
2011-11-07 05:31:48 +01:00
|
|
|
if (prev_priority == -1) add(block.piece_index);
|
2009-06-10 10:30:55 +02:00
|
|
|
else update(prev_priority, p.index);
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void piece_picker::mark_as_canceled(piece_block block, void* peer)
|
|
|
|
{
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "mark_as_cancelled( {" << block.piece_index << ", " << block.block_index << "} )" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TORRENT_ASSERT(block.piece_index >= 0);
|
|
|
|
TORRENT_ASSERT(block.block_index >= 0);
|
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(int(block.block_index) < blocks_in_piece(block.piece_index));
|
|
|
|
|
|
|
|
piece_pos& p = m_piece_map[block.piece_index];
|
|
|
|
|
|
|
|
if (p.state == piece_pos::piece_open) return;
|
|
|
|
|
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(p.state - 1, block.piece_index);
|
|
|
|
|
|
|
|
TORRENT_ASSERT(i != m_downloads[p.state - 1].end());
|
|
|
|
block_info& info = i->info[block.block_index];
|
|
|
|
|
|
|
|
if (info.state == block_info::state_finished) return;
|
|
|
|
|
|
|
|
TORRENT_ASSERT(info.num_peers == 0);
|
|
|
|
info.peer = peer;
|
|
|
|
TORRENT_ASSERT(info.state == block_info::state_writing
|
|
|
|
|| peer == 0);
|
|
|
|
TORRENT_ASSERT(i->writing >= 0);
|
|
|
|
if (info.state == block_info::state_writing)
|
|
|
|
{
|
|
|
|
--i->writing;
|
|
|
|
info.state = block_info::state_none;
|
|
|
|
}
|
2009-06-10 10:30:55 +02:00
|
|
|
else
|
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(info.state == block_info::state_none);
|
2009-06-10 10:30:55 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
2008-02-18 04:07:14 +01:00
|
|
|
}
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
void piece_picker::mark_as_finished(piece_block block, void* peer)
|
2007-06-10 22:46:09 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "mark_as_finished( {" << block.piece_index << ", " << block.block_index << "} )" << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TORRENT_ASSERT(peer == 0 || static_cast<torrent_peer*>(peer)->in_use);
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(block.piece_index >= 0);
|
|
|
|
TORRENT_ASSERT(block.block_index >= 0);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(int(block.block_index) < blocks_in_piece(block.piece_index));
|
2007-06-10 22:46:09 +02:00
|
|
|
|
|
|
|
piece_pos& p = m_piece_map[block.piece_index];
|
2005-05-25 12:01:01 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (p.state == piece_pos::piece_open)
|
2003-11-05 00:27:06 +01:00
|
|
|
{
|
2011-02-09 03:56:00 +01:00
|
|
|
// if we already have this piece, just ignore this
|
|
|
|
if (have_piece(block.piece_index)) return;
|
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2007-06-10 22:46:09 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2008-01-31 18:52:29 +01:00
|
|
|
int prio = p.priority(this);
|
|
|
|
TORRENT_ASSERT(prio < int(m_priority_boundries.size())
|
|
|
|
|| m_dirty);
|
2014-07-06 21:18:00 +02:00
|
|
|
p.state = piece_pos::piece_downloading;
|
2008-01-31 18:52:29 +01:00
|
|
|
if (prio >= 0 && !m_dirty) update(prio, p.index);
|
2003-11-05 00:27:06 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
dlpiece_iter dp = add_download_piece(block.piece_index);
|
|
|
|
dp->state = none;
|
|
|
|
block_info& info = dp->info[block.block_index];
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(&info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(&info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2007-06-10 22:46:09 +02:00
|
|
|
info.peer = peer;
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(info.state == block_info::state_none);
|
2008-02-18 04:07:14 +01:00
|
|
|
TORRENT_ASSERT(info.num_peers == 0);
|
2014-07-06 21:18:00 +02:00
|
|
|
++dp->finished;
|
2007-06-10 22:46:09 +02:00
|
|
|
info.state = block_info::state_finished;
|
2014-07-06 21:18:00 +02:00
|
|
|
// dp may be invalid after this call
|
|
|
|
update_piece_state(dp);
|
2003-11-05 00:27:06 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2007-06-10 22:46:09 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-09-06 23:04:57 +02:00
|
|
|
#endif
|
2007-06-10 22:46:09 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(p.state - 1, block.piece_index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[p.state - 1].end());
|
2007-05-08 13:13:13 +02:00
|
|
|
block_info& info = i->info[block.block_index];
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(&info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(&info < &m_block_info[0] + m_block_info.size());
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2009-01-14 08:41:25 +01:00
|
|
|
|
|
|
|
if (info.state == block_info::state_finished) return;
|
|
|
|
|
2008-02-18 04:07:14 +01:00
|
|
|
TORRENT_ASSERT(info.num_peers == 0);
|
2013-08-19 05:54:45 +02:00
|
|
|
|
|
|
|
// peers may have been disconnected in between mark_as_writing
|
|
|
|
// and mark_as_finished. When a peer disconnects, its m_peer_info
|
|
|
|
// pointer is set to NULL. If so, preserve the previous peer
|
|
|
|
// pointer, instead of forgetting who we downloaded this block from
|
|
|
|
if (info.state != block_info::state_writing || peer != 0)
|
|
|
|
info.peer = peer;
|
|
|
|
|
2007-06-10 22:46:09 +02:00
|
|
|
++i->finished;
|
2007-07-15 17:41:55 +02:00
|
|
|
if (info.state == block_info::state_writing)
|
2007-08-22 20:40:31 +02:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(i->writing > 0);
|
2007-07-15 17:41:55 +02:00
|
|
|
--i->writing;
|
2007-08-22 20:40:31 +02:00
|
|
|
info.state = block_info::state_finished;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(info.state == block_info::state_none);
|
2007-08-22 20:40:31 +02:00
|
|
|
info.state = block_info::state_finished;
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
i = update_piece_state(i);
|
|
|
|
|
|
|
|
if (i->finished < blocks_in_piece(i->index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (i->passed_hash_check)
|
|
|
|
we_have(i->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
|
|
|
check_piece_state();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void piece_picker::mark_as_checking(int index)
|
|
|
|
{
|
|
|
|
int state = m_piece_map[index].state;
|
|
|
|
if (state == piece_pos::piece_open) return;
|
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(state - 1, index);
|
|
|
|
if (i == m_downloads[state - 1].end()) return;
|
|
|
|
TORRENT_ASSERT(i->outstanding_hash_check == false);
|
|
|
|
i->outstanding_hash_check = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void piece_picker::mark_as_done_checking(int index)
|
|
|
|
{
|
|
|
|
int state = m_piece_map[index].state;
|
|
|
|
if (state == piece_pos::piece_open) return;
|
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(state - 1, index);
|
|
|
|
if (i == m_downloads[state - 1].end()) return;
|
|
|
|
i->outstanding_hash_check = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void piece_picker::get_requestors(std::vector<void*>& d, int index) const
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(index >= 0 && index <= (int)m_piece_map.size());
|
|
|
|
|
|
|
|
int state = m_piece_map[index].state;
|
|
|
|
TORRENT_ASSERT(state != piece_pos::piece_open);
|
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(state - 1, index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
|
|
|
|
|
|
|
d.clear();
|
|
|
|
for (int j = 0, end(blocks_in_piece(index)); j != end; ++j)
|
|
|
|
{
|
|
|
|
if (i->info[j].state != block_info::state_requested) continue;
|
|
|
|
if (i->info[j].peer == 0) continue;
|
|
|
|
d.push_back(i->info[j].peer);
|
2003-11-05 00:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
void piece_picker::get_downloaders(std::vector<void*>& d, int index) const
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(index >= 0 && index <= (int)m_piece_map.size());
|
2003-11-02 22:06:50 +01:00
|
|
|
|
|
|
|
d.clear();
|
2014-07-06 21:18:00 +02:00
|
|
|
int state = m_piece_map[index].state;
|
|
|
|
if (state == piece_pos::piece_open)
|
|
|
|
{
|
|
|
|
int num_blocks = blocks_in_piece(index);
|
|
|
|
for (int i = 0; i < num_blocks; ++i) d.push_back(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(state - 1, index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
|
|
|
TORRENT_ASSERT(i->info >= &m_block_info[0]
|
|
|
|
&& i->info < &m_block_info[0] + m_block_info.size());
|
2011-08-13 20:46:52 +02:00
|
|
|
for (int j = 0, end(blocks_in_piece(index)); j != end; ++j)
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(i->info[j].peer == 0 || static_cast<torrent_peer*>(i->info[j].peer)->in_use);
|
2003-11-02 22:06:50 +01:00
|
|
|
d.push_back(i->info[j].peer);
|
|
|
|
}
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-07-04 04:16:49 +02:00
|
|
|
void* piece_picker::get_downloader(piece_block block) const
|
2004-01-13 04:08:59 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
int state = m_piece_map[block.piece_index].state;
|
|
|
|
if (state == piece_pos::piece_open) return 0;
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::const_iterator i = find_dl_piece(state - 1, block.piece_index);
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(i->info[block.block_index].piece_index == block.piece_index);
|
2007-06-10 22:46:09 +02:00
|
|
|
if (i->info[block.block_index].state == block_info::state_none)
|
2007-07-04 04:16:49 +02:00
|
|
|
return 0;
|
2004-01-13 04:08:59 +01:00
|
|
|
|
2012-04-12 19:10:22 +02:00
|
|
|
void* peer = i->info[block.block_index].peer;
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(peer == 0 || static_cast<torrent_peer*>(peer)->in_use);
|
2012-04-12 19:10:22 +02:00
|
|
|
return peer;
|
2004-01-13 04:08:59 +01:00
|
|
|
}
|
|
|
|
|
2008-02-18 21:55:03 +01:00
|
|
|
// this is called when a request is rejected or when
|
|
|
|
// a peer disconnects. The piece might be in any state
|
2010-10-04 00:06:53 +02:00
|
|
|
void piece_picker::abort_download(piece_block block, void* peer)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-10-10 09:08:46 +02:00
|
|
|
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
|
2006-07-16 02:08:50 +02:00
|
|
|
TORRENT_PIECE_PICKER_INVARIANT_CHECK;
|
2008-10-10 09:08:46 +02:00
|
|
|
#endif
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
#ifdef TORRENT_PICKER_LOG
|
|
|
|
std::cerr << "[" << this << "] " << "abort_download( {" << block.piece_index << ", " << block.block_index << "} )" << std::endl;
|
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(peer == 0 || static_cast<torrent_peer*>(peer)->in_use);
|
2012-04-12 19:10:22 +02:00
|
|
|
|
2012-05-03 03:51:56 +02:00
|
|
|
TORRENT_ASSERT(block.block_index != piece_block::invalid.block_index);
|
|
|
|
TORRENT_ASSERT(block.piece_index != piece_block::invalid.piece_index);
|
2011-02-21 06:24:41 +01:00
|
|
|
TORRENT_ASSERT(block.piece_index < m_piece_map.size());
|
|
|
|
TORRENT_ASSERT(int(block.block_index) < blocks_in_piece(block.piece_index));
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
int state = m_piece_map[block.piece_index].state;
|
|
|
|
if (state == piece_pos::piece_open) return;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<downloading_piece>::iterator i = find_dl_piece(state - 1, block.piece_index);
|
|
|
|
TORRENT_ASSERT(i != m_downloads[state - 1].end());
|
2003-11-05 00:27:06 +01:00
|
|
|
|
2007-07-06 19:15:35 +02:00
|
|
|
block_info& info = i->info[block.block_index];
|
2013-10-06 19:00:07 +02:00
|
|
|
TORRENT_ASSERT(&info >= &m_block_info[0]);
|
|
|
|
TORRENT_ASSERT(&info < &m_block_info[0] + m_block_info.size());
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(info.peer == 0 || static_cast<torrent_peer*>(info.peer)->in_use);
|
2012-04-12 07:00:20 +02:00
|
|
|
TORRENT_ASSERT(info.piece_index == block.piece_index);
|
2008-02-18 04:07:14 +01:00
|
|
|
|
2008-09-06 23:04:57 +02:00
|
|
|
TORRENT_ASSERT(info.state != block_info::state_none);
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (info.state != block_info::state_requested) return;
|
2008-02-18 21:55:03 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
piece_pos& p = m_piece_map[block.piece_index];
|
|
|
|
int prev_prio = p.priority(this);
|
|
|
|
|
|
|
|
#if TORRENT_USE_ASSERTS
|
|
|
|
TORRENT_ASSERT(info.peers.count(peer));
|
|
|
|
info.peers.erase(peer);
|
|
|
|
#endif
|
|
|
|
TORRENT_ASSERT(info.num_peers > 0);
|
|
|
|
if (info.num_peers > 0) --info.num_peers;
|
|
|
|
if (info.peer == peer) info.peer = 0;
|
|
|
|
TORRENT_ASSERT(info.peers.size() == info.num_peers);
|
2008-09-06 23:04:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
TORRENT_ASSERT(int(block.block_index) < blocks_in_piece(block.piece_index));
|
2008-02-18 04:07:14 +01:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// if there are other peers, leave the block requested
|
|
|
|
if (info.num_peers > 0) return;
|
2007-07-06 19:15:35 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// clear the downloader of this block
|
|
|
|
info.peer = 0;
|
2007-03-30 21:18:03 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// clear this block as being downloaded
|
|
|
|
info.state = block_info::state_none;
|
|
|
|
TORRENT_ASSERT(i->requested > 0);
|
|
|
|
--i->requested;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2006-12-15 03:26:11 +01:00
|
|
|
// if there are no other blocks in this piece
|
2003-10-23 01:00:57 +02:00
|
|
|
// that's being downloaded, remove it from the list
|
2007-06-10 22:46:09 +02:00
|
|
|
if (i->requested + i->finished + i->writing == 0)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2008-01-31 18:52:29 +01:00
|
|
|
TORRENT_ASSERT(prev_prio < int(m_priority_boundries.size())
|
|
|
|
|| m_dirty);
|
2009-06-10 10:30:55 +02:00
|
|
|
erase_download_piece(i);
|
2014-07-06 21:18:00 +02:00
|
|
|
int prio = p.priority(this);
|
2008-09-06 23:04:57 +02:00
|
|
|
if (!m_dirty)
|
2008-01-31 18:52:29 +01:00
|
|
|
{
|
|
|
|
if (prev_prio == -1 && prio >= 0) add(block.piece_index);
|
|
|
|
else if (prev_prio >= 0) update(prev_prio, p.index);
|
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
return;
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2007-06-10 22:46:09 +02:00
|
|
|
else if (i->requested == 0)
|
2007-04-27 02:27:37 +02:00
|
|
|
{
|
2007-05-08 13:13:13 +02:00
|
|
|
// there are no blocks requested in this piece.
|
|
|
|
// remove the fast/slow state from it
|
|
|
|
i->state = none;
|
2007-04-27 02:27:37 +02:00
|
|
|
}
|
2014-07-06 21:18:00 +02:00
|
|
|
|
|
|
|
i = update_piece_state(i);
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
2003-10-30 00:28:09 +01:00
|
|
|
int piece_picker::unverified_blocks() const
|
|
|
|
{
|
|
|
|
int counter = 0;
|
2014-07-06 21:18:00 +02:00
|
|
|
for (int k = 0; k < num_download_categories; ++k)
|
2003-10-30 00:28:09 +01:00
|
|
|
{
|
2014-07-06 21:18:00 +02:00
|
|
|
for (std::vector<downloading_piece>::const_iterator i = m_downloads[k].begin();
|
|
|
|
i != m_downloads[k].end(); ++i)
|
|
|
|
{
|
|
|
|
counter += (int)i->finished;
|
|
|
|
}
|
2003-10-30 00:28:09 +01:00
|
|
|
}
|
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
2005-05-13 02:39:39 +02:00
|
|
|
|