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 void enqueue(boost::function<void(int)> const& on_connect
, boost::function<void()> const& on_timeout , boost::function<void()> const& on_timeout
, time_duration timeout); , time_duration timeout, int priority = 0);
void done(int ticket); void done(int ticket);
void limit(int limit); void limit(int limit);
int limit() const; int limit() const;
@ -71,7 +71,7 @@ private:
struct entry 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 // called when the connection is initiated
boost::function<void(int)> on_connect; boost::function<void(int)> on_connect;
// called if done hasn't been called within the timeout // called if done hasn't been called within the timeout
@ -80,6 +80,7 @@ private:
int ticket; int ticket;
ptime expires; ptime expires;
time_duration timeout; time_duration timeout;
int priority;
}; };
std::list<entry> m_queue; 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_connection_ticket(-1)
, m_cc(cc) , m_cc(cc)
, m_ssl(false) , m_ssl(false)
, m_priority(0)
{ {
TORRENT_ASSERT(!m_handler.empty()); TORRENT_ASSERT(!m_handler.empty());
} }
@ -102,12 +103,13 @@ struct http_connection : boost::enable_shared_from_this<http_connection>, boost:
std::string sendbuffer; std::string sendbuffer;
void get(std::string const& url, time_duration timeout = seconds(30) 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()); , std::string const& user_agent = "", address const& bind_addr = address_v4::any());
void start(std::string const& hostname, std::string const& port void start(std::string const& hostname, std::string const& port
, time_duration timeout, proxy_settings const* ps = 0, bool ssl = false , time_duration timeout, int prio = 0, proxy_settings const* ps = 0
, int handle_redirect = 5, address const& bind_addr = address_v4::any()); , bool ssl = false, int handle_redirect = 5
, address const& bind_addr = address_v4::any());
void close(); void close();
@ -189,6 +191,10 @@ private:
// the address to bind to. address_v4::any() // the address to bind to. address_v4::any()
// means do not bind // means do not bind
address m_bind_addr; 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 void connection_queue::enqueue(boost::function<void(int)> const& on_connect
, boost::function<void()> const& on_timeout , boost::function<void()> const& on_timeout
, time_duration timeout) , time_duration timeout, int priority)
{ {
mutex_t::scoped_lock l(m_mutex); mutex_t::scoped_lock l(m_mutex);
INVARIANT_CHECK; INVARIANT_CHECK;
m_queue.push_back(entry()); TORRENT_ASSERT(priority >= 0);
entry& e = m_queue.back(); TORRENT_ASSERT(priority < 2);
e.on_connect = on_connect;
e.on_timeout = on_timeout; entry* e = 0;
e.ticket = m_next_ticket;
e.timeout = timeout; 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; ++m_next_ticket;
try_connect(); try_connect();
} }

View File

@ -48,7 +48,7 @@ namespace libtorrent {
enum { max_bottled_buffer = 1024 * 1024 }; 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 , proxy_settings const* ps, int handle_redirects, std::string const& user_agent
, address const& bind_addr) , address const& bind_addr)
{ {
@ -102,12 +102,12 @@ void http_connection::get(std::string const& url, time_duration timeout
"\r\n"; "\r\n";
sendbuffer = headers.str(); sendbuffer = headers.str();
start(hostname, boost::lexical_cast<std::string>(port), timeout, ps start(hostname, boost::lexical_cast<std::string>(port), timeout, prio
, ssl, handle_redirects, bind_addr); , ps, ssl, handle_redirects, bind_addr);
} }
void http_connection::start(std::string const& hostname, std::string const& port 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) , address const& bind_addr)
{ {
m_redirects = handle_redirects; m_redirects = handle_redirects;
@ -122,6 +122,7 @@ void http_connection::start(std::string const& hostname, std::string const& port
m_parser.reset(); m_parser.reset();
m_recvbuffer.clear(); m_recvbuffer.clear();
m_read_pos = 0; m_read_pos = 0;
m_priority = prio;
if (ec) 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) m_cc.enqueue(bind(&http_connection::connect, shared_from_this(), _1, target_address)
, bind(&http_connection::on_connect_timeout, shared_from_this()) , 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) 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_sock.close();
m_cc.enqueue(bind(&http_connection::connect, shared_from_this(), _1, *i) m_cc.enqueue(bind(&http_connection::connect, shared_from_this(), _1, *i)
, bind(&http_connection::on_connect_timeout, shared_from_this()) , bind(&http_connection::on_connect_timeout, shared_from_this())
, m_timeout); , m_timeout, m_priority);
} }
*/ else */ else
{ {

View File

@ -167,7 +167,7 @@ namespace libtorrent
, boost::bind(&http_tracker_connection::on_response, self(), _1, _2, _3, _4))); , boost::bind(&http_tracker_connection::on_response, self(), _1, _2, _3, _4)));
m_tracker_connection->get(url, seconds(stn.tracker_completion_timeout) 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) #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 d.upnp_connection.reset(new http_connection(m_io_service
, m_cc, bind(&upnp::on_upnp_xml, self(), _1, _2 , m_cc, bind(&upnp::on_upnp_xml, self(), _1, _2
, boost::ref(d)))); , boost::ref(d))));
d.upnp_connection->get(d.url); d.upnp_connection->get(d.url, seconds(30), 1);
} }
catch (std::exception& e) catch (std::exception& e)
{ {