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