basic_resolver.hpp 8.2 KB

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