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 ASIO_DETAIL_DESCRIPTOR_WRITE_OP_HPP
12 #define 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 "asio/detail/config.hpp"
19 
20 #if !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
21 
22 #include "asio/detail/bind_handler.hpp"
23 #include "asio/detail/buffer_sequence_adapter.hpp"
24 #include "asio/detail/descriptor_ops.hpp"
25 #include "asio/detail/fenced_block.hpp"
26 #include "asio/detail/handler_work.hpp"
27 #include "asio/detail/memory.hpp"
28 #include "asio/detail/reactor_op.hpp"
29 
30 #include "asio/detail/push_options.hpp"
31 
32 namespace asio {
33 namespace detail {
34 
35 template <typename ConstBufferSequence>
36 class descriptor_write_op_base : public reactor_op
37 {
38 public:
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 
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<asio::const_buffer,
52         ConstBufferSequence> bufs(o->buffers_);
53 
54     bool result = descriptor_ops::non_blocking_write(o->descriptor_,
55         bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_);
56 
57     ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_write",
58           o->ec_, o->bytes_transferred_));
59 
60     return result;
61   }
62 
63 private:
64   int descriptor_;
65   ConstBufferSequence buffers_;
66 };
67 
68 template <typename ConstBufferSequence, typename Handler>
69 class descriptor_write_op
70   : public descriptor_write_op_base<ConstBufferSequence>
71 {
72 public:
73   ASIO_DEFINE_HANDLER_PTR(descriptor_write_op);
74 
75   descriptor_write_op(int descriptor,
76       const ConstBufferSequence& buffers, Handler& handler)
77     : descriptor_write_op_base<ConstBufferSequence>(
78         descriptor, buffers, &descriptor_write_op::do_complete),
79       handler_(ASIO_MOVE_CAST(Handler)(handler))
80   {
81     handler_work<Handler>::start(handler_);
82   }
83 
84   static void do_complete(void* owner, operation* base,
85       const asio::error_code& /*ec*/,
86       std::size_t /*bytes_transferred*/)
87   {
88     // Take ownership of the handler object.
89     descriptor_write_op* o(static_cast<descriptor_write_op*>(base));
90     ptr p = { asio::detail::addressof(o->handler_), o, o };
91     handler_work<Handler> w(o->handler_);
92 
93     ASIO_HANDLER_COMPLETION((*o));
94 
95     // Make a copy of the handler so that the memory can be deallocated before
96     // the upcall is made. Even if we're not about to make an upcall, a
97     // sub-object of the handler may be the true owner of the memory associated
98     // with the handler. Consequently, a local copy of the handler is required
99     // to ensure that any owning sub-object remains valid until after we have
100     // deallocated the memory here.
101     detail::binder2<Handler, asio::error_code, std::size_t>
102       handler(o->handler_, o->ec_, o->bytes_transferred_);
103     p.h = asio::detail::addressof(handler.handler_);
104     p.reset();
105 
106     // Make the upcall if required.
107     if (owner)
108     {
109       fenced_block b(fenced_block::half);
110       ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
111       w.complete(handler, handler.handler_);
112       ASIO_HANDLER_INVOCATION_END;
113     }
114   }
115 
116 private:
117   Handler handler_;
118 };
119 
120 } // namespace detail
121 } // namespace asio
122 
123 #include "asio/detail/pop_options.hpp"
124 
125 #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
126 
127 #endif // ASIO_DETAIL_DESCRIPTOR_WRITE_OP_HPP
128