Chris@16: // Chris@16: // detail/winrt_resolver_service.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_WINRT_RESOLVER_SERVICE_HPP Chris@16: #define BOOST_ASIO_DETAIL_WINRT_RESOLVER_SERVICE_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_WINDOWS_RUNTIME) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: class winrt_resolver_service Chris@16: { Chris@16: public: Chris@16: // The implementation type of the resolver. A cancellation token is used to Chris@16: // indicate to the asynchronous operation that the operation has been Chris@16: // cancelled. Chris@16: typedef socket_ops::shared_cancel_token_type implementation_type; Chris@16: Chris@16: // The endpoint type. Chris@16: typedef typename Protocol::endpoint endpoint_type; Chris@16: Chris@16: // The query type. Chris@16: typedef boost::asio::ip::basic_resolver_query query_type; Chris@16: Chris@16: // The iterator type. Chris@16: typedef boost::asio::ip::basic_resolver_iterator iterator_type; Chris@16: Chris@16: // Constructor. Chris@16: winrt_resolver_service(boost::asio::io_service& io_service) Chris@16: : io_service_(use_service(io_service)), Chris@16: async_manager_(use_service(io_service)) Chris@16: { Chris@16: } Chris@16: Chris@16: // Destructor. Chris@16: ~winrt_resolver_service() Chris@16: { Chris@16: } Chris@16: Chris@16: // Destroy all user-defined handler objects owned by the service. Chris@16: void shutdown_service() Chris@16: { Chris@16: } Chris@16: Chris@16: // Perform any fork-related housekeeping. Chris@16: void fork_service(boost::asio::io_service::fork_event) Chris@16: { Chris@16: } Chris@16: Chris@16: // Construct a new resolver implementation. Chris@16: void construct(implementation_type&) Chris@16: { Chris@16: } Chris@16: Chris@16: // Destroy a resolver implementation. Chris@16: void destroy(implementation_type&) Chris@16: { Chris@16: } Chris@16: Chris@16: // Cancel pending asynchronous operations. Chris@16: void cancel(implementation_type&) Chris@16: { Chris@16: } Chris@16: Chris@16: // Resolve a query to a list of entries. Chris@16: iterator_type resolve(implementation_type&, Chris@16: const query_type& query, boost::system::error_code& ec) Chris@16: { Chris@16: try Chris@16: { Chris@16: using namespace Windows::Networking::Sockets; Chris@16: auto endpoint_pairs = async_manager_.sync( Chris@16: DatagramSocket::GetEndpointPairsAsync( Chris@16: winrt_utils::host_name(query.host_name()), Chris@16: winrt_utils::string(query.service_name())), ec); Chris@16: Chris@16: if (ec) Chris@16: return iterator_type(); Chris@16: Chris@16: return iterator_type::create( Chris@16: endpoint_pairs, query.hints(), Chris@16: query.host_name(), query.service_name()); Chris@16: } Chris@16: catch (Platform::Exception^ e) Chris@16: { Chris@16: ec = boost::system::error_code(e->HResult, Chris@16: boost::system::system_category()); Chris@16: return iterator_type(); Chris@16: } Chris@16: } Chris@16: Chris@16: // Asynchronously resolve a query to a list of entries. Chris@16: template Chris@16: void async_resolve(implementation_type&, Chris@16: const query_type& query, Handler& handler) Chris@16: { Chris@16: bool is_continuation = Chris@16: boost_asio_handler_cont_helpers::is_continuation(handler); Chris@16: Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef winrt_resolve_op op; Chris@16: typename op::ptr p = { boost::asio::detail::addressof(handler), Chris@16: boost_asio_handler_alloc_helpers::allocate( Chris@16: sizeof(op), handler), 0 }; Chris@16: p.p = new (p.v) op(query, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve")); Chris@16: Chris@16: try Chris@16: { Chris@16: using namespace Windows::Networking::Sockets; Chris@16: async_manager_.async(DatagramSocket::GetEndpointPairsAsync( Chris@16: winrt_utils::host_name(query.host_name()), Chris@16: winrt_utils::string(query.service_name())), p.p); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: catch (Platform::Exception^ e) Chris@16: { Chris@16: p.p->ec_ = boost::system::error_code( Chris@16: e->HResult, boost::system::system_category()); Chris@16: io_service_.post_immediate_completion(p.p, is_continuation); Chris@16: p.v = p.p = 0; Chris@16: } Chris@16: } Chris@16: Chris@16: // Resolve an endpoint to a list of entries. Chris@16: iterator_type resolve(implementation_type&, Chris@16: const endpoint_type&, boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::asio::error::operation_not_supported; Chris@16: return iterator_type(); Chris@16: } Chris@16: Chris@16: // Asynchronously resolve an endpoint to a list of entries. Chris@16: template Chris@16: void async_resolve(implementation_type&, Chris@16: const endpoint_type&, Handler& handler) Chris@16: { Chris@16: boost::system::error_code ec = boost::asio::error::operation_not_supported; Chris@16: const iterator_type iterator; Chris@16: io_service_.get_io_service().post( Chris@16: detail::bind_handler(handler, ec, iterator)); Chris@16: } Chris@16: Chris@16: private: Chris@16: io_service_impl& io_service_; Chris@16: winrt_async_manager& async_manager_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_WINRT_RESOLVER_SERVICE_HPP