buffered_handshake_op.hpp 3.2 KB

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