diff --git a/vxi11/rpc.py b/vxi11/rpc.py index 9d889a6..34b86f0 100644 --- a/vxi11/rpc.py +++ b/vxi11/rpc.py @@ -251,12 +251,13 @@ def recvrecord(sock): # Client using TCP to a specific port class RawTCPClient(Client): - def __init__(self, host, prog, vers, port): + def __init__(self, host, prog, vers, port, timeout=None): Client.__init__(self, host, prog, vers, port) - self.connect() + self.connect(timeout) - def connect(self): + def connect(self, timeout=None): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.settimeout(timeout) self.sock.connect((self.host, self.port)) def close(self): @@ -285,12 +286,13 @@ def do_call(self): # Client using UDP to a specific port class RawUDPClient(Client): - def __init__(self, host, prog, vers, port): + def __init__(self, host, prog, vers, port, timeout=None): Client.__init__(self, host, prog, vers, port) - self.connect() + self.connect(timeout) - def connect(self): + def connect(self, timeout=None): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.settimeout(timeout) self.sock.connect((self.host, self.port)) def close(self): @@ -337,8 +339,9 @@ def __init__(self, bcastaddr, prog, vers, port): self.reply_handler = None self.timeout = 30 - def connect(self): + def connect(self, timeout=None): self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.settimeout(timeout) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) def set_reply_handler(self, reply_handler): @@ -488,15 +491,15 @@ def callit(self, ca): class TCPPortMapperClient(PartialPortMapperClient, RawTCPClient): - def __init__(self, host): - RawTCPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT) + def __init__(self, host, timeout=None): + RawTCPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT, timeout) PartialPortMapperClient.__init__(self) class UDPPortMapperClient(PartialPortMapperClient, RawUDPClient): - def __init__(self, host): - RawUDPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT) + def __init__(self, host, timeout=None): + RawUDPClient.__init__(self, host, PMAP_PROG, PMAP_VERS, PMAP_PORT, timeout) PartialPortMapperClient.__init__(self) @@ -511,26 +514,26 @@ def __init__(self, bcastaddr): class TCPClient(RawTCPClient): - def __init__(self, host, prog, vers, port=0): + def __init__(self, host, prog, vers, port=0, timeout=None): if port == 0: - pmap = TCPPortMapperClient(host) + pmap = TCPPortMapperClient(host, timeout) port = pmap.get_port((prog, vers, IPPROTO_TCP, 0)) pmap.close() if port == 0: raise RPCError('program not registered') - RawTCPClient.__init__(self, host, prog, vers, port) + RawTCPClient.__init__(self, host, prog, vers, port, timeout) class UDPClient(RawUDPClient): - def __init__(self, host, prog, vers, port=0): + def __init__(self, host, prog, vers, port=0, timeout=None): if port == 0: - pmap = UDPPortMapperClient(host) + pmap = UDPPortMapperClient(host, timeout) port = pmap.get_port((prog, vers, IPPROTO_UDP, 0)) pmap.close() if port == 0: raise RPCError('program not registered') - RawUDPClient.__init__(self, host, prog, vers, port) + RawUDPClient.__init__(self, host, prog, vers, port, timeout) class BroadcastUDPClient(Client): diff --git a/vxi11/vxi11.py b/vxi11/vxi11.py index ee2bc44..4d62c8b 100644 --- a/vxi11/vxi11.py +++ b/vxi11/vxi11.py @@ -393,10 +393,10 @@ def done(self): class CoreClient(rpc.TCPClient): - def __init__(self, host, port=0): + def __init__(self, host, port=0, timeout=None): self.packer = Packer() self.unpacker = Unpacker('') - rpc.TCPClient.__init__(self, host, DEVICE_CORE_PROG, DEVICE_CORE_VERS, port) + rpc.TCPClient.__init__(self, host, DEVICE_CORE_PROG, DEVICE_CORE_VERS, port, timeout) def create_link(self, id, lock_device, lock_timeout, name): params = (id, lock_device, lock_timeout, name) @@ -487,10 +487,10 @@ def destroy_intr_chan(self): class AbortClient(rpc.TCPClient): - def __init__(self, host, port=0): + def __init__(self, host, port=0, timeout=None): self.packer = Packer() self.unpacker = Unpacker('') - rpc.TCPClient.__init__(self, host, DEVICE_ASYNC_PROG, DEVICE_ASYNC_VERS, port) + rpc.TCPClient.__init__(self, host, DEVICE_ASYNC_PROG, DEVICE_ASYNC_VERS, port, timeout) def device_abort(self, link): return self.make_call(DEVICE_ABORT, link, @@ -572,7 +572,6 @@ def __init__(self, host, name = None, client_id = None, term_char = None): self.abort_port = 0 self.link = None self.max_recv_size = 0 - self.max_read_len = 128*1024*1024 self.locked = False def __del__(self): @@ -607,7 +606,7 @@ def open(self): return if self.client is None: - self.client = CoreClient(self.host) + self.client = CoreClient(self.host, timeout=self.timeout) self.client.sock.settimeout(self.timeout+1) error, link, abort_port, max_recv_size = self.client.create_link( @@ -627,13 +626,15 @@ def open(self): def close(self): "Close connection" - if self.link is None: + if self.link is None or self.client is None: return - self.client.destroy_link(self.link) - self.client.close() - self.link = None - self.client = None + try: + self.client.destroy_link(self.link) + finally: + self.client.close() + self.link = None + self.client = None def abort(self): "Asynchronous abort" @@ -641,7 +642,7 @@ def abort(self): self.open() if self.abort_client is None: - self.abort_client = AbortClient(self.host, self.abort_port) + self.abort_client = AbortClient(self.host, self.abort_port, timeout=self.timeout) self.abort_client.sock.settimeout(self.timeout) error = self.abort_client.device_abort(self.link) @@ -692,9 +693,9 @@ def read_raw(self, num=-1): if self.link is None: self.open() - read_len = self.max_read_len - if num > 0: - read_len = min(num, self.max_read_len) + read_len = self.max_recv_size + if num > 0 and num < self.max_recv_size: + read_len = num flags = 0 reason = 0