From cd65fb8b0ca1695dfde248c219890c055eac0aee Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 12 Mar 2008 07:44:27 +0000 Subject: [PATCH] added priority to connection queue. trackers and upnp connections have higher priority than peers --- include/libtorrent/connection_queue.hpp | 5 +++-- include/libtorrent/http_connection.hpp | 12 +++++++--- src/connection_queue.cpp | 30 +++++++++++++++++++------ src/http_connection.cpp | 13 ++++++----- src/http_tracker_connection.cpp | 2 +- src/upnp.cpp | 2 +- 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/include/libtorrent/connection_queue.hpp b/include/libtorrent/connection_queue.hpp index c229ec217..83930a4f2 100644 --- a/include/libtorrent/connection_queue.hpp +++ b/include/libtorrent/connection_queue.hpp @@ -52,7 +52,7 @@ public: void enqueue(boost::function const& on_connect , boost::function const& on_timeout - , time_duration timeout); + , time_duration timeout, int priority = 0); void done(int ticket); void limit(int limit); int limit() const; @@ -71,7 +71,7 @@ private: struct entry { - entry(): connecting(false), ticket(0), expires(max_time()) {} + entry(): connecting(false), ticket(0), expires(max_time()), priority(0) {} // called when the connection is initiated boost::function on_connect; // called if done hasn't been called within the timeout @@ -80,6 +80,7 @@ private: int ticket; ptime expires; time_duration timeout; + int priority; }; std::list m_queue; diff --git a/include/libtorrent/http_connection.hpp b/include/libtorrent/http_connection.hpp index 758433296..f9b521862 100644 --- a/include/libtorrent/http_connection.hpp +++ b/include/libtorrent/http_connection.hpp @@ -90,6 +90,7 @@ struct http_connection : boost::enable_shared_from_this, boost: , m_connection_ticket(-1) , m_cc(cc) , m_ssl(false) + , m_priority(0) { TORRENT_ASSERT(!m_handler.empty()); } @@ -102,12 +103,13 @@ struct http_connection : boost::enable_shared_from_this, boost: std::string sendbuffer; void get(std::string const& url, time_duration timeout = seconds(30) - , proxy_settings const* ps = 0, int handle_redirects = 5 + , int prio = 0, proxy_settings const* ps = 0, int handle_redirects = 5 , std::string const& user_agent = "", address const& bind_addr = address_v4::any()); void start(std::string const& hostname, std::string const& port - , time_duration timeout, proxy_settings const* ps = 0, bool ssl = false - , int handle_redirect = 5, address const& bind_addr = address_v4::any()); + , time_duration timeout, int prio = 0, proxy_settings const* ps = 0 + , bool ssl = false, int handle_redirect = 5 + , address const& bind_addr = address_v4::any()); void close(); @@ -189,6 +191,10 @@ private: // the address to bind to. address_v4::any() // means do not bind address m_bind_addr; + + // the priority we have in the connection queue. + // 0 is normal, 1 is high + int m_priority; }; } diff --git a/src/connection_queue.cpp b/src/connection_queue.cpp index d54912951..3dfaa2a88 100644 --- a/src/connection_queue.cpp +++ b/src/connection_queue.cpp @@ -52,18 +52,34 @@ namespace libtorrent void connection_queue::enqueue(boost::function const& on_connect , boost::function const& on_timeout - , time_duration timeout) + , time_duration timeout, int priority) { mutex_t::scoped_lock l(m_mutex); INVARIANT_CHECK; - m_queue.push_back(entry()); - entry& e = m_queue.back(); - e.on_connect = on_connect; - e.on_timeout = on_timeout; - e.ticket = m_next_ticket; - e.timeout = timeout; + TORRENT_ASSERT(priority >= 0); + TORRENT_ASSERT(priority < 2); + + entry* e = 0; + + switch (priority) + { + case 0: + m_queue.push_back(entry()); + e = &m_queue.back(); + break; + case 1: + m_queue.push_front(entry()); + e = &m_queue.front(); + break; + } + + e->priority = priority; + e->on_connect = on_connect; + e->on_timeout = on_timeout; + e->ticket = m_next_ticket; + e->timeout = timeout; ++m_next_ticket; try_connect(); } diff --git a/src/http_connection.cpp b/src/http_connection.cpp index f7ec6cbea..96d385a20 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -48,7 +48,7 @@ namespace libtorrent { enum { max_bottled_buffer = 1024 * 1024 }; -void http_connection::get(std::string const& url, time_duration timeout +void http_connection::get(std::string const& url, time_duration timeout, int prio , proxy_settings const* ps, int handle_redirects, std::string const& user_agent , address const& bind_addr) { @@ -102,12 +102,12 @@ void http_connection::get(std::string const& url, time_duration timeout "\r\n"; sendbuffer = headers.str(); - start(hostname, boost::lexical_cast(port), timeout, ps - , ssl, handle_redirects, bind_addr); + start(hostname, boost::lexical_cast(port), timeout, prio + , ps, ssl, handle_redirects, bind_addr); } void http_connection::start(std::string const& hostname, std::string const& port - , time_duration timeout, proxy_settings const* ps, bool ssl, int handle_redirects + , time_duration timeout, int prio, proxy_settings const* ps, bool ssl, int handle_redirects , address const& bind_addr) { m_redirects = handle_redirects; @@ -122,6 +122,7 @@ void http_connection::start(std::string const& hostname, std::string const& port m_parser.reset(); m_recvbuffer.clear(); m_read_pos = 0; + m_priority = prio; if (ec) { @@ -254,7 +255,7 @@ void http_connection::on_resolve(asio::error_code const& e m_cc.enqueue(bind(&http_connection::connect, shared_from_this(), _1, target_address) , bind(&http_connection::on_connect_timeout, shared_from_this()) - , m_timeout); + , m_timeout, m_priority); } void http_connection::connect(int ticket, tcp::endpoint target_address) @@ -280,7 +281,7 @@ void http_connection::on_connect(asio::error_code const& e m_sock.close(); m_cc.enqueue(bind(&http_connection::connect, shared_from_this(), _1, *i) , bind(&http_connection::on_connect_timeout, shared_from_this()) - , m_timeout); + , m_timeout, m_priority); } */ else { diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index 2d993f82d..0dbbb837a 100755 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -167,7 +167,7 @@ namespace libtorrent , boost::bind(&http_tracker_connection::on_response, self(), _1, _2, _3, _4))); m_tracker_connection->get(url, seconds(stn.tracker_completion_timeout) - , &ps, 5, stn.user_agent, bind_infc); + , 1, &ps, 5, stn.user_agent, bind_infc); #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) diff --git a/src/upnp.cpp b/src/upnp.cpp index 31a5117c6..3fc586204 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -204,7 +204,7 @@ void upnp::resend_request(asio::error_code const& e) d.upnp_connection.reset(new http_connection(m_io_service , m_cc, bind(&upnp::on_upnp_xml, self(), _1, _2 , boost::ref(d)))); - d.upnp_connection->get(d.url); + d.upnp_connection->get(d.url, seconds(30), 1); } catch (std::exception& e) {