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