]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // ssl/detail/buffered_handshake_op.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
92f5a8d4 | 5 | // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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 | ||
11 | #ifndef BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP | |
12 | #define BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
b32b8144 | 20 | #include <boost/asio/ssl/detail/engine.hpp> |
7c673cae FG |
21 | |
22 | #include <boost/asio/detail/push_options.hpp> | |
23 | ||
24 | namespace boost { | |
25 | namespace asio { | |
26 | namespace ssl { | |
27 | namespace detail { | |
28 | ||
7c673cae FG |
29 | template <typename ConstBufferSequence> |
30 | class buffered_handshake_op | |
31 | { | |
32 | public: | |
33 | buffered_handshake_op(stream_base::handshake_type type, | |
34 | const ConstBufferSequence& buffers) | |
35 | : type_(type), | |
36 | buffers_(buffers), | |
37 | total_buffer_size_(boost::asio::buffer_size(buffers_)) | |
38 | { | |
39 | } | |
40 | ||
41 | engine::want operator()(engine& eng, | |
42 | boost::system::error_code& ec, | |
43 | std::size_t& bytes_transferred) const | |
44 | { | |
b32b8144 FG |
45 | return this->process(eng, ec, bytes_transferred, |
46 | boost::asio::buffer_sequence_begin(buffers_), | |
47 | boost::asio::buffer_sequence_end(buffers_)); | |
48 | } | |
49 | ||
50 | template <typename Handler> | |
51 | void call_handler(Handler& handler, | |
52 | const boost::system::error_code& ec, | |
53 | const std::size_t& bytes_transferred) const | |
54 | { | |
55 | handler(ec, bytes_transferred); | |
56 | } | |
57 | ||
58 | private: | |
59 | template <typename Iterator> | |
60 | engine::want process(engine& eng, | |
61 | boost::system::error_code& ec, | |
62 | std::size_t& bytes_transferred, | |
63 | Iterator begin, Iterator end) const | |
64 | { | |
65 | Iterator iter = begin; | |
7c673cae FG |
66 | std::size_t accumulated_size = 0; |
67 | ||
68 | for (;;) | |
69 | { | |
70 | engine::want want = eng.handshake(type_, ec); | |
71 | if (want != engine::want_input_and_retry | |
72 | || bytes_transferred == total_buffer_size_) | |
73 | return want; | |
74 | ||
75 | // Find the next buffer piece to be fed to the engine. | |
76 | while (iter != end) | |
77 | { | |
78 | const_buffer buffer(*iter); | |
79 | ||
80 | // Skip over any buffers which have already been consumed by the engine. | |
b32b8144 | 81 | if (bytes_transferred >= accumulated_size + buffer.size()) |
7c673cae | 82 | { |
b32b8144 | 83 | accumulated_size += buffer.size(); |
7c673cae FG |
84 | ++iter; |
85 | continue; | |
86 | } | |
87 | ||
88 | // The current buffer may have been partially consumed by the engine on | |
89 | // a previous iteration. If so, adjust the buffer to point to the | |
90 | // unused portion. | |
91 | if (bytes_transferred > accumulated_size) | |
92 | buffer = buffer + (bytes_transferred - accumulated_size); | |
93 | ||
94 | // Pass the buffer to the engine, and update the bytes transferred to | |
95 | // reflect the total number of bytes consumed so far. | |
b32b8144 | 96 | bytes_transferred += buffer.size(); |
7c673cae | 97 | buffer = eng.put_input(buffer); |
b32b8144 | 98 | bytes_transferred -= buffer.size(); |
7c673cae FG |
99 | break; |
100 | } | |
101 | } | |
102 | } | |
103 | ||
7c673cae FG |
104 | stream_base::handshake_type type_; |
105 | ConstBufferSequence buffers_; | |
106 | std::size_t total_buffer_size_; | |
107 | }; | |
108 | ||
7c673cae FG |
109 | } // namespace detail |
110 | } // namespace ssl | |
111 | } // namespace asio | |
112 | } // namespace boost | |
113 | ||
114 | #include <boost/asio/detail/pop_options.hpp> | |
115 | ||
116 | #endif // BOOST_ASIO_SSL_DETAIL_BUFFERED_HANDSHAKE_OP_HPP |