basic_resolver.hpp 9.4 KB

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