From 111f5faecbe676514ac6df62cd8b18bd470d8ebd Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 18 May 2008 05:59:47 +0000 Subject: [PATCH] upload only extension --- include/libtorrent/peer_connection.hpp | 6 ++++++ src/bt_peer_connection.cpp | 14 ++++++++++++++ src/peer_connection.cpp | 7 +++++-- src/web_peer_connection.cpp | 2 ++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index a2c1792a9..3084aec3a 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -198,6 +198,9 @@ namespace libtorrent bool is_seed() const; + void set_upload_only(bool u) { m_upload_only = u; } + bool upload_only() const { return m_upload_only; } + bool has_timed_out() const; // will send a keep-alive message to the peer @@ -638,6 +641,9 @@ namespace libtorrent // is used to fill the bitmask in init() bool m_have_all; + // set to true when this peer is only uploading + bool m_upload_only; + // the number of pieces this peer // has. Must be the same as // std::count(m_have_piece.begin(), diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index bf45c2f23..80ee9fe1c 100755 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -1301,6 +1301,12 @@ namespace libtorrent m_max_out_request_queue = 1; } + if (entry* upload_only = root.find_key("upload_only")) + { + if (upload_only->type() == entry::int_t && upload_only->integer() != 0) + set_upload_only(true); + } + if (entry* myip = root.find_key("yourip")) { // TODO: don't trust this blindly @@ -1321,6 +1327,11 @@ namespace libtorrent } } } + + // if we're finished and this peer is uploading only + // disconnect it + if (t->is_finished() && upload_only()) + disconnect("upload to upload connection, closing"); } bool bt_peer_connection::dispatch_message(int received) @@ -1556,6 +1567,9 @@ namespace libtorrent detail::write_address(remote().address(), out); handshake["yourip"] = remote_address; handshake["reqq"] = m_ses.settings().max_allowed_in_request_queue; + boost::shared_ptr t = associated_torrent().lock(); + TORRENT_ASSERT(t); + if (t->is_finished()) handshake["upload_only"] = 1; tcp::endpoint ep = m_ses.get_ipv6_interface(); if (ep != tcp::endpoint()) diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 363be476d..03d9deb57 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -96,6 +96,7 @@ namespace libtorrent , m_failed(false) , m_ignore_bandwidth_limits(false) , m_have_all(false) + , m_upload_only(false) , m_num_pieces(0) , m_desired_queue_size(2) , m_free_upload(0) @@ -195,6 +196,7 @@ namespace libtorrent , m_failed(false) , m_ignore_bandwidth_limits(false) , m_have_all(false) + , m_upload_only(false) , m_num_pieces(0) , m_desired_queue_size(2) , m_free_upload(0) @@ -1108,10 +1110,10 @@ namespace libtorrent } } - if (is_seed()) + if (upload_only()) { TORRENT_ASSERT(m_peer_info); - m_peer_info->seed = true; + if (is_seed()) m_peer_info->seed = true; if (t->is_finished() && m_ses.settings().close_redundant_connections) { disconnect("seed to seed connection redundant"); @@ -1231,6 +1233,7 @@ namespace libtorrent m_num_pieces = num_pieces; if (interesting) t->get_policy().peer_is_interesting(*this); + else if (upload_only()) disconnect("upload to upload connections"); } // ----------------------------- diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index a23cc7cbc..6d7db636e 100755 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -73,6 +73,8 @@ namespace libtorrent // we want large blocks as well, so // we can request more bytes at once request_large_blocks(true); + set_upload_only(true); + // we only want left-over bandwidth set_priority(0); shared_ptr tor = t.lock();