Automatically break large buffers into smaller chunks when using read(),

async_read(), write() or async_write().
This commit is contained in:
chris_kohlhoff 2007-01-13 13:30:12 +00:00
parent 49238b394c
commit 268ce68b78
3 changed files with 31 additions and 13 deletions

View File

@ -49,21 +49,30 @@ public:
: at_end_(at_end),
first_(first),
begin_remainder_(begin_remainder),
end_remainder_(end_remainder)
end_remainder_(end_remainder),
offset_(0)
{
}
private:
friend class boost::iterator_core_access;
enum { max_size = 65536 };
void increment()
{
if (!at_end_)
{
if (begin_remainder_ == end_remainder_)
if (begin_remainder_ == end_remainder_
|| offset_ + buffer_size(first_) >= max_size)
{
at_end_ = true;
}
else
{
offset_ += buffer_size(first_);
first_ = *begin_remainder_++;
}
}
}
@ -81,13 +90,14 @@ private:
const Buffer& dereference() const
{
return first_;
return buffer(first_, max_size - offset_);
}
bool at_end_;
Buffer first_;
Buffer_Iterator begin_remainder_;
Buffer_Iterator end_remainder_;
std::size_t offset_;
};
// A proxy for a sub-range in a list of buffers.

View File

@ -121,7 +121,10 @@ namespace detail
class read_handler
{
public:
read_handler(AsyncReadStream& stream, const MutableBufferSequence& buffers,
typedef asio::detail::consuming_buffers<
mutable_buffer, MutableBufferSequence> buffers_type;
read_handler(AsyncReadStream& stream, const buffers_type& buffers,
CompletionCondition completion_condition, ReadHandler handler)
: stream_(stream),
buffers_(buffers),
@ -149,8 +152,7 @@ namespace detail
//private:
AsyncReadStream& stream_;
asio::detail::consuming_buffers<
mutable_buffer, MutableBufferSequence> buffers_;
buffers_type buffers_;
std::size_t total_transferred_;
CompletionCondition completion_condition_;
ReadHandler handler_;
@ -193,10 +195,12 @@ template <typename AsyncReadStream, typename MutableBufferSequence,
inline void async_read(AsyncReadStream& s, const MutableBufferSequence& buffers,
CompletionCondition completion_condition, ReadHandler handler)
{
s.async_read_some(buffers,
asio::detail::consuming_buffers<
mutable_buffer, MutableBufferSequence> tmp(buffers);
s.async_read_some(tmp,
detail::read_handler<AsyncReadStream, MutableBufferSequence,
CompletionCondition, ReadHandler>(
s, buffers, completion_condition, handler));
s, tmp, completion_condition, handler));
}
template <typename AsyncReadStream, typename MutableBufferSequence,

View File

@ -107,7 +107,10 @@ namespace detail
class write_handler
{
public:
write_handler(AsyncWriteStream& stream, const ConstBufferSequence& buffers,
typedef asio::detail::consuming_buffers<
const_buffer, ConstBufferSequence> buffers_type;
write_handler(AsyncWriteStream& stream, const buffers_type& buffers,
CompletionCondition completion_condition, WriteHandler handler)
: stream_(stream),
buffers_(buffers),
@ -135,8 +138,7 @@ namespace detail
//private:
AsyncWriteStream& stream_;
asio::detail::consuming_buffers<
const_buffer, ConstBufferSequence> buffers_;
buffers_type buffers_;
std::size_t total_transferred_;
CompletionCondition completion_condition_;
WriteHandler handler_;
@ -179,10 +181,12 @@ template <typename AsyncWriteStream, typename ConstBufferSequence,
inline void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
CompletionCondition completion_condition, WriteHandler handler)
{
s.async_write_some(buffers,
asio::detail::consuming_buffers<
const_buffer, ConstBufferSequence> tmp(buffers);
s.async_write_some(tmp,
detail::write_handler<AsyncWriteStream, ConstBufferSequence,
CompletionCondition, WriteHandler>(
s, buffers, completion_condition, handler));
s, tmp, completion_condition, handler));
}
template <typename AsyncWriteStream, typename ConstBufferSequence,