added priority to connection queue. trackers and upnp connections have higher priority than peers

This commit is contained in:
Arvid Norberg 2008-03-12 07:44:27 +00:00
parent e5c77c284a
commit cd65fb8b0c
6 changed files with 44 additions and 20 deletions

View File

@ -52,7 +52,7 @@ public:
void enqueue(boost::function<void(int)> const& on_connect
, boost::function<void()> 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<void(int)> 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<entry> m_queue;

View File

@ -90,6 +90,7 @@ struct http_connection : boost::enable_shared_from_this<http_connection>, 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<http_connection>, 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;
};
}

View File

@ -52,18 +52,34 @@ namespace libtorrent
void connection_queue::enqueue(boost::function<void(int)> const& on_connect
, boost::function<void()> 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();
}

View File

@ -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<std::string>(port), timeout, ps
, ssl, handle_redirects, bind_addr);
start(hostname, boost::lexical_cast<std::string>(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
{

View File

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

View File

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