improved connection queue handling slightly. Will require further improvements

This commit is contained in:
Arvid Norberg 2007-05-02 17:38:37 +00:00
parent b98162621e
commit 04e41cf702
3 changed files with 20 additions and 8 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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<socket_type> 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);