Clementine
reactive_socket_recvmsg_op.hpp
1 //
2 // detail/reactive_socket_recvmsg_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_RECVMSG_OP_HPP
12 #define ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_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/buffer_sequence_adapter.hpp"
21 #include "asio/detail/fenced_block.hpp"
22 #include "asio/detail/handler_alloc_helpers.hpp"
23 #include "asio/detail/handler_invoke_helpers.hpp"
24 #include "asio/detail/handler_work.hpp"
25 #include "asio/detail/memory.hpp"
26 #include "asio/detail/reactor_op.hpp"
27 #include "asio/detail/socket_ops.hpp"
28 #include "asio/socket_base.hpp"
29 
30 #include "asio/detail/push_options.hpp"
31 
32 namespace asio {
33 namespace detail {
34 
35 template <typename MutableBufferSequence>
37 {
38 public:
40  socket_type socket, const MutableBufferSequence& buffers,
42  socket_base::message_flags& out_flags, func_type complete_func)
43  : reactor_op(success_ec,
44  &reactive_socket_recvmsg_op_base::do_perform, complete_func),
45  socket_(socket),
46  buffers_(buffers),
47  in_flags_(in_flags),
48  out_flags_(out_flags)
49  {
50  }
51 
52  static status do_perform(reactor_op* base)
53  {
55  static_cast<reactive_socket_recvmsg_op_base*>(base));
56 
58  MutableBufferSequence> bufs(o->buffers_);
59 
60  status result = socket_ops::non_blocking_recvmsg(o->socket_,
61  bufs.buffers(), bufs.count(),
62  o->in_flags_, o->out_flags_,
63  o->ec_, o->bytes_transferred_) ? done : not_done;
64 
65  ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recvmsg",
66  o->ec_, o->bytes_transferred_));
67 
68  return result;
69  }
70 
71 private:
72  socket_type socket_;
73  MutableBufferSequence buffers_;
75  socket_base::message_flags& out_flags_;
76 };
77 
78 template <typename MutableBufferSequence, typename Handler, typename IoExecutor>
80  public reactive_socket_recvmsg_op_base<MutableBufferSequence>
81 {
82 public:
83  ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op);
84 
86  socket_type socket, const MutableBufferSequence& buffers,
88  socket_base::message_flags& out_flags, Handler& handler,
89  const IoExecutor& io_ex)
91  success_ec, socket, buffers, in_flags, out_flags,
92  &reactive_socket_recvmsg_op::do_complete),
93  handler_(ASIO_MOVE_CAST(Handler)(handler)),
94  work_(handler_, io_ex)
95  {
96  }
97 
98  static void do_complete(void* owner, operation* base,
99  const asio::error_code& /*ec*/,
100  std::size_t /*bytes_transferred*/)
101  {
102  // Take ownership of the handler object.
104  static_cast<reactive_socket_recvmsg_op*>(base));
105  ptr p = { asio::detail::addressof(o->handler_), o, o };
106 
107  ASIO_HANDLER_COMPLETION((*o));
108 
109  // Take ownership of the operation's outstanding work.
111  ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
112  o->work_));
113 
114  // Make a copy of the handler so that the memory can be deallocated before
115  // the upcall is made. Even if we're not about to make an upcall, a
116  // sub-object of the handler may be the true owner of the memory associated
117  // with the handler. Consequently, a local copy of the handler is required
118  // to ensure that any owning sub-object remains valid until after we have
119  // deallocated the memory here.
121  handler(o->handler_, o->ec_, o->bytes_transferred_);
122  p.h = asio::detail::addressof(handler.handler_);
123  p.reset();
124 
125  // Make the upcall if required.
126  if (owner)
127  {
128  fenced_block b(fenced_block::half);
129  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
130  w.complete(handler, handler.handler_);
131  ASIO_HANDLER_INVOCATION_END;
132  }
133  }
134 
135 private:
136  Handler handler_;
138 };
139 
140 } // namespace detail
141 } // namespace asio
142 
143 #include "asio/detail/pop_options.hpp"
144 
145 #endif // ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
int message_flags
Bitmask type for flags that can be passed to send and receive operations.
Definition: socket_base.hpp:53
Definition: chrono.h:284
Definition: reactive_socket_recvmsg_op.hpp:79
Definition: reactive_socket_recvmsg_op.hpp:36
Definition: buffer_sequence_adapter.hpp:103
Holds a buffer that can be modified.
Definition: buffer.hpp:92
Definition: null_fenced_block.hpp:25
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: reactor_op.hpp:26
Definition: any_io_executor.hpp:28
Definition: bind_handler.hpp:144