2 // ip/basic_resolver.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_IP_BASIC_RESOLVER_HPP
12 #define BOOST_ASIO_IP_BASIC_RESOLVER_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/basic_io_object.hpp>
20 #include <boost/asio/detail/handler_type_requirements.hpp>
21 #include <boost/asio/detail/throw_error.hpp>
22 #include <boost/asio/error.hpp>
23 #include <boost/asio/ip/basic_resolver_iterator.hpp>
24 #include <boost/asio/ip/basic_resolver_query.hpp>
25 #include <boost/asio/ip/resolver_service.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 /// Provides endpoint resolution functionality.
35 * The basic_resolver class template provides the ability to resolve a query
36 * to a list of endpoints.
39 * @e Distinct @e objects: Safe.@n
40 * @e Shared @e objects: Unsafe.
42 template <typename InternetProtocol,
43 typename ResolverService = resolver_service<InternetProtocol> >
45 : public basic_io_object<ResolverService>
48 /// The protocol type.
49 typedef InternetProtocol protocol_type;
51 /// The endpoint type.
52 typedef typename InternetProtocol::endpoint endpoint_type;
55 typedef basic_resolver_query<InternetProtocol> query;
57 /// The iterator type.
58 typedef basic_resolver_iterator<InternetProtocol> iterator;
62 * This constructor creates a basic_resolver.
64 * @param io_service The io_service object that the resolver will use to
65 * dispatch handlers for any asynchronous operations performed on the timer.
67 explicit basic_resolver(boost::asio::io_service& io_service)
68 : basic_io_object<ResolverService>(io_service)
72 /// Cancel any asynchronous operations that are waiting on the resolver.
74 * This function forces the completion of any pending asynchronous
75 * operations on the host resolver. The handler for each cancelled operation
76 * will be invoked with the boost::asio::error::operation_aborted error code.
80 return this->service.cancel(this->implementation);
83 /// Perform forward resolution of a query to a list of entries.
85 * This function is used to resolve a query into a list of endpoint entries.
87 * @param q A query object that determines what endpoints will be returned.
89 * @returns A forward-only iterator that can be used to traverse the list
90 * of endpoint entries.
92 * @throws boost::system::system_error Thrown on failure.
94 * @note A default constructed iterator represents the end of the list.
96 * A successful call to this function is guaranteed to return at least one
99 iterator resolve(const query& q)
101 boost::system::error_code ec;
102 iterator i = this->service.resolve(this->implementation, q, ec);
103 boost::asio::detail::throw_error(ec, "resolve");
107 /// Perform forward resolution of a query to a list of entries.
109 * This function is used to resolve a query into a list of endpoint entries.
111 * @param q A query object that determines what endpoints will be returned.
113 * @param ec Set to indicate what error occurred, if any.
115 * @returns A forward-only iterator that can be used to traverse the list
116 * of endpoint entries. Returns a default constructed iterator if an error
119 * @note A default constructed iterator represents the end of the list.
121 * A successful call to this function is guaranteed to return at least one
124 iterator resolve(const query& q, boost::system::error_code& ec)
126 return this->service.resolve(this->implementation, q, ec);
129 /// Asynchronously perform forward resolution of a query to a list of entries.
131 * This function is used to asynchronously resolve a query into a list of
134 * @param q A query object that determines what endpoints will be returned.
136 * @param handler The handler to be called when the resolve operation
137 * completes. Copies will be made of the handler as required. The function
138 * signature of the handler must be:
139 * @code void handler(
140 * const boost::system::error_code& error, // Result of operation.
141 * resolver::iterator iterator // Forward-only iterator that can
142 * // be used to traverse the list
143 * // of endpoint entries.
145 * Regardless of whether the asynchronous operation completes immediately or
146 * not, the handler will not be invoked from within this function. Invocation
147 * of the handler will be performed in a manner equivalent to using
148 * boost::asio::io_service::post().
150 * @note A default constructed iterator represents the end of the list.
152 * A successful resolve operation is guaranteed to pass at least one entry to
155 template <typename ResolveHandler>
156 BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
157 void (boost::system::error_code, iterator))
158 async_resolve(const query& q,
159 BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
161 // If you get an error on the following line it means that your handler does
162 // not meet the documented type requirements for a ResolveHandler.
163 BOOST_ASIO_RESOLVE_HANDLER_CHECK(
164 ResolveHandler, handler, iterator) type_check;
166 return this->service.async_resolve(this->implementation, q,
167 BOOST_ASIO_MOVE_CAST(ResolveHandler)(handler));
170 /// Perform reverse resolution of an endpoint to a list of entries.
172 * This function is used to resolve an endpoint into a list of endpoint
175 * @param e An endpoint object that determines what endpoints will be
178 * @returns A forward-only iterator that can be used to traverse the list
179 * of endpoint entries.
181 * @throws boost::system::system_error Thrown on failure.
183 * @note A default constructed iterator represents the end of the list.
185 * A successful call to this function is guaranteed to return at least one
188 iterator resolve(const endpoint_type& e)
190 boost::system::error_code ec;
191 iterator i = this->service.resolve(this->implementation, e, ec);
192 boost::asio::detail::throw_error(ec, "resolve");
196 /// Perform reverse resolution of an endpoint to a list of entries.
198 * This function is used to resolve an endpoint into a list of endpoint
201 * @param e An endpoint object that determines what endpoints will be
204 * @param ec Set to indicate what error occurred, if any.
206 * @returns A forward-only iterator that can be used to traverse the list
207 * of endpoint entries. Returns a default constructed iterator if an error
210 * @note A default constructed iterator represents the end of the list.
212 * A successful call to this function is guaranteed to return at least one
215 iterator resolve(const endpoint_type& e, boost::system::error_code& ec)
217 return this->service.resolve(this->implementation, e, ec);
220 /// Asynchronously perform reverse resolution of an endpoint to a list of
223 * This function is used to asynchronously resolve an endpoint into a list of
226 * @param e An endpoint object that determines what endpoints will be
229 * @param handler The handler to be called when the resolve operation
230 * completes. Copies will be made of the handler as required. The function
231 * signature of the handler must be:
232 * @code void handler(
233 * const boost::system::error_code& error, // Result of operation.
234 * resolver::iterator iterator // Forward-only iterator that can
235 * // be used to traverse the list
236 * // of endpoint entries.
238 * Regardless of whether the asynchronous operation completes immediately or
239 * not, the handler will not be invoked from within this function. Invocation
240 * of the handler will be performed in a manner equivalent to using
241 * boost::asio::io_service::post().
243 * @note A default constructed iterator represents the end of the list.
245 * A successful resolve operation is guaranteed to pass at least one entry to
248 template <typename ResolveHandler>
249 BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
250 void (boost::system::error_code, iterator))
251 async_resolve(const endpoint_type& e,
252 BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
254 // If you get an error on the following line it means that your handler does
255 // not meet the documented type requirements for a ResolveHandler.
256 BOOST_ASIO_RESOLVE_HANDLER_CHECK(
257 ResolveHandler, handler, iterator) type_check;
259 return this->service.async_resolve(this->implementation, e,
260 BOOST_ASIO_MOVE_CAST(ResolveHandler)(handler));
268 #include <boost/asio/detail/pop_options.hpp>
270 #endif // BOOST_ASIO_IP_BASIC_RESOLVER_HPP