basic_resolver.hpp 8.3 KB

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