]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/descriptor_write_op.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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 | ||
7c673cae FG |
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> | |
b32b8144 FG |
26 | #include <boost/asio/detail/handler_work.hpp> |
27 | #include <boost/asio/detail/memory.hpp> | |
7c673cae FG |
28 | #include <boost/asio/detail/reactor_op.hpp> |
29 | ||
30 | #include <boost/asio/detail/push_options.hpp> | |
31 | ||
32 | namespace boost { | |
33 | namespace asio { | |
34 | namespace detail { | |
35 | ||
36 | template <typename ConstBufferSequence> | |
37 | class descriptor_write_op_base : public reactor_op | |
38 | { | |
39 | public: | |
20effc67 TL |
40 | descriptor_write_op_base(const boost::system::error_code& success_ec, |
41 | int descriptor, const ConstBufferSequence& buffers, | |
42 | func_type complete_func) | |
43 | : reactor_op(success_ec, | |
44 | &descriptor_write_op_base::do_perform, complete_func), | |
7c673cae FG |
45 | descriptor_(descriptor), |
46 | buffers_(buffers) | |
47 | { | |
48 | } | |
49 | ||
b32b8144 | 50 | static status do_perform(reactor_op* base) |
7c673cae FG |
51 | { |
52 | descriptor_write_op_base* o(static_cast<descriptor_write_op_base*>(base)); | |
53 | ||
20effc67 TL |
54 | typedef buffer_sequence_adapter<boost::asio::const_buffer, |
55 | ConstBufferSequence> bufs_type; | |
7c673cae | 56 | |
20effc67 TL |
57 | status result; |
58 | if (bufs_type::is_single_buffer) | |
59 | { | |
60 | result = descriptor_ops::non_blocking_write1(o->descriptor_, | |
61 | bufs_type::first(o->buffers_).data(), | |
62 | bufs_type::first(o->buffers_).size(), | |
63 | o->ec_, o->bytes_transferred_) ? done : not_done; | |
64 | } | |
65 | else | |
66 | { | |
67 | bufs_type bufs(o->buffers_); | |
68 | result = descriptor_ops::non_blocking_write(o->descriptor_, | |
69 | bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_) | |
70 | ? done : not_done; | |
71 | } | |
b32b8144 FG |
72 | |
73 | BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_write", | |
74 | o->ec_, o->bytes_transferred_)); | |
75 | ||
76 | return result; | |
7c673cae FG |
77 | } |
78 | ||
79 | private: | |
80 | int descriptor_; | |
81 | ConstBufferSequence buffers_; | |
82 | }; | |
83 | ||
92f5a8d4 | 84 | template <typename ConstBufferSequence, typename Handler, typename IoExecutor> |
7c673cae FG |
85 | class descriptor_write_op |
86 | : public descriptor_write_op_base<ConstBufferSequence> | |
87 | { | |
88 | public: | |
89 | BOOST_ASIO_DEFINE_HANDLER_PTR(descriptor_write_op); | |
90 | ||
20effc67 TL |
91 | descriptor_write_op(const boost::system::error_code& success_ec, |
92 | int descriptor, const ConstBufferSequence& buffers, | |
92f5a8d4 | 93 | Handler& handler, const IoExecutor& io_ex) |
20effc67 | 94 | : descriptor_write_op_base<ConstBufferSequence>(success_ec, |
7c673cae | 95 | descriptor, buffers, &descriptor_write_op::do_complete), |
92f5a8d4 | 96 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), |
20effc67 | 97 | work_(handler_, io_ex) |
7c673cae FG |
98 | { |
99 | } | |
100 | ||
b32b8144 | 101 | static void do_complete(void* owner, operation* base, |
7c673cae FG |
102 | const boost::system::error_code& /*ec*/, |
103 | std::size_t /*bytes_transferred*/) | |
104 | { | |
105 | // Take ownership of the handler object. | |
106 | descriptor_write_op* o(static_cast<descriptor_write_op*>(base)); | |
107 | ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; | |
108 | ||
b32b8144 | 109 | BOOST_ASIO_HANDLER_COMPLETION((*o)); |
7c673cae | 110 | |
20effc67 TL |
111 | // Take ownership of the operation's outstanding work. |
112 | handler_work<Handler, IoExecutor> w( | |
113 | BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)( | |
114 | o->work_)); | |
115 | ||
7c673cae FG |
116 | // Make a copy of the handler so that the memory can be deallocated before |
117 | // the upcall is made. Even if we're not about to make an upcall, a | |
118 | // sub-object of the handler may be the true owner of the memory associated | |
119 | // with the handler. Consequently, a local copy of the handler is required | |
120 | // to ensure that any owning sub-object remains valid until after we have | |
121 | // deallocated the memory here. | |
122 | detail::binder2<Handler, boost::system::error_code, std::size_t> | |
123 | handler(o->handler_, o->ec_, o->bytes_transferred_); | |
124 | p.h = boost::asio::detail::addressof(handler.handler_); | |
125 | p.reset(); | |
126 | ||
127 | // Make the upcall if required. | |
128 | if (owner) | |
129 | { | |
130 | fenced_block b(fenced_block::half); | |
131 | BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); | |
b32b8144 | 132 | w.complete(handler, handler.handler_); |
7c673cae FG |
133 | BOOST_ASIO_HANDLER_INVOCATION_END; |
134 | } | |
135 | } | |
136 | ||
137 | private: | |
138 | Handler handler_; | |
20effc67 | 139 | handler_work<Handler, IoExecutor> work_; |
7c673cae FG |
140 | }; |
141 | ||
142 | } // namespace detail | |
143 | } // namespace asio | |
144 | } // namespace boost | |
145 | ||
146 | #include <boost/asio/detail/pop_options.hpp> | |
147 | ||
148 | #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__) | |
149 | ||
150 | #endif // BOOST_ASIO_DETAIL_DESCRIPTOR_WRITE_OP_HPP |