buffered_handshake_op.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // ssl/detail/buffered_handshake_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
  11. #define BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/ssl/detail/engine.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace ssl {
  21. namespace detail {
  22. template <typename ConstBufferSequence>
  23. class buffered_handshake_op
  24. {
  25. public:
  26. static constexpr const char* tracking_name()
  27. {
  28. return "ssl::stream<>::async_buffered_handshake";
  29. }
  30. buffered_handshake_op(stream_base::handshake_type type,
  31. const ConstBufferSequence& buffers)
  32. : type_(type),
  33. buffers_(buffers),
  34. total_buffer_size_(boost::asio::buffer_size(buffers_))
  35. {
  36. }
  37. engine::want operator()(engine& eng,
  38. boost::system::error_code& ec,
  39. std::size_t& bytes_transferred) const
  40. {
  41. return this->process(eng, ec, bytes_transferred,
  42. boost::asio::buffer_sequence_begin(buffers_),
  43. boost::asio::buffer_sequence_end(buffers_));
  44. }
  45. void complete_sync(boost::system::error_code&) const
  46. {
  47. }
  48. template <typename Handler>
  49. void call_handler(Handler& handler,
  50. const boost::system::error_code& ec,
  51. const std::size_t& bytes_transferred) const
  52. {
  53. static_cast<Handler&&>(handler)(ec, bytes_transferred);
  54. }
  55. private:
  56. template <typename Iterator>
  57. engine::want process(engine& eng,
  58. boost::system::error_code& ec,
  59. std::size_t& bytes_transferred,
  60. Iterator begin, Iterator end) const
  61. {
  62. Iterator iter = begin;
  63. std::size_t accumulated_size = 0;
  64. for (;;)
  65. {
  66. engine::want want = eng.handshake(type_, ec);
  67. if (want != engine::want_input_and_retry
  68. || bytes_transferred == total_buffer_size_)
  69. return want;
  70. // Find the next buffer piece to be fed to the engine.
  71. while (iter != end)
  72. {
  73. const_buffer buffer(*iter);
  74. // Skip over any buffers which have already been consumed by the engine.
  75. if (bytes_transferred >= accumulated_size + buffer.size())
  76. {
  77. accumulated_size += buffer.size();
  78. ++iter;
  79. continue;
  80. }
  81. // The current buffer may have been partially consumed by the engine on
  82. // a previous iteration. If so, adjust the buffer to point to the
  83. // unused portion.
  84. if (bytes_transferred > accumulated_size)
  85. buffer = buffer + (bytes_transferred - accumulated_size);
  86. // Pass the buffer to the engine, and update the bytes transferred to
  87. // reflect the total number of bytes consumed so far.
  88. bytes_transferred += buffer.size();
  89. buffer = eng.put_input(buffer);
  90. bytes_transferred -= buffer.size();
  91. break;
  92. }
  93. }
  94. }
  95. stream_base::handshake_type type_;
  96. ConstBufferSequence buffers_;
  97. std::size_t total_buffer_size_;
  98. };
  99. } // namespace detail
  100. } // namespace ssl
  101. } // namespace asio
  102. } // namespace boost
  103. #include <boost/asio/detail/pop_options.hpp>
  104. #endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP