2 // buffered_read_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_BUFFERED_READ_STREAM_HPP
12 #define BOOST_ASIO_BUFFERED_READ_STREAM_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
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>
31 #include <boost/asio/detail/push_options.hpp>
36 /// Adds buffering to the read-related operations of a stream.
38 * The buffered_read_stream class template can be used to add buffering to the
39 * synchronous and asynchronous read operations of a stream.
42 * @e Distinct @e objects: Safe.@n
43 * @e Shared @e objects: Unsafe.
46 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
48 template <typename Stream>
49 class buffered_read_stream
53 /// The type of the next layer.
54 typedef typename remove_reference<Stream>::type next_layer_type;
56 /// The type of the lowest layer.
57 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
59 /// The type of the executor associated with the object.
60 typedef typename lowest_layer_type::executor_type executor_type;
62 #if defined(GENERATING_DOCUMENTATION)
63 /// The default buffer size.
64 static const std::size_t default_buffer_size = implementation_defined;
66 BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
69 /// Construct, passing the specified argument to initialise the next layer.
70 template <typename Arg>
71 explicit buffered_read_stream(Arg& a)
73 storage_(default_buffer_size)
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)
85 /// Get a reference to the next layer.
86 next_layer_type& next_layer()
91 /// Get a reference to the lowest layer.
92 lowest_layer_type& lowest_layer()
94 return next_layer_.lowest_layer();
97 /// Get a const reference to the lowest layer.
98 const lowest_layer_type& lowest_layer() const
100 return next_layer_.lowest_layer();
103 /// Get the executor associated with the object.
104 executor_type get_executor() BOOST_ASIO_NOEXCEPT
106 return next_layer_.lowest_layer().get_executor();
109 #if !defined(BOOST_ASIO_NO_DEPRECATED)
110 /// (Deprecated: Use get_executor().) Get the io_context associated with the
112 boost::asio::io_context& get_io_context()
114 return next_layer_.get_io_context();
117 /// (Deprecated: Use get_executor().) Get the io_context associated with the
119 boost::asio::io_context& get_io_service()
121 return next_layer_.get_io_service();
123 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
125 /// Close the stream.
131 /// Close the stream.
132 BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
134 next_layer_.close(ec);
135 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
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)
143 return next_layer_.write_some(buffers);
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)
152 return next_layer_.write_some(buffers, ec);
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)
163 return next_layer_.async_write_some(buffers,
164 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
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.
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);
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);
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);
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);
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);
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);
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);
211 /// Determine the amount of data that may be read without blocking.
212 std::size_t in_avail()
214 return storage_.size();
217 /// Determine the amount of data that may be read without blocking.
218 std::size_t in_avail(boost::system::error_code& ec)
220 ec = boost::system::error_code();
221 return storage_.size();
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)
230 std::size_t bytes_copied = boost::asio::buffer_copy(
231 buffers, storage_.data(), storage_.size());
232 storage_.consume(bytes_copied);
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
239 template <typename MutableBufferSequence>
240 std::size_t peek_copy(const MutableBufferSequence& buffers)
242 return boost::asio::buffer_copy(buffers, storage_.data(), storage_.size());
248 // The data in the buffer.
249 detail::buffered_stream_storage storage_;
255 #include <boost/asio/detail/pop_options.hpp>
257 #include <boost/asio/impl/buffered_read_stream.hpp>
259 #endif // BOOST_ASIO_BUFFERED_READ_STREAM_HPP