def start(self, spawn=None, socket_cls=None): if spawn is None: from multiprocessing.pool import ThreadPool self._thread_pool = ThreadPool(2) spawn = self._thread_pool.apply_async if socket_cls is None: from socket import socket as socket_cls assert not self.started, "Send and recv threads already started" # Holds (message, response_queue) to send next self._send_queue = self.queue_cls() # Holds response_queue to send next self._response_queues = self.queue_cls() self._socket = socket_cls() try: self._socket.connect((self.hostname, self.port)) except OSError as e: raise ConnectionError( f"Can't connect to '{self.hostname}:{self.port}', " "did all services on the PandA start correctly?" ) from e self._send_spawned = spawn(self._send_loop) self._recv_spawned = spawn(self._recv_loop) self.started = True
def __init__(self, ed, address, *, family:int=AF_INET, proto:int=0, type_:int=SOCK_STREAM, backlog:int=16): self.sock = socket_cls(family, type_, proto) self.sock.setblocking(0) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind(address) self.sock.listen(backlog) self._fw = ed.fd_wrap(self.sock.fileno(), fl=self.sock) self._fw.process_readability = self._connect_process self._fw.read_r()
def connect_async_sock(self, ed, addr, port, connect_callback=None, *, type_:int=SOCK_STREAM, proto:int=0, bind_target=None, **kwargs): """Nonblockingly open outgoing SOCK_STREAM/SOCK_SEQPACKET connection.""" def connect_process(): err = sock.getsockopt(SOL_SOCKET, SO_ERROR) if (err): _log(25, ('Async stream connection to {!a}:{!a} failed. Error: {!a}({!a})').format(addr, port, err, errno.errorcode[err])) self._fw.write_u() raise CloseFD() self.state = self.CS_UP if (self.ssl_handshake_pending): (ssl_args, ssl_kwargs) = self.ssl_handshake_pending self._do_ssl_handshake(*ssl_args, **ssl_kwargs) else: self._fw.read_r() # Write output, if we have any pending; else, turn writability # notification off self._fw.process_writability = self._output_write self._output_write(_known_writable=False) if not (connect_callback is None): connect_callback(self) #def try_again(): # return ed.set_timer(0, try_connect, interval_relative=False) #get_next_addr = iter(addresses).next sock = socket_cls(addr.AF, type_, proto) sock.setblocking(0) s_addr = (str(addr), port) if not (bind_target is None): sock.bind(bind_target) try: sock.connect(s_addr) except sockerr as exc: if (exc.errno == EINPROGRESS): pass else: return self.start(ed, sock, read_r=False, **kwargs) self.state = self.CS_CONNECT self._fw.process_writability = connect_process self._fw.write_r() return self
def start(self, spawn=None, socket_cls=None): if spawn is None: from multiprocessing.pool import ThreadPool self._thread_pool = ThreadPool(2) spawn = self._thread_pool.apply_async if socket_cls is None: from socket import socket as socket_cls assert not self.started, "Send and recv threads already started" # Holds (message, response_queue) to send next self._send_queue = self.queue_cls() # Holds response_queue to send next self._response_queues = self.queue_cls() self._socket = socket_cls() self._socket.connect((self.hostname, self.port)) self._send_spawned = spawn(self._send_loop) self._recv_spawned = spawn(self._recv_loop) self.started = True