Clementine
resolve_query_op.hpp
1 //
2 // detail/resolve_query_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 ASIO_DETAIL_RESOLVE_QUERY_OP_HPP
12 #define 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 "asio/detail/config.hpp"
19 #include "asio/detail/bind_handler.hpp"
20 #include "asio/detail/fenced_block.hpp"
21 #include "asio/detail/handler_alloc_helpers.hpp"
22 #include "asio/detail/handler_invoke_helpers.hpp"
23 #include "asio/detail/handler_work.hpp"
24 #include "asio/detail/memory.hpp"
25 #include "asio/detail/resolve_op.hpp"
26 #include "asio/detail/socket_ops.hpp"
27 #include "asio/error.hpp"
28 #include "asio/ip/basic_resolver_query.hpp"
29 #include "asio/ip/basic_resolver_results.hpp"
30 
31 #if defined(ASIO_HAS_IOCP)
32 # include "asio/detail/win_iocp_io_context.hpp"
33 #else // defined(ASIO_HAS_IOCP)
34 # include "asio/detail/scheduler.hpp"
35 #endif // defined(ASIO_HAS_IOCP)
36 
37 #include "asio/detail/push_options.hpp"
38 
39 namespace asio {
40 namespace detail {
41 
42 template <typename Protocol, typename Handler, typename IoExecutor>
44 {
45 public:
46  ASIO_DEFINE_HANDLER_PTR(resolve_query_op);
47 
50 
51 #if defined(ASIO_HAS_IOCP)
52  typedef class win_iocp_io_context scheduler_impl;
53 #else
54  typedef class scheduler scheduler_impl;
55 #endif
56 
57  resolve_query_op(socket_ops::weak_cancel_token_type cancel_token,
58  const query_type& qry, scheduler_impl& sched,
59  Handler& handler, const IoExecutor& io_ex)
60  : resolve_op(&resolve_query_op::do_complete),
61  cancel_token_(cancel_token),
62  query_(qry),
63  scheduler_(sched),
64  handler_(ASIO_MOVE_CAST(Handler)(handler)),
65  work_(handler_, io_ex),
66  addrinfo_(0)
67  {
68  }
69 
70  ~resolve_query_op()
71  {
72  if (addrinfo_)
73  socket_ops::freeaddrinfo(addrinfo_);
74  }
75 
76  static void do_complete(void* owner, operation* base,
77  const asio::error_code& /*ec*/,
78  std::size_t /*bytes_transferred*/)
79  {
80  // Take ownership of the operation object.
81  resolve_query_op* o(static_cast<resolve_query_op*>(base));
82  ptr p = { asio::detail::addressof(o->handler_), o, o };
83 
84  if (owner && owner != &o->scheduler_)
85  {
86  // The operation is being run on the worker io_context. Time to perform
87  // the resolver operation.
88 
89  // Perform the blocking host resolution operation.
90  socket_ops::background_getaddrinfo(o->cancel_token_,
91  o->query_.host_name().c_str(), o->query_.service_name().c_str(),
92  o->query_.hints(), &o->addrinfo_, o->ec_);
93 
94  // Pass operation back to main io_context for completion.
95  o->scheduler_.post_deferred_completion(o);
96  p.v = p.p = 0;
97  }
98  else
99  {
100  // The operation has been returned to the main io_context. The completion
101  // handler is ready to be delivered.
102 
103  ASIO_HANDLER_COMPLETION((*o));
104 
105  // Take ownership of the operation's outstanding work.
107  ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
108  o->work_));
109 
110  // Make a copy of the handler so that the memory can be deallocated
111  // before the upcall is made. Even if we're not about to make an upcall,
112  // a sub-object of the handler may be the true owner of the memory
113  // associated with the handler. Consequently, a local copy of the handler
114  // is required to ensure that any owning sub-object remains valid until
115  // after we have deallocated the memory here.
117  handler(o->handler_, o->ec_, results_type());
118  p.h = asio::detail::addressof(handler.handler_);
119  if (o->addrinfo_)
120  {
121  handler.arg2_ = results_type::create(o->addrinfo_,
122  o->query_.host_name(), o->query_.service_name());
123  }
124  p.reset();
125 
126  if (owner)
127  {
128  fenced_block b(fenced_block::half);
129  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
130  w.complete(handler, handler.handler_);
131  ASIO_HANDLER_INVOCATION_END;
132  }
133  }
134  }
135 
136 private:
137  socket_ops::weak_cancel_token_type cancel_token_;
138  query_type query_;
139  scheduler_impl& scheduler_;
140  Handler handler_;
142  asio::detail::addrinfo_type* addrinfo_;
143 };
144 
145 } // namespace detail
146 } // namespace asio
147 
148 #include "asio/detail/pop_options.hpp"
149 
150 #endif // ASIO_DETAIL_RESOLVE_QUERY_OP_HPP
Definition: resolve_op.hpp:27
Definition: resolve_query_op.hpp:43
Definition: chrono.h:284
Definition: null_fenced_block.hpp:25
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: any_io_executor.hpp:28
Definition: bind_handler.hpp:144
Definition: scheduler.hpp:38