Chris@16
|
1 //
|
Chris@16
|
2 // ip/basic_resolver.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_IP_BASIC_RESOLVER_HPP
|
Chris@16
|
12 #define BOOST_ASIO_IP_BASIC_RESOLVER_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <boost/asio/basic_io_object.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
22 #include <boost/asio/error.hpp>
|
Chris@16
|
23 #include <boost/asio/ip/basic_resolver_iterator.hpp>
|
Chris@16
|
24 #include <boost/asio/ip/basic_resolver_query.hpp>
|
Chris@16
|
25 #include <boost/asio/ip/resolver_service.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 namespace boost {
|
Chris@16
|
30 namespace asio {
|
Chris@16
|
31 namespace ip {
|
Chris@16
|
32
|
Chris@16
|
33 /// Provides endpoint resolution functionality.
|
Chris@16
|
34 /**
|
Chris@16
|
35 * The basic_resolver class template provides the ability to resolve a query
|
Chris@16
|
36 * to a list of endpoints.
|
Chris@16
|
37 *
|
Chris@16
|
38 * @par Thread Safety
|
Chris@16
|
39 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
40 * @e Shared @e objects: Unsafe.
|
Chris@16
|
41 */
|
Chris@16
|
42 template <typename InternetProtocol,
|
Chris@16
|
43 typename ResolverService = resolver_service<InternetProtocol> >
|
Chris@16
|
44 class basic_resolver
|
Chris@16
|
45 : public basic_io_object<ResolverService>
|
Chris@16
|
46 {
|
Chris@16
|
47 public:
|
Chris@16
|
48 /// The protocol type.
|
Chris@16
|
49 typedef InternetProtocol protocol_type;
|
Chris@16
|
50
|
Chris@16
|
51 /// The endpoint type.
|
Chris@16
|
52 typedef typename InternetProtocol::endpoint endpoint_type;
|
Chris@16
|
53
|
Chris@16
|
54 /// The query type.
|
Chris@16
|
55 typedef basic_resolver_query<InternetProtocol> query;
|
Chris@16
|
56
|
Chris@16
|
57 /// The iterator type.
|
Chris@16
|
58 typedef basic_resolver_iterator<InternetProtocol> iterator;
|
Chris@16
|
59
|
Chris@16
|
60 /// Constructor.
|
Chris@16
|
61 /**
|
Chris@16
|
62 * This constructor creates a basic_resolver.
|
Chris@16
|
63 *
|
Chris@16
|
64 * @param io_service The io_service object that the resolver will use to
|
Chris@16
|
65 * dispatch handlers for any asynchronous operations performed on the timer.
|
Chris@16
|
66 */
|
Chris@16
|
67 explicit basic_resolver(boost::asio::io_service& io_service)
|
Chris@16
|
68 : basic_io_object<ResolverService>(io_service)
|
Chris@16
|
69 {
|
Chris@16
|
70 }
|
Chris@16
|
71
|
Chris@16
|
72 /// Cancel any asynchronous operations that are waiting on the resolver.
|
Chris@16
|
73 /**
|
Chris@16
|
74 * This function forces the completion of any pending asynchronous
|
Chris@16
|
75 * operations on the host resolver. The handler for each cancelled operation
|
Chris@16
|
76 * will be invoked with the boost::asio::error::operation_aborted error code.
|
Chris@16
|
77 */
|
Chris@16
|
78 void cancel()
|
Chris@16
|
79 {
|
Chris@16
|
80 return this->service.cancel(this->implementation);
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 /// Perform forward resolution of a query to a list of entries.
|
Chris@16
|
84 /**
|
Chris@16
|
85 * This function is used to resolve a query into a list of endpoint entries.
|
Chris@16
|
86 *
|
Chris@16
|
87 * @param q A query object that determines what endpoints will be returned.
|
Chris@16
|
88 *
|
Chris@16
|
89 * @returns A forward-only iterator that can be used to traverse the list
|
Chris@16
|
90 * of endpoint entries.
|
Chris@16
|
91 *
|
Chris@16
|
92 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
93 *
|
Chris@16
|
94 * @note A default constructed iterator represents the end of the list.
|
Chris@16
|
95 *
|
Chris@16
|
96 * A successful call to this function is guaranteed to return at least one
|
Chris@16
|
97 * entry.
|
Chris@16
|
98 */
|
Chris@16
|
99 iterator resolve(const query& q)
|
Chris@16
|
100 {
|
Chris@16
|
101 boost::system::error_code ec;
|
Chris@16
|
102 iterator i = this->service.resolve(this->implementation, q, ec);
|
Chris@16
|
103 boost::asio::detail::throw_error(ec, "resolve");
|
Chris@16
|
104 return i;
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 /// Perform forward resolution of a query to a list of entries.
|
Chris@16
|
108 /**
|
Chris@16
|
109 * This function is used to resolve a query into a list of endpoint entries.
|
Chris@16
|
110 *
|
Chris@16
|
111 * @param q A query object that determines what endpoints will be returned.
|
Chris@16
|
112 *
|
Chris@16
|
113 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
114 *
|
Chris@16
|
115 * @returns A forward-only iterator that can be used to traverse the list
|
Chris@16
|
116 * of endpoint entries. Returns a default constructed iterator if an error
|
Chris@16
|
117 * occurs.
|
Chris@16
|
118 *
|
Chris@16
|
119 * @note A default constructed iterator represents the end of the list.
|
Chris@16
|
120 *
|
Chris@16
|
121 * A successful call to this function is guaranteed to return at least one
|
Chris@16
|
122 * entry.
|
Chris@16
|
123 */
|
Chris@16
|
124 iterator resolve(const query& q, boost::system::error_code& ec)
|
Chris@16
|
125 {
|
Chris@16
|
126 return this->service.resolve(this->implementation, q, ec);
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 /// Asynchronously perform forward resolution of a query to a list of entries.
|
Chris@16
|
130 /**
|
Chris@16
|
131 * This function is used to asynchronously resolve a query into a list of
|
Chris@16
|
132 * endpoint entries.
|
Chris@16
|
133 *
|
Chris@16
|
134 * @param q A query object that determines what endpoints will be returned.
|
Chris@16
|
135 *
|
Chris@16
|
136 * @param handler The handler to be called when the resolve operation
|
Chris@16
|
137 * completes. Copies will be made of the handler as required. The function
|
Chris@16
|
138 * signature of the handler must be:
|
Chris@16
|
139 * @code void handler(
|
Chris@16
|
140 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
141 * resolver::iterator iterator // Forward-only iterator that can
|
Chris@16
|
142 * // be used to traverse the list
|
Chris@16
|
143 * // of endpoint entries.
|
Chris@16
|
144 * ); @endcode
|
Chris@16
|
145 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
146 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
147 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
148 * boost::asio::io_service::post().
|
Chris@16
|
149 *
|
Chris@16
|
150 * @note A default constructed iterator represents the end of the list.
|
Chris@16
|
151 *
|
Chris@16
|
152 * A successful resolve operation is guaranteed to pass at least one entry to
|
Chris@16
|
153 * the handler.
|
Chris@16
|
154 */
|
Chris@16
|
155 template <typename ResolveHandler>
|
Chris@16
|
156 BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
|
Chris@16
|
157 void (boost::system::error_code, iterator))
|
Chris@16
|
158 async_resolve(const query& q,
|
Chris@16
|
159 BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
|
Chris@16
|
160 {
|
Chris@16
|
161 // If you get an error on the following line it means that your handler does
|
Chris@16
|
162 // not meet the documented type requirements for a ResolveHandler.
|
Chris@16
|
163 BOOST_ASIO_RESOLVE_HANDLER_CHECK(
|
Chris@16
|
164 ResolveHandler, handler, iterator) type_check;
|
Chris@16
|
165
|
Chris@16
|
166 return this->service.async_resolve(this->implementation, q,
|
Chris@16
|
167 BOOST_ASIO_MOVE_CAST(ResolveHandler)(handler));
|
Chris@16
|
168 }
|
Chris@16
|
169
|
Chris@16
|
170 /// Perform reverse resolution of an endpoint to a list of entries.
|
Chris@16
|
171 /**
|
Chris@16
|
172 * This function is used to resolve an endpoint into a list of endpoint
|
Chris@16
|
173 * entries.
|
Chris@16
|
174 *
|
Chris@16
|
175 * @param e An endpoint object that determines what endpoints will be
|
Chris@16
|
176 * returned.
|
Chris@16
|
177 *
|
Chris@16
|
178 * @returns A forward-only iterator that can be used to traverse the list
|
Chris@16
|
179 * of endpoint entries.
|
Chris@16
|
180 *
|
Chris@16
|
181 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
182 *
|
Chris@16
|
183 * @note A default constructed iterator represents the end of the list.
|
Chris@16
|
184 *
|
Chris@16
|
185 * A successful call to this function is guaranteed to return at least one
|
Chris@16
|
186 * entry.
|
Chris@16
|
187 */
|
Chris@16
|
188 iterator resolve(const endpoint_type& e)
|
Chris@16
|
189 {
|
Chris@16
|
190 boost::system::error_code ec;
|
Chris@16
|
191 iterator i = this->service.resolve(this->implementation, e, ec);
|
Chris@16
|
192 boost::asio::detail::throw_error(ec, "resolve");
|
Chris@16
|
193 return i;
|
Chris@16
|
194 }
|
Chris@16
|
195
|
Chris@16
|
196 /// Perform reverse resolution of an endpoint to a list of entries.
|
Chris@16
|
197 /**
|
Chris@16
|
198 * This function is used to resolve an endpoint into a list of endpoint
|
Chris@16
|
199 * entries.
|
Chris@16
|
200 *
|
Chris@16
|
201 * @param e An endpoint object that determines what endpoints will be
|
Chris@16
|
202 * returned.
|
Chris@16
|
203 *
|
Chris@16
|
204 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
205 *
|
Chris@16
|
206 * @returns A forward-only iterator that can be used to traverse the list
|
Chris@16
|
207 * of endpoint entries. Returns a default constructed iterator if an error
|
Chris@16
|
208 * occurs.
|
Chris@16
|
209 *
|
Chris@16
|
210 * @note A default constructed iterator represents the end of the list.
|
Chris@16
|
211 *
|
Chris@16
|
212 * A successful call to this function is guaranteed to return at least one
|
Chris@16
|
213 * entry.
|
Chris@16
|
214 */
|
Chris@16
|
215 iterator resolve(const endpoint_type& e, boost::system::error_code& ec)
|
Chris@16
|
216 {
|
Chris@16
|
217 return this->service.resolve(this->implementation, e, ec);
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@16
|
220 /// Asynchronously perform reverse resolution of an endpoint to a list of
|
Chris@16
|
221 /// entries.
|
Chris@16
|
222 /**
|
Chris@16
|
223 * This function is used to asynchronously resolve an endpoint into a list of
|
Chris@16
|
224 * endpoint entries.
|
Chris@16
|
225 *
|
Chris@16
|
226 * @param e An endpoint object that determines what endpoints will be
|
Chris@16
|
227 * returned.
|
Chris@16
|
228 *
|
Chris@16
|
229 * @param handler The handler to be called when the resolve operation
|
Chris@16
|
230 * completes. Copies will be made of the handler as required. The function
|
Chris@16
|
231 * signature of the handler must be:
|
Chris@16
|
232 * @code void handler(
|
Chris@16
|
233 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
234 * resolver::iterator iterator // Forward-only iterator that can
|
Chris@16
|
235 * // be used to traverse the list
|
Chris@16
|
236 * // of endpoint entries.
|
Chris@16
|
237 * ); @endcode
|
Chris@16
|
238 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
239 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
240 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
241 * boost::asio::io_service::post().
|
Chris@16
|
242 *
|
Chris@16
|
243 * @note A default constructed iterator represents the end of the list.
|
Chris@16
|
244 *
|
Chris@16
|
245 * A successful resolve operation is guaranteed to pass at least one entry to
|
Chris@16
|
246 * the handler.
|
Chris@16
|
247 */
|
Chris@16
|
248 template <typename ResolveHandler>
|
Chris@16
|
249 BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
|
Chris@16
|
250 void (boost::system::error_code, iterator))
|
Chris@16
|
251 async_resolve(const endpoint_type& e,
|
Chris@16
|
252 BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
|
Chris@16
|
253 {
|
Chris@16
|
254 // If you get an error on the following line it means that your handler does
|
Chris@16
|
255 // not meet the documented type requirements for a ResolveHandler.
|
Chris@16
|
256 BOOST_ASIO_RESOLVE_HANDLER_CHECK(
|
Chris@16
|
257 ResolveHandler, handler, iterator) type_check;
|
Chris@16
|
258
|
Chris@16
|
259 return this->service.async_resolve(this->implementation, e,
|
Chris@16
|
260 BOOST_ASIO_MOVE_CAST(ResolveHandler)(handler));
|
Chris@16
|
261 }
|
Chris@16
|
262 };
|
Chris@16
|
263
|
Chris@16
|
264 } // namespace ip
|
Chris@16
|
265 } // namespace asio
|
Chris@16
|
266 } // namespace boost
|
Chris@16
|
267
|
Chris@16
|
268 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
269
|
Chris@16
|
270 #endif // BOOST_ASIO_IP_BASIC_RESOLVER_HPP
|