make resolver_flags use enum class for improved type-safety

This commit is contained in:
arvidn 2017-05-28 15:01:46 -04:00 committed by Arvid Norberg
parent 98a0344196
commit afce0d3a86
11 changed files with 50 additions and 39 deletions

View File

@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/session_settings.hpp" #include "libtorrent/session_settings.hpp"
#include "libtorrent/i2p_stream.hpp" #include "libtorrent/i2p_stream.hpp"
#include "libtorrent/aux_/vector.hpp" #include "libtorrent/aux_/vector.hpp"
#include "libtorrent/resolver_interface.hpp"
namespace libtorrent { namespace libtorrent {
@ -103,7 +104,7 @@ struct TORRENT_EXTRA_EXPORT http_connection
, int prio = 0, aux::proxy_settings const* ps = NULL, int handle_redirects = 5 , int prio = 0, aux::proxy_settings const* ps = NULL, int handle_redirects = 5
, std::string const& user_agent = std::string() , std::string const& user_agent = std::string()
, boost::optional<address> const& bind_addr = boost::optional<address>() , boost::optional<address> const& bind_addr = boost::optional<address>()
, int resolve_flags = 0, std::string const& auth_ = std::string() , resolver_flags resolve_flags = resolver_flags::none, std::string const& auth_ = std::string()
#if TORRENT_USE_I2P #if TORRENT_USE_I2P
, i2p_connection* i2p_conn = 0 , i2p_connection* i2p_conn = 0
#endif #endif
@ -113,7 +114,7 @@ struct TORRENT_EXTRA_EXPORT http_connection
, time_duration timeout, int prio = 0, aux::proxy_settings const* ps = NULL , time_duration timeout, int prio = 0, aux::proxy_settings const* ps = NULL
, bool ssl = false, int handle_redirect = 5 , bool ssl = false, int handle_redirect = 5
, boost::optional<address> const& bind_addr = boost::optional<address>() , boost::optional<address> const& bind_addr = boost::optional<address>()
, int resolve_flags = 0 , resolver_flags resolve_flags = resolver_flags::none
#if TORRENT_USE_I2P #if TORRENT_USE_I2P
, i2p_connection* i2p_conn = 0 , i2p_connection* i2p_conn = 0
#endif #endif
@ -215,7 +216,7 @@ private:
int m_priority; int m_priority;
// used for DNS lookups // used for DNS lookups
int m_resolve_flags; resolver_flags m_resolve_flags;
std::uint16_t m_port; std::uint16_t m_port;

View File

@ -52,7 +52,7 @@ struct TORRENT_EXTRA_EXPORT resolver final : resolver_interface
{ {
explicit resolver(io_service& ios); explicit resolver(io_service& ios);
virtual void async_resolve(std::string const& host, int flags virtual void async_resolve(std::string const& host, resolver_flags flags
, callback_t const& h) override; , callback_t const& h) override;
virtual void abort() override; virtual void abort() override;

View File

@ -39,27 +39,37 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/error_code.hpp" #include "libtorrent/error_code.hpp"
#include "libtorrent/address.hpp" #include "libtorrent/address.hpp"
#include "libtorrent/time.hpp" #include "libtorrent/time.hpp"
#include "libtorrent/flags.hpp"
namespace libtorrent { namespace libtorrent {
enum class resolver_flags : std::uint8_t
{
none = 0,
// this flag will make async_resolve() only use the cache and fail if we
// don't have a cache entry, regardless of how old it is. This is usefull
// when completing the lookup quickly is more important than accuracy,
// like on shutdown
cache_only = 1,
// set this flag for lookups that are not critical during shutdown. i.e.
// for looking up tracker names _except_ when stopping a tracker.
abort_on_shutdown = 2
};
namespace flags {
template <>
struct enable_flag_operators<resolver_flags> : std::true_type {};
}
using namespace flags;
struct TORRENT_EXTRA_EXPORT resolver_interface struct TORRENT_EXTRA_EXPORT resolver_interface
{ {
using callback_t = std::function<void(error_code const&, std::vector<address> const&)>; using callback_t = std::function<void(error_code const&, std::vector<address> const&)>;
enum flags_t virtual void async_resolve(std::string const& host, resolver_flags flags
{
// this flag will make async_resolve() only use the cache and fail if we
// don't have a cache entry, regardless of how old it is. This is usefull
// when completing the lookup quickly is more important than accuracy,
// like on shutdown
cache_only = 1,
// set this flag for lookups that are not critical during shutdown. i.e.
// for looking up tracker names _except_ when stopping a tracker.
abort_on_shutdown = 2
};
virtual void async_resolve(std::string const& host, int flags
, callback_t const& h) = 0; , callback_t const& h) = 0;
virtual void abort() = 0; virtual void abort() = 0;

View File

@ -179,7 +179,7 @@ std::shared_ptr<http_connection> test_request(io_service& ios
}); });
h->get(url, seconds(1), 0, &ps, 5, "test/user-agent", boost::optional<address>() h->get(url, seconds(1), 0, &ps, 5, "test/user-agent", boost::optional<address>()
, 0, auth); , resolver_flags::none, auth);
return h; return h;
} }

View File

@ -88,7 +88,7 @@ http_connection::http_connection(io_service& ios
, m_rate_limit(0) , m_rate_limit(0)
, m_download_quota(0) , m_download_quota(0)
, m_priority(0) , m_priority(0)
, m_resolve_flags(0) , m_resolve_flags(resolver_flags::none)
, m_port(0) , m_port(0)
, m_bottled(bottled) , m_bottled(bottled)
, m_called(false) , m_called(false)
@ -109,7 +109,7 @@ http_connection::~http_connection()
void http_connection::get(std::string const& url, time_duration timeout, int prio void http_connection::get(std::string const& url, time_duration timeout, int prio
, aux::proxy_settings const* ps, int handle_redirects, std::string const& user_agent , aux::proxy_settings const* ps, int handle_redirects, std::string const& user_agent
, boost::optional<address> const& bind_addr, int resolve_flags, std::string const& auth_ , boost::optional<address> const& bind_addr, resolver_flags const resolve_flags, std::string const& auth_
#if TORRENT_USE_I2P #if TORRENT_USE_I2P
, i2p_connection* i2p_conn , i2p_connection* i2p_conn
#endif #endif
@ -226,7 +226,7 @@ void http_connection::start(std::string const& hostname, int port
, time_duration timeout, int prio, aux::proxy_settings const* ps, bool ssl , time_duration timeout, int prio, aux::proxy_settings const* ps, bool ssl
, int handle_redirects , int handle_redirects
, boost::optional<address> const& bind_addr , boost::optional<address> const& bind_addr
, int resolve_flags , resolver_flags const resolve_flags
#if TORRENT_USE_I2P #if TORRENT_USE_I2P
, i2p_connection* i2p_conn , i2p_connection* i2p_conn
#endif #endif

View File

@ -223,9 +223,9 @@ namespace libtorrent {
, tracker_req().event == tracker_request::stopped ? 2 : 1 , tracker_req().event == tracker_request::stopped ? 2 : 1
, ps.proxy_tracker_connections ? &ps : nullptr , ps.proxy_tracker_connections ? &ps : nullptr
, 5, user_agent, bind_interface() , 5, user_agent, bind_interface()
, tracker_req().event == tracker_request::stopped , (tracker_req().event == tracker_request::stopped
? resolver_interface::cache_only : 0 ? resolver_flags::cache_only : resolver_flags::none)
| resolver_interface::abort_on_shutdown | resolver_flags::abort_on_shutdown
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
, tracker_req().auth , tracker_req().auth
#else #else

View File

@ -83,7 +83,7 @@ namespace libtorrent {
} }
} }
void resolver::async_resolve(std::string const& host, int const flags void resolver::async_resolve(std::string const& host, resolver_flags const flags
, resolver_interface::callback_t const& h) , resolver_interface::callback_t const& h)
{ {
// special handling for raw IP addresses. There's no need to get in line // special handling for raw IP addresses. There's no need to get in line
@ -103,7 +103,7 @@ namespace libtorrent {
if (i != m_cache.end()) if (i != m_cache.end())
{ {
// keep cache entries valid for m_timeout seconds // keep cache entries valid for m_timeout seconds
if ((flags & resolver_interface::cache_only) if (test(flags & resolver_flags::cache_only)
|| i->second.last_seen + m_timeout >= aux::time_now()) || i->second.last_seen + m_timeout >= aux::time_now())
{ {
m_ios.post(std::bind(h, ec, i->second.addresses)); m_ios.post(std::bind(h, ec, i->second.addresses));
@ -111,7 +111,7 @@ namespace libtorrent {
} }
} }
if (flags & resolver_interface::cache_only) if (test(flags & resolver_flags::cache_only))
{ {
// we did not find a cache entry, fail the lookup // we did not find a cache entry, fail the lookup
m_ios.post(std::bind(h, boost::asio::error::host_not_found m_ios.post(std::bind(h, boost::asio::error::host_not_found
@ -124,7 +124,7 @@ namespace libtorrent {
using namespace std::placeholders; using namespace std::placeholders;
ADD_OUTSTANDING_ASYNC("resolver::on_lookup"); ADD_OUTSTANDING_ASYNC("resolver::on_lookup");
if (flags & resolver_interface::abort_on_shutdown) if (test(flags & resolver_flags::abort_on_shutdown))
{ {
m_resolver.async_resolve(q, std::bind(&resolver::on_lookup, this, _1, _2 m_resolver.async_resolve(q, std::bind(&resolver::on_lookup, this, _1, _2
, h, host)); , h, host));

View File

@ -5766,7 +5766,7 @@ namespace {
void session_impl::add_dht_node_name(std::pair<std::string, int> const& node) void session_impl::add_dht_node_name(std::pair<std::string, int> const& node)
{ {
ADD_OUTSTANDING_ASYNC("session_impl::on_dht_name_lookup"); ADD_OUTSTANDING_ASYNC("session_impl::on_dht_name_lookup");
m_host_resolver.async_resolve(node.first, resolver_interface::abort_on_shutdown m_host_resolver.async_resolve(node.first, resolver_flags::abort_on_shutdown
, std::bind(&session_impl::on_dht_name_lookup , std::bind(&session_impl::on_dht_name_lookup
, this, _1, _2, node.second)); , this, _1, _2, node.second));
} }
@ -5795,7 +5795,7 @@ namespace {
{ {
ADD_OUTSTANDING_ASYNC("session_impl::on_dht_router_name_lookup"); ADD_OUTSTANDING_ASYNC("session_impl::on_dht_router_name_lookup");
++m_outstanding_router_lookups; ++m_outstanding_router_lookups;
m_host_resolver.async_resolve(node.first, resolver_interface::abort_on_shutdown m_host_resolver.async_resolve(node.first, resolver_flags::abort_on_shutdown
, std::bind(&session_impl::on_dht_router_name_lookup , std::bind(&session_impl::on_dht_router_name_lookup
, this, _1, _2, node.second)); , this, _1, _2, node.second));
} }

View File

@ -3140,7 +3140,7 @@ namespace libtorrent {
#endif #endif
{ {
ADD_OUTSTANDING_ASYNC("torrent::on_peer_name_lookup"); ADD_OUTSTANDING_ASYNC("torrent::on_peer_name_lookup");
m_ses.get_resolver().async_resolve(i.hostname, resolver_interface::abort_on_shutdown m_ses.get_resolver().async_resolve(i.hostname, resolver_flags::abort_on_shutdown
, std::bind(&torrent::on_peer_name_lookup, shared_from_this(), _1, _2, i.port)); , std::bind(&torrent::on_peer_name_lookup, shared_from_this(), _1, _2, i.port));
} }
} }
@ -5621,7 +5621,7 @@ namespace libtorrent {
// use proxy // use proxy
web->resolving = true; web->resolving = true;
m_ses.get_resolver().async_resolve(ps.hostname, resolver_interface::abort_on_shutdown m_ses.get_resolver().async_resolve(ps.hostname, resolver_flags::abort_on_shutdown
, [self, web, proxy_port](error_code const& e, std::vector<address> const& addrs) , [self, web, proxy_port](error_code const& e, std::vector<address> const& addrs)
{ {
self->wrap(&torrent::on_proxy_name_lookup, e, addrs, web, proxy_port); self->wrap(&torrent::on_proxy_name_lookup, e, addrs, web, proxy_port);
@ -5643,7 +5643,7 @@ namespace libtorrent {
auto self = shared_from_this(); auto self = shared_from_this();
web->resolving = true; web->resolving = true;
m_ses.get_resolver().async_resolve(hostname, resolver_interface::abort_on_shutdown m_ses.get_resolver().async_resolve(hostname, resolver_flags::abort_on_shutdown
, [self, web, port](error_code const& e, std::vector<address> const& addrs) , [self, web, port](error_code const& e, std::vector<address> const& addrs)
{ {
self->wrap(&torrent::on_name_lookup, e, addrs, port, web); self->wrap(&torrent::on_name_lookup, e, addrs, port, web);
@ -5728,7 +5728,7 @@ namespace libtorrent {
auto self = shared_from_this(); auto self = shared_from_this();
web->resolving = true; web->resolving = true;
m_ses.get_resolver().async_resolve(hostname, resolver_interface::abort_on_shutdown m_ses.get_resolver().async_resolve(hostname, resolver_flags::abort_on_shutdown
, [self, web, port](error_code const& err, std::vector<address> const& addr) , [self, web, port](error_code const& err, std::vector<address> const& addr)
{ {
self->wrap(&torrent::on_name_lookup, err, addr, port, web); self->wrap(&torrent::on_name_lookup, err, addr, port, web);

View File

@ -106,9 +106,9 @@ namespace libtorrent {
// when stopping, pass in the cache-only flag, because we // when stopping, pass in the cache-only flag, because we
// don't want to get stuck on DNS lookups when shutting down // don't want to get stuck on DNS lookups when shutting down
m_man.host_resolver().async_resolve(hostname m_man.host_resolver().async_resolve(hostname
, tracker_req().event == tracker_request::stopped , (tracker_req().event == tracker_request::stopped
? resolver_interface::cache_only : 0 ? resolver_flags::cache_only : resolver_flags::none)
| resolver_interface::abort_on_shutdown | resolver_flags::abort_on_shutdown
, std::bind(&udp_tracker_connection::name_lookup , std::bind(&udp_tracker_connection::name_lookup
, shared_from_this(), _1, _2, port)); , shared_from_this(), _1, _2, port));

View File

@ -121,7 +121,7 @@ void run_test(std::string const& url, int size, int status, int connected
std::shared_ptr<http_connection> h = std::make_shared<http_connection>(ios std::shared_ptr<http_connection> h = std::make_shared<http_connection>(ios
, res, &::http_handler, true, 1024*1024, &::http_connect_handler); , res, &::http_handler, true, 1024*1024, &::http_connect_handler);
h->get(url, seconds(1), 0, &ps, 5, "test/user-agent", address(address_v4::any()) h->get(url, seconds(1), 0, &ps, 5, "test/user-agent", address(address_v4::any())
, 0, auth); , resolver_flags::none, auth);
ios.reset(); ios.reset();
error_code e; error_code e;
ios.run(e); ios.run(e);