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