Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.9KB

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