2 // ssl/detail/buffered_handshake_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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_BUFFERED_HANDSHAKE_OP_HPP
12 #define BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_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 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
24 #include <boost/asio/detail/push_options.hpp>
31 #if !defined(BOOST_ASIO_ENABLE_OLD_SSL)
33 template <typename ConstBufferSequence>
34 class buffered_handshake_op
37 buffered_handshake_op(stream_base::handshake_type type,
38 const ConstBufferSequence& buffers)
41 total_buffer_size_(boost::asio::buffer_size(buffers_))
45 engine::want operator()(engine& eng,
46 boost::system::error_code& ec,
47 std::size_t& bytes_transferred) const
49 typename ConstBufferSequence::const_iterator iter = buffers_.begin();
50 typename ConstBufferSequence::const_iterator end = buffers_.end();
51 std::size_t accumulated_size = 0;
55 engine::want want = eng.handshake(type_, ec);
56 if (want != engine::want_input_and_retry
57 || bytes_transferred == total_buffer_size_)
60 // Find the next buffer piece to be fed to the engine.
63 const_buffer buffer(*iter);
65 // Skip over any buffers which have already been consumed by the engine.
66 if (bytes_transferred >= accumulated_size + buffer_size(buffer))
68 accumulated_size += buffer_size(buffer);
73 // The current buffer may have been partially consumed by the engine on
74 // a previous iteration. If so, adjust the buffer to point to the
76 if (bytes_transferred > accumulated_size)
77 buffer = buffer + (bytes_transferred - accumulated_size);
79 // Pass the buffer to the engine, and update the bytes transferred to
80 // reflect the total number of bytes consumed so far.
81 bytes_transferred += buffer_size(buffer);
82 buffer = eng.put_input(buffer);
83 bytes_transferred -= buffer_size(buffer);
89 template <typename Handler>
90 void call_handler(Handler& handler,
91 const boost::system::error_code& ec,
92 const std::size_t& bytes_transferred) const
94 handler(ec, bytes_transferred);
98 stream_base::handshake_type type_;
99 ConstBufferSequence buffers_;
100 std::size_t total_buffer_size_;
103 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SSL)
105 } // namespace detail
110 #include <boost/asio/detail/pop_options.hpp>
112 #endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP