From 04e41cf702e12d8cc54b1a6d387ad47dfa394765 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 2 May 2007 17:38:37 +0000 Subject: [PATCH] improved connection queue handling slightly. Will require further improvements --- include/libtorrent/torrent.hpp | 4 +++- src/policy.cpp | 5 +++-- src/torrent.cpp | 19 ++++++++++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index c71480da1..0dde2dcd9 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -199,7 +199,7 @@ namespace libtorrent tcp::endpoint const& get_interface() const { return m_net_interface; } void connect_to_url_seed(std::string const& url); - peer_connection& connect_to_peer(policy::peer* peerinfo); + peer_connection* connect_to_peer(policy::peer* peerinfo); void set_ratio(float ratio) { assert(ratio >= 0.0f); m_ratio = ratio; } @@ -247,6 +247,8 @@ namespace libtorrent // decreased in the piece_picker void remove_peer(peer_connection* p); + bool want_more_peers() const; + peer_connection* connection_for(tcp::endpoint const& a) { peer_iterator i = m_connections.find(a); diff --git a/src/policy.cpp b/src/policy.cpp index 6e524d1ba..001a3ef0c 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -1201,7 +1201,7 @@ namespace libtorrent { INVARIANT_CHECK; - if(m_torrent->num_peers() >= m_torrent->m_connections_quota.given) + if (!m_torrent->want_more_peers()) return false; bool succeed = false; @@ -1223,7 +1223,8 @@ namespace libtorrent try { assert(!p->connection); - p->connection = &m_torrent->connect_to_peer(&*p); + p->connection = m_torrent->connect_to_peer(&*p); + if (p->connection == 0) return false; assert(p->connection); p->connection->add_stat(p->prev_amount_download, p->prev_amount_upload); p->prev_amount_download = 0; diff --git a/src/torrent.cpp b/src/torrent.cpp index fe6497dc1..72255806c 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1877,9 +1877,12 @@ namespace libtorrent } } - peer_connection& torrent::connect_to_peer(policy::peer* peerinfo) + peer_connection* torrent::connect_to_peer(policy::peer* peerinfo) { INVARIANT_CHECK; + + if (!want_more_peers()) return 0; + tcp::endpoint const& a(peerinfo->ip); if (m_ses.m_ip_filter.access(a.address()) & ip_filter::blocked) { @@ -1888,11 +1891,10 @@ namespace libtorrent m_ses.m_alerts.post_alert(peer_blocked_alert(a.address() , "peer connection blocked by IP filter")); } - throw protocol_error(a.address().to_string() + " blocked by ip filter"); + return 0; } - if (m_connections.find(a) != m_connections.end()) - throw protocol_error("already connected to peer"); + if (m_connections.find(a) != m_connections.end()) return 0; boost::shared_ptr s = instantiate_connection(m_ses.m_io_service, m_ses.peer_proxy()); @@ -1941,7 +1943,7 @@ namespace libtorrent throw; } if (c->is_disconnecting()) throw protocol_error("failed to connect"); - return *c; + return c.get(); } void torrent::set_metadata(entry const& metadata) @@ -2036,6 +2038,13 @@ namespace libtorrent #endif } + bool torrent::want_more_peers() const + { + return m_connections.size() < m_connections_quota.given + && m_ses.m_half_open.size() < m_ses.m_half_open_limit; + } + + void torrent::disconnect_all() { session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);