Skip to content

Commit

Permalink
Avoid coroutine lambda captures in tcp_channel
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 19, 2024
1 parent 601fd3a commit e366465
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
28 changes: 17 additions & 11 deletions nano/node/transport/tcp_channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ())
Expand Down
1 change: 1 addition & 0 deletions nano/node/transport/tcp_channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class tcp_channel final : public nano::transport::channel, public std::enable_sh
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 &);

Expand Down

0 comments on commit e366465

Please sign in to comment.