1//
2// detail/reactive_socket_recvmsg_op.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 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 BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
12#define BOOST_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 <boost/asio/detail/config.hpp>
19#include <boost/asio/detail/bind_handler.hpp>
20#include <boost/asio/detail/buffer_sequence_adapter.hpp>
21#include <boost/asio/detail/fenced_block.hpp>
22#include <boost/asio/detail/handler_alloc_helpers.hpp>
23#include <boost/asio/detail/handler_work.hpp>
24#include <boost/asio/detail/memory.hpp>
25#include <boost/asio/detail/reactor_op.hpp>
26#include <boost/asio/detail/socket_ops.hpp>
27#include <boost/asio/socket_base.hpp>
28
29#include <boost/asio/detail/push_options.hpp>
30
31namespace boost {
32namespace asio {
33namespace detail {
34
35template <typename MutableBufferSequence>
36class reactive_socket_recvmsg_op_base : public reactor_op
37{
38public:
39 reactive_socket_recvmsg_op_base(const boost::system::error_code& success_ec,
40 socket_type socket, const MutableBufferSequence& buffers,
41 socket_base::message_flags in_flags,
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 {
54 BOOST_ASIO_ASSUME(base != 0);
55 reactive_socket_recvmsg_op_base* o(
56 static_cast<reactive_socket_recvmsg_op_base*>(base));
57
58 buffer_sequence_adapter<boost::asio::mutable_buffer,
59 MutableBufferSequence> bufs(o->buffers_);
60
61 status result = socket_ops::non_blocking_recvmsg(s: o->socket_,
62 bufs: bufs.buffers(), count: bufs.count(),
63 in_flags: o->in_flags_, out_flags&: o->out_flags_,
64 ec&: o->ec_, bytes_transferred&: o->bytes_transferred_) ? done : not_done;
65
66 BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recvmsg",
67 o->ec_, o->bytes_transferred_));
68
69 return result;
70 }
71
72private:
73 socket_type socket_;
74 MutableBufferSequence buffers_;
75 socket_base::message_flags in_flags_;
76 socket_base::message_flags& out_flags_;
77};
78
79template <typename MutableBufferSequence, typename Handler, typename IoExecutor>
80class reactive_socket_recvmsg_op :
81 public reactive_socket_recvmsg_op_base<MutableBufferSequence>
82{
83public:
84 typedef Handler handler_type;
85 typedef IoExecutor io_executor_type;
86
87 BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op);
88
89 reactive_socket_recvmsg_op(const boost::system::error_code& success_ec,
90 socket_type socket, const MutableBufferSequence& buffers,
91 socket_base::message_flags in_flags,
92 socket_base::message_flags& out_flags, Handler& handler,
93 const IoExecutor& io_ex)
94 : reactive_socket_recvmsg_op_base<MutableBufferSequence>(
95 success_ec, socket, buffers, in_flags, out_flags,
96 &reactive_socket_recvmsg_op::do_complete),
97 handler_(static_cast<Handler&&>(handler)),
98 work_(handler_, io_ex)
99 {
100 }
101
102 static void do_complete(void* owner, operation* base,
103 const boost::system::error_code& /*ec*/,
104 std::size_t /*bytes_transferred*/)
105 {
106 // Take ownership of the handler object.
107 BOOST_ASIO_ASSUME(base != 0);
108 reactive_socket_recvmsg_op* o(
109 static_cast<reactive_socket_recvmsg_op*>(base));
110 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
111
112 BOOST_ASIO_HANDLER_COMPLETION((*o));
113
114 // Take ownership of the operation's outstanding work.
115 handler_work<Handler, IoExecutor> w(
116 static_cast<handler_work<Handler, IoExecutor>&&>(
117 o->work_));
118
119 BOOST_ASIO_ERROR_LOCATION(o->ec_);
120
121 // Make a copy of the handler so that the memory can be deallocated before
122 // the upcall is made. Even if we're not about to make an upcall, a
123 // sub-object of the handler may be the true owner of the memory associated
124 // with the handler. Consequently, a local copy of the handler is required
125 // to ensure that any owning sub-object remains valid until after we have
126 // deallocated the memory here.
127 detail::binder2<Handler, boost::system::error_code, std::size_t>
128 handler(o->handler_, o->ec_, o->bytes_transferred_);
129 p.h = boost::asio::detail::addressof(handler.handler_);
130 p.reset();
131
132 // Make the upcall if required.
133 if (owner)
134 {
135 fenced_block b(fenced_block::half);
136 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
137 w.complete(handler, handler.handler_);
138 BOOST_ASIO_HANDLER_INVOCATION_END;
139 }
140 }
141
142 static void do_immediate(operation* base, bool, const void* io_ex)
143 {
144 // Take ownership of the handler object.
145 BOOST_ASIO_ASSUME(base != 0);
146 reactive_socket_recvmsg_op* o(
147 static_cast<reactive_socket_recvmsg_op*>(base));
148 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
149
150 BOOST_ASIO_HANDLER_COMPLETION((*o));
151
152 // Take ownership of the operation's outstanding work.
153 immediate_handler_work<Handler, IoExecutor> w(
154 static_cast<handler_work<Handler, IoExecutor>&&>(
155 o->work_));
156
157 BOOST_ASIO_ERROR_LOCATION(o->ec_);
158
159 // Make a copy of the handler so that the memory can be deallocated before
160 // the upcall is made. Even if we're not about to make an upcall, a
161 // sub-object of the handler may be the true owner of the memory associated
162 // with the handler. Consequently, a local copy of the handler is required
163 // to ensure that any owning sub-object remains valid until after we have
164 // deallocated the memory here.
165 detail::binder2<Handler, boost::system::error_code, std::size_t>
166 handler(o->handler_, o->ec_, o->bytes_transferred_);
167 p.h = boost::asio::detail::addressof(handler.handler_);
168 p.reset();
169
170 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
171 w.complete(handler, handler.handler_, io_ex);
172 BOOST_ASIO_HANDLER_INVOCATION_END;
173 }
174
175private:
176 Handler handler_;
177 handler_work<Handler, IoExecutor> work_;
178};
179
180} // namespace detail
181} // namespace asio
182} // namespace boost
183
184#include <boost/asio/detail/pop_options.hpp>
185
186#endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
187

source code of boost/libs/asio/include/boost/asio/detail/reactive_socket_recvmsg_op.hpp