xref: /xnu-8796.141.3/tools/lldbmacros/usertaskdebugging/interface.py (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributionsfrom __future__ import absolute_import
2*1b191cb5SApple OSS Distributions
3*1b191cb5SApple OSS Distributionsfrom builtins import bytes
4*1b191cb5SApple OSS Distributionsfrom builtins import object
5*1b191cb5SApple OSS Distributions
6*1b191cb5SApple OSS Distributionsimport logging
7*1b191cb5SApple OSS Distributionsimport socket
8*1b191cb5SApple OSS Distributionsimport select
9*1b191cb5SApple OSS Distributions
10*1b191cb5SApple OSS Distributionsclass Interface(object):
11*1b191cb5SApple OSS Distributions    """Basic communication interface."""
12*1b191cb5SApple OSS Distributions    def __init__(self, host_cfg, portnum):
13*1b191cb5SApple OSS Distributions        super(Interface, self).__init__()
14*1b191cb5SApple OSS Distributions        self.host_cfg = host_cfg
15*1b191cb5SApple OSS Distributions        self.portnum = portnum
16*1b191cb5SApple OSS Distributions        self.pkt_size = 8192
17*1b191cb5SApple OSS Distributions        self.socket = None
18*1b191cb5SApple OSS Distributions        self.isblocking = True
19*1b191cb5SApple OSS Distributions        logging.debug("created  %s" % str(self))
20*1b191cb5SApple OSS Distributions
21*1b191cb5SApple OSS Distributions    def connect(self):
22*1b191cb5SApple OSS Distributions        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23*1b191cb5SApple OSS Distributions        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
24*1b191cb5SApple OSS Distributions        self.socket.bind((self.host_cfg, self.portnum))
25*1b191cb5SApple OSS Distributions        logging.debug("Initializing network interface for communication host: %s:%d", self.host_cfg, self.portnum)
26*1b191cb5SApple OSS Distributions        self.socket.listen(5)
27*1b191cb5SApple OSS Distributions        num_retries = 3
28*1b191cb5SApple OSS Distributions        while num_retries > 0:
29*1b191cb5SApple OSS Distributions            ra,wa,ea = select.select([self.socket], [], [], 30)
30*1b191cb5SApple OSS Distributions            if not ra:
31*1b191cb5SApple OSS Distributions                num_retries -= 1
32*1b191cb5SApple OSS Distributions                logging.warning("timeout: select returned empty list. retrying..")
33*1b191cb5SApple OSS Distributions                continue
34*1b191cb5SApple OSS Distributions            self.connection, addr = self.socket.accept()
35*1b191cb5SApple OSS Distributions            logging.info("Connected to client from %s" % str(addr))
36*1b191cb5SApple OSS Distributions            return True
37*1b191cb5SApple OSS Distributions        logging.error("Failed to connect. Exiting after multiple attempts.")
38*1b191cb5SApple OSS Distributions        return False
39*1b191cb5SApple OSS Distributions
40*1b191cb5SApple OSS Distributions    def read(self):
41*1b191cb5SApple OSS Distributions        if self.isblocking:
42*1b191cb5SApple OSS Distributions            #BUG TODO make this unblocking soon
43*1b191cb5SApple OSS Distributions            #logging.warn("blocking read bug")
44*1b191cb5SApple OSS Distributions            self.connection.settimeout(15)
45*1b191cb5SApple OSS Distributions            self.isblocking = False
46*1b191cb5SApple OSS Distributions        r_bytes = bytes()
47*1b191cb5SApple OSS Distributions        try:
48*1b191cb5SApple OSS Distributions            r_bytes = self.connection.recv(self.pkt_size)
49*1b191cb5SApple OSS Distributions        except Exception as e:
50*1b191cb5SApple OSS Distributions            #logging.debug("Found exception in recv. %s " % (str(e)))
51*1b191cb5SApple OSS Distributions            pass
52*1b191cb5SApple OSS Distributions
53*1b191cb5SApple OSS Distributions        return r_bytes
54*1b191cb5SApple OSS Distributions
55*1b191cb5SApple OSS Distributions    def write(self, str):
56*1b191cb5SApple OSS Distributions        if not self.isblocking:
57*1b191cb5SApple OSS Distributions            self.connection.setblocking(1)
58*1b191cb5SApple OSS Distributions            self.isblocking = True
59*1b191cb5SApple OSS Distributions        return self.connection.send(str.encode())
60*1b191cb5SApple OSS Distributions
61*1b191cb5SApple OSS Distributions    def close(self):
62*1b191cb5SApple OSS Distributions        if self.connection:
63*1b191cb5SApple OSS Distributions            logging.debug('closing connection.')
64*1b191cb5SApple OSS Distributions            self.connection.close()
65*1b191cb5SApple OSS Distributions        return self.socket
66*1b191cb5SApple OSS Distributions
67*1b191cb5SApple OSS Distributions    def __str__(self):
68*1b191cb5SApple OSS Distributions        return "interface: %s %d" % (self.host_cfg, self.portnum)
69