25 #include <boost/asio/io_service.hpp> 26 #include <boost/asio/ip/udp.hpp> 27 #if BOOST_VERSION >= 106600 28 #include <boost/asio/post.hpp> 34 class Resolver : noncopyable
37 typedef boost::asio::ip::udp protocol;
38 typedef protocol::resolver::iterator iterator;
39 typedef protocol::resolver::query query;
42 Resolver(boost::asio::io_service& ioService,
44 : m_resolver(ioService)
45 , m_addressSelector(addressSelector)
46 , m_scheduler(ioService)
48 BOOST_ASSERT(m_addressSelector !=
nullptr);
55 time::nanoseconds timeout,
56 const shared_ptr<Resolver>&
self)
58 m_onSuccess = onSuccess;
61 m_resolver.async_resolve(q, bind(&Resolver::onResolveResult,
this, _1, _2,
self));
63 m_resolveTimeout = m_scheduler.schedule(timeout, [=] { onResolveTimeout(
self); });
69 return selectAddress(m_resolver.resolve(q));
74 onResolveResult(
const boost::system::error_code& error,
75 iterator it,
const shared_ptr<Resolver>&
self)
77 m_resolveTimeout.cancel();
80 #if BOOST_VERSION >= 106600 81 boost::asio::post(m_resolver.get_executor(), [
self] {});
83 m_resolver.get_io_service().post([
self] {});
87 if (error == boost::asio::error::operation_aborted)
91 m_onError(
"Hostname cannot be resolved: " + error.message());
96 it = selectAddress(it);
98 if (it != iterator() && m_onSuccess) {
99 m_onSuccess(it->endpoint().address());
101 else if (m_onError) {
102 m_onError(
"No endpoints match the specified address selector");
107 onResolveTimeout(
const shared_ptr<Resolver>&
self)
112 #if BOOST_VERSION >= 106600 113 boost::asio::post(m_resolver.get_executor(), [
self] {});
115 m_resolver.get_io_service().post([
self] {});
119 m_onError(
"Hostname resolution timed out");
123 selectAddress(iterator it)
const 125 while (it != iterator() &&
126 !m_addressSelector(it->endpoint().address())) {
134 protocol::resolver m_resolver;
148 boost::asio::io_service& ioService,
150 time::nanoseconds timeout)
152 auto resolver = make_shared<Resolver>(ref(ioService), addressSelector);
153 resolver->asyncResolve(Resolver::query(host,
""), onSuccess, onError, timeout, resolver);
159 boost::asio::io_service& ioService,
162 Resolver resolver(ioService, addressSelector);
163 auto it = resolver.syncResolve(Resolver::query(host,
""));
165 if (it == Resolver::iterator()) {
166 NDN_THROW(
Error(
"No endpoints match the specified address selector"));
169 return it->endpoint().address();
function< void(const IpAddress &address)> SuccessCallback
IpAddress syncResolve(const std::string &host, boost::asio::io_service &ioService, const AddressSelector &addressSelector)
Synchronously resolve host.
function< bool(const IpAddress &address)> AddressSelector
A handle for a scheduled event.
function< void(const std::string &reason)> ErrorCallback
Generic time-based scheduler.
boost::asio::ip::address IpAddress
void asyncResolve(const std::string &host, const SuccessCallback &onSuccess, const ErrorCallback &onError, boost::asio::io_service &ioService, const AddressSelector &addressSelector, time::nanoseconds timeout)
Asynchronously resolve host.