From 54422b62740a4e8ab265db9a6dac6907f69d5643 Mon Sep 17 00:00:00 2001 From: cnbatch Date: Sun, 27 Oct 2024 15:44:11 +0800 Subject: [PATCH] stability fix --- src/main.cpp | 4 ++-- src/networks/client.cpp | 4 ++-- src/networks/connections.cpp | 12 +++++++++--- src/networks/relay.cpp | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 045a8e2..55bfcc5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,7 +16,7 @@ int main(int argc, char *argv[]) { #ifdef __cpp_lib_format - std::cout << std::format("{} version 20241026\n", app_name); + std::cout << std::format("{} version 20241027\n", app_name); if (argc <= 1) { std::cout << std::format("Usage: {} config1.conf\n", app_name); @@ -24,7 +24,7 @@ int main(int argc, char *argv[]) return 0; } #else - std::cout << app_name << " version 20241026\n"; + std::cout << app_name << " version 20241027\n"; if (argc <= 1) { std::cout << "Usage: " << app_name << " config1.conf\n"; diff --git a/src/networks/client.cpp b/src/networks/client.cpp index 72a41ad..3049caa 100644 --- a/src/networks/client.cpp +++ b/src/networks/client.cpp @@ -499,11 +499,11 @@ void client_mode::cleanup_expiring_forwarders() int64_t time_elapsed = calculate_difference(time_right_now, expire_time); if (time_elapsed > CLEANUP_WAITS / 3 && - time_elapsed <= CLEANUP_WAITS / 3 * 2 && + time_elapsed <= CLEANUP_WAITS * 2 / 3 && forwarder_ptr != nullptr) forwarder_ptr->pause(true); - if (time_elapsed > CLEANUP_WAITS / 3 * 2 && + if (time_elapsed > CLEANUP_WAITS * 2 / 3 && forwarder_ptr != nullptr) forwarder_ptr->stop(); diff --git a/src/networks/connections.cpp b/src/networks/connections.cpp index 3ea123b..a031b92 100644 --- a/src/networks/connections.cpp +++ b/src/networks/connections.cpp @@ -344,7 +344,7 @@ void udp_server::handle_receive(std::unique_ptr buffer_cache, const a { if (error) { - if (!connection_socket.is_open()) + if (error == asio::error::operation_aborted || !connection_socket.is_open()) return; } @@ -396,6 +396,9 @@ void udp_client::pause(bool set_as_pause) void udp_client::stop() { + bool expect = true; + if (stopped.compare_exchange_strong(expect, true)) + return; stopped.store(true); callback = empty_udp_callback; if (connection_socket.is_open()) @@ -544,7 +547,10 @@ void udp_client::start_receive() std::unique_ptr buffer_cache = std::make_unique(BUFFER_SIZE); uint8_t *buffer_raw_ptr = buffer_cache.get(); - connection_socket.async_receive_from(asio::buffer(buffer_raw_ptr, BUFFER_SIZE), incoming_endpoint, + auto asio_buffer = asio::buffer(buffer_raw_ptr, BUFFER_SIZE); + if (!connection_socket.is_open()) + return; + connection_socket.async_receive_from(asio_buffer, incoming_endpoint, [buffer = std::move(buffer_cache), this](const asio::error_code &error, std::size_t bytes_transferred) mutable { handle_receive(std::move(buffer), error, bytes_transferred); @@ -555,7 +561,7 @@ void udp_client::handle_receive(std::unique_ptr buffer_cache, const a { if (error) { - if (!stopped.load() && !paused.load() && connection_socket.is_open()) + if (error != asio::error::operation_aborted && !stopped.load() && !paused.load() && connection_socket.is_open()) start_receive(); return; } diff --git a/src/networks/relay.cpp b/src/networks/relay.cpp index 2b7d0c6..47e1cf2 100644 --- a/src/networks/relay.cpp +++ b/src/networks/relay.cpp @@ -748,11 +748,11 @@ void relay_mode::cleanup_expiring_data_connections() std::shared_ptr egress_forwarder = std::atomic_load(&udp_session_ptr->egress_forwarder); if (time_elapsed > CLEANUP_WAITS / 3 && - time_elapsed <= CLEANUP_WAITS / 3 * 2 && + time_elapsed <= CLEANUP_WAITS * 2 / 3 && egress_forwarder != nullptr) egress_forwarder->pause(true); - if (time_elapsed > CLEANUP_WAITS / 3 * 2 && + if (time_elapsed > CLEANUP_WAITS * 2 / 3 && egress_forwarder != nullptr) egress_forwarder->stop();