From 889ad70cee76274c36439dcd2c16d6904ef0b4ce Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 15 Feb 2007 03:03:50 +0000 Subject: [PATCH] fixes for metadata-less torrents, and fixes for peer requests --- src/peer_connection.cpp | 24 ++++++++++++++++++------ src/torrent.cpp | 13 ++++++++++--- src/torrent_info.cpp | 16 ++++++++-------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index dafe03bbc..25b3a799c 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -943,6 +943,10 @@ namespace libtorrent , m_download_queue.end() , block_finished); + // if there's another peer that needs to do another + // piece request, this will point to it + peer_connection* request_peer = 0; + if (b != m_download_queue.end()) { if (m_assume_fifo) @@ -985,11 +989,7 @@ namespace libtorrent if (pc && pc != this) { pc->cancel_request(block_finished); - if (!pc->has_peer_choked() && !t->is_seed()) - { - request_a_block(*t, *pc); - pc->send_block_requests(); - } + request_peer = pc; } } else @@ -1015,6 +1015,12 @@ namespace libtorrent t->received_redundant_data(t->block_size()); pol.block_finished(*this, block_finished); send_block_requests(); + + if (request_peer && !request_peer->has_peer_choked() && !t->is_seed()) + { + request_a_block(*t, *request_peer); + request_peer->send_block_requests(); + } return; } @@ -1029,6 +1035,12 @@ namespace libtorrent } catch (std::exception const&) {} + if (request_peer && !request_peer->has_peer_choked() && !t->is_seed()) + { + request_a_block(*t, *request_peer); + request_peer->send_block_requests(); + } + #ifndef NDEBUG try { @@ -1765,7 +1777,7 @@ namespace libtorrent #endif // the upload queue should not have non-prioritized peers - t->request_bandwidth(upload_channel, self(), /*m_non_prioritized*/ false); + t->request_bandwidth(upload_channel, self(), false); m_writing = true; } return; diff --git a/src/torrent.cpp b/src/torrent.cpp index dcec48d50..53f5c4c6b 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2199,10 +2199,17 @@ namespace libtorrent } size_type total_done = quantized_bytes_done(); - if (is_seed()) - assert(total_done == m_torrent_file.total_size()); + if (m_torrent_file.is_valid()) + { + if (is_seed()) + assert(total_done == m_torrent_file.total_size()); + else + assert(total_done != m_torrent_file.total_size()); + } else - assert(total_done != m_torrent_file.total_size()); + { + assert(total_done == 0); + } // This check is very expensive. assert(m_num_pieces diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index a388d8e9b..d8601a536 100755 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -238,7 +238,7 @@ namespace libtorrent // just the necessary to use it with piece manager // used for torrents with no metadata torrent_info::torrent_info(sha1_hash const& info_hash) - : m_piece_length(256 * 1024) + : m_piece_length(0) , m_total_size(0) , m_info_hash(info_hash) , m_name() @@ -250,7 +250,7 @@ namespace libtorrent } torrent_info::torrent_info() - : m_piece_length(256 * 1024) + : m_piece_length(0) , m_total_size(0) , m_info_hash(0) , m_name() @@ -543,18 +543,18 @@ namespace libtorrent m_files.push_back(e); m_total_size += size; + + if (m_piece_length == 0) + m_piece_length = 256 * 1024; int num_pieces = static_cast( (m_total_size + m_piece_length - 1) / m_piece_length); int old_num_pieces = static_cast(m_piece_hash.size()); m_piece_hash.resize(num_pieces); - for (std::vector::iterator i = m_piece_hash.begin() + old_num_pieces; - i != m_piece_hash.end(); ++i) - { - i->clear(); - } - + if (num_pieces > old_num_pieces) + std::for_each(m_piece_hash.begin() + old_num_pieces + , m_piece_hash.end(), boost::bind(&sha1_hash::clear, _1)); } void torrent_info::add_url_seed(std::string const& url)