winrt_resolver_service.hpp 5.0 KB

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