diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 22fe34005..52ab67766 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -573,7 +573,7 @@ namespace libtorrent , boost::intrusive_ptr p) const; bool request_bandwidth_from_session(int channel) const; - void update_peer_interest(); + void update_peer_interest(bool was_finished); boost::intrusive_ptr m_torrent_file; diff --git a/src/torrent.cpp b/src/torrent.cpp index 35509ce04..a211ec3e4 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1488,9 +1488,10 @@ namespace libtorrent TORRENT_ASSERT(index >= 0); TORRENT_ASSERT(index < m_torrent_file->num_pieces()); + bool was_finished = is_finished(); bool filter_updated = m_picker->set_piece_priority(index, priority); TORRENT_ASSERT(m_num_pieces >= m_picker->num_have_filtered()); - if (filter_updated) update_peer_interest(); + if (filter_updated) update_peer_interest(was_finished); } int torrent::piece_priority(int index) const @@ -1520,6 +1521,7 @@ namespace libtorrent int index = 0; bool filter_updated = false; + bool was_finished = is_finished(); for (std::vector::const_iterator i = pieces.begin() , end(pieces.end()); i != end; ++i, ++index) { @@ -1528,7 +1530,7 @@ namespace libtorrent filter_updated |= m_picker->set_piece_priority(index, *i); TORRENT_ASSERT(m_num_pieces >= m_picker->num_have_filtered()); } - if (filter_updated) update_peer_interest(); + if (filter_updated) update_peer_interest(was_finished); } void torrent::piece_priorities(std::vector& pieces) const @@ -1571,6 +1573,8 @@ namespace libtorrent if (m_torrent_file->num_pieces() == 0) return; + bool was_finished = is_finished(); + int piece_length = m_torrent_file->piece_length(); // initialize the piece priorities to 0, then only allow // setting higher priorities @@ -1594,19 +1598,24 @@ namespace libtorrent , bind(&set_if_greater, _1, files[i])); } prioritize_pieces(pieces); - update_peer_interest(); + update_peer_interest(was_finished); } + // this is called when piece priorities have been updated // updates the interested flag in peers - void torrent::update_peer_interest() + void torrent::update_peer_interest(bool was_finished) { for (peer_iterator i = begin(); i != end(); ++i) (*i)->update_interest(); // if we used to be finished, but we aren't anymore // we may need to connect to peers again - if (!is_finished()) + if (!is_finished() && was_finished) m_policy.recalculate_connect_candidates(); + + // the torrent just became finished + if (is_finished() && !was_finished) + finished(); } void torrent::filter_piece(int index, bool filter) @@ -1621,8 +1630,9 @@ namespace libtorrent TORRENT_ASSERT(index >= 0); TORRENT_ASSERT(index < m_torrent_file->num_pieces()); + bool was_finished = is_finished(); m_picker->set_piece_priority(index, filter ? 1 : 0); - update_peer_interest(); + update_peer_interest(was_finished); } void torrent::filter_pieces(std::vector const& bitmask) @@ -1635,6 +1645,7 @@ namespace libtorrent TORRENT_ASSERT(m_picker.get()); + bool was_finished = is_finished(); int index = 0; for (std::vector::const_iterator i = bitmask.begin() , end(bitmask.end()); i != end; ++i, ++index) @@ -1645,7 +1656,7 @@ namespace libtorrent else m_picker->set_piece_priority(index, 1); } - update_peer_interest(); + update_peer_interest(was_finished); } bool torrent::is_piece_filtered(int index) const