Skip to content

Commit

Permalink
Fix completion handler invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
terrakuh committed Jun 5, 2022
1 parent 89e279c commit 0e23926
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 26 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [v0.3.2]
### Fixed
- Post completion handlers for execution instead of direct invocation

## [v0.3.1]
### Fixed
- Remove easy handle before destruction
Expand All @@ -26,7 +30,8 @@
### Fixed
- Cookie share support

[Unreleased]: https://github.com/terrakuh/curlio/compare/v0.3.1..dev
[Unreleased]: https://github.com/terrakuh/curlio/compare/v0.3.2..dev
[v0.3.2]: https://github.com/terrakuh/curlio/compare/v0.3.1..v0.3.2
[v0.3.1]: https://github.com/terrakuh/curlio/compare/v0.3..v0.3.1
[v0.3]: https://github.com/terrakuh/curlio/compare/v0.2..v0.3
[v0.2]: https://github.com/terrakuh/curlio/compare/v0.1..v0.2
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16)

project(
curlio
VERSION 0.3.0
VERSION 0.3.2
DESCRIPTION "The simple glue for cURL and Boost.ASIO"
HOMEPAGE_URL "https://github.com/terrakuh/curlio"
LANGUAGES CXX
Expand Down
7 changes: 4 additions & 3 deletions curlio/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

#if defined(CURLIO_ENABLE_LOGGING)
# include <iostream>
# include <thread>

# define CURLIO_TRACE(stream) static_cast<void>(0)
// # define CURLIO_TRACE(stream) std::cout << "TRACE: " << stream << "\n"
# define CURLIO_DEBUG(stream) std::cout << "DEBUG: " << stream << "\n"
# define CURLIO_INFO(stream) std::cout << "INFO : " << stream << "\n"
# define CURLIO_ERROR(stream) std::cout << "ERROR: " << stream << "\n"
# define CURLIO_DEBUG(stream) std::cout << "DEBUG " << std::this_thread::get_id() << ":" << stream << "\n"
# define CURLIO_INFO(stream) std::cout << "INFO " << std::this_thread::get_id() << ":" << stream << "\n"
# define CURLIO_ERROR(stream) std::cout << "ERROR " << std::this_thread::get_id() << ":" << stream << "\n"
#else
# define CURLIO_TRACE(stream) static_cast<void>(0)
# define CURLIO_DEBUG(stream) static_cast<void>(0)
Expand Down
12 changes: 7 additions & 5 deletions curlio/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,21 @@ inline auto Request::async_write_some(const Const_buffer_sequence& buffers, Toke
return boost::asio::async_initiate<Token, void(boost::system::error_code, std::size_t)>(
[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) {
std::move(handler)(boost::asio::error::eof, 0);
boost::asio::post(executor,
[handler = std::move(handler)]() mutable { handler(boost::asio::error::eof, 0); });
} else if (_send_handler) {
std::move(handler)(Code::multiple_writes, 0);
boost::asio::post(executor,
[handler = std::move(handler)]() mutable { handler(Code::multiple_writes, 0); });
} else {
// set write handler when cURL calls the write callback
_send_handler = [this, buffers, handler = std::move(handler)](boost::system::error_code ec,
void* data, std::size_t size) mutable {
_send_handler = [this, buffers, handler = std::move(handler), executor = ptr->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);
auto executor = boost::asio::get_associated_executor(handler);
boost::asio::post(executor, [handler = std::move(handler), ec, copied]() mutable {
std::move(handler)(ec, copied);
});
Expand Down
35 changes: 20 additions & 15 deletions curlio/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,18 @@ inline auto Response::async_await_completion(Token&& token)
return boost::asio::async_initiate<Token, void(boost::system::error_code)>(
[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) {
std::move(handler)(boost::system::error_code{});
boost::asio::post(executor,
[handler = std::move(handler)]() mutable { handler(boost::system::error_code{}); });
} else if (_finish_handler) {
std::move(handler)(Code::multiple_completion_awaitings);
boost::asio::post(executor, [handler = std::move(handler)]() mutable {
handler(Code::multiple_completion_awaitings);
});
} else {
_finish_handler = [handler = std::move(handler)]() mutable {
auto executor = boost::asio::get_associated_executor(handler);
boost::asio::post(executor, [handler = std::move(handler)]() mutable {
std::move(handler)(boost::system::error_code{});
});
_finish_handler = [handler = std::move(handler), executor]() mutable {
boost::asio::post(
executor, [handler = std::move(handler)]() mutable { handler(boost::system::error_code{}); });
};
}
},
Expand All @@ -156,19 +158,24 @@ inline auto Response::async_read_some(const Mutable_buffer_sequence& buffers, To
return boost::asio::async_initiate<Token, void(boost::system::error_code, std::size_t)>(
[this, buffers](auto handler) {
const auto ptr = _data.lock();
auto executor = boost::asio::get_associated_executor(handler, 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);
std::move(handler)(boost::system::error_code{}, copied);
boost::asio::post(executor, [copied, handler = std::move(handler)]() mutable {
handler(boost::system::error_code{}, copied);
});
} else if (_receive_handler) {
std::move(handler)(Code::multiple_reads, 0);
boost::asio::post(
executor, [handler = std::move(handler)]() mutable { handler(boost::system::error_code{}, 0); });
} else if (ptr == nullptr || ptr->status & detail::finished) {
std::move(handler)(boost::asio::error::eof, 0);
boost::asio::post(executor,
[handler = std::move(handler)]() mutable { handler(boost::asio::error::eof, 0); });
} else {
// set write handler when cURL calls the write callback
_receive_handler = [this, buffers,
_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
Expand All @@ -177,10 +184,8 @@ inline auto Response::async_read_some(const Mutable_buffer_sequence& buffers, To
_input_buffer.consume(copied);
CURLIO_DEBUG("Read " << copied << " bytes");
}
auto executor = boost::asio::get_associated_executor(handler);
boost::asio::post(executor, [handler = std::move(handler), ec, copied]() mutable {
std::move(handler)(ec, copied);
});
boost::asio::post(executor,
[handler = std::move(handler), ec, copied]() mutable { handler(ec, copied); });
};

// TODO check for errors
Expand Down
2 changes: 1 addition & 1 deletion curlio/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ inline void Session::_async_wait(boost::asio::ip::tcp::socket& socket,
const int mask = (type == boost::asio::socket_base::wait_read ? CURL_CSELECT_IN : CURL_CSELECT_OUT) |
(ec ? CURL_CSELECT_ERR : 0);
const auto code = curl_multi_socket_action(_multi_handle, handle, mask, &still_running);
CURLIO_ERROR("====================" << curl_multi_strerror(code));
if (code != CURLM_OK) {
CURLIO_ERROR("Socket action: " << curl_multi_strerror(code));
}
_clean_finished();

Expand Down

0 comments on commit 0e23926

Please sign in to comment.