From 6d515c8f3983fef6a20293358a0b394759f05446 Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Sat, 26 May 2007 23:32:19 +0000 Subject: [PATCH] sync to lt 1293 --- .../include/libtorrent/session_settings.hpp | 7 +++ libtorrent/include/libtorrent/torrent.hpp | 10 ---- libtorrent/src/bt_peer_connection.cpp | 52 +++++++++++++++++-- libtorrent/src/peer_connection.cpp | 6 ++- libtorrent/src/torrent.cpp | 52 +------------------ 5 files changed, 62 insertions(+), 65 deletions(-) diff --git a/libtorrent/include/libtorrent/session_settings.hpp b/libtorrent/include/libtorrent/session_settings.hpp index 9b797b017..b8c8c2c22 100644 --- a/libtorrent/include/libtorrent/session_settings.hpp +++ b/libtorrent/include/libtorrent/session_settings.hpp @@ -101,6 +101,7 @@ namespace libtorrent , ignore_limits_on_local_network(true) , connection_speed(20) , send_redundant_have(false) + , lazy_bitfields(true) #ifndef TORRENT_DISABLE_DHT , use_dht_as_fallback(true) #endif @@ -219,6 +220,12 @@ namespace libtorrent // for collecting statistics in some cases. Default is false. bool send_redundant_have; + // if this is true, outgoing bitfields will never be fuil. If the + // client is seed, a few bits will be set to 0, and later filled + // in with have messages. This is to prevent certain ISPs + // from stopping people from seeding. + bool lazy_bitfields; + #ifndef TORRENT_DISABLE_DHT // while this is true, the dht will note be used unless the // tracker is online diff --git a/libtorrent/include/libtorrent/torrent.hpp b/libtorrent/include/libtorrent/torrent.hpp index af007df21..f0ad4222e 100755 --- a/libtorrent/include/libtorrent/torrent.hpp +++ b/libtorrent/include/libtorrent/torrent.hpp @@ -730,16 +730,6 @@ namespace libtorrent // total_done - m_initial_done <= total_payload_download size_type m_initial_done; #endif - -#ifdef TORRENT_LOGGING - boost::shared_ptr m_log; - boost::shared_ptr m_peer_log; - int m_second_count; - - enum { debug_bw_history_size = 10 }; - int m_ul_history[debug_bw_history_size]; - int m_dl_history[debug_bw_history_size]; -#endif }; inline ptime torrent::next_announce() const diff --git a/libtorrent/src/bt_peer_connection.cpp b/libtorrent/src/bt_peer_connection.cpp index 169c5f613..6d475cb0c 100755 --- a/libtorrent/src/bt_peer_connection.cpp +++ b/libtorrent/src/bt_peer_connection.cpp @@ -775,19 +775,46 @@ namespace libtorrent assert(m_sent_bitfield == false); assert(t->valid_metadata()); + int num_pieces = bitfield.size(); + int lazy_pieces[50]; + int num_lazy_pieces = 0; + int lazy_piece = 0; + + assert(t->is_seed() == (std::count(bitfield.begin(), bitfield.end(), true) == num_pieces)); + if (t->is_seed() && m_ses.settings().lazy_bitfields) + { + num_lazy_pieces = std::min(50, num_pieces / 10); + if (num_lazy_pieces < 1) num_lazy_pieces = 1; + for (int i = 0; i < num_pieces; ++i) + { + if (rand() % (num_pieces - i) >= num_lazy_pieces - lazy_piece) continue; + lazy_pieces[lazy_piece++] = i; + } + assert(lazy_piece == num_lazy_pieces); + lazy_piece = 0; + } + #ifdef TORRENT_VERBOSE_LOGGING (*m_logger) << time_now_string() << " ==> BITFIELD "; std::stringstream bitfield_string; for (int i = 0; i < (int)get_bitfield().size(); ++i) { + if (lazy_piece < num_lazy_pieces + && lazy_pieces[lazy_piece] == i) + { + bitfield_string << "0"; + ++lazy_piece; + continue; + } if (bitfield[i]) bitfield_string << "1"; else bitfield_string << "0"; } bitfield_string << "\n"; (*m_logger) << bitfield_string.str(); + lazy_piece = 0; #endif - const int packet_size = ((int)bitfield.size() + 7) / 8 + 5; + const int packet_size = (num_pieces + 7) / 8 + 5; buffer::interval i = allocate_send_buffer(packet_size); @@ -795,12 +822,31 @@ namespace libtorrent detail::write_uint8(msg_bitfield, i.begin); std::fill(i.begin, i.end, 0); - for (int c = 0; c < (int)bitfield.size(); ++c) + for (int c = 0; c < num_pieces; ++c) { + if (lazy_piece < num_lazy_pieces + && lazy_pieces[lazy_piece]) + { + ++lazy_piece; + continue; + } if (bitfield[c]) i.begin[c >> 3] |= 1 << (7 - (c & 7)); } - assert(i.end - i.begin == ((int)bitfield.size() + 7) / 8); + assert(i.end - i.begin == (num_pieces + 7) / 8); + + if (num_lazy_pieces > 0) + { + for (int i = 0; i < num_lazy_pieces; ++i) + { + write_have(lazy_pieces[i]); +#ifdef TORRENT_VERBOSE_LOGGING + (*m_logger) << time_now_string() + << " ==> HAVE [ piece: " << lazy_pieces[i] << "]\n"; +#endif + } + } + #ifndef NDEBUG m_sent_bitfield = true; #endif diff --git a/libtorrent/src/peer_connection.cpp b/libtorrent/src/peer_connection.cpp index a246814ba..db1d11eac 100755 --- a/libtorrent/src/peer_connection.cpp +++ b/libtorrent/src/peer_connection.cpp @@ -2581,6 +2581,10 @@ namespace libtorrent d = time_now() - m_last_receive; if (d > seconds(m_timeout)) return true; + // TODO: as long as we have less than 95% of the + // global (or local) connection limit, connections should + // never time out for another reason + // if the peer hasn't become interested and we haven't // become interested in the peer for 10 minutes, it // has also timed out. @@ -2636,7 +2640,7 @@ namespace libtorrent bool peer_connection::is_seed() const { INVARIANT_CHECK; - // if m_num_pieces == 0, we probably doesn't have the + // if m_num_pieces == 0, we probably don't have the // metadata yet. return m_num_pieces == (int)m_have_piece.size() && m_num_pieces > 0; } diff --git a/libtorrent/src/torrent.cpp b/libtorrent/src/torrent.cpp index d9a3e2407..d760fb593 100755 --- a/libtorrent/src/torrent.cpp +++ b/libtorrent/src/torrent.cpp @@ -143,33 +143,6 @@ namespace peer_id const& pid; }; - -#ifdef TORRENT_LOGGING - void print_legend(boost::shared_ptr l) - { - (*l) << "1. time, seconds\n" - << "2. hard send quota, bytes\n" - << "3. soft send quota, bytes\n" - << "4. excess bytes sent\n" - << "5. excess bytes sent last time slice\n" - << "6. hard receive quota, bytes\n" - << "7. soft receive quota, bytes\n" - << "8. excess bytes received\n" - << "9. excess bytes received last time slice\n" - << "10. num peers\n" - << "11. max ul quota limit\n" - << "12. max dl quota limit\n" - << "13. bytes sent\n" - << "14. bytes sent 10 seconds mean\n" - << "15. bytes received\n" - << "16. bytes received 10 seconds mean\n" - << "17. total payload download\n" - << "18. total web seed payload download\n" - << "19. total redundant bytes downloaded\n" - << "\n"; - } -#endif - } namespace libtorrent @@ -228,20 +201,7 @@ namespace libtorrent #ifndef NDEBUG m_initial_done = 0; #endif -#ifdef TORRENT_LOGGING - m_log = ses.create_log("torrent_" - + boost::lexical_cast(tf.info_hash()) - , m_ses.listen_port(), false); - print_legend(m_log); - m_second_count = 0; - std::fill_n(m_ul_history, 10, 0); - std::fill_n(m_dl_history, 10, 0); - m_peer_log = ses.create_log("torrent_peers_" - + boost::lexical_cast(tf.info_hash()) - , m_ses.listen_port(), false); - -#endif INVARIANT_CHECK; m_uploads_quota.min = 2; @@ -312,16 +272,6 @@ namespace libtorrent m_initial_done = 0; #endif -#ifdef TORRENT_LOGGING - m_log = ses.create_log("torrent_" - + boost::lexical_cast(info_hash) - , m_ses.listen_port(), true); - print_legend(m_log); - m_second_count = 0; - std::fill_n(m_ul_history, 10, 0); - std::fill_n(m_dl_history, 10, 0); -#endif - INVARIANT_CHECK; if (name) m_name.reset(new std::string(name)); @@ -903,8 +853,8 @@ namespace libtorrent , end(peers.end()); i != end; ++i) { peer_iterator p = m_connections.find(*i); - peer_connection& peer = *p->second; if (p == m_connections.end()) continue; + peer_connection& peer = *p->second; peer.received_invalid_data(index); // either, we have received too many failed hashes