From 60beb1f3637d3cc36b8cb9af8a482cd46cdf5c06 Mon Sep 17 00:00:00 2001 From: cheney Date: Tue, 26 May 2026 21:50:53 +0800 Subject: [PATCH] =?UTF-8?q?client=203=20=E6=AC=A1=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=80=80=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/proxy_client.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/proxy/proxy_client.py b/proxy/proxy_client.py index 3c89e74..420beea 100644 --- a/proxy/proxy_client.py +++ b/proxy/proxy_client.py @@ -264,6 +264,9 @@ def listen_for_requests(sock): def connect_to_b_device(b_host, b_port): """Connect to device B and handle requests""" + failure_count = 0 + max_failures = 3 + while True: try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -278,6 +281,10 @@ def connect_to_b_device(b_host, b_port): if not response or b"PROXY_CONNECTED" not in response: log_error("System", "Failed to receive connection confirmation") sock.close() + failure_count += 1 + if failure_count >= max_failures: + log_error("System", "Maximum connection attempts reached (%d), exiting..." % max_failures) + sys.exit(1) time.sleep(3) continue @@ -285,6 +292,7 @@ def connect_to_b_device(b_host, b_port): sock.sendall(b"READY\r\n") log_info("System", "Successfully connected to device B: %s:%d" % (b_host, b_port)) + failure_count = 0 listen_for_requests(sock) except socket.error as e: @@ -292,9 +300,17 @@ def connect_to_b_device(b_host, b_port): log_info("System", "Device B not ready (%s:%d), retrying in 3 seconds..." % (b_host, b_port)) else: log_error("System", "Connection error: %s" % str(e)) + failure_count += 1 + if failure_count >= max_failures: + log_error("System", "Maximum connection attempts reached (%d), exiting..." % max_failures) + sys.exit(1) time.sleep(3) except Exception as e: log_error("System", "Unexpected error: %s" % str(e)) + failure_count += 1 + if failure_count >= max_failures: + log_error("System", "Maximum connection attempts reached (%d), exiting..." % max_failures) + sys.exit(1) time.sleep(3) def main():