2 // detail/descriptor_write_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 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_DESCRIPTOR_WRITE_OP_HPP
12 #define BOOST_ASIO_DETAIL_DESCRIPTOR_WRITE_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>
20 #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
22 #include <boost/asio/detail/bind_handler.hpp>
23 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
24 #include <boost/asio/detail/descriptor_ops.hpp>
25 #include <boost/asio/detail/fenced_block.hpp>
26 #include <boost/asio/detail/handler_work.hpp>
27 #include <boost/asio/detail/memory.hpp>
28 #include <boost/asio/detail/reactor_op.hpp>
30 #include <boost/asio/detail/push_options.hpp>
36 template <typename ConstBufferSequence>
37 class descriptor_write_op_base : public reactor_op
40 descriptor_write_op_base(int descriptor,
41 const ConstBufferSequence& buffers, func_type complete_func)
42 : reactor_op(&descriptor_write_op_base::do_perform, complete_func),
43 descriptor_(descriptor),
48 static status do_perform(reactor_op* base)
50 descriptor_write_op_base* o(static_cast<descriptor_write_op_base*>(base));
52 buffer_sequence_adapter<boost::asio::const_buffer,
53 ConstBufferSequence> bufs(o->buffers_);
55 status result = descriptor_ops::non_blocking_write(o->descriptor_,
56 bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_)
59 BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_write",
60 o->ec_, o->bytes_transferred_));
67 ConstBufferSequence buffers_;
70 template <typename ConstBufferSequence, typename Handler>
71 class descriptor_write_op
72 : public descriptor_write_op_base<ConstBufferSequence>
75 BOOST_ASIO_DEFINE_HANDLER_PTR(descriptor_write_op);
77 descriptor_write_op(int descriptor,
78 const ConstBufferSequence& buffers, Handler& handler)
79 : descriptor_write_op_base<ConstBufferSequence>(
80 descriptor, buffers, &descriptor_write_op::do_complete),
81 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
83 handler_work<Handler>::start(handler_);
86 static void do_complete(void* owner, operation* base,
87 const boost::system::error_code& /*ec*/,
88 std::size_t /*bytes_transferred*/)
90 // Take ownership of the handler object.
91 descriptor_write_op* o(static_cast<descriptor_write_op*>(base));
92 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
93 handler_work<Handler> w(o->handler_);
95 BOOST_ASIO_HANDLER_COMPLETION((*o));
97 // Make a copy of the handler so that the memory can be deallocated before
98 // the upcall is made. Even if we're not about to make an upcall, a
99 // sub-object of the handler may be the true owner of the memory associated
100 // with the handler. Consequently, a local copy of the handler is required
101 // to ensure that any owning sub-object remains valid until after we have
102 // deallocated the memory here.
103 detail::binder2<Handler, boost::system::error_code, std::size_t>
104 handler(o->handler_, o->ec_, o->bytes_transferred_);
105 p.h = boost::asio::detail::addressof(handler.handler_);
108 // Make the upcall if required.
111 fenced_block b(fenced_block::half);
112 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
113 w.complete(handler, handler.handler_);
114 BOOST_ASIO_HANDLER_INVOCATION_END;
122 } // namespace detail
126 #include <boost/asio/detail/pop_options.hpp>
128 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
130 #endif // BOOST_ASIO_DETAIL_DESCRIPTOR_WRITE_OP_HPP