buffered_handshake_op.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // ssl/detail/buffered_handshake_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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. template <typename Handler>
  46. void call_handler(Handler& handler,
  47. const boost::system::error_code& ec,
  48. const std::size_t& bytes_transferred) const
  49. {
  50. static_cast<Handler&&>(handler)(ec, bytes_transferred);
  51. }
  52. private:
  53. template <typename Iterator>
  54. engine::want process(engine& eng,
  55. boost::system::error_code& ec,
  56. std::size_t& bytes_transferred,
  57. Iterator begin, Iterator end) const
  58. {
  59. Iterator iter = begin;
  60. std::size_t accumulated_size = 0;
  61. for (;;)
  62. {
  63. engine::want want = eng.handshake(type_, ec);
  64. if (want != engine::want_input_and_retry
  65. || bytes_transferred == total_buffer_size_)
  66. return want;
  67. // Find the next buffer piece to be fed to the engine.
  68. while (iter != end)
  69. {
  70. const_buffer buffer(*iter);
  71. // Skip over any buffers which have already been consumed by the engine.
  72. if (bytes_transferred >= accumulated_size + buffer.size())
  73. {
  74. accumulated_size += buffer.size();
  75. ++iter;
  76. continue;
  77. }
  78. // The current buffer may have been partially consumed by the engine on
  79. // a previous iteration. If so, adjust the buffer to point to the
  80. // unused portion.
  81. if (bytes_transferred > accumulated_size)
  82. buffer = buffer + (bytes_transferred - accumulated_size);
  83. // Pass the buffer to the engine, and update the bytes transferred to
  84. // reflect the total number of bytes consumed so far.
  85. bytes_transferred += buffer.size();
  86. buffer = eng.put_input(buffer);
  87. bytes_transferred -= buffer.size();
  88. break;
  89. }
  90. }
  91. }
  92. stream_base::handshake_type type_;
  93. ConstBufferSequence buffers_;
  94. std::size_t total_buffer_size_;
  95. };
  96. } // namespace detail
  97. } // namespace ssl
  98. } // namespace asio
  99. } // namespace boost
  100. #include <boost/asio/detail/pop_options.hpp>
  101. #endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP