resolver_service.hpp 5.0 KB

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