]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/buffered_read_stream.hpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / boost / asio / buffered_read_stream.hpp
1 //
2 // buffered_read_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 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
11 #ifndef BOOST_ASIO_BUFFERED_READ_STREAM_HPP
12 #define BOOST_ASIO_BUFFERED_READ_STREAM_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 #include <cstddef>
20 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/buffered_read_stream_fwd.hpp>
22 #include <boost/asio/buffer.hpp>
23 #include <boost/asio/detail/bind_handler.hpp>
24 #include <boost/asio/detail/buffer_resize_guard.hpp>
25 #include <boost/asio/detail/buffered_stream_storage.hpp>
26 #include <boost/asio/detail/noncopyable.hpp>
27 #include <boost/asio/detail/type_traits.hpp>
28 #include <boost/asio/error.hpp>
29
30 #include <boost/asio/detail/push_options.hpp>
31
32 namespace boost {
33 namespace asio {
34
35 /// Adds buffering to the read-related operations of a stream.
36 /**
37 * The buffered_read_stream class template can be used to add buffering to the
38 * synchronous and asynchronous read operations of a stream.
39 *
40 * @par Thread Safety
41 * @e Distinct @e objects: Safe.@n
42 * @e Shared @e objects: Unsafe.
43 *
44 * @par Concepts:
45 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
46 */
47 template <typename Stream>
48 class buffered_read_stream
49 : private noncopyable
50 {
51 public:
52 /// The type of the next layer.
53 typedef typename remove_reference<Stream>::type next_layer_type;
54
55 /// The type of the lowest layer.
56 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
57
58 /// The type of the executor associated with the object.
59 typedef typename lowest_layer_type::executor_type executor_type;
60
61 #if defined(GENERATING_DOCUMENTATION)
62 /// The default buffer size.
63 static const std::size_t default_buffer_size = implementation_defined;
64 #else
65 BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
66 #endif
67
68 /// Construct, passing the specified argument to initialise the next layer.
69 template <typename Arg>
70 explicit buffered_read_stream(Arg& a)
71 : next_layer_(a),
72 storage_(default_buffer_size)
73 {
74 }
75
76 /// Construct, passing the specified argument to initialise the next layer.
77 template <typename Arg>
78 buffered_read_stream(Arg& a, std::size_t buffer_size)
79 : next_layer_(a),
80 storage_(buffer_size)
81 {
82 }
83
84 /// Get a reference to the next layer.
85 next_layer_type& next_layer()
86 {
87 return next_layer_;
88 }
89
90 /// Get a reference to the lowest layer.
91 lowest_layer_type& lowest_layer()
92 {
93 return next_layer_.lowest_layer();
94 }
95
96 /// Get a const reference to the lowest layer.
97 const lowest_layer_type& lowest_layer() const
98 {
99 return next_layer_.lowest_layer();
100 }
101
102 /// Get the executor associated with the object.
103 executor_type get_executor() BOOST_ASIO_NOEXCEPT
104 {
105 return next_layer_.lowest_layer().get_executor();
106 }
107
108 /// Close the stream.
109 void close()
110 {
111 next_layer_.close();
112 }
113
114 /// Close the stream.
115 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
116 {
117 next_layer_.close(ec);
118 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
119 }
120
121 /// Write the given data to the stream. Returns the number of bytes written.
122 /// Throws an exception on failure.
123 template <typename ConstBufferSequence>
124 std::size_t write_some(const ConstBufferSequence& buffers)
125 {
126 return next_layer_.write_some(buffers);
127 }
128
129 /// Write the given data to the stream. Returns the number of bytes written,
130 /// or 0 if an error occurred.
131 template <typename ConstBufferSequence>
132 std::size_t write_some(const ConstBufferSequence& buffers,
133 boost::system::error_code& ec)
134 {
135 return next_layer_.write_some(buffers, ec);
136 }
137
138 /// Start an asynchronous write. The data being written must be valid for the
139 /// lifetime of the asynchronous operation.
140 /**
141 * @par Completion Signature
142 * @code void(boost::system::error_code, std::size_t) @endcode
143 */
144 template <typename ConstBufferSequence,
145 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
146 std::size_t)) WriteHandler
147 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
148 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
149 void (boost::system::error_code, std::size_t))
150 async_write_some(const ConstBufferSequence& buffers,
151 BOOST_ASIO_MOVE_ARG(WriteHandler) handler
152 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
153 {
154 return next_layer_.async_write_some(buffers,
155 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
156 }
157
158 /// Fill the buffer with some data. Returns the number of bytes placed in the
159 /// buffer as a result of the operation. Throws an exception on failure.
160 std::size_t fill();
161
162 /// Fill the buffer with some data. Returns the number of bytes placed in the
163 /// buffer as a result of the operation, or 0 if an error occurred.
164 std::size_t fill(boost::system::error_code& ec);
165
166 /// Start an asynchronous fill.
167 /**
168 * @par Completion Signature
169 * @code void(boost::system::error_code, std::size_t) @endcode
170 */
171 template <
172 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
173 std::size_t)) ReadHandler
174 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
175 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
176 void (boost::system::error_code, std::size_t))
177 async_fill(
178 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
179 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type));
180
181 /// Read some data from the stream. Returns the number of bytes read. Throws
182 /// an exception on failure.
183 template <typename MutableBufferSequence>
184 std::size_t read_some(const MutableBufferSequence& buffers);
185
186 /// Read some data from the stream. Returns the number of bytes read or 0 if
187 /// an error occurred.
188 template <typename MutableBufferSequence>
189 std::size_t read_some(const MutableBufferSequence& buffers,
190 boost::system::error_code& ec);
191
192 /// Start an asynchronous read. The buffer into which the data will be read
193 /// must be valid for the lifetime of the asynchronous operation.
194 /**
195 * @par Completion Signature
196 * @code void(boost::system::error_code, std::size_t) @endcode
197 */
198 template <typename MutableBufferSequence,
199 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
200 std::size_t)) ReadHandler
201 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
202 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
203 void (boost::system::error_code, std::size_t))
204 async_read_some(const MutableBufferSequence& buffers,
205 BOOST_ASIO_MOVE_ARG(ReadHandler) handler
206 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type));
207
208 /// Peek at the incoming data on the stream. Returns the number of bytes read.
209 /// Throws an exception on failure.
210 template <typename MutableBufferSequence>
211 std::size_t peek(const MutableBufferSequence& buffers);
212
213 /// Peek at the incoming data on the stream. Returns the number of bytes read,
214 /// or 0 if an error occurred.
215 template <typename MutableBufferSequence>
216 std::size_t peek(const MutableBufferSequence& buffers,
217 boost::system::error_code& ec);
218
219 /// Determine the amount of data that may be read without blocking.
220 std::size_t in_avail()
221 {
222 return storage_.size();
223 }
224
225 /// Determine the amount of data that may be read without blocking.
226 std::size_t in_avail(boost::system::error_code& ec)
227 {
228 ec = boost::system::error_code();
229 return storage_.size();
230 }
231
232 private:
233 /// Copy data out of the internal buffer to the specified target buffer.
234 /// Returns the number of bytes copied.
235 template <typename MutableBufferSequence>
236 std::size_t copy(const MutableBufferSequence& buffers)
237 {
238 std::size_t bytes_copied = boost::asio::buffer_copy(
239 buffers, storage_.data(), storage_.size());
240 storage_.consume(bytes_copied);
241 return bytes_copied;
242 }
243
244 /// Copy data from the internal buffer to the specified target buffer, without
245 /// removing the data from the internal buffer. Returns the number of bytes
246 /// copied.
247 template <typename MutableBufferSequence>
248 std::size_t peek_copy(const MutableBufferSequence& buffers)
249 {
250 return boost::asio::buffer_copy(buffers, storage_.data(), storage_.size());
251 }
252
253 /// The next layer.
254 Stream next_layer_;
255
256 // The data in the buffer.
257 detail::buffered_stream_storage storage_;
258 };
259
260 } // namespace asio
261 } // namespace boost
262
263 #include <boost/asio/detail/pop_options.hpp>
264
265 #include <boost/asio/impl/buffered_read_stream.hpp>
266
267 #endif // BOOST_ASIO_BUFFERED_READ_STREAM_HPP