From 2a9353cf48ccbde949ef6dc8575b156950404841 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 8 Dec 2008 09:13:21 +0000 Subject: [PATCH] prefer udp trackers. Fixes #433 --- docs/manual.rst | 6 ++++ include/libtorrent/session_settings.hpp | 6 ++++ include/libtorrent/torrent.hpp | 1 + src/torrent.cpp | 41 +++++++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/docs/manual.rst b/docs/manual.rst index 84058de91..5b55b6665 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -3146,6 +3146,7 @@ that will be sent to the tracker. The user-agent is a good way to identify your bool rate_limit_ip_overhead; bool announce_to_all_trackers; + bool prefer_udp_trackers; }; ``user_agent`` this is the client identification to the tracker. @@ -3438,6 +3439,11 @@ set to false, the behavior is as defined by the multi tracker specification. It defaults to false, which is the same behavior previous versions of libtorrent has had as well. +``prefer_udp_trackers`` is true by default. It means that trackers may +be rearranged in a way that udp trackers are always tried before http +trackers for the same hostname. Setting this to fails means that the +trackers' tier is respected and there's no preference of one protocol +over another. pe_settings =========== diff --git a/include/libtorrent/session_settings.hpp b/include/libtorrent/session_settings.hpp index d5251da49..c13ddc8d3 100644 --- a/include/libtorrent/session_settings.hpp +++ b/include/libtorrent/session_settings.hpp @@ -145,6 +145,7 @@ namespace libtorrent , auto_manage_startup(120) , rate_limit_ip_overhead(true) , announce_to_all_trackers(false) + , prefer_udp_trackers(true) {} // this is the user agent that will be sent to the tracker @@ -462,6 +463,11 @@ namespace libtorrent // the same way uTorrent treats them. It defaults to // false in order to comply with the extension definition. bool announce_to_all_trackers; + + // when this is set to true, if there is a tracker entry + // with udp:// protocol, it is preferred over the same + // tracker over http://. + bool prefer_udp_trackers; }; #ifndef TORRENT_DISABLE_DHT diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 4409fa267..834fa2f79 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -680,6 +680,7 @@ namespace libtorrent bool request_bandwidth_from_session(int channel) const; void update_peer_interest(bool was_finished); + void prioritize_udp_trackers(); policy m_policy; diff --git a/src/torrent.cpp b/src/torrent.cpp index b455a7d28..3ef7da6f8 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -191,6 +191,9 @@ namespace libtorrent { if (resume_data) m_resume_data.swap(*resume_data); + if (m_settings.prefer_udp_trackers) + prioritize_udp_trackers(); + #ifndef TORRENT_DISABLE_ENCRYPTION hasher h; h.update("req2", 4); @@ -2206,10 +2209,45 @@ namespace libtorrent , end(m_trackers.end()); i != end; ++i) if (i->source == 0) i->source = announce_entry::source_client; + if (m_settings.prefer_udp_trackers) + prioritize_udp_trackers(); + if (!m_trackers.empty()) start_announcing(); else stop_announcing(); } + void torrent::prioritize_udp_trackers() + { + // look for udp-trackers + for (std::vector::iterator i = m_trackers.begin() + , end(m_trackers.end()); i != end; ++i) + { + if (i->url.substr(0, 6) != "udp://") continue; + // now, look for trackers with the same hostname + // that is has higher priority than this one + // if we find one, swap with the udp-tracker + std::string udp_hostname; + using boost::tuples::ignore; + boost::tie(ignore, ignore, udp_hostname, ignore, ignore, ignore) + = parse_url_components(i->url); + for (std::vector::iterator j = m_trackers.begin(); + j != i; ++j) + { + std::string hostname; + boost::tie(ignore, ignore, hostname, ignore, ignore, ignore) + = parse_url_components(j->url); + if (hostname != udp_hostname) continue; + if (j->url.substr(0, 6) == "udp://") continue; + using std::swap; + using std::iter_swap; + swap(i->tier, j->tier); + iter_swap(i, j); + break; + } + } + + } + void torrent::add_tracker(announce_entry const& url) { std::vector::iterator k = std::find_if(m_trackers.begin() @@ -2788,6 +2826,9 @@ namespace libtorrent } std::sort(m_trackers.begin(), m_trackers.end(), boost::bind(&announce_entry::tier, _1) < boost::bind(&announce_entry::tier, _2)); + + if (m_settings.prefer_udp_trackers) + prioritize_udp_trackers(); } lazy_entry const* mapped_files = rd.dict_find_list("mapped_files");