25 #include <boost/asio/ip/udp.hpp>
26 #include <boost/asio/post.hpp>
30 class Resolver : noncopyable
33 Resolver(boost::asio::io_context& ioCtx,
36 , m_addressSelector(addressSelector)
39 BOOST_ASSERT(m_addressSelector !=
nullptr);
47 const shared_ptr<Resolver>&
self)
49 m_onSuccess = onSuccess;
52 m_resolver.async_resolve(host,
"", [=] (
auto&&... args) {
53 onResolveResult(std::forward<decltype(args)>(args)...,
self);
56 m_resolveTimeout = m_scheduler.schedule(timeout, [=] { onResolveTimeout(
self); });
61 onResolveResult(
const boost::system::error_code& error,
62 const boost::asio::ip::udp::resolver::results_type& results,
63 const shared_ptr<Resolver>&
self)
65 m_resolveTimeout.cancel();
68 boost::asio::post(m_resolver.get_executor(), [
self] {});
71 if (m_onError && error != boost::asio::error::operation_aborted) {
72 m_onError(
"Hostname could not be resolved: " + error.message());
77 for (
const auto& entry : results) {
78 if (m_addressSelector(entry.endpoint().address())) {
80 m_onSuccess(entry.endpoint().address());
87 m_onError(
"No endpoints match the specified address selector");
92 onResolveTimeout(
const shared_ptr<Resolver>&
self)
97 boost::asio::post(m_resolver.get_executor(), [
self] {});
100 m_onError(
"Hostname resolution timed out");
105 boost::asio::ip::udp::resolver m_resolver;
119 boost::asio::io_context& ioCtx,
123 auto resolver = make_shared<Resolver>(ioCtx, addressSelector);
124 resolver->asyncResolve(host, onSuccess, onError, timeout, resolver);
A handle for a scheduled event.
Generic time-based event scheduler.
std::function< bool(const boost::asio::ip::address &)> AddressSelector
void asyncResolve(const std::string &host, const SuccessCallback &onSuccess, const ErrorCallback &onError, boost::asio::io_context &ioCtx, const AddressSelector &addressSelector, time::nanoseconds timeout)
Asynchronously resolve host.
std::function< void(const boost::asio::ip::address &address)> SuccessCallback
std::function< void(const std::string &reason)> ErrorCallback
::boost::chrono::nanoseconds nanoseconds