buffered_handshake_op.hpp 3.2 KB

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