2 // buffered_read_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_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/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_read_stream_fwd.hpp>
28 #include <boost/asio/buffer.hpp>
29 #include <boost/asio/error.hpp>
30 #include <boost/asio/io_service.hpp>
31 #include <boost/asio/detail/bind_handler.hpp>
32 #include <boost/asio/detail/buffer_resize_guard.hpp>
33 #include <boost/asio/detail/buffered_stream_storage.hpp>
34 #include <boost/asio/detail/noncopyable.hpp>
39 /// Adds buffering to the read-related operations of a stream.
41 * The buffered_read_stream class template can be used to add buffering to the
42 * synchronous and asynchronous read operations of a stream.
45 * @e Distinct @e objects: Safe.@n
46 * @e Shared @e objects: Unsafe.
49 * AsyncReadStream, AsyncWriteStream, Stream, Sync_Read_Stream, SyncWriteStream.
51 template <typename Stream
>
52 class buffered_read_stream
56 /// The type of the next layer.
57 typedef typename
boost::remove_reference
<Stream
>::type next_layer_type
;
59 /// The type of the lowest layer.
60 typedef typename
next_layer_type::lowest_layer_type lowest_layer_type
;
62 #if defined(GENERATING_DOCUMENTATION)
63 /// The default buffer size.
64 static const std::size_t default_buffer_size
= implementation_defined
;
66 BOOST_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 /// (Deprecated: use get_io_service().) Get the io_service associated with
105 boost::asio::io_service
& io_service()
107 return next_layer_
.get_io_service();
110 /// Get the io_service associated with the object.
111 boost::asio::io_service
& get_io_service()
113 return next_layer_
.get_io_service();
116 /// Close the stream.
122 /// Close the stream.
123 boost::system::error_code
close(boost::system::error_code
& ec
)
125 return next_layer_
.close(ec
);
128 /// Write the given data to the stream. Returns the number of bytes written.
129 /// Throws an exception on failure.
130 template <typename ConstBufferSequence
>
131 std::size_t write_some(const ConstBufferSequence
& buffers
)
133 return next_layer_
.write_some(buffers
);
136 /// Write the given data to the stream. Returns the number of bytes written,
137 /// or 0 if an error occurred.
138 template <typename ConstBufferSequence
>
139 std::size_t write_some(const ConstBufferSequence
& buffers
,
140 boost::system::error_code
& ec
)
142 return next_layer_
.write_some(buffers
, ec
);
145 /// Start an asynchronous write. The data being written must be valid for the
146 /// lifetime of the asynchronous operation.
147 template <typename ConstBufferSequence
, typename WriteHandler
>
148 void async_write_some(const ConstBufferSequence
& buffers
,
149 WriteHandler handler
)
151 next_layer_
.async_write_some(buffers
, handler
);
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 detail::buffer_resize_guard
<detail::buffered_stream_storage
>
159 resize_guard(storage_
);
160 std::size_t previous_size
= storage_
.size();
161 storage_
.resize(storage_
.capacity());
162 storage_
.resize(previous_size
+ next_layer_
.read_some(buffer(
163 storage_
.data() + previous_size
,
164 storage_
.size() - previous_size
)));
165 resize_guard
.commit();
166 return storage_
.size() - previous_size
;
169 /// Fill the buffer with some data. Returns the number of bytes placed in the
170 /// buffer as a result of the operation, or 0 if an error occurred.
171 std::size_t fill(boost::system::error_code
& ec
)
173 detail::buffer_resize_guard
<detail::buffered_stream_storage
>
174 resize_guard(storage_
);
175 std::size_t previous_size
= storage_
.size();
176 storage_
.resize(storage_
.capacity());
177 storage_
.resize(previous_size
+ next_layer_
.read_some(buffer(
178 storage_
.data() + previous_size
,
179 storage_
.size() - previous_size
),
181 resize_guard
.commit();
182 return storage_
.size() - previous_size
;
185 template <typename ReadHandler
>
189 fill_handler(boost::asio::io_service
& io_service
,
190 detail::buffered_stream_storage
& storage
,
191 std::size_t previous_size
, ReadHandler handler
)
192 : io_service_(io_service
),
194 previous_size_(previous_size
),
199 void operator()(const boost::system::error_code
& ec
,
200 std::size_t bytes_transferred
)
202 storage_
.resize(previous_size_
+ bytes_transferred
);
203 io_service_
.dispatch(detail::bind_handler(
204 handler_
, ec
, bytes_transferred
));
208 boost::asio::io_service
& io_service_
;
209 detail::buffered_stream_storage
& storage_
;
210 std::size_t previous_size_
;
211 ReadHandler handler_
;
214 /// Start an asynchronous fill.
215 template <typename ReadHandler
>
216 void async_fill(ReadHandler handler
)
218 std::size_t previous_size
= storage_
.size();
219 storage_
.resize(storage_
.capacity());
220 next_layer_
.async_read_some(
222 storage_
.data() + previous_size
,
223 storage_
.size() - previous_size
),
224 fill_handler
<ReadHandler
>(get_io_service(),
225 storage_
, previous_size
, handler
));
228 /// Read some data from the stream. Returns the number of bytes read. Throws
229 /// an exception on failure.
230 template <typename MutableBufferSequence
>
231 std::size_t read_some(const MutableBufferSequence
& buffers
)
233 if (storage_
.empty())
235 return copy(buffers
);
238 /// Read some data from the stream. Returns the number of bytes read or 0 if
239 /// an error occurred.
240 template <typename MutableBufferSequence
>
241 std::size_t read_some(const MutableBufferSequence
& buffers
,
242 boost::system::error_code
& ec
)
244 ec
= boost::system::error_code();
245 if (storage_
.empty() && !fill(ec
))
247 return copy(buffers
);
250 template <typename MutableBufferSequence
, typename ReadHandler
>
251 class read_some_handler
254 read_some_handler(boost::asio::io_service
& io_service
,
255 detail::buffered_stream_storage
& storage
,
256 const MutableBufferSequence
& buffers
, ReadHandler handler
)
257 : io_service_(io_service
),
264 void operator()(const boost::system::error_code
& ec
, std::size_t)
266 if (ec
|| storage_
.empty())
268 std::size_t length
= 0;
269 io_service_
.dispatch(detail::bind_handler(handler_
, ec
, length
));
273 using namespace std
; // For memcpy.
275 std::size_t bytes_avail
= storage_
.size();
276 std::size_t bytes_copied
= 0;
278 typename
MutableBufferSequence::const_iterator iter
= buffers_
.begin();
279 typename
MutableBufferSequence::const_iterator end
= buffers_
.end();
280 for (; iter
!= end
&& bytes_avail
> 0; ++iter
)
282 std::size_t max_length
= buffer_size(*iter
);
283 std::size_t length
= (max_length
< bytes_avail
)
284 ? max_length
: bytes_avail
;
285 memcpy(buffer_cast
<void*>(*iter
),
286 storage_
.data() + bytes_copied
, length
);
287 bytes_copied
+= length
;
288 bytes_avail
-= length
;
291 storage_
.consume(bytes_copied
);
292 io_service_
.dispatch(detail::bind_handler(handler_
, ec
, bytes_copied
));
297 boost::asio::io_service
& io_service_
;
298 detail::buffered_stream_storage
& storage_
;
299 MutableBufferSequence buffers_
;
300 ReadHandler handler_
;
303 /// Start an asynchronous read. The buffer into which the data will be read
304 /// must be valid for the lifetime of the asynchronous operation.
305 template <typename MutableBufferSequence
, typename ReadHandler
>
306 void async_read_some(const MutableBufferSequence
& buffers
,
309 if (storage_
.empty())
311 async_fill(read_some_handler
<MutableBufferSequence
, ReadHandler
>(
312 get_io_service(), storage_
, buffers
, handler
));
316 std::size_t length
= copy(buffers
);
317 get_io_service().post(detail::bind_handler(
318 handler
, boost::system::error_code(), length
));
322 /// Peek at the incoming data on the stream. Returns the number of bytes read.
323 /// Throws an exception on failure.
324 template <typename MutableBufferSequence
>
325 std::size_t peek(const MutableBufferSequence
& buffers
)
327 if (storage_
.empty())
329 return peek_copy(buffers
);
332 /// Peek at the incoming data on the stream. Returns the number of bytes read,
333 /// or 0 if an error occurred.
334 template <typename MutableBufferSequence
>
335 std::size_t peek(const MutableBufferSequence
& buffers
,
336 boost::system::error_code
& ec
)
338 ec
= boost::system::error_code();
339 if (storage_
.empty() && !fill(ec
))
341 return peek_copy(buffers
);
344 /// Determine the amount of data that may be read without blocking.
345 std::size_t in_avail()
347 return storage_
.size();
350 /// Determine the amount of data that may be read without blocking.
351 std::size_t in_avail(boost::system::error_code
& ec
)
353 ec
= boost::system::error_code();
354 return storage_
.size();
358 /// Copy data out of the internal buffer to the specified target buffer.
359 /// Returns the number of bytes copied.
360 template <typename MutableBufferSequence
>
361 std::size_t copy(const MutableBufferSequence
& buffers
)
363 using namespace std
; // For memcpy.
365 std::size_t bytes_avail
= storage_
.size();
366 std::size_t bytes_copied
= 0;
368 typename
MutableBufferSequence::const_iterator iter
= buffers
.begin();
369 typename
MutableBufferSequence::const_iterator end
= buffers
.end();
370 for (; iter
!= end
&& bytes_avail
> 0; ++iter
)
372 std::size_t max_length
= buffer_size(*iter
);
373 std::size_t length
= (max_length
< bytes_avail
)
374 ? max_length
: bytes_avail
;
375 memcpy(buffer_cast
<void*>(*iter
), storage_
.data() + bytes_copied
, length
);
376 bytes_copied
+= length
;
377 bytes_avail
-= length
;
380 storage_
.consume(bytes_copied
);
384 /// Copy data from the internal buffer to the specified target buffer, without
385 /// removing the data from the internal buffer. Returns the number of bytes
387 template <typename MutableBufferSequence
>
388 std::size_t peek_copy(const MutableBufferSequence
& buffers
)
390 using namespace std
; // For memcpy.
392 std::size_t bytes_avail
= storage_
.size();
393 std::size_t bytes_copied
= 0;
395 typename
MutableBufferSequence::const_iterator iter
= buffers
.begin();
396 typename
MutableBufferSequence::const_iterator end
= buffers
.end();
397 for (; iter
!= end
&& bytes_avail
> 0; ++iter
)
399 std::size_t max_length
= buffer_size(*iter
);
400 std::size_t length
= (max_length
< bytes_avail
)
401 ? max_length
: bytes_avail
;
402 memcpy(buffer_cast
<void*>(*iter
), storage_
.data() + bytes_copied
, length
);
403 bytes_copied
+= length
;
404 bytes_avail
-= length
;
413 // The data in the buffer.
414 detail::buffered_stream_storage storage_
;
420 #include <boost/asio/detail/pop_options.hpp>
422 #endif // BOOST_ASIO_BUFFERED_READ_STREAM_HPP