Chris@16: // Chris@16: // detail/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_RESOLVER_SERVICE_HPP Chris@16: #define BOOST_ASIO_DETAIL_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: 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 resolver_service : public resolver_service_base Chris@16: { Chris@16: public: Chris@16: // The implementation type of the resolver. A cancellation token is used to Chris@16: // indicate to the background thread that the operation has been 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: resolver_service(boost::asio::io_service& io_service) Chris@16: : resolver_service_base(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: // Resolve a query to a list of entries. Chris@16: iterator_type resolve(implementation_type&, const query_type& query, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: boost::asio::detail::addrinfo_type* address_info = 0; Chris@16: Chris@16: socket_ops::getaddrinfo(query.host_name().c_str(), Chris@16: query.service_name().c_str(), query.hints(), &address_info, ec); Chris@16: auto_addrinfo auto_address_info(address_info); Chris@16: Chris@16: return ec ? iterator_type() : iterator_type::create( Chris@16: address_info, query.host_name(), query.service_name()); 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& impl, Chris@16: const query_type& query, Handler& handler) Chris@16: { Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef 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(impl, query, io_service_impl_, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve")); Chris@16: Chris@16: start_resolve_op(p.p); Chris@16: p.v = p.p = 0; 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& endpoint, boost::system::error_code& ec) Chris@16: { Chris@16: char host_name[NI_MAXHOST]; Chris@16: char service_name[NI_MAXSERV]; Chris@16: socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(), Chris@16: host_name, NI_MAXHOST, service_name, NI_MAXSERV, Chris@16: endpoint.protocol().type(), ec); Chris@16: Chris@16: return ec ? iterator_type() : iterator_type::create( Chris@16: endpoint, host_name, service_name); 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& impl, Chris@16: const endpoint_type& endpoint, Handler& handler) Chris@16: { Chris@16: // Allocate and construct an operation to wrap the handler. Chris@16: typedef resolve_endpoint_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(impl, endpoint, io_service_impl_, handler); Chris@16: Chris@16: BOOST_ASIO_HANDLER_CREATION((p.p, "resolver", &impl, "async_resolve")); Chris@16: Chris@16: start_resolve_op(p.p); Chris@16: p.v = p.p = 0; Chris@16: } 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_RESOLVER_SERVICE_HPP