2 // detail/descriptor_read_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_READ_OP_HPP
12 #define BOOST_ASIO_DETAIL_DESCRIPTOR_READ_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/addressof.hpp>
23 #include <boost/asio/detail/bind_handler.hpp>
24 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
25 #include <boost/asio/detail/descriptor_ops.hpp>
26 #include <boost/asio/detail/fenced_block.hpp>
27 #include <boost/asio/detail/reactor_op.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 template <typename MutableBufferSequence>
36 class descriptor_read_op_base : public reactor_op
39 descriptor_read_op_base(int descriptor,
40 const MutableBufferSequence& buffers, func_type complete_func)
41 : reactor_op(&descriptor_read_op_base::do_perform, complete_func),
42 descriptor_(descriptor),
47 static bool do_perform(reactor_op* base)
49 descriptor_read_op_base* o(static_cast<descriptor_read_op_base*>(base));
51 buffer_sequence_adapter<boost::asio::mutable_buffer,
52 MutableBufferSequence> bufs(o->buffers_);
54 return descriptor_ops::non_blocking_read(o->descriptor_,
55 bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_);
60 MutableBufferSequence buffers_;
63 template <typename MutableBufferSequence, typename Handler>
64 class descriptor_read_op
65 : public descriptor_read_op_base<MutableBufferSequence>
68 BOOST_ASIO_DEFINE_HANDLER_PTR(descriptor_read_op);
70 descriptor_read_op(int descriptor,
71 const MutableBufferSequence& buffers, Handler& handler)
72 : descriptor_read_op_base<MutableBufferSequence>(
73 descriptor, buffers, &descriptor_read_op::do_complete),
74 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
78 static void do_complete(io_service_impl* owner, operation* base,
79 const boost::system::error_code& /*ec*/,
80 std::size_t /*bytes_transferred*/)
82 // Take ownership of the handler object.
83 descriptor_read_op* o(static_cast<descriptor_read_op*>(base));
84 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
86 BOOST_ASIO_HANDLER_COMPLETION((o));
88 // Make a copy of the handler so that the memory can be deallocated before
89 // the upcall is made. Even if we're not about to make an upcall, a
90 // sub-object of the handler may be the true owner of the memory associated
91 // with the handler. Consequently, a local copy of the handler is required
92 // to ensure that any owning sub-object remains valid until after we have
93 // deallocated the memory here.
94 detail::binder2<Handler, boost::system::error_code, std::size_t>
95 handler(o->handler_, o->ec_, o->bytes_transferred_);
96 p.h = boost::asio::detail::addressof(handler.handler_);
99 // Make the upcall if required.
102 fenced_block b(fenced_block::half);
103 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
104 boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
105 BOOST_ASIO_HANDLER_INVOCATION_END;
113 } // namespace detail
117 #include <boost/asio/detail/pop_options.hpp>
119 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
121 #endif // BOOST_ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP