From 73b4af0eeb0937e571e22ba30742210fb8187371 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 25 Aug 2008 22:32:50 +0000 Subject: [PATCH] fixes bug where priorities where lost when force-rechecking. git-svn-id: http://libtorrent.svn.sourceforge.net/svnroot/libtorrent/trunk@2637 a83610d8-ad2a-0410-a6ab-fc0612d85776 --- include/libtorrent/piece_picker.hpp | 1 + src/piece_picker.cpp | 16 +++++++++++++--- src/torrent.cpp | 1 - test/test_piece_picker.cpp | 27 +++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/include/libtorrent/piece_picker.hpp b/include/libtorrent/piece_picker.hpp index bd0f862b..5bcae3c9 100644 --- a/include/libtorrent/piece_picker.hpp +++ b/include/libtorrent/piece_picker.hpp @@ -161,6 +161,7 @@ namespace libtorrent void we_have(int index); void we_dont_have(int index); + // sets all pieces to dont-have void init(int blocks_per_piece, int total_num_blocks); int num_pieces() const { return int(m_piece_map.size()); } diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index b6dbc2a8..bb0b460a 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -82,6 +82,18 @@ namespace libtorrent m_piece_map.resize((total_num_blocks + blocks_per_piece-1) / blocks_per_piece , piece_pos(0, 0)); + m_num_filtered += m_num_have_filtered; + m_num_have_filtered = 0; + m_num_have = 0; + m_dirty = true; + for (std::vector::iterator i = m_piece_map.begin() + , end(m_piece_map.end()); i != end; ++i) + { + i->peer_count = 0; + i->downloading = 0; + i->index = 0; + } + // the piece index is stored in 20 bits, which limits the allowed // number of pieces somewhat if (m_piece_map.size() >= piece_pos::we_have_index) @@ -1074,7 +1086,7 @@ namespace libtorrent if (new_piece_priority == int(p.piece_priority)) return false; int prev_priority = p.priority(this); - TORRENT_ASSERT(prev_priority < int(m_priority_boundries.size())); + TORRENT_ASSERT(m_dirty | prev_priority < int(m_priority_boundries.size())); bool ret = false; if (new_piece_priority == piece_pos::filter_priority @@ -1101,8 +1113,6 @@ namespace libtorrent if (prev_priority == new_priority) return ret; - TORRENT_ASSERT(prev_priority < int(m_priority_boundries.size())); - if (m_dirty) return ret; if (prev_priority == -1) { diff --git a/src/torrent.cpp b/src/torrent.cpp index 555d4856..f0a16bd4 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -667,7 +667,6 @@ namespace libtorrent , m_save_path, m_ses.m_files, m_ses.m_disk_thread, m_storage_constructor , m_storage_mode); m_storage = m_owning_storage.get(); - m_picker.reset(new piece_picker); m_picker->init(m_torrent_file->piece_length() / m_block_size , int((m_torrent_file->total_size()+m_block_size-1)/m_block_size)); // assume that we don't have anything diff --git a/test/test_piece_picker.cpp b/test/test_piece_picker.cpp index dff64de3..b90fa223 100644 --- a/test/test_piece_picker.cpp +++ b/test/test_piece_picker.cpp @@ -280,6 +280,33 @@ int test_main() // ======================================================== + // make sure init preserves priorities + print_title("test init"); + p = setup_picker("1111111", " ", "1111111", ""); + + TEST_CHECK(p->num_filtered() == 0); + TEST_CHECK(p->num_have_filtered() == 0); + TEST_CHECK(p->num_have() == 0); + + p->set_piece_priority(0, 0); + TEST_CHECK(p->num_filtered() == 1); + TEST_CHECK(p->num_have_filtered() == 0); + TEST_CHECK(p->num_have() == 0); + + p->we_have(0); + + TEST_CHECK(p->num_filtered() == 0); + TEST_CHECK(p->num_have_filtered() == 1); + TEST_CHECK(p->num_have() == 1); + + p->init(blocks_per_piece, blocks_per_piece * 7); + TEST_CHECK(p->piece_priority(0) == 0); + TEST_CHECK(p->num_filtered() == 1); + TEST_CHECK(p->num_have_filtered() == 0); + TEST_CHECK(p->num_have() == 0); + +// ======================================================== + // make sure requested blocks aren't picked print_title("test don't pick requested blocks"); p = setup_picker("1234567", " ", "", ""); -- 2.11.4.GIT