resolver_service.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // ip/resolver_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_RESOLVER_SERVICE_HPP
  11. #define BOOST_ASIO_IP_RESOLVER_SERVICE_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. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  17. #include <boost/asio/async_result.hpp>
  18. #include <boost/system/error_code.hpp>
  19. #include <boost/asio/io_context.hpp>
  20. #include <boost/asio/ip/basic_resolver_iterator.hpp>
  21. #include <boost/asio/ip/basic_resolver_query.hpp>
  22. #include <boost/asio/ip/basic_resolver_results.hpp>
  23. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  24. # include <boost/asio/detail/winrt_resolver_service.hpp>
  25. #else
  26. # include <boost/asio/detail/resolver_service.hpp>
  27. #endif
  28. #include <boost/asio/detail/push_options.hpp>
  29. namespace boost {
  30. namespace asio {
  31. namespace ip {
  32. /// Default service implementation for a resolver.
  33. template <typename InternetProtocol>
  34. class resolver_service
  35. #if defined(GENERATING_DOCUMENTATION)
  36. : public boost::asio::io_context::service
  37. #else
  38. : public boost::asio::detail::service_base<
  39. resolver_service<InternetProtocol> >
  40. #endif
  41. {
  42. public:
  43. #if defined(GENERATING_DOCUMENTATION)
  44. /// The unique service identifier.
  45. static boost::asio::io_context::id id;
  46. #endif
  47. /// The protocol type.
  48. typedef InternetProtocol protocol_type;
  49. /// The endpoint type.
  50. typedef typename InternetProtocol::endpoint endpoint_type;
  51. /// The query type.
  52. typedef basic_resolver_query<InternetProtocol> query_type;
  53. /// The iterator type.
  54. typedef basic_resolver_iterator<InternetProtocol> iterator_type;
  55. /// The results type.
  56. typedef basic_resolver_results<InternetProtocol> results_type;
  57. private:
  58. // The type of the platform-specific implementation.
  59. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  60. typedef boost::asio::detail::winrt_resolver_service<InternetProtocol>
  61. service_impl_type;
  62. #else
  63. typedef boost::asio::detail::resolver_service<InternetProtocol>
  64. service_impl_type;
  65. #endif
  66. public:
  67. /// The type of a resolver implementation.
  68. #if defined(GENERATING_DOCUMENTATION)
  69. typedef implementation_defined implementation_type;
  70. #else
  71. typedef typename service_impl_type::implementation_type implementation_type;
  72. #endif
  73. /// Construct a new resolver service for the specified io_context.
  74. explicit resolver_service(boost::asio::io_context& io_context)
  75. : boost::asio::detail::service_base<
  76. resolver_service<InternetProtocol> >(io_context),
  77. service_impl_(io_context)
  78. {
  79. }
  80. /// Construct a new resolver implementation.
  81. void construct(implementation_type& impl)
  82. {
  83. service_impl_.construct(impl);
  84. }
  85. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  86. /// Move-construct a new resolver implementation.
  87. void move_construct(implementation_type& impl,
  88. implementation_type& other_impl)
  89. {
  90. service_impl_.move_construct(impl, other_impl);
  91. }
  92. /// Move-assign from another resolver implementation.
  93. void move_assign(implementation_type& impl,
  94. resolver_service& other_service,
  95. implementation_type& other_impl)
  96. {
  97. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  98. }
  99. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  100. /// Destroy a resolver implementation.
  101. void destroy(implementation_type& impl)
  102. {
  103. service_impl_.destroy(impl);
  104. }
  105. /// Cancel pending asynchronous operations.
  106. void cancel(implementation_type& impl)
  107. {
  108. service_impl_.cancel(impl);
  109. }
  110. /// Resolve a query to a list of entries.
  111. results_type resolve(implementation_type& impl, const query_type& query,
  112. boost::system::error_code& ec)
  113. {
  114. return service_impl_.resolve(impl, query, ec);
  115. }
  116. /// Asynchronously resolve a query to a list of entries.
  117. template <typename ResolveHandler>
  118. BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
  119. void (boost::system::error_code, results_type))
  120. async_resolve(implementation_type& impl, const query_type& query,
  121. BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
  122. {
  123. boost::asio::async_completion<ResolveHandler,
  124. void (boost::system::error_code, results_type)> init(handler);
  125. service_impl_.async_resolve(impl, query, init.completion_handler);
  126. return init.result.get();
  127. }
  128. /// Resolve an endpoint to a list of entries.
  129. results_type resolve(implementation_type& impl,
  130. const endpoint_type& endpoint, boost::system::error_code& ec)
  131. {
  132. return service_impl_.resolve(impl, endpoint, ec);
  133. }
  134. /// Asynchronously resolve an endpoint to a list of entries.
  135. template <typename ResolveHandler>
  136. BOOST_ASIO_INITFN_RESULT_TYPE(ResolveHandler,
  137. void (boost::system::error_code, results_type))
  138. async_resolve(implementation_type& impl, const endpoint_type& endpoint,
  139. BOOST_ASIO_MOVE_ARG(ResolveHandler) handler)
  140. {
  141. boost::asio::async_completion<ResolveHandler,
  142. void (boost::system::error_code, results_type)> init(handler);
  143. service_impl_.async_resolve(impl, endpoint, init.completion_handler);
  144. return init.result.get();
  145. }
  146. private:
  147. // Destroy all user-defined handler objects owned by the service.
  148. void shutdown()
  149. {
  150. service_impl_.shutdown();
  151. }
  152. // Perform any fork-related housekeeping.
  153. void notify_fork(boost::asio::io_context::fork_event event)
  154. {
  155. service_impl_.notify_fork(event);
  156. }
  157. // The platform-specific implementation.
  158. service_impl_type service_impl_;
  159. };
  160. } // namespace ip
  161. } // namespace asio
  162. } // namespace boost
  163. #include <boost/asio/detail/pop_options.hpp>
  164. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  165. #endif // BOOST_ASIO_IP_RESOLVER_SERVICE_HPP