Clementine
reactive_socket_send_op.hpp
1 //
2 // detail/reactive_socket_send_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_SEND_OP_HPP
12 #define ASIO_DETAIL_REACTIVE_SOCKET_SEND_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 
29 #include "asio/detail/push_options.hpp"
30 
31 namespace asio {
32 namespace detail {
33 
34 template <typename ConstBufferSequence>
36 {
37 public:
39  socket_type socket, socket_ops::state_type state,
40  const ConstBufferSequence& buffers,
41  socket_base::message_flags flags, func_type complete_func)
42  : reactor_op(success_ec,
43  &reactive_socket_send_op_base::do_perform, complete_func),
44  socket_(socket),
45  state_(state),
46  buffers_(buffers),
47  flags_(flags)
48  {
49  }
50 
51  static status do_perform(reactor_op* base)
52  {
54  static_cast<reactive_socket_send_op_base*>(base));
55 
57  ConstBufferSequence> bufs_type;
58 
59  status result;
60  if (bufs_type::is_single_buffer)
61  {
62  result = socket_ops::non_blocking_send1(o->socket_,
63  bufs_type::first(o->buffers_).data(),
64  bufs_type::first(o->buffers_).size(), o->flags_,
65  o->ec_, o->bytes_transferred_) ? done : not_done;
66 
67  if (result == done)
68  if ((o->state_ & socket_ops::stream_oriented) != 0)
69  if (o->bytes_transferred_ < bufs_type::first(o->buffers_).size())
70  result = done_and_exhausted;
71  }
72  else
73  {
74  bufs_type bufs(o->buffers_);
75  result = socket_ops::non_blocking_send(o->socket_,
76  bufs.buffers(), bufs.count(), o->flags_,
77  o->ec_, o->bytes_transferred_) ? done : not_done;
78 
79  if (result == done)
80  if ((o->state_ & socket_ops::stream_oriented) != 0)
81  if (o->bytes_transferred_ < bufs.total_size())
82  result = done_and_exhausted;
83  }
84 
85  ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_send",
86  o->ec_, o->bytes_transferred_));
87 
88  return result;
89  }
90 
91 private:
92  socket_type socket_;
93  socket_ops::state_type state_;
94  ConstBufferSequence buffers_;
96 };
97 
98 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
100  public reactive_socket_send_op_base<ConstBufferSequence>
101 {
102 public:
103  ASIO_DEFINE_HANDLER_PTR(reactive_socket_send_op);
104 
105  reactive_socket_send_op(const asio::error_code& success_ec,
106  socket_type socket, socket_ops::state_type state,
107  const ConstBufferSequence& buffers, socket_base::message_flags flags,
108  Handler& handler, const IoExecutor& io_ex)
110  state, buffers, flags, &reactive_socket_send_op::do_complete),
111  handler_(ASIO_MOVE_CAST(Handler)(handler)),
112  work_(handler_, io_ex)
113  {
114  }
115 
116  static void do_complete(void* owner, operation* base,
117  const asio::error_code& /*ec*/,
118  std::size_t /*bytes_transferred*/)
119  {
120  // Take ownership of the handler object.
121  reactive_socket_send_op* o(static_cast<reactive_socket_send_op*>(base));
122  ptr p = { asio::detail::addressof(o->handler_), o, o };
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_, o->bytes_transferred_);
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_, handler.arg2_));
147  w.complete(handler, handler.handler_);
148  ASIO_HANDLER_INVOCATION_END;
149  }
150  }
151 
152 private:
153  Handler handler_;
155 };
156 
157 } // namespace detail
158 } // namespace asio
159 
160 #include "asio/detail/pop_options.hpp"
161 
162 #endif // ASIO_DETAIL_REACTIVE_SOCKET_SEND_OP_HPP
int message_flags
Bitmask type for flags that can be passed to send and receive operations.
Definition: socket_base.hpp:53
Holds a buffer that cannot be modified.
Definition: buffer.hpp:226
Definition: reactive_socket_send_op.hpp:99
Definition: reactive_socket_send_op.hpp:35
Definition: chrono.h:284
Definition: buffer_sequence_adapter.hpp:103
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