basic_resolver.hpp 9.1 KB

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