winrt_resolver_service.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // detail/winrt_resolver_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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_DETAIL_WINRT_RESOLVER_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_WINRT_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_WINDOWS_RUNTIME)
  17. #include <boost/asio/ip/basic_resolver_query.hpp>
  18. #include <boost/asio/ip/basic_resolver_results.hpp>
  19. #include <boost/asio/post.hpp>
  20. #include <boost/asio/detail/bind_handler.hpp>
  21. #include <boost/asio/detail/memory.hpp>
  22. #include <boost/asio/detail/socket_ops.hpp>
  23. #include <boost/asio/detail/winrt_async_manager.hpp>
  24. #include <boost/asio/detail/winrt_resolve_op.hpp>
  25. #include <boost/asio/detail/winrt_utils.hpp>
  26. #if defined(BOOST_ASIO_HAS_IOCP)
  27. # include <boost/asio/detail/win_iocp_io_context.hpp>
  28. #else // defined(BOOST_ASIO_HAS_IOCP)
  29. # include <boost/asio/detail/scheduler.hpp>
  30. #endif // defined(BOOST_ASIO_HAS_IOCP)
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace detail {
  35. template <typename Protocol>
  36. class winrt_resolver_service :
  37. public execution_context_service_base<winrt_resolver_service<Protocol> >
  38. {
  39. public:
  40. // The implementation type of the resolver. A cancellation token is used to
  41. // indicate to the asynchronous operation that the operation has been
  42. // cancelled.
  43. typedef socket_ops::shared_cancel_token_type implementation_type;
  44. // The endpoint type.
  45. typedef typename Protocol::endpoint endpoint_type;
  46. // The query type.
  47. typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
  48. // The results type.
  49. typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
  50. // Constructor.
  51. winrt_resolver_service(execution_context& context)
  52. : execution_context_service_base<
  53. winrt_resolver_service<Protocol> >(context),
  54. scheduler_(use_service<scheduler_impl>(context)),
  55. async_manager_(use_service<winrt_async_manager>(context))
  56. {
  57. }
  58. // Destructor.
  59. ~winrt_resolver_service()
  60. {
  61. }
  62. // Destroy all user-defined handler objects owned by the service.
  63. void shutdown()
  64. {
  65. }
  66. // Perform any fork-related housekeeping.
  67. void notify_fork(execution_context::fork_event)
  68. {
  69. }
  70. // Construct a new resolver implementation.
  71. void construct(implementation_type&)
  72. {
  73. }
  74. // Move-construct a new resolver implementation.
  75. void move_construct(implementation_type&,
  76. implementation_type&)
  77. {
  78. }
  79. // Move-assign from another resolver implementation.
  80. void move_assign(implementation_type&,
  81. winrt_resolver_service&, implementation_type&)
  82. {
  83. }
  84. // Destroy a resolver implementation.
  85. void destroy(implementation_type&)
  86. {
  87. }
  88. // Cancel pending asynchronous operations.
  89. void cancel(implementation_type&)
  90. {
  91. }
  92. // Resolve a query to a list of entries.
  93. results_type resolve(implementation_type&,
  94. const query_type& query, boost::system::error_code& ec)
  95. {
  96. try
  97. {
  98. using namespace Windows::Networking::Sockets;
  99. auto endpoint_pairs = async_manager_.sync(
  100. DatagramSocket::GetEndpointPairsAsync(
  101. winrt_utils::host_name(query.host_name()),
  102. winrt_utils::string(query.service_name())), ec);
  103. if (ec)
  104. return results_type();
  105. return results_type::create(
  106. endpoint_pairs, query.hints(),
  107. query.host_name(), query.service_name());
  108. }
  109. catch (Platform::Exception^ e)
  110. {
  111. ec = boost::system::error_code(e->HResult,
  112. boost::system::system_category());
  113. return results_type();
  114. }
  115. }
  116. // Asynchronously resolve a query to a list of entries.
  117. template <typename Handler, typename IoExecutor>
  118. void async_resolve(implementation_type& impl, const query_type& query,
  119. Handler& handler, const IoExecutor& io_ex)
  120. {
  121. bool is_continuation =
  122. boost_asio_handler_cont_helpers::is_continuation(handler);
  123. // Allocate and construct an operation to wrap the handler.
  124. typedef winrt_resolve_op<Protocol, Handler, IoExecutor> op;
  125. typename op::ptr p = { boost::asio::detail::addressof(handler),
  126. op::ptr::allocate(handler), 0 };
  127. p.p = new (p.v) op(query, handler, io_ex);
  128. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  129. *p.p, "resolver", &impl, 0, "async_resolve"));
  130. (void)impl;
  131. try
  132. {
  133. using namespace Windows::Networking::Sockets;
  134. async_manager_.async(DatagramSocket::GetEndpointPairsAsync(
  135. winrt_utils::host_name(query.host_name()),
  136. winrt_utils::string(query.service_name())), p.p);
  137. p.v = p.p = 0;
  138. }
  139. catch (Platform::Exception^ e)
  140. {
  141. p.p->ec_ = boost::system::error_code(
  142. e->HResult, boost::system::system_category());
  143. scheduler_.post_immediate_completion(p.p, is_continuation);
  144. p.v = p.p = 0;
  145. }
  146. }
  147. // Resolve an endpoint to a list of entries.
  148. results_type resolve(implementation_type&,
  149. const endpoint_type&, boost::system::error_code& ec)
  150. {
  151. ec = boost::asio::error::operation_not_supported;
  152. return results_type();
  153. }
  154. // Asynchronously resolve an endpoint to a list of entries.
  155. template <typename Handler, typename IoExecutor>
  156. void async_resolve(implementation_type&, const endpoint_type&,
  157. Handler& handler, const IoExecutor& io_ex)
  158. {
  159. boost::system::error_code ec = boost::asio::error::operation_not_supported;
  160. const results_type results;
  161. boost::asio::post(io_ex, detail::bind_handler(handler, ec, results));
  162. }
  163. private:
  164. // The scheduler implementation used for delivering completions.
  165. #if defined(BOOST_ASIO_HAS_IOCP)
  166. typedef class win_iocp_io_context scheduler_impl;
  167. #else
  168. typedef class scheduler scheduler_impl;
  169. #endif
  170. scheduler_impl& scheduler_;
  171. winrt_async_manager& async_manager_;
  172. };
  173. } // namespace detail
  174. } // namespace asio
  175. } // namespace boost
  176. #include <boost/asio/detail/pop_options.hpp>
  177. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  178. #endif // BOOST_ASIO_DETAIL_WINRT_RESOLVER_SERVICE_HPP