Clementine
reactive_socket_recvfrom_op.hpp
1 //
2 // detail/reactive_socket_recvfrom_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_RECVFROM_OP_HPP
12 #define ASIO_DETAIL_REACTIVE_SOCKET_RECVFROM_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 MutableBufferSequence, typename Endpoint>
36 {
37 public:
39  socket_type socket, int protocol_type,
40  const MutableBufferSequence& buffers, Endpoint& endpoint,
41  socket_base::message_flags flags, func_type complete_func)
42  : reactor_op(success_ec,
43  &reactive_socket_recvfrom_op_base::do_perform, complete_func),
44  socket_(socket),
45  protocol_type_(protocol_type),
46  buffers_(buffers),
47  sender_endpoint_(endpoint),
48  flags_(flags)
49  {
50  }
51 
52  static status do_perform(reactor_op* base)
53  {
55  static_cast<reactive_socket_recvfrom_op_base*>(base));
56 
58  MutableBufferSequence> bufs_type;
59 
60  std::size_t addr_len = o->sender_endpoint_.capacity();
61  status result;
62  if (bufs_type::is_single_buffer)
63  {
64  result = socket_ops::non_blocking_recvfrom1(
65  o->socket_, bufs_type::first(o->buffers_).data(),
66  bufs_type::first(o->buffers_).size(), o->flags_,
67  o->sender_endpoint_.data(), &addr_len,
68  o->ec_, o->bytes_transferred_) ? done : not_done;
69  }
70  else
71  {
72  bufs_type bufs(o->buffers_);
73  result = socket_ops::non_blocking_recvfrom(o->socket_,
74  bufs.buffers(), bufs.count(), o->flags_,
75  o->sender_endpoint_.data(), &addr_len,
76  o->ec_, o->bytes_transferred_) ? done : not_done;
77  }
78 
79  if (result && !o->ec_)
80  o->sender_endpoint_.resize(addr_len);
81 
82  ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recvfrom",
83  o->ec_, o->bytes_transferred_));
84 
85  return result;
86  }
87 
88 private:
89  socket_type socket_;
90  int protocol_type_;
91  MutableBufferSequence buffers_;
92  Endpoint& sender_endpoint_;
94 };
95 
96 template <typename MutableBufferSequence, typename Endpoint,
97  typename Handler, typename IoExecutor>
99  public reactive_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>
100 {
101 public:
102  ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvfrom_op);
103 
105  socket_type socket, int protocol_type,
106  const MutableBufferSequence& buffers, Endpoint& endpoint,
107  socket_base::message_flags flags, Handler& handler,
108  const IoExecutor& io_ex)
110  success_ec, socket, protocol_type, buffers, endpoint, flags,
111  &reactive_socket_recvfrom_op::do_complete),
112  handler_(ASIO_MOVE_CAST(Handler)(handler)),
113  work_(handler_, io_ex)
114  {
115  }
116 
117  static void do_complete(void* owner, operation* base,
118  const asio::error_code& /*ec*/,
119  std::size_t /*bytes_transferred*/)
120  {
121  // Take ownership of the handler object.
123  static_cast<reactive_socket_recvfrom_op*>(base));
124  ptr p = { asio::detail::addressof(o->handler_), o, o };
125 
126  ASIO_HANDLER_COMPLETION((*o));
127 
128  // Take ownership of the operation's outstanding work.
130  ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
131  o->work_));
132 
133  // Make a copy of the handler so that the memory can be deallocated before
134  // the upcall is made. Even if we're not about to make an upcall, a
135  // sub-object of the handler may be the true owner of the memory associated
136  // with the handler. Consequently, a local copy of the handler is required
137  // to ensure that any owning sub-object remains valid until after we have
138  // deallocated the memory here.
140  handler(o->handler_, o->ec_, o->bytes_transferred_);
141  p.h = asio::detail::addressof(handler.handler_);
142  p.reset();
143 
144  // Make the upcall if required.
145  if (owner)
146  {
147  fenced_block b(fenced_block::half);
148  ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
149  w.complete(handler, handler.handler_);
150  ASIO_HANDLER_INVOCATION_END;
151  }
152  }
153 
154 private:
155  Handler handler_;
157 };
158 
159 } // namespace detail
160 } // namespace asio
161 
162 #include "asio/detail/pop_options.hpp"
163 
164 #endif // ASIO_DETAIL_REACTIVE_SOCKET_RECVFROM_OP_HPP
int message_flags
Bitmask type for flags that can be passed to send and receive operations.
Definition: socket_base.hpp:53
Definition: reactive_socket_recvfrom_op.hpp:35
Definition: chrono.h:284
Definition: reactive_socket_recvfrom_op.hpp:98
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