Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:01

0001 //
0002 // ssl/detail/buffered_handshake_op.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 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   template <typename Handler>
0056   void call_handler(Handler& handler,
0057       const boost::system::error_code& ec,
0058       const std::size_t& bytes_transferred) const
0059   {
0060     static_cast<Handler&&>(handler)(ec, bytes_transferred);
0061   }
0062 
0063 private:
0064   template <typename Iterator>
0065   engine::want process(engine& eng,
0066       boost::system::error_code& ec,
0067       std::size_t& bytes_transferred,
0068       Iterator begin, Iterator end) const
0069   {
0070     Iterator iter = begin;
0071     std::size_t accumulated_size = 0;
0072 
0073     for (;;)
0074     {
0075       engine::want want = eng.handshake(type_, ec);
0076       if (want != engine::want_input_and_retry
0077           || bytes_transferred == total_buffer_size_)
0078         return want;
0079 
0080       // Find the next buffer piece to be fed to the engine.
0081       while (iter != end)
0082       {
0083         const_buffer buffer(*iter);
0084 
0085         // Skip over any buffers which have already been consumed by the engine.
0086         if (bytes_transferred >= accumulated_size + buffer.size())
0087         {
0088           accumulated_size += buffer.size();
0089           ++iter;
0090           continue;
0091         }
0092 
0093         // The current buffer may have been partially consumed by the engine on
0094         // a previous iteration. If so, adjust the buffer to point to the
0095         // unused portion.
0096         if (bytes_transferred > accumulated_size)
0097           buffer = buffer + (bytes_transferred - accumulated_size);
0098 
0099         // Pass the buffer to the engine, and update the bytes transferred to
0100         // reflect the total number of bytes consumed so far.
0101         bytes_transferred += buffer.size();
0102         buffer = eng.put_input(buffer);
0103         bytes_transferred -= buffer.size();
0104         break;
0105       }
0106     }
0107   }
0108 
0109   stream_base::handshake_type type_;
0110   ConstBufferSequence buffers_;
0111   std::size_t total_buffer_size_;
0112 };
0113 
0114 } // namespace detail
0115 } // namespace ssl
0116 } // namespace asio
0117 } // namespace boost
0118 
0119 #include <boost/asio/detail/pop_options.hpp>
0120 
0121 #endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP