2 // buffered_write_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 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_WRITE_STREAM_HPP
12 #define BOOST_ASIO_BUFFERED_WRITE_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/buffered_write_stream_fwd.hpp>
21 #include <boost/asio/buffer.hpp>
22 #include <boost/asio/completion_condition.hpp>
23 #include <boost/asio/detail/bind_handler.hpp>
24 #include <boost/asio/detail/buffered_stream_storage.hpp>
25 #include <boost/asio/detail/noncopyable.hpp>
26 #include <boost/asio/detail/type_traits.hpp>
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/io_context.hpp>
29 #include <boost/asio/write.hpp>
31 #include <boost/asio/detail/push_options.hpp>
36 /// Adds buffering to the write-related operations of a stream.
38 * The buffered_write_stream class template can be used to add buffering to the
39 * synchronous and asynchronous write 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_write_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_write_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_write_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 /// Flush all data from the buffer to the next layer. Returns the number of
139 /// bytes written to the next layer on the last write operation. Throws an
140 /// exception on failure.
143 /// Flush all data from the buffer to the next layer. Returns the number of
144 /// bytes written to the next layer on the last write operation, or 0 if an
146 std::size_t flush(boost::system::error_code& ec);
148 /// Start an asynchronous flush.
149 template <typename WriteHandler>
150 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
151 void (boost::system::error_code, std::size_t))
152 async_flush(BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
154 /// Write the given data to the stream. Returns the number of bytes written.
155 /// Throws an exception on failure.
156 template <typename ConstBufferSequence>
157 std::size_t write_some(const ConstBufferSequence& buffers);
159 /// Write the given data to the stream. Returns the number of bytes written,
160 /// or 0 if an error occurred and the error handler did not throw.
161 template <typename ConstBufferSequence>
162 std::size_t write_some(const ConstBufferSequence& buffers,
163 boost::system::error_code& ec);
165 /// Start an asynchronous write. The data being written must be valid for the
166 /// lifetime of the asynchronous operation.
167 template <typename ConstBufferSequence, typename WriteHandler>
168 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
169 void (boost::system::error_code, std::size_t))
170 async_write_some(const ConstBufferSequence& buffers,
171 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
173 /// Read some data from the stream. Returns the number of bytes read. Throws
174 /// an exception on failure.
175 template <typename MutableBufferSequence>
176 std::size_t read_some(const MutableBufferSequence& buffers)
178 return next_layer_.read_some(buffers);
181 /// Read some data from the stream. Returns the number of bytes read or 0 if
182 /// an error occurred.
183 template <typename MutableBufferSequence>
184 std::size_t read_some(const MutableBufferSequence& buffers,
185 boost::system::error_code& ec)
187 return next_layer_.read_some(buffers, ec);
190 /// Start an asynchronous read. The buffer into which the data will be read
191 /// must be valid for the lifetime of the asynchronous operation.
192 template <typename MutableBufferSequence, typename ReadHandler>
193 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
194 void (boost::system::error_code, std::size_t))
195 async_read_some(const MutableBufferSequence& buffers,
196 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
198 return next_layer_.async_read_some(buffers,
199 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
202 /// Peek at the incoming data on the stream. Returns the number of bytes read.
203 /// Throws an exception on failure.
204 template <typename MutableBufferSequence>
205 std::size_t peek(const MutableBufferSequence& buffers)
207 return next_layer_.peek(buffers);
210 /// Peek at the incoming data on the stream. Returns the number of bytes read,
211 /// or 0 if an error occurred.
212 template <typename MutableBufferSequence>
213 std::size_t peek(const MutableBufferSequence& buffers,
214 boost::system::error_code& ec)
216 return next_layer_.peek(buffers, ec);
219 /// Determine the amount of data that may be read without blocking.
220 std::size_t in_avail()
222 return next_layer_.in_avail();
225 /// Determine the amount of data that may be read without blocking.
226 std::size_t in_avail(boost::system::error_code& ec)
228 return next_layer_.in_avail(ec);
232 /// Copy data into the internal buffer from the specified source buffer.
233 /// Returns the number of bytes copied.
234 template <typename ConstBufferSequence>
235 std::size_t copy(const ConstBufferSequence& buffers);
240 // The data in the buffer.
241 detail::buffered_stream_storage storage_;
247 #include <boost/asio/detail/pop_options.hpp>
249 #include <boost/asio/impl/buffered_write_stream.hpp>
251 #endif // BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP