diff --git a/asio/include/asio/impl/write.hpp b/asio/include/asio/impl/write.hpp index b4e447b4..d5b83233 100644 --- a/asio/include/asio/impl/write.hpp +++ b/asio/include/asio/impl/write.hpp @@ -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 + class write_op, + CompletionCondition, WriteHandler> + : detail::base_from_completion_cond + { + public: + write_op(AsyncWriteStream& stream, const boost::array& 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(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(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 >::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(total_transferred_)); + } + } + + //private: + AsyncWriteStream& stream_; + boost::array buffers_; + std::size_t total_transferred_; + WriteHandler handler_; + }; + +#if defined(ASIO_HAS_STD_ARRAY) + + template + class write_op, + CompletionCondition, WriteHandler> + : detail::base_from_completion_cond + { + public: + write_op(AsyncWriteStream& stream, const std::array& 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(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(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 >::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(total_transferred_)); + } + } + + //private: + AsyncWriteStream& stream_; + std::array buffers_; + std::size_t total_transferred_; + WriteHandler handler_; + }; + +#endif // defined(ASIO_HAS_STD_ARRAY) + template inline void* asio_handler_allocate(std::size_t size,