Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/resolve_endpoint_op.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_ENDPOINT_OP_HPP
0012 #define BOOST_ASIO_DETAIL_RESOLVER_ENDPOINT_OP_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 #include <boost/asio/detail/bind_handler.hpp>
0020 #include <boost/asio/detail/fenced_block.hpp>
0021 #include <boost/asio/detail/handler_alloc_helpers.hpp>
0022 #include <boost/asio/detail/handler_work.hpp>
0023 #include <boost/asio/detail/memory.hpp>
0024 #include <boost/asio/detail/resolve_op.hpp>
0025 #include <boost/asio/detail/socket_ops.hpp>
0026 #include <boost/asio/error.hpp>
0027 #include <boost/asio/ip/basic_resolver_results.hpp>
0028 
0029 #if defined(BOOST_ASIO_HAS_IOCP)
0030 # include <boost/asio/detail/win_iocp_io_context.hpp>
0031 #else // defined(BOOST_ASIO_HAS_IOCP)
0032 # include <boost/asio/detail/scheduler.hpp>
0033 #endif // defined(BOOST_ASIO_HAS_IOCP)
0034 
0035 #include <boost/asio/detail/push_options.hpp>
0036 
0037 namespace boost {
0038 namespace asio {
0039 namespace detail {
0040 
0041 template <typename Protocol, typename Handler, typename IoExecutor>
0042 class resolve_endpoint_op : public resolve_op
0043 {
0044 public:
0045   BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_endpoint_op);
0046 
0047   typedef typename Protocol::endpoint endpoint_type;
0048   typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
0049 
0050 #if defined(BOOST_ASIO_HAS_IOCP)
0051   typedef class win_iocp_io_context scheduler_impl;
0052 #else
0053   typedef class scheduler scheduler_impl;
0054 #endif
0055 
0056   resolve_endpoint_op(socket_ops::weak_cancel_token_type cancel_token,
0057       const endpoint_type& endpoint, scheduler_impl& sched,
0058       Handler& handler, const IoExecutor& io_ex)
0059     : resolve_op(&resolve_endpoint_op::do_complete),
0060       cancel_token_(cancel_token),
0061       endpoint_(endpoint),
0062       scheduler_(sched),
0063       handler_(static_cast<Handler&&>(handler)),
0064       work_(handler_, io_ex)
0065   {
0066   }
0067 
0068   static void do_complete(void* owner, operation* base,
0069       const boost::system::error_code& /*ec*/,
0070       std::size_t /*bytes_transferred*/)
0071   {
0072     // Take ownership of the operation object.
0073     BOOST_ASIO_ASSUME(base != 0);
0074     resolve_endpoint_op* o(static_cast<resolve_endpoint_op*>(base));
0075     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0076 
0077     if (owner && owner != &o->scheduler_)
0078     {
0079       // The operation is being run on the worker io_context. Time to perform
0080       // the resolver operation.
0081     
0082       // Perform the blocking endpoint resolution operation.
0083       char host_name[NI_MAXHOST] = "";
0084       char service_name[NI_MAXSERV] = "";
0085       socket_ops::background_getnameinfo(o->cancel_token_, o->endpoint_.data(),
0086           o->endpoint_.size(), host_name, NI_MAXHOST, service_name, NI_MAXSERV,
0087           o->endpoint_.protocol().type(), o->ec_);
0088       o->results_ = results_type::create(o->endpoint_, host_name, service_name);
0089 
0090       // Pass operation back to main io_context for completion.
0091       o->scheduler_.post_deferred_completion(o);
0092       p.v = p.p = 0;
0093     }
0094     else
0095     {
0096       // The operation has been returned to the main io_context. The completion
0097       // handler is ready to be delivered.
0098 
0099       BOOST_ASIO_HANDLER_COMPLETION((*o));
0100 
0101       // Take ownership of the operation's outstanding work.
0102       handler_work<Handler, IoExecutor> w(
0103           static_cast<handler_work<Handler, IoExecutor>&&>(
0104             o->work_));
0105 
0106       // Make a copy of the handler so that the memory can be deallocated
0107       // before the upcall is made. Even if we're not about to make an upcall,
0108       // a sub-object of the handler may be the true owner of the memory
0109       // associated with the handler. Consequently, a local copy of the handler
0110       // is required to ensure that any owning sub-object remains valid until
0111       // after we have deallocated the memory here.
0112       detail::binder2<Handler, boost::system::error_code, results_type>
0113         handler(o->handler_, o->ec_, o->results_);
0114       p.h = boost::asio::detail::addressof(handler.handler_);
0115       p.reset();
0116 
0117       if (owner)
0118       {
0119         fenced_block b(fenced_block::half);
0120         BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
0121         w.complete(handler, handler.handler_);
0122         BOOST_ASIO_HANDLER_INVOCATION_END;
0123       }
0124     }
0125   }
0126 
0127 private:
0128   socket_ops::weak_cancel_token_type cancel_token_;
0129   endpoint_type endpoint_;
0130   scheduler_impl& scheduler_;
0131   Handler handler_;
0132   handler_work<Handler, IoExecutor> work_;
0133   results_type results_;
0134 };
0135 
0136 } // namespace detail
0137 } // namespace asio
0138 } // namespace boost
0139 
0140 #include <boost/asio/detail/pop_options.hpp>
0141 
0142 #endif // BOOST_ASIO_DETAIL_RESOLVER_ENDPOINT_OP_HPP