Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-20 08:17:50

0001 //
0002 // detail/resolver_service.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
0012 #define BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
0021 
0022 #include <boost/asio/ip/basic_resolver_query.hpp>
0023 #include <boost/asio/ip/basic_resolver_results.hpp>
0024 #include <boost/asio/detail/memory.hpp>
0025 #include <boost/asio/detail/resolve_endpoint_op.hpp>
0026 #include <boost/asio/detail/resolve_query_op.hpp>
0027 #include <boost/asio/detail/resolver_service_base.hpp>
0028 
0029 #include <boost/asio/detail/push_options.hpp>
0030 
0031 namespace boost {
0032 namespace asio {
0033 namespace detail {
0034 
0035 template <typename Protocol>
0036 class resolver_service :
0037   public execution_context_service_base<resolver_service<Protocol>>,
0038   public resolver_service_base
0039 {
0040 public:
0041   // The implementation type of the resolver. A cancellation token is used to
0042   // indicate to the background thread that the operation has been cancelled.
0043   typedef socket_ops::shared_cancel_token_type implementation_type;
0044 
0045   // The endpoint type.
0046   typedef typename Protocol::endpoint endpoint_type;
0047 
0048   // The query type.
0049   typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
0050 
0051   // The results type.
0052   typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
0053 
0054   // Constructor.
0055   resolver_service(execution_context& context)
0056     : execution_context_service_base<resolver_service<Protocol>>(context),
0057       resolver_service_base(context)
0058   {
0059   }
0060 
0061   // Destroy all user-defined handler objects owned by the service.
0062   void shutdown()
0063   {
0064     this->base_shutdown();
0065   }
0066 
0067   // Perform any fork-related housekeeping.
0068   void notify_fork(execution_context::fork_event fork_ev)
0069   {
0070     this->base_notify_fork(fork_ev);
0071   }
0072 
0073   // Resolve a query to a list of entries.
0074   results_type resolve(implementation_type&, const query_type& qry,
0075       boost::system::error_code& ec)
0076   {
0077     boost::asio::detail::addrinfo_type* address_info = 0;
0078 
0079     socket_ops::getaddrinfo(qry.host_name().c_str(),
0080         qry.service_name().c_str(), qry.hints(), &address_info, ec);
0081     auto_addrinfo auto_address_info(address_info);
0082 
0083     BOOST_ASIO_ERROR_LOCATION(ec);
0084     return ec ? results_type() : results_type::create(
0085         address_info, qry.host_name(), qry.service_name());
0086   }
0087 
0088   // Asynchronously resolve a query to a list of entries.
0089   template <typename Handler, typename IoExecutor>
0090   void async_resolve(implementation_type& impl, const query_type& qry,
0091       Handler& handler, const IoExecutor& io_ex)
0092   {
0093     // Allocate and construct an operation to wrap the handler.
0094     typedef resolve_query_op<Protocol, Handler, IoExecutor> op;
0095     typename op::ptr p = { boost::asio::detail::addressof(handler),
0096       op::ptr::allocate(handler), 0 };
0097     p.p = new (p.v) op(impl, qry, scheduler_, handler, io_ex);
0098 
0099     BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
0100           *p.p, "resolver", &impl, 0, "async_resolve"));
0101 
0102     start_resolve_op(p.p);
0103     p.v = p.p = 0;
0104   }
0105 
0106   // Resolve an endpoint to a list of entries.
0107   results_type resolve(implementation_type&,
0108       const endpoint_type& endpoint, boost::system::error_code& ec)
0109   {
0110     char host_name[NI_MAXHOST];
0111     char service_name[NI_MAXSERV];
0112     socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
0113         host_name, NI_MAXHOST, service_name, NI_MAXSERV,
0114         endpoint.protocol().type(), ec);
0115 
0116     BOOST_ASIO_ERROR_LOCATION(ec);
0117     return ec ? results_type() : results_type::create(
0118         endpoint, host_name, service_name);
0119   }
0120 
0121   // Asynchronously resolve an endpoint to a list of entries.
0122   template <typename Handler, typename IoExecutor>
0123   void async_resolve(implementation_type& impl, const endpoint_type& endpoint,
0124       Handler& handler, const IoExecutor& io_ex)
0125   {
0126     // Allocate and construct an operation to wrap the handler.
0127     typedef resolve_endpoint_op<Protocol, Handler, IoExecutor> op;
0128     typename op::ptr p = { boost::asio::detail::addressof(handler),
0129       op::ptr::allocate(handler), 0 };
0130     p.p = new (p.v) op(impl, endpoint, scheduler_, handler, io_ex);
0131 
0132     BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
0133           *p.p, "resolver", &impl, 0, "async_resolve"));
0134 
0135     start_resolve_op(p.p);
0136     p.v = p.p = 0;
0137   }
0138 };
0139 
0140 } // namespace detail
0141 } // namespace asio
0142 } // namespace boost
0143 
0144 #include <boost/asio/detail/pop_options.hpp>
0145 
0146 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
0147 
0148 #endif // BOOST_ASIO_DETAIL_RESOLVER_SERVICE_HPP