From ec6835420cd9c9b592bfc2d1c1f01ceaec59596a Mon Sep 17 00:00:00 2001 From: "legolas.li" Date: Mon, 21 Oct 2024 17:51:06 +0800 Subject: [PATCH] Update: set TCP keepalive Signed-off-by: legolas.li --- adbutils/_adb.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/adbutils/_adb.py b/adbutils/_adb.py index f7d5512..cb10fd0 100644 --- a/adbutils/_adb.py +++ b/adbutils/_adb.py @@ -7,6 +7,7 @@ from __future__ import annotations import os +import platform import socket import subprocess import typing @@ -50,6 +51,13 @@ def _create_socket(self): adb_host = self.__host adb_port = self.__port s = socket.socket() + s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Set TCP keepalive + if platform.system() == "Darwin": + pass + else: + s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10) + s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3) + s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10) try: s.settimeout(3) # prevent socket hang s.connect((adb_host, adb_port))