1 // 2 // detail/resolve_query_op.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com) 6 // 7 // Distributed under the Boost Software License, Version 1.0. (See accompanying 8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 9 // 10 11 #ifndef BOOST_ASIO_DETAIL_RESOLVE_QUERY_OP_HPP 12 #define BOOST_ASIO_DETAIL_RESOLVE_QUERY_OP_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include <boost/asio/detail/config.hpp> 19 #include <boost/asio/detail/bind_handler.hpp> 20 #include <boost/asio/detail/fenced_block.hpp> 21 #include <boost/asio/detail/handler_alloc_helpers.hpp> 22 #include <boost/asio/detail/handler_invoke_helpers.hpp> 23 #include <boost/asio/detail/handler_work.hpp> 24 #include <boost/asio/detail/memory.hpp> 25 #include <boost/asio/detail/resolve_op.hpp> 26 #include <boost/asio/detail/socket_ops.hpp> 27 #include <boost/asio/error.hpp> 28 #include <boost/asio/ip/basic_resolver_query.hpp> 29 #include <boost/asio/ip/basic_resolver_results.hpp> 30 31 #if defined(BOOST_ASIO_HAS_IOCP) 32 # include <boost/asio/detail/win_iocp_io_context.hpp> 33 #else // defined(BOOST_ASIO_HAS_IOCP) 34 # include <boost/asio/detail/scheduler.hpp> 35 #endif // defined(BOOST_ASIO_HAS_IOCP) 36 37 #include <boost/asio/detail/push_options.hpp> 38 39 namespace boost { 40 namespace asio { 41 namespace detail { 42 43 template <typename Protocol, typename Handler, typename IoExecutor> 44 class resolve_query_op : public resolve_op 45 { 46 public: 47 BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_query_op); 48 49 typedef boost::asio::ip::basic_resolver_query<Protocol> query_type; 50 typedef boost::asio::ip::basic_resolver_results<Protocol> results_type; 51 52 #if defined(BOOST_ASIO_HAS_IOCP) 53 typedef class win_iocp_io_context scheduler_impl; 54 #else 55 typedef class scheduler scheduler_impl; 56 #endif 57 resolve_query_op(socket_ops::weak_cancel_token_type cancel_token,const query_type & qry,scheduler_impl & sched,Handler & handler,const IoExecutor & io_ex)58 resolve_query_op(socket_ops::weak_cancel_token_type cancel_token, 59 const query_type& qry, scheduler_impl& sched, 60 Handler& handler, const IoExecutor& io_ex) 61 : resolve_op(&resolve_query_op::do_complete), 62 cancel_token_(cancel_token), 63 query_(qry), 64 scheduler_(sched), 65 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), 66 work_(handler_, io_ex), 67 addrinfo_(0) 68 { 69 } 70 ~resolve_query_op()71 ~resolve_query_op() 72 { 73 if (addrinfo_) 74 socket_ops::freeaddrinfo(addrinfo_); 75 } 76 do_complete(void * owner,operation * base,const boost::system::error_code &,std::size_t)77 static void do_complete(void* owner, operation* base, 78 const boost::system::error_code& /*ec*/, 79 std::size_t /*bytes_transferred*/) 80 { 81 // Take ownership of the operation object. 82 resolve_query_op* o(static_cast<resolve_query_op*>(base)); 83 ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; 84 85 if (owner && owner != &o->scheduler_) 86 { 87 // The operation is being run on the worker io_context. Time to perform 88 // the resolver operation. 89 90 // Perform the blocking host resolution operation. 91 socket_ops::background_getaddrinfo(o->cancel_token_, 92 o->query_.host_name().c_str(), o->query_.service_name().c_str(), 93 o->query_.hints(), &o->addrinfo_, o->ec_); 94 95 // Pass operation back to main io_context for completion. 96 o->scheduler_.post_deferred_completion(o); 97 p.v = p.p = 0; 98 } 99 else 100 { 101 // The operation has been returned to the main io_context. The completion 102 // handler is ready to be delivered. 103 104 BOOST_ASIO_HANDLER_COMPLETION((*o)); 105 106 // Take ownership of the operation's outstanding work. 107 handler_work<Handler, IoExecutor> w( 108 BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)( 109 o->work_)); 110 111 // Make a copy of the handler so that the memory can be deallocated 112 // before the upcall is made. Even if we're not about to make an upcall, 113 // a sub-object of the handler may be the true owner of the memory 114 // associated with the handler. Consequently, a local copy of the handler 115 // is required to ensure that any owning sub-object remains valid until 116 // after we have deallocated the memory here. 117 detail::binder2<Handler, boost::system::error_code, results_type> 118 handler(o->handler_, o->ec_, results_type()); 119 p.h = boost::asio::detail::addressof(handler.handler_); 120 if (o->addrinfo_) 121 { 122 handler.arg2_ = results_type::create(o->addrinfo_, 123 o->query_.host_name(), o->query_.service_name()); 124 } 125 p.reset(); 126 127 if (owner) 128 { 129 fenced_block b(fenced_block::half); 130 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "...")); 131 w.complete(handler, handler.handler_); 132 BOOST_ASIO_HANDLER_INVOCATION_END; 133 } 134 } 135 } 136 137 private: 138 socket_ops::weak_cancel_token_type cancel_token_; 139 query_type query_; 140 scheduler_impl& scheduler_; 141 Handler handler_; 142 handler_work<Handler, IoExecutor> work_; 143 boost::asio::detail::addrinfo_type* addrinfo_; 144 }; 145 146 } // namespace detail 147 } // namespace asio 148 } // namespace boost 149 150 #include <boost/asio/detail/pop_options.hpp> 151 152 #endif // BOOST_ASIO_DETAIL_RESOLVE_QUERY_OP_HPP 153