2 // detail/reactive_socket_recvmsg_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
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/memory.hpp>
23 #include <boost/asio/detail/reactor_op.hpp>
24 #include <boost/asio/detail/socket_ops.hpp>
25 #include <boost/asio/socket_base.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 template <typename MutableBufferSequence>
34 class reactive_socket_recvmsg_op_base : public reactor_op
37 reactive_socket_recvmsg_op_base(socket_type socket,
38 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
39 socket_base::message_flags& out_flags, func_type complete_func)
40 : reactor_op(&reactive_socket_recvmsg_op_base::do_perform, complete_func),
48 static status do_perform(reactor_op* base)
50 reactive_socket_recvmsg_op_base* o(
51 static_cast<reactive_socket_recvmsg_op_base*>(base));
53 buffer_sequence_adapter<boost::asio::mutable_buffer,
54 MutableBufferSequence> bufs(o->buffers_);
56 status result = socket_ops::non_blocking_recvmsg(o->socket_,
57 bufs.buffers(), bufs.count(),
58 o->in_flags_, o->out_flags_,
59 o->ec_, o->bytes_transferred_) ? done : not_done;
61 BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recvmsg",
62 o->ec_, o->bytes_transferred_));
69 MutableBufferSequence buffers_;
70 socket_base::message_flags in_flags_;
71 socket_base::message_flags& out_flags_;
74 template <typename MutableBufferSequence, typename Handler>
75 class reactive_socket_recvmsg_op :
76 public reactive_socket_recvmsg_op_base<MutableBufferSequence>
79 BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op);
81 reactive_socket_recvmsg_op(socket_type socket,
82 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
83 socket_base::message_flags& out_flags, Handler& handler)
84 : reactive_socket_recvmsg_op_base<MutableBufferSequence>(socket, buffers,
85 in_flags, out_flags, &reactive_socket_recvmsg_op::do_complete),
86 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
88 handler_work<Handler>::start(handler_);
91 static void do_complete(void* owner, operation* base,
92 const boost::system::error_code& /*ec*/,
93 std::size_t /*bytes_transferred*/)
95 // Take ownership of the handler object.
96 reactive_socket_recvmsg_op* o(
97 static_cast<reactive_socket_recvmsg_op*>(base));
98 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
99 handler_work<Handler> w(o->handler_);
101 BOOST_ASIO_HANDLER_COMPLETION((*o));
103 // Make a copy of the handler so that the memory can be deallocated before
104 // the upcall is made. Even if we're not about to make an upcall, a
105 // sub-object of the handler may be the true owner of the memory associated
106 // with the handler. Consequently, a local copy of the handler is required
107 // to ensure that any owning sub-object remains valid until after we have
108 // deallocated the memory here.
109 detail::binder2<Handler, boost::system::error_code, std::size_t>
110 handler(o->handler_, o->ec_, o->bytes_transferred_);
111 p.h = boost::asio::detail::addressof(handler.handler_);
114 // Make the upcall if required.
117 fenced_block b(fenced_block::half);
118 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
119 w.complete(handler, handler.handler_);
120 BOOST_ASIO_HANDLER_INVOCATION_END;
128 } // namespace detail
132 #include <boost/asio/detail/pop_options.hpp>
134 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP