Skip to content

Commit

Permalink
Use async accept
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Apr 1, 2024
1 parent bde2f1d commit afd3dcf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 21 additions & 3 deletions nano/node/transport/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <nano/node/transport/tcp_listener.hpp>
#include <nano/node/transport/tcp_server.hpp>

#include <boost/asio/use_future.hpp>

#include <memory>

using namespace std::chrono_literals;
Expand Down Expand Up @@ -86,11 +88,16 @@ void nano::transport::tcp_listener::stop ()
{
nano::lock_guard<nano::mutex> lock{ mutex };
stopped = true;

boost::system::error_code ec;
acceptor.close (ec); // Best effort to close the acceptor, ignore errors
if (ec)
{
logger.debug (nano::log::type::tcp_listener, "Error while closing acceptor: {}", ec.message ());
}
}
condition.notify_all ();

acceptor.close ();

if (thread.joinable ())
{
thread.join ();
Expand Down Expand Up @@ -193,9 +200,20 @@ void nano::transport::tcp_listener::run ()
}
}

boost::asio::ip::tcp::socket nano::transport::tcp_listener::accept_socket ()
{
std::future<boost::asio::ip::tcp::socket> future;
{
nano::unique_lock<nano::mutex> lock{ mutex };
future = acceptor.async_accept (boost::asio::use_future);
}
future.wait ();
return future.get ();
}

auto nano::transport::tcp_listener::accept_one () -> accept_result
{
auto raw_socket = acceptor.accept ();
auto raw_socket = accept_socket ();
auto const remote_endpoint = raw_socket.remote_endpoint ();
auto const local_endpoint = raw_socket.local_endpoint ();

Expand Down
2 changes: 2 additions & 0 deletions nano/node/transport/tcp_listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class tcp_listener final

accept_result accept_one ();
accept_result check_limits (boost::asio::ip::address const & ip);
boost::asio::ip::tcp::socket accept_socket ();

size_t count_per_ip (boost::asio::ip::address const & ip) const;
size_t count_per_subnetwork (boost::asio::ip::address const & ip) const;

Expand Down

0 comments on commit afd3dcf

Please sign in to comment.