Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:44

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