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():