]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/reactive_socket_send_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_REACTIVE_SOCKET_SEND_OP_HPP | |
12 | #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SEND_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> | |
7c673cae FG |
19 | #include <boost/asio/detail/bind_handler.hpp> |
20 | #include <boost/asio/detail/buffer_sequence_adapter.hpp> | |
21 | #include <boost/asio/detail/fenced_block.hpp> | |
20effc67 TL |
22 | #include <boost/asio/detail/handler_alloc_helpers.hpp> |
23 | #include <boost/asio/detail/handler_invoke_helpers.hpp> | |
24 | #include <boost/asio/detail/handler_work.hpp> | |
b32b8144 | 25 | #include <boost/asio/detail/memory.hpp> |
7c673cae FG |
26 | #include <boost/asio/detail/reactor_op.hpp> |
27 | #include <boost/asio/detail/socket_ops.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 reactive_socket_send_op_base : public reactor_op | |
37 | { | |
38 | public: | |
20effc67 TL |
39 | reactive_socket_send_op_base(const boost::system::error_code& success_ec, |
40 | socket_type socket, socket_ops::state_type state, | |
41 | const ConstBufferSequence& buffers, | |
7c673cae | 42 | socket_base::message_flags flags, func_type complete_func) |
20effc67 TL |
43 | : reactor_op(success_ec, |
44 | &reactive_socket_send_op_base::do_perform, complete_func), | |
7c673cae | 45 | socket_(socket), |
b32b8144 | 46 | state_(state), |
7c673cae FG |
47 | buffers_(buffers), |
48 | flags_(flags) | |
49 | { | |
50 | } | |
51 | ||
b32b8144 | 52 | static status do_perform(reactor_op* base) |
7c673cae FG |
53 | { |
54 | reactive_socket_send_op_base* o( | |
55 | static_cast<reactive_socket_send_op_base*>(base)); | |
56 | ||
20effc67 TL |
57 | typedef buffer_sequence_adapter<boost::asio::const_buffer, |
58 | ConstBufferSequence> bufs_type; | |
7c673cae | 59 | |
20effc67 TL |
60 | status result; |
61 | if (bufs_type::is_single_buffer) | |
62 | { | |
63 | result = socket_ops::non_blocking_send1(o->socket_, | |
64 | bufs_type::first(o->buffers_).data(), | |
65 | bufs_type::first(o->buffers_).size(), o->flags_, | |
b32b8144 FG |
66 | o->ec_, o->bytes_transferred_) ? done : not_done; |
67 | ||
20effc67 TL |
68 | if (result == done) |
69 | if ((o->state_ & socket_ops::stream_oriented) != 0) | |
70 | if (o->bytes_transferred_ < bufs_type::first(o->buffers_).size()) | |
71 | result = done_and_exhausted; | |
72 | } | |
73 | else | |
74 | { | |
75 | bufs_type bufs(o->buffers_); | |
76 | result = socket_ops::non_blocking_send(o->socket_, | |
77 | bufs.buffers(), bufs.count(), o->flags_, | |
78 | o->ec_, o->bytes_transferred_) ? done : not_done; | |
79 | ||
80 | if (result == done) | |
81 | if ((o->state_ & socket_ops::stream_oriented) != 0) | |
82 | if (o->bytes_transferred_ < bufs.total_size()) | |
83 | result = done_and_exhausted; | |
84 | } | |
b32b8144 FG |
85 | |
86 | BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_send", | |
87 | o->ec_, o->bytes_transferred_)); | |
88 | ||
89 | return result; | |
7c673cae FG |
90 | } |
91 | ||
92 | private: | |
93 | socket_type socket_; | |
b32b8144 | 94 | socket_ops::state_type state_; |
7c673cae FG |
95 | ConstBufferSequence buffers_; |
96 | socket_base::message_flags flags_; | |
97 | }; | |
98 | ||
92f5a8d4 | 99 | template <typename ConstBufferSequence, typename Handler, typename IoExecutor> |
7c673cae FG |
100 | class reactive_socket_send_op : |
101 | public reactive_socket_send_op_base<ConstBufferSequence> | |
102 | { | |
103 | public: | |
104 | BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_send_op); | |
105 | ||
20effc67 TL |
106 | reactive_socket_send_op(const boost::system::error_code& success_ec, |
107 | socket_type socket, socket_ops::state_type state, | |
92f5a8d4 TL |
108 | const ConstBufferSequence& buffers, socket_base::message_flags flags, |
109 | Handler& handler, const IoExecutor& io_ex) | |
20effc67 | 110 | : reactive_socket_send_op_base<ConstBufferSequence>(success_ec, socket, |
b32b8144 | 111 | state, buffers, flags, &reactive_socket_send_op::do_complete), |
92f5a8d4 | 112 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)), |
20effc67 | 113 | work_(handler_, io_ex) |
7c673cae FG |
114 | { |
115 | } | |
116 | ||
b32b8144 | 117 | static void do_complete(void* owner, operation* base, |
7c673cae FG |
118 | const boost::system::error_code& /*ec*/, |
119 | std::size_t /*bytes_transferred*/) | |
120 | { | |
121 | // Take ownership of the handler object. | |
122 | reactive_socket_send_op* o(static_cast<reactive_socket_send_op*>(base)); | |
123 | ptr p = { boost::asio::detail::addressof(o->handler_), o, o }; | |
124 | ||
b32b8144 | 125 | BOOST_ASIO_HANDLER_COMPLETION((*o)); |
7c673cae | 126 | |
20effc67 TL |
127 | // Take ownership of the operation's outstanding work. |
128 | handler_work<Handler, IoExecutor> w( | |
129 | BOOST_ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)( | |
130 | o->work_)); | |
131 | ||
7c673cae FG |
132 | // Make a copy of the handler so that the memory can be deallocated before |
133 | // the upcall is made. Even if we're not about to make an upcall, a | |
134 | // sub-object of the handler may be the true owner of the memory associated | |
135 | // with the handler. Consequently, a local copy of the handler is required | |
136 | // to ensure that any owning sub-object remains valid until after we have | |
137 | // deallocated the memory here. | |
138 | detail::binder2<Handler, boost::system::error_code, std::size_t> | |
139 | handler(o->handler_, o->ec_, o->bytes_transferred_); | |
140 | p.h = boost::asio::detail::addressof(handler.handler_); | |
141 | p.reset(); | |
142 | ||
143 | // Make the upcall if required. | |
144 | if (owner) | |
145 | { | |
146 | fenced_block b(fenced_block::half); | |
147 | BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); | |
b32b8144 | 148 | w.complete(handler, handler.handler_); |
7c673cae FG |
149 | BOOST_ASIO_HANDLER_INVOCATION_END; |
150 | } | |
151 | } | |
152 | ||
153 | private: | |
154 | Handler handler_; | |
20effc67 | 155 | handler_work<Handler, IoExecutor> work_; |
7c673cae FG |
156 | }; |
157 | ||
158 | } // namespace detail | |
159 | } // namespace asio | |
160 | } // namespace boost | |
161 | ||
162 | #include <boost/asio/detail/pop_options.hpp> | |
163 | ||
164 | #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SEND_OP_HPP |