Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 22, 2024
1 parent fc2c12c commit 0b7b39c
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions nano/node/transport/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,23 @@ void nano::transport::tcp_listener::start (std::function<bool (std::shared_ptr<n
acceptor.set_option (boost::asio::ip::tcp::acceptor::reuse_address (true));
acceptor.bind (local);
acceptor.listen (boost::asio::socket_base::max_listen_connections);

logger.info (nano::log::type::tcp_listener, "Listening for incoming connections on: {}", fmt::streamed (acceptor.local_endpoint ()));
}
catch (boost::system::system_error const & ex)
{
node.logger.critical (nano::log::type::tcp_listener, "Error while binding for incoming TCP: {} (port: {})", ex.what (), acceptor.local_endpoint ().port ());
node.logger.critical (nano::log::type::tcp_listener, "Error while binding for incoming TCP: {} (port: {})", ex.what (), port);

throw std::runtime_error (ex.code ().message ());
}

thread = std::thread ([this, callback_a] {
nano::thread_role::set (nano::thread_role::name::tcp_listener);
try
{
logger.info (nano::log::type::tcp_listener, "Starting TCP listener on: {}", fmt::streamed (acceptor.local_endpoint ()));
logger.debug (nano::log::type::tcp_listener, "Starting acceptor thread");
run ();
logger.info (nano::log::type::tcp_listener, "Stopped TCP listener");
logger.debug (nano::log::type::tcp_listener, "Stopped acceptor thread");
}
catch (std::exception const & ex)
{
Expand All @@ -95,6 +98,8 @@ void nano::transport::tcp_listener::start (std::function<bool (std::shared_ptr<n

void nano::transport::tcp_listener::stop ()
{
logger.info (nano::log::type::tcp_listener, "Stopping listeninig for incoming connections and closing all sockets...");

{
nano::lock_guard<nano::mutex> lock{ mutex };
stopped = true;
Expand Down Expand Up @@ -161,16 +166,22 @@ void nano::transport::tcp_listener::run ()

try
{
auto result = accept_one ();
accept_one ();
}
catch (boost::system::system_error const & ex)
{
stats.inc (nano::stat::type::tcp_listener, nano::stat::detail::accept_failure, nano::stat::dir::in);
logger.error (nano::log::type::tcp_listener, "Error accepting incoming connection: {}", ex.what ());
logger.log (stopped ? nano::log::level::debug : nano::log::level::error, // Avoid logging expected errors when stopping
nano::log::type::tcp_listener, "Error accepting incoming connection: {}", ex.what ());
}

std::this_thread::sleep_for (100ms); // Sleep for a while to prevent busy loop
}
if (!stopped)
{
debug_assert (false, "acceptor stopped unexpectedly");
logger.error (nano::log::type::tcp_listener, "Acceptor stopped unexpectedly");
}
}

auto nano::transport::tcp_listener::accept_one () -> check_result
Expand Down

0 comments on commit 0b7b39c

Please sign in to comment.