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_SSL_DETAIL_IO_HPP
12 #define BOOST_ASIO_SSL_DETAIL_IO_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_ENABLE_OLD_SSL)
21 # include <boost/asio/ssl/detail/engine.hpp>
22 # include <boost/asio/ssl/detail/stream_core.hpp>
23 # include <boost/asio/write.hpp>
24 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
26 #include <boost/asio/detail/push_options.hpp>
33 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
35 template <typename Stream, typename Operation>
36 std::size_t io(Stream& next_layer, stream_core& core,
37 const Operation& op, boost::system::error_code& ec)
39 std::size_t bytes_transferred = 0;
40 do switch (op(core.engine_, ec, bytes_transferred))
42 case engine::want_input_and_retry:
44 // If the input buffer is empty then we need to read some more data from
45 // the underlying transport.
46 if (boost::asio::buffer_size(core.input_) == 0)
47 core.input_ = boost::asio::buffer(core.input_buffer_,
48 next_layer.read_some(core.input_buffer_, ec));
50 // Pass the new input data to the engine.
51 core.input_ = core.engine_.put_input(core.input_);
53 // Try the operation again.
56 case engine::want_output_and_retry:
58 // Get output data from the engine and write it to the underlying
60 boost::asio::write(next_layer,
61 core.engine_.get_output(core.output_buffer_), ec);
63 // Try the operation again.
66 case engine::want_output:
68 // Get output data from the engine and write it to the underlying
70 boost::asio::write(next_layer,
71 core.engine_.get_output(core.output_buffer_), ec);
73 // Operation is complete. Return result to caller.
74 core.engine_.map_error_code(ec);
75 return bytes_transferred;
79 // Operation is complete. Return result to caller.
80 core.engine_.map_error_code(ec);
81 return bytes_transferred;
85 // Operation failed. Return result to caller.
86 core.engine_.map_error_code(ec);
90 template <typename Stream, typename Operation, typename Handler>
94 io_op(Stream& next_layer, stream_core& core,
95 const Operation& op, Handler& handler)
96 : next_layer_(next_layer),
100 want_(engine::want_nothing),
101 bytes_transferred_(0),
102 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
106 #if defined(BOOST_ASIO_HAS_MOVE)
107 io_op(const io_op& other)
108 : next_layer_(other.next_layer_),
111 start_(other.start_),
114 bytes_transferred_(other.bytes_transferred_),
115 handler_(other.handler_)
120 : next_layer_(other.next_layer_),
123 start_(other.start_),
126 bytes_transferred_(other.bytes_transferred_),
127 handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
130 #endif // defined(BOOST_ASIO_HAS_MOVE)
132 void operator()(boost::system::error_code ec,
133 std::size_t bytes_transferred = ~std::size_t(0), int start = 0)
135 switch (start_ = start)
137 case 1: // Called after at least one async operation.
140 switch (want_ = op_(core_.engine_, ec_, bytes_transferred_))
142 case engine::want_input_and_retry:
144 // If the input buffer already has data in it we can pass it to the
145 // engine and then retry the operation immediately.
146 if (boost::asio::buffer_size(core_.input_) != 0)
148 core_.input_ = core_.engine_.put_input(core_.input_);
152 // The engine wants more data to be read from input. However, we
153 // cannot allow more than one read operation at a time on the
154 // underlying transport. The pending_read_ timer's expiry is set to
155 // pos_infin if a read is in progress, and neg_infin otherwise.
156 if (core_.pending_read_.expires_at() == core_.neg_infin())
158 // Prevent other read operations from being started.
159 core_.pending_read_.expires_at(core_.pos_infin());
161 // Start reading some data from the underlying transport.
162 next_layer_.async_read_some(
163 boost::asio::buffer(core_.input_buffer_),
164 BOOST_ASIO_MOVE_CAST(io_op)(*this));
168 // Wait until the current read operation completes.
169 core_.pending_read_.async_wait(BOOST_ASIO_MOVE_CAST(io_op)(*this));
172 // Yield control until asynchronous operation completes. Control
173 // resumes at the "default:" label below.
176 case engine::want_output_and_retry:
177 case engine::want_output:
179 // The engine wants some data to be written to the output. However, we
180 // cannot allow more than one write operation at a time on the
181 // underlying transport. The pending_write_ timer's expiry is set to
182 // pos_infin if a write is in progress, and neg_infin otherwise.
183 if (core_.pending_write_.expires_at() == core_.neg_infin())
185 // Prevent other write operations from being started.
186 core_.pending_write_.expires_at(core_.pos_infin());
188 // Start writing all the data to the underlying transport.
189 boost::asio::async_write(next_layer_,
190 core_.engine_.get_output(core_.output_buffer_),
191 BOOST_ASIO_MOVE_CAST(io_op)(*this));
195 // Wait until the current write operation completes.
196 core_.pending_write_.async_wait(BOOST_ASIO_MOVE_CAST(io_op)(*this));
199 // Yield control until asynchronous operation completes. Control
200 // resumes at the "default:" label below.
205 // The SSL operation is done and we can invoke the handler, but we
206 // have to keep in mind that this function might be being called from
207 // the async operation's initiating function. In this case we're not
208 // allowed to call the handler directly. Instead, issue a zero-sized
209 // read so the handler runs "as-if" posted using io_service::post().
212 next_layer_.async_read_some(
213 boost::asio::buffer(core_.input_buffer_, 0),
214 BOOST_ASIO_MOVE_CAST(io_op)(*this));
216 // Yield control until asynchronous operation completes. Control
217 // resumes at the "default:" label below.
222 // Continue on to run handler directly.
228 if (bytes_transferred == ~std::size_t(0))
229 bytes_transferred = 0; // Timer cancellation, no data transferred.
235 case engine::want_input_and_retry:
237 // Add received data to the engine's input.
238 core_.input_ = boost::asio::buffer(
239 core_.input_buffer_, bytes_transferred);
240 core_.input_ = core_.engine_.put_input(core_.input_);
242 // Release any waiting read operations.
243 core_.pending_read_.expires_at(core_.neg_infin());
245 // Try the operation again.
248 case engine::want_output_and_retry:
250 // Release any waiting write operations.
251 core_.pending_write_.expires_at(core_.neg_infin());
253 // Try the operation again.
256 case engine::want_output:
258 // Release any waiting write operations.
259 core_.pending_write_.expires_at(core_.neg_infin());
261 // Fall through to call handler.
265 // Pass the result to the handler.
266 op_.call_handler(handler_,
267 core_.engine_.map_error_code(ec_),
268 ec_ ? 0 : bytes_transferred_);
270 // Our work here is done.
275 // Operation failed. Pass the result to the handler.
276 op_.call_handler(handler_, core_.engine_.map_error_code(ec_), 0);
286 boost::system::error_code ec_;
287 std::size_t bytes_transferred_;
291 template <typename Stream, typename Operation, typename Handler>
292 inline void* asio_handler_allocate(std::size_t size,
293 io_op<Stream, Operation, Handler>* this_handler)
295 return boost_asio_handler_alloc_helpers::allocate(
296 size, this_handler->handler_);
299 template <typename Stream, typename Operation, typename Handler>
300 inline void asio_handler_deallocate(void* pointer, std::size_t size,
301 io_op<Stream, Operation, Handler>* this_handler)
303 boost_asio_handler_alloc_helpers::deallocate(
304 pointer, size, this_handler->handler_);
307 template <typename Stream, typename Operation, typename Handler>
308 inline bool asio_handler_is_continuation(
309 io_op<Stream, Operation, Handler>* this_handler)
311 return this_handler->start_ == 0 ? true
312 : boost_asio_handler_cont_helpers::is_continuation(this_handler->handler_);
315 template <typename Function, typename Stream,
316 typename Operation, typename Handler>
317 inline void asio_handler_invoke(Function& function,
318 io_op<Stream, Operation, Handler>* this_handler)
320 boost_asio_handler_invoke_helpers::invoke(
321 function, this_handler->handler_);
324 template <typename Function, typename Stream,
325 typename Operation, typename Handler>
326 inline void asio_handler_invoke(const Function& function,
327 io_op<Stream, Operation, Handler>* this_handler)
329 boost_asio_handler_invoke_helpers::invoke(
330 function, this_handler->handler_);
333 template <typename Stream, typename Operation, typename Handler>
334 inline void async_io(Stream& next_layer, stream_core& core,
335 const Operation& op, Handler& handler)
337 io_op<Stream, Operation, Handler>(
338 next_layer, core, op, handler)(
339 boost::system::error_code(), 0, 1);
342 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
344 } // namespace detail
349 #include <boost/asio/detail/pop_options.hpp>
351 #endif // BOOST_ASIO_SSL_DETAIL_IO_HPP