@@ -167,24 +167,50 @@ class TimeoutException(Exception):
167167 pass
168168
169169
170+ def connect (hostname , port , timeout = TIMEOUT ):
171+ """
172+ Connect to ADB server.
173+ :param hostname: the hostname
174+ :param port: the port
175+ :param timeout: the timeout in seconds
176+ :return:
177+ """
178+
179+ if DEBUG :
180+ print ("AdbClient.connect(%s, %s, %s)" % (hostname , port , timeout ), file = sys .stderr )
181+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
182+ # SO_LINGER: Idea proposed by kysersozelee (#173)
183+ l_onoff = 1
184+ l_linger = 0
185+ s .setsockopt (socket .SOL_SOCKET , socket .SO_LINGER ,
186+ struct .pack ('ii' , l_onoff , l_linger ))
187+ s .settimeout (timeout )
188+ try :
189+ s .connect ((hostname , port ))
190+ except socket .error as ex :
191+ raise RuntimeError ("ERROR: Connecting to %s:%d: %s.\n Is adb running on your computer?" % (s , port , ex ))
192+ return s
193+
194+
170195class AdbClient :
171196 UP = UP
172197 DOWN = DOWN
173198 DOWN_AND_UP = DOWN_AND_UP
174199
175200 def __init__ (self , serialno = None , hostname = HOSTNAME , port = PORT , settransport = True , reconnect = True ,
176- ignoreversioncheck = False , timeout = TIMEOUT ):
201+ ignoreversioncheck = False , timeout = TIMEOUT , connect = connect ):
177202 self .Log = AdbClient .__Log (self )
178203
179204 self .serialno = serialno
180205 self .hostname = hostname
181206 self .port = port
182207 self .timeout = timeout
208+ self .__connect = connect
183209 self .timerId = - 1
184210 self .timers = {}
185211
186212 self .reconnect = reconnect
187- self .socket = AdbClient . connect (self .hostname , self .port , self .timeout )
213+ self .socket = connect (self .hostname , self .port , self .timeout )
188214
189215 self .lock = threading .RLock ()
190216
@@ -247,23 +273,6 @@ def setSerialno(self, serialno):
247273 def setReconnect (self , val ):
248274 self .reconnect = val
249275
250- @staticmethod
251- def connect (hostname , port , timeout = TIMEOUT ):
252- if DEBUG :
253- print ("AdbClient.connect(%s, %s, %s)" % (hostname , port , timeout ), file = sys .stderr )
254- s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
255- # SO_LINGER: Idea proposed by kysersozelee (#173)
256- l_onoff = 1
257- l_linger = 0
258- s .setsockopt (socket .SOL_SOCKET , socket .SO_LINGER ,
259- struct .pack ('ii' , l_onoff , l_linger ))
260- s .settimeout (timeout )
261- try :
262- s .connect ((hostname , port ))
263- except socket .error as ex :
264- raise RuntimeError ("ERROR: Connecting to %s:%d: %s.\n Is adb running on your computer?" % (s , port , ex ))
265- return s
266-
267276 def close (self ):
268277 if DEBUG :
269278 print ("Closing socket..." , self .socket , file = sys .stderr )
@@ -300,7 +309,7 @@ def __send(self, msg, checkok=True, reconnect=False):
300309 if reconnect :
301310 if DEBUG :
302311 print (" __send: reconnecting" , file = sys .stderr )
303- self .socket = AdbClient . connect (self .hostname , self .port , self .timeout )
312+ self .socket = self . __connect (self .hostname , self .port , self .timeout )
304313 self .__setTransport ()
305314
306315 def __receive (self , nob = None , sock = None ):
@@ -389,7 +398,7 @@ def checkVersion(self, ignoreversioncheck=False, reconnect=True):
389398 raise RuntimeError (
390399 "ERROR: Incorrect ADB server version %s (expecting one of %s)" % (version , VALID_ADB_VERSIONS ))
391400 if reconnect :
392- self .socket = AdbClient . connect (self .hostname , self .port , self .timeout )
401+ self .socket = self . __connect (self .hostname , self .port , self .timeout )
393402
394403 def __setTransport (self , timeout = 60 ):
395404 if DEBUG :
@@ -403,7 +412,7 @@ def __setTransport(self, timeout=60):
403412 if len (devices ) == 0 and timeout > 0 :
404413 print ("Empty device list, will wait %s secs for devices to appear" % self .timeout , file = sys .stderr )
405414 # Sets the timeout to 5 to be able to loop while trying to receive new devices being added
406- _s = AdbClient . connect (self .hostname , self .port , timeout = 5 )
415+ _s = self . __connect (self .hostname , self .port , timeout = 5 )
407416 msg = 'host:track-devices'
408417 b = bytearray (msg , 'utf-8' )
409418 timerId = self .setTimer (timeout = timeout , description = "setTransport" )
@@ -500,7 +509,7 @@ def getDevices(self):
500509 devices = []
501510 for line in self .__receive ().splitlines ():
502511 devices .append (Device .factory (line ))
503- self .socket = AdbClient . connect (self .hostname , self .port , self .timeout )
512+ self .socket = self . __connect (self .hostname , self .port , self .timeout )
504513 return devices
505514
506515 def shell (self , _cmd = None , _convertOutputToString = True ):
@@ -527,7 +536,7 @@ def shell(self, _cmd=None, _convertOutputToString=True):
527536 if DEBUG :
528537 print ("Reconnecting..." , file = sys .stderr )
529538 self .close ()
530- self .socket = AdbClient . connect (self .hostname , self .port , self .timeout )
539+ self .socket = self . __connect (self .hostname , self .port , self .timeout )
531540 self .__setTransport ()
532541 if _convertOutputToString :
533542 return b'' .join (chunks ).decode ('utf-8' )
@@ -853,7 +862,7 @@ def takeSnapshot(self, reconnect=False):
853862 print (" takeSnapshot: reading %d bytes" % size , file = sys .stderr )
854863 received = self .__receive (size )
855864 if reconnect :
856- self .socket = AdbClient . connect (self .hostname , self .port , self .timeout )
865+ self .socket = self . __connect (self .hostname , self .port , self .timeout )
857866 self .__setTransport ()
858867 if DEBUG :
859868 print (" takeSnapshot: Image.frombuffer(%s, %s, %s, %s, %s, %s, %s)" % (
0 commit comments