Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adb/adb_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def ConnectDevice(
used instead of a USB connection.
"""
if serial and b':' in serial:
handle = common.TcpHandle(serial)
handle = common.TcpHandle(serial,timeout_ms=default_timeout_ms)
else:
handle = common.UsbHandle.FindAndOpen(
DeviceIsAvailable, port_path=port_path, serial=serial,
Expand Down
21 changes: 15 additions & 6 deletions adb/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import socket
import threading
import weakref
import select

import libusb1
import usb1
Expand Down Expand Up @@ -278,9 +279,9 @@ def FindDevices(cls, setting_matcher, device_matcher=None,
class TcpHandle(object):
"""TCP connection object.

Provides same interface as UsbHandle but ignores timeout."""
Provides same interface as UsbHandle. """

def __init__(self, serial):
def __init__(self, serial, timeout_ms=None):
"""Initialize the TCP Handle.
Arguments:
serial: Android device serial of the form host or host:port.
Expand All @@ -292,9 +293,12 @@ def __init__(self, serial):
else:
host = serial
port = 5555
self._timeout_ms = timeout_ms
self._serial_number = '%s:%s' % (host, port)
timeout = self._timeout_ms/1000.0 if self._timeout_ms else None
self._connection = socket.create_connection((host,port),timeout=timeout)

self._connection = socket.create_connection((host, port))
self._connection.setblocking(0)

@property
def serial_number(self):
Expand All @@ -303,11 +307,16 @@ def serial_number(self):
def BulkWrite(self, data, timeout=None):
return self._connection.sendall(data)

def BulkRead(self, numbytes, timeout=None):
return self._connection.recv(numbytes)
def BulkRead(self, numbytes, timeout_ms=None):
t = self.Timeout(timeout_ms)/1000.0 if self.Timeout(timeout_ms) else None
ready = select.select([self._connection], [], [], t)
if ready[0]:
return self._connection.recv(numbytes)
msg = 'Reading from {} timed out (Timeout {}s)'.format(self._serial_number,t)
raise usb_exceptions.TCPTimeoutException(msg)

def Timeout(self, timeout_ms):
return timeout_ms
return timeout_ms if timeout_ms is not None else self._timeout_ms

def Close(self):
return self._connection.close()
4 changes: 4 additions & 0 deletions adb/usb_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ class AdbCommandFailureException(Exception):

class AdbOperationException(Exception):
"""Failed to communicate over adb with device after multiple retries."""


class TCPTimeoutException(FormatMessageWithArgumentsException):
"""TCP connection timed out in the time out given."""