buffered_handshake_op.hpp 3.2 KB

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