Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-18 08:44:58

0001 //
0002 // ssl/detail/buffered_handshake_op.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
0012 #define BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #include <boost/asio/ssl/detail/engine.hpp>
0021 
0022 #include <boost/asio/detail/push_options.hpp>
0023 
0024 namespace boost {
0025 namespace asio {
0026 namespace ssl {
0027 namespace detail {
0028 
0029 template <typename ConstBufferSequence>
0030 class buffered_handshake_op
0031 {
0032 public:
0033   static constexpr const char* tracking_name()
0034   {
0035     return "ssl::stream<>::async_buffered_handshake";
0036   }
0037 
0038   buffered_handshake_op(stream_base::handshake_type type,
0039       const ConstBufferSequence& buffers)
0040     : type_(type),
0041       buffers_(buffers),
0042       total_buffer_size_(boost::asio::buffer_size(buffers_))
0043   {
0044   }
0045 
0046   engine::want operator()(engine& eng,
0047       boost::system::error_code& ec,
0048       std::size_t& bytes_transferred) const
0049   {
0050     return this->process(eng, ec, bytes_transferred,
0051         boost::asio::buffer_sequence_begin(buffers_),
0052         boost::asio::buffer_sequence_end(buffers_));
0053   }
0054 
0055   void complete_sync(boost::system::error_code&) const
0056   {
0057   }
0058 
0059   template <typename Handler>
0060   void call_handler(Handler& handler,
0061       const boost::system::error_code& ec,
0062       const std::size_t& bytes_transferred) const
0063   {
0064     static_cast<Handler&&>(handler)(ec, bytes_transferred);
0065   }
0066 
0067 private:
0068   template <typename Iterator>
0069   engine::want process(engine& eng,
0070       boost::system::error_code& ec,
0071       std::size_t& bytes_transferred,
0072       Iterator begin, Iterator end) const
0073   {
0074     Iterator iter = begin;
0075     std::size_t accumulated_size = 0;
0076 
0077     for (;;)
0078     {
0079       engine::want want = eng.handshake(type_, ec);
0080       if (want != engine::want_input_and_retry
0081           || bytes_transferred == total_buffer_size_)
0082         return want;
0083 
0084       // Find the next buffer piece to be fed to the engine.
0085       while (iter != end)
0086       {
0087         const_buffer buffer(*iter);
0088 
0089         // Skip over any buffers which have already been consumed by the engine.
0090         if (bytes_transferred >= accumulated_size + buffer.size())
0091         {
0092           accumulated_size += buffer.size();
0093           ++iter;
0094           continue;
0095         }
0096 
0097         // The current buffer may have been partially consumed by the engine on
0098         // a previous iteration. If so, adjust the buffer to point to the
0099         // unused portion.
0100         if (bytes_transferred > accumulated_size)
0101           buffer = buffer + (bytes_transferred - accumulated_size);
0102 
0103         // Pass the buffer to the engine, and update the bytes transferred to
0104         // reflect the total number of bytes consumed so far.
0105         bytes_transferred += buffer.size();
0106         buffer = eng.put_input(buffer);
0107         bytes_transferred -= buffer.size();
0108         break;
0109       }
0110     }
0111   }
0112 
0113   stream_base::handshake_type type_;
0114   ConstBufferSequence buffers_;
0115   std::size_t total_buffer_size_;
0116 };
0117 
0118 } // namespace detail
0119 } // namespace ssl
0120 } // namespace asio
0121 } // namespace boost
0122 
0123 #include <boost/asio/detail/pop_options.hpp>
0124 
0125 #endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP