2 // buffered_read_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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_service.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 #if defined(GENERATING_DOCUMENTATION)
60 /// The default buffer size.
61 static const std::size_t default_buffer_size = implementation_defined;
63 BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
66 /// Construct, passing the specified argument to initialise the next layer.
67 template <typename Arg>
68 explicit buffered_read_stream(Arg& a)
70 storage_(default_buffer_size)
74 /// Construct, passing the specified argument to initialise the next layer.
75 template <typename Arg>
76 buffered_read_stream(Arg& a, std::size_t buffer_size)
82 /// Get a reference to the next layer.
83 next_layer_type& next_layer()
88 /// Get a reference to the lowest layer.
89 lowest_layer_type& lowest_layer()
91 return next_layer_.lowest_layer();
94 /// Get a const reference to the lowest layer.
95 const lowest_layer_type& lowest_layer() const
97 return next_layer_.lowest_layer();
100 /// Get the io_service associated with the object.
101 boost::asio::io_service& get_io_service()
103 return next_layer_.get_io_service();
106 /// Close the stream.
112 /// Close the stream.
113 boost::system::error_code close(boost::system::error_code& ec)
115 return next_layer_.close(ec);
118 /// Write the given data to the stream. Returns the number of bytes written.
119 /// Throws an exception on failure.
120 template <typename ConstBufferSequence>
121 std::size_t write_some(const ConstBufferSequence& buffers)
123 return next_layer_.write_some(buffers);
126 /// Write the given data to the stream. Returns the number of bytes written,
127 /// or 0 if an error occurred.
128 template <typename ConstBufferSequence>
129 std::size_t write_some(const ConstBufferSequence& buffers,
130 boost::system::error_code& ec)
132 return next_layer_.write_some(buffers, ec);
135 /// Start an asynchronous write. The data being written must be valid for the
136 /// lifetime of the asynchronous operation.
137 template <typename ConstBufferSequence, typename WriteHandler>
138 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
139 void (boost::system::error_code, std::size_t))
140 async_write_some(const ConstBufferSequence& buffers,
141 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
143 detail::async_result_init<
144 WriteHandler, void (boost::system::error_code, std::size_t)> init(
145 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
147 next_layer_.async_write_some(buffers,
148 BOOST_ASIO_MOVE_CAST(BOOST_ASIO_HANDLER_TYPE(WriteHandler,
149 void (boost::system::error_code, std::size_t)))(init.handler));
151 return init.result.get();
154 /// Fill the buffer with some data. Returns the number of bytes placed in the
155 /// buffer as a result of the operation. Throws an exception on failure.
158 /// Fill the buffer with some data. Returns the number of bytes placed in the
159 /// buffer as a result of the operation, or 0 if an error occurred.
160 std::size_t fill(boost::system::error_code& ec);
162 /// Start an asynchronous fill.
163 template <typename ReadHandler>
164 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
165 void (boost::system::error_code, std::size_t))
166 async_fill(BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
168 /// Read some data from the stream. Returns the number of bytes read. Throws
169 /// an exception on failure.
170 template <typename MutableBufferSequence>
171 std::size_t read_some(const MutableBufferSequence& buffers);
173 /// Read some data from the stream. Returns the number of bytes read or 0 if
174 /// an error occurred.
175 template <typename MutableBufferSequence>
176 std::size_t read_some(const MutableBufferSequence& buffers,
177 boost::system::error_code& ec);
179 /// Start an asynchronous read. The buffer into which the data will be read
180 /// must be valid for the lifetime of the asynchronous operation.
181 template <typename MutableBufferSequence, typename ReadHandler>
182 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
183 void (boost::system::error_code, std::size_t))
184 async_read_some(const MutableBufferSequence& buffers,
185 BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
187 /// Peek at the incoming data on the stream. Returns the number of bytes read.
188 /// Throws an exception on failure.
189 template <typename MutableBufferSequence>
190 std::size_t peek(const MutableBufferSequence& buffers);
192 /// Peek at the incoming data on the stream. Returns the number of bytes read,
193 /// or 0 if an error occurred.
194 template <typename MutableBufferSequence>
195 std::size_t peek(const MutableBufferSequence& buffers,
196 boost::system::error_code& ec);
198 /// Determine the amount of data that may be read without blocking.
199 std::size_t in_avail()
201 return storage_.size();
204 /// Determine the amount of data that may be read without blocking.
205 std::size_t in_avail(boost::system::error_code& ec)
207 ec = boost::system::error_code();
208 return storage_.size();
212 /// Copy data out of the internal buffer to the specified target buffer.
213 /// Returns the number of bytes copied.
214 template <typename MutableBufferSequence>
215 std::size_t copy(const MutableBufferSequence& buffers)
217 std::size_t bytes_copied = boost::asio::buffer_copy(
218 buffers, storage_.data(), storage_.size());
219 storage_.consume(bytes_copied);
223 /// Copy data from the internal buffer to the specified target buffer, without
224 /// removing the data from the internal buffer. Returns the number of bytes
226 template <typename MutableBufferSequence>
227 std::size_t peek_copy(const MutableBufferSequence& buffers)
229 return boost::asio::buffer_copy(buffers, storage_.data(), storage_.size());
235 // The data in the buffer.
236 detail::buffered_stream_storage storage_;
242 #include <boost/asio/detail/pop_options.hpp>
244 #include <boost/asio/impl/buffered_read_stream.hpp>
246 #endif // BOOST_ASIO_BUFFERED_READ_STREAM_HPP