Specialise the async_write implementation for buffer sequences based on a

boost::array or std::array of two elements.
This commit is contained in:
Christopher Kohlhoff 2011-09-21 10:00:07 +10:00
parent 018788a93a
commit ace36f3703

View File

@ -17,9 +17,11 @@
#include "asio/buffer.hpp"
#include "asio/completion_condition.hpp"
#include "asio/detail/array_fwd.hpp"
#include "asio/detail/base_from_completion_cond.hpp"
#include "asio/detail/bind_handler.hpp"
#include "asio/detail/consuming_buffers.hpp"
#include "asio/detail/dependent_type.hpp"
#include "asio/detail/handler_alloc_helpers.hpp"
#include "asio/detail/handler_invoke_helpers.hpp"
#include "asio/detail/handler_type_requirements.hpp"
@ -338,6 +340,168 @@ namespace detail
WriteHandler handler_;
};
template <typename AsyncWriteStream, typename Elem,
typename CompletionCondition, typename WriteHandler>
class write_op<AsyncWriteStream, boost::array<Elem, 2>,
CompletionCondition, WriteHandler>
: detail::base_from_completion_cond<CompletionCondition>
{
public:
write_op(AsyncWriteStream& stream, const boost::array<Elem, 2>& buffers,
CompletionCondition completion_condition, WriteHandler& handler)
: detail::base_from_completion_cond<
CompletionCondition>(completion_condition),
stream_(stream),
buffers_(buffers),
total_transferred_(0),
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
{
}
#if defined(ASIO_HAS_MOVE)
write_op(const write_op& other)
: detail::base_from_completion_cond<CompletionCondition>(other),
stream_(other.stream_),
buffers_(other.buffers_),
total_transferred_(other.total_transferred_),
handler_(other.handler_)
{
}
write_op(write_op&& other)
: detail::base_from_completion_cond<CompletionCondition>(other),
stream_(other.stream_),
buffers_(other.buffers_),
total_transferred_(other.total_transferred_),
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
{
}
#endif // defined(ASIO_HAS_MOVE)
void operator()(const asio::error_code& ec,
std::size_t bytes_transferred, int start = 0)
{
typename asio::detail::dependent_type<Elem,
boost::array<asio::const_buffer, 2> >::type bufs = {{
asio::const_buffer(buffers_[0]),
asio::const_buffer(buffers_[1]) }};
std::size_t buffer_size0 = asio::buffer_size(bufs[0]);
std::size_t buffer_size1 = asio::buffer_size(bufs[1]);
std::size_t n = 0;
switch (start)
{
case 1:
n = this->check_for_completion(ec, total_transferred_);
for (;;)
{
bufs[0] = asio::buffer(bufs[0] + total_transferred_, n);
bufs[1] = asio::buffer(
bufs[1] + (total_transferred_ < buffer_size0
? 0 : total_transferred_ - buffer_size0),
n - asio::buffer_size(bufs[0]));
stream_.async_write_some(bufs, ASIO_MOVE_CAST(write_op)(*this));
return; default:
total_transferred_ += bytes_transferred;
if ((!ec && bytes_transferred == 0)
|| (n = this->check_for_completion(ec, total_transferred_)) == 0
|| total_transferred_ == buffer_size0 + buffer_size1)
break;
}
handler_(ec, static_cast<const std::size_t&>(total_transferred_));
}
}
//private:
AsyncWriteStream& stream_;
boost::array<Elem, 2> buffers_;
std::size_t total_transferred_;
WriteHandler handler_;
};
#if defined(ASIO_HAS_STD_ARRAY)
template <typename AsyncWriteStream, typename Elem,
typename CompletionCondition, typename WriteHandler>
class write_op<AsyncWriteStream, std::array<Elem, 2>,
CompletionCondition, WriteHandler>
: detail::base_from_completion_cond<CompletionCondition>
{
public:
write_op(AsyncWriteStream& stream, const std::array<Elem, 2>& buffers,
CompletionCondition completion_condition, WriteHandler& handler)
: detail::base_from_completion_cond<
CompletionCondition>(completion_condition),
stream_(stream),
buffers_(buffers),
total_transferred_(0),
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
{
}
#if defined(ASIO_HAS_MOVE)
write_op(const write_op& other)
: detail::base_from_completion_cond<CompletionCondition>(other),
stream_(other.stream_),
buffers_(other.buffers_),
total_transferred_(other.total_transferred_),
handler_(other.handler_)
{
}
write_op(write_op&& other)
: detail::base_from_completion_cond<CompletionCondition>(other),
stream_(other.stream_),
buffers_(other.buffers_),
total_transferred_(other.total_transferred_),
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
{
}
#endif // defined(ASIO_HAS_MOVE)
void operator()(const asio::error_code& ec,
std::size_t bytes_transferred, int start = 0)
{
typename asio::detail::dependent_type<Elem,
std::array<asio::const_buffer, 2> >::type bufs = {{
asio::const_buffer(buffers_[0]),
asio::const_buffer(buffers_[1]) }};
std::size_t buffer_size0 = asio::buffer_size(bufs[0]);
std::size_t buffer_size1 = asio::buffer_size(bufs[1]);
std::size_t n = 0;
switch (start)
{
case 1:
n = this->check_for_completion(ec, total_transferred_);
for (;;)
{
bufs[0] = asio::buffer(bufs[0] + total_transferred_, n);
bufs[1] = asio::buffer(
bufs[1] + (total_transferred_ < buffer_size0
? 0 : total_transferred_ - buffer_size0),
n - asio::buffer_size(bufs[0]));
stream_.async_write_some(bufs, ASIO_MOVE_CAST(write_op)(*this));
return; default:
total_transferred_ += bytes_transferred;
if ((!ec && bytes_transferred == 0)
|| (n = this->check_for_completion(ec, total_transferred_)) == 0
|| total_transferred_ == buffer_size0 + buffer_size1)
break;
}
handler_(ec, static_cast<const std::size_t&>(total_transferred_));
}
}
//private:
AsyncWriteStream& stream_;
std::array<Elem, 2> buffers_;
std::size_t total_transferred_;
WriteHandler handler_;
};
#endif // defined(ASIO_HAS_STD_ARRAY)
template <typename AsyncWriteStream, typename ConstBufferSequence,
typename CompletionCondition, typename WriteHandler>
inline void* asio_handler_allocate(std::size_t size,