buffered_handshake_op.hpp 3.0 KB

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