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 24, 2024
1 parent 9be1b81 commit b06d3e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
30 changes: 18 additions & 12 deletions nano/node/transport/tcp_channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand All @@ -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 b06d3e2

Please sign in to comment.