1 //
2 // detail/descriptor_write_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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_DESCRIPTOR_WRITE_OP_HPP
12 #define BOOST_ASIO_DETAIL_DESCRIPTOR_WRITE_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 
20 #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
21 
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>
28 
29 #include <boost/asio/detail/push_options.hpp>
30 
31 namespace boost {
32 namespace asio {
33 namespace detail {
34 
35 template <typename ConstBufferSequence>
36 class descriptor_write_op_base : public reactor_op
37 {
38 public:
descriptor_write_op_base(int descriptor,const ConstBufferSequence & buffers,func_type complete_func)39   descriptor_write_op_base(int descriptor,
40       const ConstBufferSequence& buffers, func_type complete_func)
41     : reactor_op(&descriptor_write_op_base::do_perform, complete_func),
42       descriptor_(descriptor),
43       buffers_(buffers)
44   {
45   }
46 
do_perform(reactor_op * base)47   static bool do_perform(reactor_op* base)
48   {
49     descriptor_write_op_base* o(static_cast<descriptor_write_op_base*>(base));
50 
51     buffer_sequence_adapter<boost::asio::const_buffer,
52         ConstBufferSequence> bufs(o->buffers_);
53 
54     return descriptor_ops::non_blocking_write(o->descriptor_,
55         bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_);
56   }
57 
58 private:
59   int descriptor_;
60   ConstBufferSequence buffers_;
61 };
62 
63 template <typename ConstBufferSequence, typename Handler>
64 class descriptor_write_op
65   : public descriptor_write_op_base<ConstBufferSequence>
66 {
67 public:
68   BOOST_ASIO_DEFINE_HANDLER_PTR(descriptor_write_op);
69 
descriptor_write_op(int descriptor,const ConstBufferSequence & buffers,Handler & handler)70   descriptor_write_op(int descriptor,
71       const ConstBufferSequence& buffers, Handler& handler)
72     : descriptor_write_op_base<ConstBufferSequence>(
73         descriptor, buffers, &descriptor_write_op::do_complete),
74       handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
75   {
76   }
77 
do_complete(io_service_impl * owner,operation * base,const boost::system::error_code &,std::size_t)78   static void do_complete(io_service_impl* owner, operation* base,
79       const boost::system::error_code& /*ec*/,
80       std::size_t /*bytes_transferred*/)
81   {
82     // Take ownership of the handler object.
83     descriptor_write_op* o(static_cast<descriptor_write_op*>(base));
84     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
85 
86     BOOST_ASIO_HANDLER_COMPLETION((o));
87 
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_);
97     p.reset();
98 
99     // Make the upcall if required.
100     if (owner)
101     {
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;
106     }
107   }
108 
109 private:
110   Handler handler_;
111 };
112 
113 } // namespace detail
114 } // namespace asio
115 } // namespace boost
116 
117 #include <boost/asio/detail/pop_options.hpp>
118 
119 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
120 
121 #endif // BOOST_ASIO_DETAIL_DESCRIPTOR_WRITE_OP_HPP
122