c++ – How do I combine multiple boost::asio::experimental::promise?
Yes, using boost::asio::async_compose is the approach here. It allows you to compose multiple asynchronous operations (like TCP connect followed by SSL handshake) into a single operation that returns a boost::asio::experimental::promise, which can be used with co_await or .operator().
You can implement it like this:
boost::asio::experimental::promise>
SocketWrapper::asyncConnectAndHandshake(
const boost::asio::ip::basic_resolver_results<:asio::ip::tcp>& resolver_results)
{
namespace asio = boost::asio;
auto executor = underlyingSocket().get_executor();
return asio::async_compose<
decltype(asio::experimental::use_promise),
void(boost::system::error_code)>(
[this, resolver_results](auto& self) mutable {
asio::async_connect(underlyingSocket(), resolver_results,
[this, &self](boost::system::error_code ec, const boost::asio::ip::tcp::endpoint&) mutable {
if (ec || !isSSL()) {
self.complete(ec);
return;
}
stream().async_handshake(asio::ssl::stream_base::client,
[self = std::move(self)](boost::system::error_code ec) mutable {
self.complete(ec);
});
});
},
asio::experimental::use_promise,
executor);
}
Read more here: Source link
