2 // detail/resolver_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
22 #include <boost/asio/ip/basic_resolver_query.hpp>
23 #include <boost/asio/ip/basic_resolver_results.hpp>
24 #include <boost/asio/detail/concurrency_hint.hpp>
25 #include <boost/asio/detail/memory.hpp>
26 #include <boost/asio/detail/resolve_endpoint_op.hpp>
27 #include <boost/asio/detail/resolve_query_op.hpp>
28 #include <boost/asio/detail/resolver_service_base.hpp>
30 #include <boost/asio/detail/push_options.hpp>
36 template <typename Protocol>
37 class resolver_service :
38 public service_base<resolver_service<Protocol> >,
39 public resolver_service_base
42 // The implementation type of the resolver. A cancellation token is used to
43 // indicate to the background thread that the operation has been cancelled.
44 typedef socket_ops::shared_cancel_token_type implementation_type;
47 typedef typename Protocol::endpoint endpoint_type;
50 typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
53 typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
56 resolver_service(boost::asio::io_context& io_context)
57 : service_base<resolver_service<Protocol> >(io_context),
58 resolver_service_base(io_context)
62 // Destroy all user-defined handler objects owned by the service.
65 this->base_shutdown();
68 // Perform any fork-related housekeeping.
69 void notify_fork(boost::asio::io_context::fork_event fork_ev)
71 this->base_notify_fork(fork_ev);
74 // Resolve a query to a list of entries.
75 results_type resolve(implementation_type&, const query_type& query,
76 boost::system::error_code& ec)
78 boost::asio::detail::addrinfo_type* address_info = 0;
80 socket_ops::getaddrinfo(query.host_name().c_str(),
81 query.service_name().c_str(), query.hints(), &address_info, ec);
82 auto_addrinfo auto_address_info(address_info);
84 return ec ? results_type() : results_type::create(
85 address_info, query.host_name(), query.service_name());
88 // Asynchronously resolve a query to a list of entries.
89 template <typename Handler>
90 void async_resolve(implementation_type& impl,
91 const query_type& query, Handler& handler)
93 // Allocate and construct an operation to wrap the handler.
94 typedef resolve_query_op<Protocol, Handler> op;
95 typename op::ptr p = { boost::asio::detail::addressof(handler),
96 op::ptr::allocate(handler), 0 };
97 p.p = new (p.v) op(impl, query, io_context_impl_, handler);
99 BOOST_ASIO_HANDLER_CREATION((io_context_impl_.context(),
100 *p.p, "resolver", &impl, 0, "async_resolve"));
102 start_resolve_op(p.p);
106 // Resolve an endpoint to a list of entries.
107 results_type resolve(implementation_type&,
108 const endpoint_type& endpoint, boost::system::error_code& ec)
110 char host_name[NI_MAXHOST];
111 char service_name[NI_MAXSERV];
112 socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
113 host_name, NI_MAXHOST, service_name, NI_MAXSERV,
114 endpoint.protocol().type(), ec);
116 return ec ? results_type() : results_type::create(
117 endpoint, host_name, service_name);
120 // Asynchronously resolve an endpoint to a list of entries.
121 template <typename Handler>
122 void async_resolve(implementation_type& impl,
123 const endpoint_type& endpoint, Handler& handler)
125 // Allocate and construct an operation to wrap the handler.
126 typedef resolve_endpoint_op<Protocol, Handler> op;
127 typename op::ptr p = { boost::asio::detail::addressof(handler),
128 op::ptr::allocate(handler), 0 };
129 p.p = new (p.v) op(impl, endpoint, io_context_impl_, handler);
131 BOOST_ASIO_HANDLER_CREATION((io_context_impl_.context(),
132 *p.p, "resolver", &impl, 0, "async_resolve"));
134 start_resolve_op(p.p);
139 } // namespace detail
143 #include <boost/asio/detail/pop_options.hpp>
145 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
147 #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP