renamed announce_piece to piece_passed and moved it to piece_finished and piece_failed
This commit is contained in:
parent
7746255494
commit
87660b96c5
|
@ -459,11 +459,6 @@ namespace libtorrent
|
|||
int block_size() const { TORRENT_ASSERT(m_block_size > 0); return m_block_size; }
|
||||
peer_request to_req(piece_block const& p);
|
||||
|
||||
// this will tell all peers that we just got his piece
|
||||
// and also let the piece picker know that we have this piece
|
||||
// so it wont pick it for download
|
||||
void announce_piece(int index);
|
||||
|
||||
void disconnect_all();
|
||||
int disconnect_peers(int num);
|
||||
|
||||
|
@ -504,6 +499,14 @@ namespace libtorrent
|
|||
// each time a piece has failed the hash
|
||||
// test
|
||||
void piece_finished(int index, int passed_hash_check);
|
||||
|
||||
// piece_passed is called when a piece passes the hash check
|
||||
// this will tell all peers that we just got his piece
|
||||
// and also let the piece picker know that we have this piece
|
||||
// so it wont pick it for download
|
||||
void piece_passed(int index);
|
||||
|
||||
// piece_failed is called when a piece fails the hash check
|
||||
void piece_failed(int index);
|
||||
|
||||
// this will restore the piece picker state for a piece
|
||||
|
|
137
src/torrent.cpp
137
src/torrent.cpp
|
@ -1238,22 +1238,80 @@ namespace libtorrent
|
|||
?"disk failed":"failed") << " ]\n";
|
||||
#endif
|
||||
|
||||
bool was_finished = m_picker->num_filtered() + num_have()
|
||||
== torrent_file().num_pieces();
|
||||
TORRENT_ASSERT(valid_metadata());
|
||||
|
||||
if (passed_hash_check == 0)
|
||||
{
|
||||
// the following call may cause picker to become invalid
|
||||
// in case we just became a seed
|
||||
piece_passed(index);
|
||||
}
|
||||
else if (passed_hash_check == -2)
|
||||
{
|
||||
// piece_failed() will restore the piece
|
||||
piece_failed(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
TORRENT_ASSERT(passed_hash_check == -1);
|
||||
m_picker->restore_piece(index);
|
||||
restore_piece_state(index);
|
||||
}
|
||||
}
|
||||
|
||||
void torrent::piece_passed(int index)
|
||||
{
|
||||
// INVARIANT_CHECK;
|
||||
|
||||
TORRENT_ASSERT(index >= 0);
|
||||
TORRENT_ASSERT(index < m_torrent_file->num_pieces());
|
||||
|
||||
if (m_ses.m_alerts.should_post(alert::debug))
|
||||
{
|
||||
m_ses.m_alerts.post_alert(piece_finished_alert(get_handle()
|
||||
, index, "piece finished"));
|
||||
}
|
||||
// the following call may cause picker to become invalid
|
||||
// in case we just became a seed
|
||||
announce_piece(index);
|
||||
TORRENT_ASSERT(valid_metadata());
|
||||
// if we just became a seed, picker is now invalid, since it
|
||||
// is deallocated by the torrent once it starts seeding
|
||||
|
||||
bool was_finished = m_picker->num_filtered() + num_have()
|
||||
== torrent_file().num_pieces();
|
||||
|
||||
std::vector<void*> downloaders;
|
||||
m_picker->get_downloaders(downloaders, index);
|
||||
|
||||
// increase the trust point of all peers that sent
|
||||
// parts of this piece.
|
||||
std::set<void*> peers;
|
||||
std::copy(downloaders.begin(), downloaders.end(), std::inserter(peers, peers.begin()));
|
||||
|
||||
m_picker->we_have(index);
|
||||
for (peer_iterator i = m_connections.begin(); i != m_connections.end(); ++i)
|
||||
(*i)->announce_piece(index);
|
||||
|
||||
for (std::set<void*>::iterator i = peers.begin()
|
||||
, end(peers.end()); i != end; ++i)
|
||||
{
|
||||
policy::peer* p = static_cast<policy::peer*>(*i);
|
||||
if (p == 0) continue;
|
||||
p->on_parole = false;
|
||||
++p->trust_points;
|
||||
// TODO: make this limit user settable
|
||||
if (p->trust_points > 20) p->trust_points = 20;
|
||||
if (p->connection) p->connection->received_valid_data(index);
|
||||
}
|
||||
|
||||
#ifndef TORRENT_DISABLE_EXTENSIONS
|
||||
for (extension_list_t::iterator i = m_extensions.begin()
|
||||
, end(m_extensions.end()); i != end; ++i)
|
||||
{
|
||||
#ifndef BOOST_NO_EXCEPTIONS
|
||||
try {
|
||||
#endif
|
||||
(*i)->on_piece_pass(index);
|
||||
#ifndef BOOST_NO_EXCEPTIONS
|
||||
} catch (std::exception&) {}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
// since this piece just passed, we might have
|
||||
// become uninterested in some peers where this
|
||||
|
@ -1270,26 +1328,15 @@ namespace libtorrent
|
|||
p->update_interest();
|
||||
}
|
||||
|
||||
if (!was_finished&& is_finished())
|
||||
if (!was_finished && is_finished())
|
||||
{
|
||||
TORRENT_ASSERT(passed_hash_check == 0);
|
||||
// torrent finished
|
||||
// i.e. all the pieces we're interested in have
|
||||
// been downloaded. Release the files (they will open
|
||||
// in read only mode if needed)
|
||||
finished();
|
||||
}
|
||||
}
|
||||
else if (passed_hash_check == -2)
|
||||
{
|
||||
// piece_failed() will restore the piece
|
||||
piece_failed(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
TORRENT_ASSERT(passed_hash_check == -1);
|
||||
m_picker->restore_piece(index);
|
||||
restore_piece_state(index);
|
||||
// if we just became a seed, picker is now invalid, since it
|
||||
// is deallocated by the torrent once it starts seeding
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1553,52 +1600,6 @@ namespace libtorrent
|
|||
}
|
||||
}
|
||||
|
||||
void torrent::announce_piece(int index)
|
||||
{
|
||||
// INVARIANT_CHECK;
|
||||
|
||||
TORRENT_ASSERT(index >= 0);
|
||||
TORRENT_ASSERT(index < m_torrent_file->num_pieces());
|
||||
|
||||
std::vector<void*> downloaders;
|
||||
m_picker->get_downloaders(downloaders, index);
|
||||
|
||||
// increase the trust point of all peers that sent
|
||||
// parts of this piece.
|
||||
std::set<void*> peers;
|
||||
std::copy(downloaders.begin(), downloaders.end(), std::inserter(peers, peers.begin()));
|
||||
|
||||
m_picker->we_have(index);
|
||||
for (peer_iterator i = m_connections.begin(); i != m_connections.end(); ++i)
|
||||
(*i)->announce_piece(index);
|
||||
|
||||
for (std::set<void*>::iterator i = peers.begin()
|
||||
, end(peers.end()); i != end; ++i)
|
||||
{
|
||||
policy::peer* p = static_cast<policy::peer*>(*i);
|
||||
if (p == 0) continue;
|
||||
p->on_parole = false;
|
||||
++p->trust_points;
|
||||
// TODO: make this limit user settable
|
||||
if (p->trust_points > 20) p->trust_points = 20;
|
||||
if (p->connection) p->connection->received_valid_data(index);
|
||||
}
|
||||
|
||||
#ifndef TORRENT_DISABLE_EXTENSIONS
|
||||
for (extension_list_t::iterator i = m_extensions.begin()
|
||||
, end(m_extensions.end()); i != end; ++i)
|
||||
{
|
||||
#ifndef BOOST_NO_EXCEPTIONS
|
||||
try {
|
||||
#endif
|
||||
(*i)->on_piece_pass(index);
|
||||
#ifndef BOOST_NO_EXCEPTIONS
|
||||
} catch (std::exception&) {}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string torrent::tracker_login() const
|
||||
{
|
||||
if (m_username.empty() && m_password.empty()) return "";
|
||||
|
|
Loading…
Reference in New Issue