resolver_service.hpp 4.8 KB

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