Clementine
reactive_socket_accept_op.hpp
1 //
2 // detail/reactive_socket_accept_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_REACTIVE_SOCKET_ACCEPT_OP_HPP
12 #define ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_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/reactor_op.hpp"
26 #include "asio/detail/socket_holder.hpp"
27 #include "asio/detail/socket_ops.hpp"
28 
29 #include "asio/detail/push_options.hpp"
30 
31 namespace asio {
32 namespace detail {
33 
34 template <typename Socket, typename Protocol>
36 {
37 public:
39  socket_type socket, socket_ops::state_type state, Socket& peer,
40  const Protocol& protocol, typename Protocol::endpoint* peer_endpoint,
41  func_type complete_func)
42  : reactor_op(success_ec,
43  &reactive_socket_accept_op_base::do_perform, complete_func),
44  socket_(socket),
45  state_(state),
46  peer_(peer),
47  protocol_(protocol),
48  peer_endpoint_(peer_endpoint),
49  addrlen_(peer_endpoint ? peer_endpoint->capacity() : 0)
50  {
51  }
52 
53  static status do_perform(reactor_op* base)
54  {
56  static_cast<reactive_socket_accept_op_base*>(base));
57 
58  socket_type new_socket = invalid_socket;
59  status result = socket_ops::non_blocking_accept(o->socket_,
60  o->state_, o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
61  o->peer_endpoint_ ? &o->addrlen_ : 0, o->ec_, new_socket)
62  ? done : not_done;
63  o->new_socket_.reset(new_socket);
64 
65  ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_accept", o->ec_));
66 
67  return result;
68  }
69 
70  void do_assign()
71  {
72  if (new_socket_.get() != invalid_socket)
73  {
74  if (peer_endpoint_)
75  peer_endpoint_->resize(addrlen_);
76  peer_.assign(protocol_, new_socket_.get(), ec_);
77  if (!ec_)
78  new_socket_.release();
79  }
80  }
81 
82 private:
83  socket_type socket_;
84  socket_ops::state_type state_;
85  socket_holder new_socket_;
86  Socket& peer_;
87  Protocol protocol_;
88  typename Protocol::endpoint* peer_endpoint_;
89  std::size_t addrlen_;
90 };
91 
92 template <typename Socket, typename Protocol,
93  typename Handler, typename IoExecutor>
95  public reactive_socket_accept_op_base<Socket, Protocol>
96 {
97 public:
98  ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
99 
101  socket_type socket, socket_ops::state_type state, Socket& peer,
102  const Protocol& protocol, typename Protocol::endpoint* peer_endpoint,
103  Handler& handler, const IoExecutor& io_ex)
105  success_ec, socket, state, peer, protocol, peer_endpoint,
106  &reactive_socket_accept_op::do_complete),
107  handler_(ASIO_MOVE_CAST(Handler)(handler)),
108  work_(handler_, io_ex)
109  {
110  }
111 
112  static void do_complete(void* owner, operation* base,
113  const asio::error_code& /*ec*/,
114  std::size_t /*bytes_transferred*/)
115  {
116  // Take ownership of the handler object.
117  reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
118  ptr p = { asio::detail::addressof(o->handler_), o, o };
119 
120  // On success, assign new connection to peer socket object.
121  if (owner)
122  o->do_assign();
123 
124  ASIO_HANDLER_COMPLETION((*o));
125 
126  // Take ownership of the operation's outstanding work.
128  ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
129  o->work_));
130 
131  // Make a copy of the handler so that the memory can be deallocated before
132  // the upcall is made. Even if we're not about to make an upcall, a
133  // sub-object of the handler may be the true owner of the memory associated
134  // with the handler. Consequently, a local copy of the handler is required
135  // to ensure that any owning sub-object remains valid until after we have
136  // deallocated the memory here.
138  handler(o->handler_, o->ec_);
139  p.h = asio::detail::addressof(handler.handler_);
140  p.reset();
141 
142  // Make the upcall if required.
143  if (owner)
144  {
145  fenced_block b(fenced_block::half);
146  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
147  w.complete(handler, handler.handler_);
148  ASIO_HANDLER_INVOCATION_END;
149  }
150  }
151 
152 private:
153  Handler handler_;
155 };
156 
157 #if defined(ASIO_HAS_MOVE)
158 
159 template <typename Protocol, typename PeerIoExecutor,
160  typename Handler, typename IoExecutor>
161 class reactive_socket_move_accept_op :
162  private Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
164  typename Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
165  Protocol>
166 {
167 public:
168  ASIO_DEFINE_HANDLER_PTR(reactive_socket_move_accept_op);
169 
170  reactive_socket_move_accept_op(const asio::error_code& success_ec,
171  const PeerIoExecutor& peer_io_ex, socket_type socket,
172  socket_ops::state_type state, const Protocol& protocol,
173  typename Protocol::endpoint* peer_endpoint, Handler& handler,
174  const IoExecutor& io_ex)
175  : peer_socket_type(peer_io_ex),
177  success_ec, socket, state, *this, protocol, peer_endpoint,
178  &reactive_socket_move_accept_op::do_complete),
179  handler_(ASIO_MOVE_CAST(Handler)(handler)),
180  work_(handler_, io_ex)
181  {
182  }
183 
184  static void do_complete(void* owner, operation* base,
185  const asio::error_code& /*ec*/,
186  std::size_t /*bytes_transferred*/)
187  {
188  // Take ownership of the handler object.
189  reactive_socket_move_accept_op* o(
190  static_cast<reactive_socket_move_accept_op*>(base));
191  ptr p = { asio::detail::addressof(o->handler_), o, o };
192 
193  // On success, assign new connection to peer socket object.
194  if (owner)
195  o->do_assign();
196 
197  ASIO_HANDLER_COMPLETION((*o));
198 
199  // Take ownership of the operation's outstanding work.
201  ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
202  o->work_));
203 
204  // Make a copy of the handler so that the memory can be deallocated before
205  // the upcall is made. Even if we're not about to make an upcall, a
206  // sub-object of the handler may be the true owner of the memory associated
207  // with the handler. Consequently, a local copy of the handler is required
208  // to ensure that any owning sub-object remains valid until after we have
209  // deallocated the memory here.
210  detail::move_binder2<Handler,
211  asio::error_code, peer_socket_type>
212  handler(0, ASIO_MOVE_CAST(Handler)(o->handler_), o->ec_,
213  ASIO_MOVE_CAST(peer_socket_type)(*o));
214  p.h = asio::detail::addressof(handler.handler_);
215  p.reset();
216 
217  // Make the upcall if required.
218  if (owner)
219  {
220  fenced_block b(fenced_block::half);
221  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
222  w.complete(handler, handler.handler_);
223  ASIO_HANDLER_INVOCATION_END;
224  }
225  }
226 
227 private:
228  typedef typename Protocol::socket::template
229  rebind_executor<PeerIoExecutor>::other peer_socket_type;
230 
231  Handler handler_;
233 };
234 
235 #endif // defined(ASIO_HAS_MOVE)
236 
237 } // namespace detail
238 } // namespace asio
239 
240 #include "asio/detail/pop_options.hpp"
241 
242 #endif // ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP
Definition: bind_handler.hpp:32
Definition: chrono.h:284
Definition: null_fenced_block.hpp:25
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: reactive_socket_accept_op.hpp:35
Definition: socket_holder.hpp:28
Definition: reactive_socket_accept_op.hpp:94
Definition: reactor_op.hpp:26
Definition: any_io_executor.hpp:28