2 // ssl/detail/buffered_handshake_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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 #include <boost/asio/ssl/detail/engine.hpp>
22 #include <boost/asio/detail/push_options.hpp>
29 template <typename ConstBufferSequence>
30 class buffered_handshake_op
33 static BOOST_ASIO_CONSTEXPR const char* tracking_name()
35 return "ssl::stream<>::async_buffered_handshake";
38 buffered_handshake_op(stream_base::handshake_type type,
39 const ConstBufferSequence& buffers)
42 total_buffer_size_(boost::asio::buffer_size(buffers_))
46 engine::want operator()(engine& eng,
47 boost::system::error_code& ec,
48 std::size_t& bytes_transferred) const
50 return this->process(eng, ec, bytes_transferred,
51 boost::asio::buffer_sequence_begin(buffers_),
52 boost::asio::buffer_sequence_end(buffers_));
55 template <typename Handler>
56 void call_handler(Handler& handler,
57 const boost::system::error_code& ec,
58 const std::size_t& bytes_transferred) const
60 BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler)(ec, bytes_transferred);
64 template <typename Iterator>
65 engine::want process(engine& eng,
66 boost::system::error_code& ec,
67 std::size_t& bytes_transferred,
68 Iterator begin, Iterator end) const
70 Iterator iter = begin;
71 std::size_t accumulated_size = 0;
75 engine::want want = eng.handshake(type_, ec);
76 if (want != engine::want_input_and_retry
77 || bytes_transferred == total_buffer_size_)
80 // Find the next buffer piece to be fed to the engine.
83 const_buffer buffer(*iter);
85 // Skip over any buffers which have already been consumed by the engine.
86 if (bytes_transferred >= accumulated_size + buffer.size())
88 accumulated_size += buffer.size();
93 // The current buffer may have been partially consumed by the engine on
94 // a previous iteration. If so, adjust the buffer to point to the
96 if (bytes_transferred > accumulated_size)
97 buffer = buffer + (bytes_transferred - accumulated_size);
99 // Pass the buffer to the engine, and update the bytes transferred to
100 // reflect the total number of bytes consumed so far.
101 bytes_transferred += buffer.size();
102 buffer = eng.put_input(buffer);
103 bytes_transferred -= buffer.size();
109 stream_base::handshake_type type_;
110 ConstBufferSequence buffers_;
111 std::size_t total_buffer_size_;
114 } // namespace detail
119 #include <boost/asio/detail/pop_options.hpp>
121 #endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP