def __init__(self, clientSocket, clientAddr, daemon): self.csock = socketutil.SocketConnection(clientSocket) self.caddr = clientAddr self.daemon = daemon
def __pyroCreateConnection(self, replaceUri=False): """ Connects this proxy to the remote Pyro daemon. Does connection handshake. Returns true if a new connection was made, false if an existing one was already present. """ with self.__pyroConnLock: if self._pyroConnection is not None: return False # already connected from Pyro4.naming import resolve # don't import this globally because of cyclic dependancy uri = resolve(self._pyroUri) # socket connection (normal or Unix domain socket) conn = None log.debug("connecting to %s", uri) connect_location = uri.sockname if uri.sockname else (uri.host, uri.port) with self.__pyroLock: try: if self._pyroConnection is not None: return False # already connected sock = socketutil.createSocket( connect=connect_location, reuseaddr=Pyro4.config.SOCK_REUSE, timeout=self.__pyroTimeout) conn = socketutil.SocketConnection(sock, uri.object) # Do handshake. For now, no need to send anything. (message type CONNECT is not yet used) msg = Message.recv(conn, None) # any trailing data (dataLen>0) is an error message, if any except Exception: x = sys.exc_info()[1] if conn: conn.close() err = "cannot connect: %s" % x log.error(err) if isinstance(x, errors.CommunicationError): raise else: ce = errors.CommunicationError(err) ce.__cause__ = x raise ce else: if msg.type == Pyro4.message.MSG_CONNECTFAIL: error = "connection rejected" if msg.data: data = msg.data if sys.version_info >= (3, 0): data = str(msg.data, "utf-8") error += ", reason: " + data conn.close() log.error(error) raise errors.CommunicationError(error) elif msg.type == Pyro4.message.MSG_CONNECTOK: self._pyroConnection = conn if replaceUri: self._pyroUri = uri log.debug("connected to %s", self._pyroUri) return True else: conn.close() err = "connect: invalid msg type %d received" % msg.type log.error(err) raise errors.ProtocolError(err)