Avoid coroutine lambda captures in tcp_channel

This commit is contained in:
Piotr Wójcik 2024-11-19 00:19:20 +01:00
commit 483062f22b
2 changed files with 19 additions and 12 deletions

View file

@ -25,7 +25,7 @@ nano::transport::tcp_channel::tcp_channel (nano::node & node_a, std::shared_ptr<
nano::transport::tcp_channel::~tcp_channel ()
{
close ();
debug_assert (!sending_task.joinable ());
release_assert (!sending_task.joinable ());
}
void nano::transport::tcp_channel::close ()
@ -37,20 +37,26 @@ void nano::transport::tcp_channel::close ()
void nano::transport::tcp_channel::start ()
{
sending_task = nano::async::task (strand, [this] (nano::async::condition & condition) -> asio::awaitable<void> {
try
{
co_await run_sending (condition);
}
catch (boost::system::system_error const & ex)
{
// Operation aborted is expected when cancelling the acceptor
debug_assert (ex.code () == asio::error::operation_aborted);
}
debug_assert (strand.running_in_this_thread ());
sending_task = nano::async::task (strand, [this] (nano::async::condition & condition) {
return start_sending (condition); // This is not a coroutine, but a corotuine factory
});
}
asio::awaitable<void> nano::transport::tcp_channel::start_sending (nano::async::condition & condition)
{
debug_assert (strand.running_in_this_thread ());
try
{
co_await run_sending (condition);
}
catch (boost::system::system_error const & ex)
{
// Operation aborted is expected when cancelling the acceptor
debug_assert (ex.code () == asio::error::operation_aborted);
}
debug_assert (strand.running_in_this_thread ());
}
void nano::transport::tcp_channel::stop ()
{
if (sending_task.joinable ())

View file

@ -72,6 +72,7 @@ private:
void start ();
void stop ();
asio::awaitable<void> start_sending (nano::async::condition &);
asio::awaitable<void> run_sending (nano::async::condition &);
asio::awaitable<void> send_one (traffic_type, tcp_channel_queue::entry_t const &);