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_STREAM_HPP
12 #define BOOST_ASIO_BUFFERED_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.hpp>
22 #include <boost/asio/buffered_write_stream.hpp>
23 #include <boost/asio/buffered_stream_fwd.hpp>
24 #include <boost/asio/detail/noncopyable.hpp>
25 #include <boost/asio/error.hpp>
26 #include <boost/asio/io_service.hpp>
28 #include <boost/asio/detail/push_options.hpp>
33 /// Adds buffering to the read- and write-related operations of a stream.
35 * The buffered_stream class template can be used to add buffering to the
36 * synchronous and asynchronous read and write operations of a stream.
39 * @e Distinct @e objects: Safe.@n
40 * @e Shared @e objects: Unsafe.
43 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
45 template <typename Stream>
50 /// The type of the next layer.
51 typedef typename remove_reference<Stream>::type next_layer_type;
53 /// The type of the lowest layer.
54 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
56 /// Construct, passing the specified argument to initialise the next layer.
57 template <typename Arg>
58 explicit buffered_stream(Arg& a)
59 : inner_stream_impl_(a),
60 stream_impl_(inner_stream_impl_)
64 /// Construct, passing the specified argument to initialise the next layer.
65 template <typename Arg>
66 explicit buffered_stream(Arg& a, std::size_t read_buffer_size,
67 std::size_t write_buffer_size)
68 : inner_stream_impl_(a, write_buffer_size),
69 stream_impl_(inner_stream_impl_, read_buffer_size)
73 /// Get a reference to the next layer.
74 next_layer_type& next_layer()
76 return stream_impl_.next_layer().next_layer();
79 /// Get a reference to the lowest layer.
80 lowest_layer_type& lowest_layer()
82 return stream_impl_.lowest_layer();
85 /// Get a const reference to the lowest layer.
86 const lowest_layer_type& lowest_layer() const
88 return stream_impl_.lowest_layer();
91 /// Get the io_service associated with the object.
92 boost::asio::io_service& get_io_service()
94 return stream_impl_.get_io_service();
100 stream_impl_.close();
103 /// Close the stream.
104 boost::system::error_code close(boost::system::error_code& ec)
106 return stream_impl_.close(ec);
109 /// Flush all data from the buffer to the next layer. Returns the number of
110 /// bytes written to the next layer on the last write operation. Throws an
111 /// exception on failure.
114 return stream_impl_.next_layer().flush();
117 /// Flush all data from the buffer to the next layer. Returns the number of
118 /// bytes written to the next layer on the last write operation, or 0 if an
120 std::size_t flush(boost::system::error_code& ec)
122 return stream_impl_.next_layer().flush(ec);
125 /// Start an asynchronous flush.
126 template <typename WriteHandler>
127 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
128 void (boost::system::error_code, std::size_t))
129 async_flush(BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
131 return stream_impl_.next_layer().async_flush(
132 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
135 /// Write the given data to the stream. Returns the number of bytes written.
136 /// Throws an exception on failure.
137 template <typename ConstBufferSequence>
138 std::size_t write_some(const ConstBufferSequence& buffers)
140 return stream_impl_.write_some(buffers);
143 /// Write the given data to the stream. Returns the number of bytes written,
144 /// or 0 if an error occurred.
145 template <typename ConstBufferSequence>
146 std::size_t write_some(const ConstBufferSequence& buffers,
147 boost::system::error_code& ec)
149 return stream_impl_.write_some(buffers, ec);
152 /// Start an asynchronous write. The data being written must be valid for the
153 /// lifetime of the asynchronous operation.
154 template <typename ConstBufferSequence, typename WriteHandler>
155 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
156 void (boost::system::error_code, std::size_t))
157 async_write_some(const ConstBufferSequence& buffers,
158 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
160 return stream_impl_.async_write_some(buffers,
161 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
164 /// Fill the buffer with some data. Returns the number of bytes placed in the
165 /// buffer as a result of the operation. Throws an exception on failure.
168 return stream_impl_.fill();
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 return stream_impl_.fill(ec);
178 /// Start an asynchronous fill.
179 template <typename ReadHandler>
180 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
181 void (boost::system::error_code, std::size_t))
182 async_fill(BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
184 return stream_impl_.async_fill(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
187 /// Read some data from the stream. Returns the number of bytes read. Throws
188 /// an exception on failure.
189 template <typename MutableBufferSequence>
190 std::size_t read_some(const MutableBufferSequence& buffers)
192 return stream_impl_.read_some(buffers);
195 /// Read some data from the stream. Returns the number of bytes read or 0 if
196 /// an error occurred.
197 template <typename MutableBufferSequence>
198 std::size_t read_some(const MutableBufferSequence& buffers,
199 boost::system::error_code& ec)
201 return stream_impl_.read_some(buffers, ec);
204 /// Start an asynchronous read. The buffer into which the data will be read
205 /// must be valid for the lifetime of the asynchronous operation.
206 template <typename MutableBufferSequence, typename ReadHandler>
207 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
208 void (boost::system::error_code, std::size_t))
209 async_read_some(const MutableBufferSequence& buffers,
210 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
212 return stream_impl_.async_read_some(buffers,
213 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
216 /// Peek at the incoming data on the stream. Returns the number of bytes read.
217 /// Throws an exception on failure.
218 template <typename MutableBufferSequence>
219 std::size_t peek(const MutableBufferSequence& buffers)
221 return stream_impl_.peek(buffers);
224 /// Peek at the incoming data on the stream. Returns the number of bytes read,
225 /// or 0 if an error occurred.
226 template <typename MutableBufferSequence>
227 std::size_t peek(const MutableBufferSequence& buffers,
228 boost::system::error_code& ec)
230 return stream_impl_.peek(buffers, ec);
233 /// Determine the amount of data that may be read without blocking.
234 std::size_t in_avail()
236 return stream_impl_.in_avail();
239 /// Determine the amount of data that may be read without blocking.
240 std::size_t in_avail(boost::system::error_code& ec)
242 return stream_impl_.in_avail(ec);
246 // The buffered write stream.
247 typedef buffered_write_stream<Stream> write_stream_type;
248 write_stream_type inner_stream_impl_;
250 // The buffered read stream.
251 typedef buffered_read_stream<write_stream_type&> read_stream_type;
252 read_stream_type stream_impl_;
258 #include <boost/asio/detail/pop_options.hpp>
260 #endif // BOOST_ASIO_BUFFERED_STREAM_HPP