2 // detail/winrt_resolver_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_iterator.hpp>
23 #include <boost/asio/ip/basic_resolver_query.hpp>
24 #include <boost/asio/detail/addressof.hpp>
25 #include <boost/asio/detail/bind_handler.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
41 // The implementation type of the resolver. A cancellation token is used to
42 // indicate to the asynchronous operation that the operation has been
44 typedef socket_ops::shared_cancel_token_type implementation_type;
47 typedef typename Protocol::endpoint endpoint_type;
50 typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
53 typedef boost::asio::ip::basic_resolver_iterator<Protocol> iterator_type;
56 winrt_resolver_service(boost::asio::io_service& io_service)
57 : io_service_(use_service<io_service_impl>(io_service)),
58 async_manager_(use_service<winrt_async_manager>(io_service))
63 ~winrt_resolver_service()
67 // Destroy all user-defined handler objects owned by the service.
68 void shutdown_service()
72 // Perform any fork-related housekeeping.
73 void fork_service(boost::asio::io_service::fork_event)
77 // Construct a new resolver implementation.
78 void construct(implementation_type&)
82 // Destroy a resolver implementation.
83 void destroy(implementation_type&)
87 // Cancel pending asynchronous operations.
88 void cancel(implementation_type&)
92 // Resolve a query to a list of entries.
93 iterator_type resolve(implementation_type&,
94 const query_type& query, boost::system::error_code& ec)
98 using namespace Windows::Networking::Sockets;
99 auto endpoint_pairs = async_manager_.sync(
100 DatagramSocket::GetEndpointPairsAsync(
101 winrt_utils::host_name(query.host_name()),
102 winrt_utils::string(query.service_name())), ec);
105 return iterator_type();
107 return iterator_type::create(
108 endpoint_pairs, query.hints(),
109 query.host_name(), query.service_name());
111 catch (Platform::Exception^ e)
113 ec = boost::system::error_code(e->HResult,
114 boost::system::system_category());
115 return iterator_type();
119 // Asynchronously resolve a query to a list of entries.
120 template <typename Handler>
121 void async_resolve(implementation_type&,
122 const query_type& query, Handler& handler)
124 bool is_continuation =
125 boost_asio_handler_cont_helpers::is_continuation(handler);
127 // Allocate and construct an operation to wrap the handler.
128 typedef winrt_resolve_op<Protocol, Handler> op;
129 typename op::ptr p = { boost::asio::detail::addressof(handler),
130 boost_asio_handler_alloc_helpers::allocate(
131 sizeof(op), handler), 0 };
132 p.p = new (p.v) op(query, handler);
134 BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve"));
138 using namespace Windows::Networking::Sockets;
139 async_manager_.async(DatagramSocket::GetEndpointPairsAsync(
140 winrt_utils::host_name(query.host_name()),
141 winrt_utils::string(query.service_name())), p.p);
144 catch (Platform::Exception^ e)
146 p.p->ec_ = boost::system::error_code(
147 e->HResult, boost::system::system_category());
148 io_service_.post_immediate_completion(p.p, is_continuation);
153 // Resolve an endpoint to a list of entries.
154 iterator_type resolve(implementation_type&,
155 const endpoint_type&, boost::system::error_code& ec)
157 ec = boost::asio::error::operation_not_supported;
158 return iterator_type();
161 // Asynchronously resolve an endpoint to a list of entries.
162 template <typename Handler>
163 void async_resolve(implementation_type&,
164 const endpoint_type&, Handler& handler)
166 boost::system::error_code ec = boost::asio::error::operation_not_supported;
167 const iterator_type iterator;
168 io_service_.get_io_service().post(
169 detail::bind_handler(handler, ec, iterator));
173 io_service_impl& io_service_;
174 winrt_async_manager& async_manager_;
177 } // namespace detail
181 #include <boost/asio/detail/pop_options.hpp>
183 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
185 #endif // BOOST_ASIO_DETAIL_WINRT_RESOLVER_SERVICE_HPP