2 // detail/winrt_resolver_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_WINRT_RESOLVER_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_WINRT_RESOLVER_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
22 #include <boost/asio/ip/basic_resolver_query.hpp>
23 #include <boost/asio/ip/basic_resolver_results.hpp>
24 #include <boost/asio/detail/bind_handler.hpp>
25 #include <boost/asio/detail/memory.hpp>
26 #include <boost/asio/detail/socket_ops.hpp>
27 #include <boost/asio/detail/winrt_async_manager.hpp>
28 #include <boost/asio/detail/winrt_resolve_op.hpp>
29 #include <boost/asio/detail/winrt_utils.hpp>
31 #include <boost/asio/detail/push_options.hpp>
37 template <typename Protocol>
38 class winrt_resolver_service :
39 public service_base<winrt_resolver_service<Protocol> >
42 // The implementation type of the resolver. A cancellation token is used to
43 // indicate to the asynchronous operation that the operation has been
45 typedef socket_ops::shared_cancel_token_type implementation_type;
48 typedef typename Protocol::endpoint endpoint_type;
51 typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
54 typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
57 winrt_resolver_service(boost::asio::io_context& io_context)
58 : service_base<winrt_resolver_service<Protocol> >(io_context),
59 io_context_(use_service<io_context_impl>(io_context)),
60 async_manager_(use_service<winrt_async_manager>(io_context))
65 ~winrt_resolver_service()
69 // Destroy all user-defined handler objects owned by the service.
74 // Perform any fork-related housekeeping.
75 void notify_fork(boost::asio::io_context::fork_event)
79 // Construct a new resolver implementation.
80 void construct(implementation_type&)
84 // Move-construct a new resolver implementation.
85 void move_construct(implementation_type&,
90 // Move-assign from another resolver implementation.
91 void move_assign(implementation_type&,
92 winrt_resolver_service&, implementation_type&)
96 // Destroy a resolver implementation.
97 void destroy(implementation_type&)
101 // Cancel pending asynchronous operations.
102 void cancel(implementation_type&)
106 // Resolve a query to a list of entries.
107 results_type resolve(implementation_type&,
108 const query_type& query, boost::system::error_code& ec)
112 using namespace Windows::Networking::Sockets;
113 auto endpoint_pairs = async_manager_.sync(
114 DatagramSocket::GetEndpointPairsAsync(
115 winrt_utils::host_name(query.host_name()),
116 winrt_utils::string(query.service_name())), ec);
119 return results_type();
121 return results_type::create(
122 endpoint_pairs, query.hints(),
123 query.host_name(), query.service_name());
125 catch (Platform::Exception^ e)
127 ec = boost::system::error_code(e->HResult,
128 boost::system::system_category());
129 return results_type();
133 // Asynchronously resolve a query to a list of entries.
134 template <typename Handler>
135 void async_resolve(implementation_type& impl,
136 const query_type& query, Handler& handler)
138 bool is_continuation =
139 boost_asio_handler_cont_helpers::is_continuation(handler);
141 // Allocate and construct an operation to wrap the handler.
142 typedef winrt_resolve_op<Protocol, Handler> op;
143 typename op::ptr p = { boost::asio::detail::addressof(handler),
144 op::ptr::allocate(handler), 0 };
145 p.p = new (p.v) op(query, handler);
147 BOOST_ASIO_HANDLER_CREATION((io_context_.context(),
148 *p.p, "resolver", &impl, 0, "async_resolve"));
153 using namespace Windows::Networking::Sockets;
154 async_manager_.async(DatagramSocket::GetEndpointPairsAsync(
155 winrt_utils::host_name(query.host_name()),
156 winrt_utils::string(query.service_name())), p.p);
159 catch (Platform::Exception^ e)
161 p.p->ec_ = boost::system::error_code(
162 e->HResult, boost::system::system_category());
163 io_context_.post_immediate_completion(p.p, is_continuation);
168 // Resolve an endpoint to a list of entries.
169 results_type resolve(implementation_type&,
170 const endpoint_type&, boost::system::error_code& ec)
172 ec = boost::asio::error::operation_not_supported;
173 return results_type();
176 // Asynchronously resolve an endpoint to a list of entries.
177 template <typename Handler>
178 void async_resolve(implementation_type&,
179 const endpoint_type&, Handler& handler)
181 boost::system::error_code ec = boost::asio::error::operation_not_supported;
182 const results_type results;
183 io_context_.get_io_context().post(
184 detail::bind_handler(handler, ec, results));
188 io_context_impl& io_context_;
189 winrt_async_manager& async_manager_;
192 } // namespace detail
196 #include <boost/asio/detail/pop_options.hpp>
198 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
200 #endif // BOOST_ASIO_DETAIL_WINRT_RESOLVER_SERVICE_HPP