From 418e33facc3e30b67a1e65c8432ed4f160b1d413 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 5 Oct 2014 01:23:22 +0000 Subject: [PATCH] use the session-wide hostname resolver in torrent.cpp --- include/libtorrent/aux_/session_impl.hpp | 2 + include/libtorrent/aux_/session_interface.hpp | 5 ++ include/libtorrent/torrent.hpp | 17 +++---- src/session_impl.cpp | 6 +++ src/torrent.cpp | 51 ++++++++++--------- 5 files changed, 48 insertions(+), 33 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index bde67909d..a1670af2d 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -269,6 +269,8 @@ namespace libtorrent io_service& get_io_service() { return m_io_service; } resolver_interface& get_resolver() { return m_host_resolver; } + void async_resolve(std::string const& host, int flags + , callback_t const& h); std::vector& torrent_list(int i) { diff --git a/include/libtorrent/aux_/session_interface.hpp b/include/libtorrent/aux_/session_interface.hpp index f8d1a19ff..f11b069be 100644 --- a/include/libtorrent/aux_/session_interface.hpp +++ b/include/libtorrent/aux_/session_interface.hpp @@ -126,6 +126,11 @@ namespace libtorrent { namespace aux virtual io_service& get_io_service() = 0; virtual resolver_interface& get_resolver() = 0; + typedef boost::function const&)> + callback_t; + virtual void async_resolve(std::string const& host, int flags + , callback_t const& h) = 0; + virtual bool has_connection(peer_connection* p) const = 0; virtual void insert_peer(boost::shared_ptr const& c) = 0; diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index c54bb02e5..0c6376aee 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -820,20 +820,23 @@ namespace libtorrent // this is the asio callback that is called when a name // lookup for a PEER is completed. void on_peer_name_lookup(error_code const& e - , std::vector
const& host_list + , std::vector
const& addrs , int port); // this is the asio callback that is called when a name // lookup for a WEB SEED is completed. - void on_name_lookup(error_code const& e, tcp::resolver::iterator i - , std::list::iterator url, tcp::endpoint proxy); + void on_name_lookup(error_code const& e + , std::vector
const& addrs + , int port + , std::list::iterator web, tcp::endpoint proxy); void connect_web_seed(std::list::iterator web, tcp::endpoint a); // this is the asio callback that is called when a name // lookup for a proxy for a web seed is completed. - void on_proxy_name_lookup(error_code const& e, tcp::resolver::iterator i - , std::list::iterator url); + void on_proxy_name_lookup(error_code const& e + , std::vector
const& addrs + , std::list::iterator web, int port); // remove a web seed, or schedule it for removal in case there // are outstanding operations on it @@ -1191,10 +1194,6 @@ namespace libtorrent // ----------------------------- - // used to resolve hostnames for web seeds - // TODO: 2 replace all usage of this with m_ses.get_resolver() - mutable tcp::resolver m_host_resolver; - // this vector is allocated lazily. If no file priorities are // ever changed, this remains empty. Any unallocated slot // implicitly means the file has priority 1. diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 6b422eccc..8886d5e6c 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -700,6 +700,12 @@ namespace aux { } } + void session_impl::async_resolve(std::string const& host, int flags + , session_interface::callback_t const& h) + { + m_host_resolver.async_resolve(host, flags, h); + } + #ifdef TORRENT_STATS void session_impl::rotate_stats_log() { diff --git a/src/torrent.cpp b/src/torrent.cpp index e841bcadb..d5fd8e36e 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -152,7 +152,6 @@ namespace libtorrent , m_total_uploaded(0) , m_total_downloaded(0) , m_tracker_timer(ses.get_io_service()) - , m_host_resolver(ses.get_io_service()) , m_trackerid(p.trackerid) , m_save_path(complete(p.save_path)) , m_url(p.url) @@ -3239,7 +3238,7 @@ namespace libtorrent add_outstanding_async("torrent::on_peer_name_lookup"); #endif tcp::resolver::query q(i->hostname, to_string(i->port).elems); - m_ses.get_resolver().async_resolve(i->hostname, 0 + m_ses.async_resolve(i->hostname, 0 , boost::bind(&torrent::on_peer_name_lookup , shared_from_this(), _1, _2, i->port)); } @@ -4688,7 +4687,7 @@ namespace libtorrent } m_storage.reset(); - m_host_resolver.cancel(); + // TODO: 2 abort lookups this torrent has made via the // session host resolver interface @@ -6000,9 +5999,9 @@ namespace libtorrent // use proxy web->resolving = true; - tcp::resolver::query q(ps.hostname, to_string(ps.port).elems); - m_host_resolver.async_resolve(q, - boost::bind(&torrent::on_proxy_name_lookup, shared_from_this(), _1, _2, web)); + m_ses.async_resolve(ps.hostname, 0 + , boost::bind(&torrent::on_proxy_name_lookup, shared_from_this() + , _1, _2, web, ps.port)); } else if (ps.proxy_hostnames && (ps.type == settings_pack::socks5 @@ -6018,14 +6017,15 @@ namespace libtorrent web->resolving = true; tcp::resolver::query q(hostname, to_string(port).elems); - m_host_resolver.async_resolve(q, - boost::bind(&torrent::on_name_lookup, shared_from_this(), _1, _2, web - , tcp::endpoint())); + m_ses.async_resolve(hostname, 0, boost::bind( + &torrent::on_name_lookup, shared_from_this(), _1, _2 + , port, web, tcp::endpoint())); } } - void torrent::on_proxy_name_lookup(error_code const& e, tcp::resolver::iterator host - , std::list::iterator web) + void torrent::on_proxy_name_lookup(error_code const& e + , std::vector
const& addrs + , std::list::iterator web, int port) { TORRENT_ASSERT(is_single_thread()); @@ -6052,7 +6052,7 @@ namespace libtorrent if (m_abort) return; - if (e || host == tcp::resolver::iterator()) + if (e || addrs.empty()) { if (m_ses.alerts().should_post()) { @@ -6072,11 +6072,10 @@ namespace libtorrent || m_ses.num_connections() >= m_ses.settings().get_int(settings_pack::connections_limit)) return; - tcp::endpoint a(host->endpoint()); + tcp::endpoint a(addrs[0], port); using boost::tuples::ignore; std::string hostname; - int port; error_code ec; std::string protocol; boost::tie(protocol, ignore, hostname, port, ignore) @@ -6105,12 +6104,16 @@ namespace libtorrent web->resolving = true; tcp::resolver::query q(hostname, to_string(port).elems); - m_host_resolver.async_resolve(q, - boost::bind(&torrent::on_name_lookup, shared_from_this(), _1, _2, web, a)); + m_ses.async_resolve(hostname, 0, boost::bind( + &torrent::on_name_lookup, shared_from_this(), _1, _2 + , port, web, a)); } - void torrent::on_name_lookup(error_code const& e, tcp::resolver::iterator host - , std::list::iterator web, tcp::endpoint proxy) + void torrent::on_name_lookup(error_code const& e + , std::vector
const& addrs + , int port + , std::list::iterator web + , tcp::endpoint proxy) { TORRENT_ASSERT(is_single_thread()); @@ -6132,7 +6135,7 @@ namespace libtorrent if (m_abort) return; - if (e || host == tcp::resolver::iterator()) + if (e || addrs.empty()) { if (m_ses.alerts().should_post()) m_ses.alerts().post_alert(url_seed_alert(get_handle(), web->url, e)); @@ -6146,15 +6149,15 @@ namespace libtorrent return; } - while (host != tcp::resolver::iterator()) + for (std::vector
::const_iterator i = addrs.begin() + , end(addrs.end()); i != end; ++i) { // fill in the peer struct's address field - web->endpoints.push_back(host->endpoint()); + web->endpoints.push_back(tcp::endpoint(*i, port)); #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING - debug_log(" -> %s", print_endpoint(host->endpoint()).c_str()); + debug_log(" -> %s", print_endpoint(tcp::endpoint(*i, port)).c_str()); #endif - ++host; } if (int(m_connections.size()) >= m_max_connections @@ -6378,7 +6381,7 @@ namespace libtorrent return; } m_resolving_country = true; - m_ses.get_resolver().async_resolve(hostname, 0 + m_ses.async_resolve(hostname, 0 , boost::bind(&torrent::on_country_lookup, shared_from_this(), _1, _2, p)); }