winrt_resolver_service.hpp 5.6 KB

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