From 865254351ddd4b569ab6c7c59229dec70bb0e6de Mon Sep 17 00:00:00 2001 From: terrakuh Date: Sun, 5 Jun 2022 11:53:35 +0000 Subject: [PATCH] Update --- curlio/request.hpp | 17 +++++++---------- curlio/response.hpp | 32 +++++++++++++------------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/curlio/request.hpp b/curlio/request.hpp index 44c93f9..54600db 100644 --- a/curlio/request.hpp +++ b/curlio/request.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -124,26 +125,22 @@ template inline auto Request::async_write_some(const Const_buffer_sequence& buffers, Token&& token) { return boost::asio::async_initiate( - [this, buffers](auto handler) { + [this, buffers](auto&& handler) { const auto ptr = _data.lock(); auto executor = boost::asio::get_associated_executor(handler, ptr->executor); // can immediately finish if (ptr == nullptr || ptr->status & detail::finished) { - boost::asio::post(executor, - [handler = std::move(handler)]() mutable { handler(boost::asio::error::eof, 0); }); + boost::asio::post(executor, std::bind(std::move(handler), boost::asio::error::eof, 0)); } else if (_send_handler) { - boost::asio::post(executor, - [handler = std::move(handler)]() mutable { handler(Code::multiple_writes, 0); }); + boost::asio::post(executor, std::bind(std::move(handler), Code::multiple_writes, 0)); } else { // set write handler when cURL calls the write callback - _send_handler = [this, buffers, handler = std::move(handler), executor = ptr->executor]( - boost::system::error_code ec, void* data, std::size_t size) mutable { + _send_handler = [this, buffers, handler = std::move(handler), + executor](boost::system::error_code ec, void* data, std::size_t size) mutable { // copy data and finish const std::size_t copied = boost::asio::buffer_copy(boost::asio::buffer(data, size), buffers); - boost::asio::post(executor, [handler = std::move(handler), ec, copied]() mutable { - std::move(handler)(ec, copied); - }); + boost::asio::post(executor, std::bind(std::move(handler), ec, copied)); return copied; }; diff --git a/curlio/response.hpp b/curlio/response.hpp index 146a932..7e0f09c 100644 --- a/curlio/response.hpp +++ b/curlio/response.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -131,20 +132,17 @@ template inline auto Response::async_await_completion(Token&& token) { return boost::asio::async_initiate( - [this](auto handler) { + [this](auto&& handler) { const auto ptr = _data.lock(); auto executor = boost::asio::get_associated_executor(handler, ptr->executor); + if (ptr == nullptr || ptr->status & detail::finished) { - boost::asio::post(executor, - [handler = std::move(handler)]() mutable { handler(boost::system::error_code{}); }); + boost::asio::post(executor, std::bind(std::move(handler), boost::system::error_code{})); } else if (_finish_handler) { - boost::asio::post(executor, [handler = std::move(handler)]() mutable { - handler(Code::multiple_completion_awaitings); - }); + boost::asio::post(executor, std::bind(std::move(handler), Code::multiple_completion_awaitings)); } else { _finish_handler = [handler = std::move(handler), executor]() mutable { - boost::asio::post( - executor, [handler = std::move(handler)]() mutable { handler(boost::system::error_code{}); }); + boost::asio::post(executor, std::bind(std::move(handler), boost::system::error_code{})); }; } }, @@ -156,26 +154,23 @@ inline auto Response::async_read_some(const Mutable_buffer_sequence& buffers, To { CURLIO_TRACE("Reading some"); return boost::asio::async_initiate( - [this, buffers](auto handler) { + [this, buffers](auto&& handler) { const auto ptr = _data.lock(); auto executor = boost::asio::get_associated_executor(handler, ptr->executor); + // executor = ptr->executor; // can immediately finish if (_input_buffer.size() > 0) { const std::size_t copied = boost::asio::buffer_copy(buffers, _input_buffer.data()); _input_buffer.consume(copied); - boost::asio::post(executor, [copied, handler = std::move(handler)]() mutable { - handler(boost::system::error_code{}, copied); - }); + boost::asio::post(executor, std::bind(std::move(handler), boost::system::error_code{}, copied)); } else if (_receive_handler) { - boost::asio::post( - executor, [handler = std::move(handler)]() mutable { handler(boost::system::error_code{}, 0); }); + boost::asio::post(executor, std::bind(std::move(handler), Code::multiple_reads, 0)); } else if (ptr == nullptr || ptr->status & detail::finished) { - boost::asio::post(executor, - [handler = std::move(handler)]() mutable { handler(boost::asio::error::eof, 0); }); + boost::asio::post(executor, std::bind(std::move(handler), boost::asio::error::eof, 0)); } else { // set write handler when cURL calls the write callback - _receive_handler = [this, buffers, executor = ptr->executor, + _receive_handler = [this, buffers, executor=ptr->executor, handler = std::move(handler)](boost::system::error_code ec) mutable { std::size_t copied = 0; // copy data and finish @@ -184,8 +179,7 @@ inline auto Response::async_read_some(const Mutable_buffer_sequence& buffers, To _input_buffer.consume(copied); CURLIO_DEBUG("Read " << copied << " bytes"); } - boost::asio::post(executor, - [handler = std::move(handler), ec, copied]() mutable { handler(ec, copied); }); + boost::asio::post(executor, std::bind(std::move(handler), ec, copied)); }; // TODO check for errors