]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/resolve_query_op.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / resolve_query_op.hpp
1 //
2 // detail/resolve_query_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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/error.hpp>
20 #include <boost/asio/io_context.hpp>
21 #include <boost/asio/ip/basic_resolver_query.hpp>
22 #include <boost/asio/ip/basic_resolver_results.hpp>
23 #include <boost/asio/detail/bind_handler.hpp>
24 #include <boost/asio/detail/fenced_block.hpp>
25 #include <boost/asio/detail/handler_alloc_helpers.hpp>
26 #include <boost/asio/detail/handler_invoke_helpers.hpp>
27 #include <boost/asio/detail/memory.hpp>
28 #include <boost/asio/detail/resolve_op.hpp>
29 #include <boost/asio/detail/socket_ops.hpp>
30
31 #include <boost/asio/detail/push_options.hpp>
32
33 namespace boost {
34 namespace asio {
35 namespace detail {
36
37 template <typename Protocol, typename Handler>
38 class resolve_query_op : public resolve_op
39 {
40 public:
41 BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_query_op);
42
43 typedef boost::asio::ip::basic_resolver_query<Protocol> query_type;
44 typedef boost::asio::ip::basic_resolver_results<Protocol> results_type;
45
46 resolve_query_op(socket_ops::weak_cancel_token_type cancel_token,
47 const query_type& query, io_context_impl& ioc, Handler& handler)
48 : resolve_op(&resolve_query_op::do_complete),
49 cancel_token_(cancel_token),
50 query_(query),
51 io_context_impl_(ioc),
52 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
53 addrinfo_(0)
54 {
55 handler_work<Handler>::start(handler_);
56 }
57
58 ~resolve_query_op()
59 {
60 if (addrinfo_)
61 socket_ops::freeaddrinfo(addrinfo_);
62 }
63
64 static void do_complete(void* owner, operation* base,
65 const boost::system::error_code& /*ec*/,
66 std::size_t /*bytes_transferred*/)
67 {
68 // Take ownership of the operation object.
69 resolve_query_op* o(static_cast<resolve_query_op*>(base));
70 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
71
72 if (owner && owner != &o->io_context_impl_)
73 {
74 // The operation is being run on the worker io_context. Time to perform
75 // the resolver operation.
76
77 // Perform the blocking host resolution operation.
78 socket_ops::background_getaddrinfo(o->cancel_token_,
79 o->query_.host_name().c_str(), o->query_.service_name().c_str(),
80 o->query_.hints(), &o->addrinfo_, o->ec_);
81
82 // Pass operation back to main io_context for completion.
83 o->io_context_impl_.post_deferred_completion(o);
84 p.v = p.p = 0;
85 }
86 else
87 {
88 // The operation has been returned to the main io_context. The completion
89 // handler is ready to be delivered.
90
91 // Take ownership of the operation's outstanding work.
92 handler_work<Handler> w(o->handler_);
93
94 BOOST_ASIO_HANDLER_COMPLETION((*o));
95
96 // Make a copy of the handler so that the memory can be deallocated
97 // before the upcall is made. Even if we're not about to make an upcall,
98 // a sub-object of the handler may be the true owner of the memory
99 // associated with the handler. Consequently, a local copy of the handler
100 // is required to ensure that any owning sub-object remains valid until
101 // after we have deallocated the memory here.
102 detail::binder2<Handler, boost::system::error_code, results_type>
103 handler(o->handler_, o->ec_, results_type());
104 p.h = boost::asio::detail::addressof(handler.handler_);
105 if (o->addrinfo_)
106 {
107 handler.arg2_ = results_type::create(o->addrinfo_,
108 o->query_.host_name(), o->query_.service_name());
109 }
110 p.reset();
111
112 if (owner)
113 {
114 fenced_block b(fenced_block::half);
115 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
116 w.complete(handler, handler.handler_);
117 BOOST_ASIO_HANDLER_INVOCATION_END;
118 }
119 }
120 }
121
122 private:
123 socket_ops::weak_cancel_token_type cancel_token_;
124 query_type query_;
125 io_context_impl& io_context_impl_;
126 Handler handler_;
127 boost::asio::detail::addrinfo_type* addrinfo_;
128 };
129
130 } // namespace detail
131 } // namespace asio
132 } // namespace boost
133
134 #include <boost/asio/detail/pop_options.hpp>
135
136 #endif // BOOST_ASIO_DETAIL_RESOLVE_QUERY_OP_HPP