]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/resolve_endpoint_op.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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> | |
7c673cae FG |
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> | |
20effc67 | 23 | #include <boost/asio/detail/handler_work.hpp> |
b32b8144 FG |
24 | #include <boost/asio/detail/memory.hpp> |
25 | #include <boost/asio/detail/resolve_op.hpp> | |
7c673cae | 26 | #include <boost/asio/detail/socket_ops.hpp> |
20effc67 TL |
27 | #include <boost/asio/error.hpp> |
28 | #include <boost/asio/ip/basic_resolver_results.hpp> | |
7c673cae | 29 | |
92f5a8d4 TL |
30 | #if defined(BOOST_ASIO_HAS_IOCP) |
31 | # include <boost/asio/detail/win_iocp_io_context.hpp> | |
32 | #else // defined(BOOST_ASIO_HAS_IOCP) | |
33 | # include <boost/asio/detail/scheduler.hpp> | |
34 | #endif // defined(BOOST_ASIO_HAS_IOCP) | |
35 | ||
7c673cae FG |
36 | #include <boost/asio/detail/push_options.hpp> |
37 | ||
38 | namespace boost { | |
39 | namespace asio { | |
40 | namespace detail { | |
41 | ||
92f5a8d4 | 42 | template <typename Protocol, typename Handler, typename IoExecutor> |
b32b8144 | 43 | class resolve_endpoint_op : public resolve_op |
7c673cae FG |
44 | { |
45 | public: | |
46 | BOOST_ASIO_DEFINE_HANDLER_PTR(resolve_endpoint_op); | |
47 | ||
48 | typedef typename Protocol::endpoint endpoint_type; | |
b32b8144 | 49 | typedef boost::asio::ip::basic_resolver_results<Protocol> results_type; |
7c673cae | 50 | |
92f5a8d4 TL |
51 | #if defined(BOOST_ASIO_HAS_IOCP) |
52 | typedef class win_iocp_io_context scheduler_impl; | |
53 | #else | |
54 | typedef class scheduler scheduler_impl; | |
55 | #endif | |
56 | ||
7c673cae | 57 | resolve_endpoint_op(socket_ops::weak_cancel_token_type cancel_token, |
92f5a8d4 TL |
58 | const endpoint_type& endpoint, scheduler_impl& sched, |
59 | Handler& handler, const IoExecutor& io_ex) | |
b32b8144 | 60 | : resolve_op(&resolve_endpoint_op::do_complete), |
7c673cae FG |
61 | cancel_token_(cancel_token), |
62 | endpoint_(endpoint), | |
92f5a8d4 TL |
63 | scheduler_(sched), |
64 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), | |
20effc67 | 65 | work_(handler_, io_ex) |
7c673cae FG |
66 | { |
67 | } | |
68 | ||
b32b8144 | 69 | static void do_complete(void* owner, operation* base, |
7c673cae FG |
70 | const boost::system::error_code& /*ec*/, |
71 | std::size_t /*bytes_transferred*/) | |
72 | { | |
73 | // Take ownership of the operation object. | |
74 | resolve_endpoint_op* o(static_cast<resolve_endpoint_op*>(base)); | |
75 | ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; | |
76 | ||
92f5a8d4 | 77 | if (owner && owner != &o->scheduler_) |
7c673cae | 78 | { |
b32b8144 | 79 | // The operation is being run on the worker io_context. Time to perform |
7c673cae FG |
80 | // the resolver operation. |
81 | ||
82 | // Perform the blocking endpoint resolution operation. | |
1e59de90 TL |
83 | char host_name[NI_MAXHOST] = ""; |
84 | char service_name[NI_MAXSERV] = ""; | |
7c673cae FG |
85 | socket_ops::background_getnameinfo(o->cancel_token_, o->endpoint_.data(), |
86 | o->endpoint_.size(), host_name, NI_MAXHOST, service_name, NI_MAXSERV, | |
87 | o->endpoint_.protocol().type(), o->ec_); | |
b32b8144 | 88 | o->results_ = results_type::create(o->endpoint_, host_name, service_name); |
7c673cae | 89 | |
b32b8144 | 90 | // Pass operation back to main io_context for completion. |
92f5a8d4 | 91 | o->scheduler_.post_deferred_completion(o); |
7c673cae FG |
92 | p.v = p.p = 0; |
93 | } | |
94 | else | |
95 | { | |
b32b8144 | 96 | // The operation has been returned to the main io_context. The completion |
7c673cae FG |
97 | // handler is ready to be delivered. |
98 | ||
b32b8144 | 99 | BOOST_ASIO_HANDLER_COMPLETION((*o)); |
7c673cae | 100 | |
20effc67 TL |
101 | // Take ownership of the operation's outstanding work. |
102 | handler_work<Handler, IoExecutor> w( | |
103 | BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)( | |
104 | o->work_)); | |
105 | ||
7c673cae FG |
106 | // Make a copy of the handler so that the memory can be deallocated |
107 | // before the upcall is made. Even if we're not about to make an upcall, | |
108 | // a sub-object of the handler may be the true owner of the memory | |
109 | // associated with the handler. Consequently, a local copy of the handler | |
110 | // is required to ensure that any owning sub-object remains valid until | |
111 | // after we have deallocated the memory here. | |
b32b8144 FG |
112 | detail::binder2<Handler, boost::system::error_code, results_type> |
113 | handler(o->handler_, o->ec_, o->results_); | |
7c673cae FG |
114 | p.h = boost::asio::detail::addressof(handler.handler_); |
115 | p.reset(); | |
116 | ||
117 | if (owner) | |
118 | { | |
119 | fenced_block b(fenced_block::half); | |
120 | BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "...")); | |
b32b8144 | 121 | w.complete(handler, handler.handler_); |
7c673cae FG |
122 | BOOST_ASIO_HANDLER_INVOCATION_END; |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | private: | |
128 | socket_ops::weak_cancel_token_type cancel_token_; | |
129 | endpoint_type endpoint_; | |
92f5a8d4 | 130 | scheduler_impl& scheduler_; |
7c673cae | 131 | Handler handler_; |
20effc67 | 132 | handler_work<Handler, IoExecutor> work_; |
b32b8144 | 133 | results_type results_; |
7c673cae FG |
134 | }; |
135 | ||
136 | } // namespace detail | |
137 | } // namespace asio | |
138 | } // namespace boost | |
139 | ||
140 | #include <boost/asio/detail/pop_options.hpp> | |
141 | ||
142 | #endif // BOOST_ASIO_DETAIL_RESOLVER_ENDPOINT_OP_HPP |