winrt_resolver_service.hpp 5.2 KB

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