2 // buffered_write_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2008 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/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
23 #include <boost/config.hpp>
24 #include <boost/type_traits.hpp>
25 #include <boost/asio/detail/pop_options.hpp>
27 #include <boost/asio/buffered_write_stream_fwd.hpp>
28 #include <boost/asio/buffer.hpp>
29 #include <boost/asio/completion_condition.hpp>
30 #include <boost/asio/error.hpp>
31 #include <boost/asio/io_service.hpp>
32 #include <boost/asio/write.hpp>
33 #include <boost/asio/detail/bind_handler.hpp>
34 #include <boost/asio/detail/buffered_stream_storage.hpp>
35 #include <boost/asio/detail/noncopyable.hpp>
40 /// Adds buffering to the write-related operations of a stream.
42 * The buffered_write_stream class template can be used to add buffering to the
43 * synchronous and asynchronous write operations of a stream.
46 * @e Distinct @e objects: Safe.@n
47 * @e Shared @e objects: Unsafe.
50 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
52 template <typename Stream
>
53 class buffered_write_stream
57 /// The type of the next layer.
58 typedef typename
boost::remove_reference
<Stream
>::type next_layer_type
;
60 /// The type of the lowest layer.
61 typedef typename
next_layer_type::lowest_layer_type lowest_layer_type
;
63 #if defined(GENERATING_DOCUMENTATION)
64 /// The default buffer size.
65 static const std::size_t default_buffer_size
= implementation_defined
;
67 BOOST_STATIC_CONSTANT(std::size_t, default_buffer_size
= 1024);
70 /// Construct, passing the specified argument to initialise the next layer.
71 template <typename Arg
>
72 explicit buffered_write_stream(Arg
& a
)
74 storage_(default_buffer_size
)
78 /// Construct, passing the specified argument to initialise the next layer.
79 template <typename Arg
>
80 buffered_write_stream(Arg
& a
, std::size_t buffer_size
)
86 /// Get a reference to the next layer.
87 next_layer_type
& next_layer()
92 /// Get a reference to the lowest layer.
93 lowest_layer_type
& lowest_layer()
95 return next_layer_
.lowest_layer();
98 /// Get a const reference to the lowest layer.
99 const lowest_layer_type
& lowest_layer() const
101 return next_layer_
.lowest_layer();
104 /// (Deprecated: use get_io_service().) Get the io_service associated with
106 boost::asio::io_service
& io_service()
108 return next_layer_
.get_io_service();
111 /// Get the io_service associated with the object.
112 boost::asio::io_service
& get_io_service()
114 return next_layer_
.get_io_service();
117 /// Close the stream.
123 /// Close the stream.
124 boost::system::error_code
close(boost::system::error_code
& ec
)
126 return next_layer_
.close(ec
);
129 /// Flush all data from the buffer to the next layer. Returns the number of
130 /// bytes written to the next layer on the last write operation. Throws an
131 /// exception on failure.
134 std::size_t bytes_written
= write(next_layer_
,
135 buffer(storage_
.data(), storage_
.size()));
136 storage_
.consume(bytes_written
);
137 return bytes_written
;
140 /// Flush all data from the buffer to the next layer. Returns the number of
141 /// bytes written to the next layer on the last write operation, or 0 if an
143 std::size_t flush(boost::system::error_code
& ec
)
145 std::size_t bytes_written
= write(next_layer_
,
146 buffer(storage_
.data(), storage_
.size()),
148 storage_
.consume(bytes_written
);
149 return bytes_written
;
152 template <typename WriteHandler
>
156 flush_handler(boost::asio::io_service
& io_service
,
157 detail::buffered_stream_storage
& storage
, WriteHandler handler
)
158 : io_service_(io_service
),
164 void operator()(const boost::system::error_code
& ec
,
165 std::size_t bytes_written
)
167 storage_
.consume(bytes_written
);
168 io_service_
.dispatch(detail::bind_handler(handler_
, ec
, bytes_written
));
172 boost::asio::io_service
& io_service_
;
173 detail::buffered_stream_storage
& storage_
;
174 WriteHandler handler_
;
177 /// Start an asynchronous flush.
178 template <typename WriteHandler
>
179 void async_flush(WriteHandler handler
)
181 async_write(next_layer_
, buffer(storage_
.data(), storage_
.size()),
182 flush_handler
<WriteHandler
>(get_io_service(), storage_
, handler
));
185 /// Write the given data to the stream. Returns the number of bytes written.
186 /// Throws an exception on failure.
187 template <typename ConstBufferSequence
>
188 std::size_t write_some(const ConstBufferSequence
& buffers
)
190 if (storage_
.size() == storage_
.capacity())
192 return copy(buffers
);
195 /// Write the given data to the stream. Returns the number of bytes written,
196 /// or 0 if an error occurred and the error handler did not throw.
197 template <typename ConstBufferSequence
>
198 std::size_t write_some(const ConstBufferSequence
& buffers
,
199 boost::system::error_code
& ec
)
201 ec
= boost::system::error_code();
202 if (storage_
.size() == storage_
.capacity() && !flush(ec
))
204 return copy(buffers
);
207 template <typename ConstBufferSequence
, typename WriteHandler
>
208 class write_some_handler
211 write_some_handler(boost::asio::io_service
& io_service
,
212 detail::buffered_stream_storage
& storage
,
213 const ConstBufferSequence
& buffers
, WriteHandler handler
)
214 : io_service_(io_service
),
221 void operator()(const boost::system::error_code
& ec
, std::size_t)
225 std::size_t length
= 0;
226 io_service_
.dispatch(detail::bind_handler(handler_
, ec
, length
));
230 using namespace std
; // For memcpy.
232 std::size_t orig_size
= storage_
.size();
233 std::size_t space_avail
= storage_
.capacity() - orig_size
;
234 std::size_t bytes_copied
= 0;
236 typename
ConstBufferSequence::const_iterator iter
= buffers_
.begin();
237 typename
ConstBufferSequence::const_iterator end
= buffers_
.end();
238 for (; iter
!= end
&& space_avail
> 0; ++iter
)
240 std::size_t bytes_avail
= buffer_size(*iter
);
241 std::size_t length
= (bytes_avail
< space_avail
)
242 ? bytes_avail
: space_avail
;
243 storage_
.resize(orig_size
+ bytes_copied
+ length
);
244 memcpy(storage_
.data() + orig_size
+ bytes_copied
,
245 buffer_cast
<const void*>(*iter
), length
);
246 bytes_copied
+= length
;
247 space_avail
-= length
;
250 io_service_
.dispatch(detail::bind_handler(handler_
, ec
, bytes_copied
));
255 boost::asio::io_service
& io_service_
;
256 detail::buffered_stream_storage
& storage_
;
257 ConstBufferSequence buffers_
;
258 WriteHandler handler_
;
261 /// Start an asynchronous write. The data being written must be valid for the
262 /// lifetime of the asynchronous operation.
263 template <typename ConstBufferSequence
, typename WriteHandler
>
264 void async_write_some(const ConstBufferSequence
& buffers
,
265 WriteHandler handler
)
267 if (storage_
.size() == storage_
.capacity())
269 async_flush(write_some_handler
<ConstBufferSequence
, WriteHandler
>(
270 get_io_service(), storage_
, buffers
, handler
));
274 std::size_t bytes_copied
= copy(buffers
);
275 get_io_service().post(detail::bind_handler(
276 handler
, boost::system::error_code(), bytes_copied
));
280 /// Read some data from the stream. Returns the number of bytes read. Throws
281 /// an exception on failure.
282 template <typename MutableBufferSequence
>
283 std::size_t read_some(const MutableBufferSequence
& buffers
)
285 return next_layer_
.read_some(buffers
);
288 /// Read some data from the stream. Returns the number of bytes read or 0 if
289 /// an error occurred.
290 template <typename MutableBufferSequence
>
291 std::size_t read_some(const MutableBufferSequence
& buffers
,
292 boost::system::error_code
& ec
)
294 return next_layer_
.read_some(buffers
, ec
);
297 /// Start an asynchronous read. The buffer into which the data will be read
298 /// must be valid for the lifetime of the asynchronous operation.
299 template <typename MutableBufferSequence
, typename ReadHandler
>
300 void async_read_some(const MutableBufferSequence
& buffers
,
303 next_layer_
.async_read_some(buffers
, handler
);
306 /// Peek at the incoming data on the stream. Returns the number of bytes read.
307 /// Throws an exception on failure.
308 template <typename MutableBufferSequence
>
309 std::size_t peek(const MutableBufferSequence
& buffers
)
311 return next_layer_
.peek(buffers
);
314 /// Peek at the incoming data on the stream. Returns the number of bytes read,
315 /// or 0 if an error occurred.
316 template <typename MutableBufferSequence
>
317 std::size_t peek(const MutableBufferSequence
& buffers
,
318 boost::system::error_code
& ec
)
320 return next_layer_
.peek(buffers
, ec
);
323 /// Determine the amount of data that may be read without blocking.
324 std::size_t in_avail()
326 return next_layer_
.in_avail();
329 /// Determine the amount of data that may be read without blocking.
330 std::size_t in_avail(boost::system::error_code
& ec
)
332 return next_layer_
.in_avail(ec
);
336 /// Copy data into the internal buffer from the specified source buffer.
337 /// Returns the number of bytes copied.
338 template <typename ConstBufferSequence
>
339 std::size_t copy(const ConstBufferSequence
& buffers
)
341 using namespace std
; // For memcpy.
343 std::size_t orig_size
= storage_
.size();
344 std::size_t space_avail
= storage_
.capacity() - orig_size
;
345 std::size_t bytes_copied
= 0;
347 typename
ConstBufferSequence::const_iterator iter
= buffers
.begin();
348 typename
ConstBufferSequence::const_iterator end
= buffers
.end();
349 for (; iter
!= end
&& space_avail
> 0; ++iter
)
351 std::size_t bytes_avail
= buffer_size(*iter
);
352 std::size_t length
= (bytes_avail
< space_avail
)
353 ? bytes_avail
: space_avail
;
354 storage_
.resize(orig_size
+ bytes_copied
+ length
);
355 memcpy(storage_
.data() + orig_size
+ bytes_copied
,
356 buffer_cast
<const void*>(*iter
), length
);
357 bytes_copied
+= length
;
358 space_avail
-= length
;
367 // The data in the buffer.
368 detail::buffered_stream_storage storage_
;
374 #include <boost/asio/detail/pop_options.hpp>
376 #endif // BOOST_ASIO_BUFFERED_WRITE_STREAM_HPP