Ejemplo n.º 1
0
 def _listen(self):
     """Listen for and dispatch new connections."""
     self.log.debug("Starting listen thread on %r" % (self.address, ))
     sock = socket.socket(*self.socketArgs)
     try:
         trySockOpt(sock, socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         sock.settimeout(self.acceptPollInterval)
         sock.bind(self.address)
         sock.listen(self.backlog)
         
         with self._lock:
             self._lock.notify()
         
         self.log.debug("Starting accept loop")
         while self._thread is not None:
             try:
                 (connection, address) = sock.accept()
             except socket.timeout:
                 pass
             else:
                 self.log.debug("Connection accepted from %r" % (address, ))
                 trySockOpt(connection, socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
                 connection.settimeout(self.timeout)
                 with self._lock:
                     self._connections.append(ThreadedConnection(self, connection, address))
     except:
         sock.close()
         self.log.exception("Exception in server thread")
     
     with self._lock:
         self._thread = None
         self._lock.notify()
Ejemplo n.º 2
0
 def _open(self):
     """Open connection to server."""
     self._close()
     sock = socket.socket(*self.socketArgs)
     trySockOpt(sock, socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
     sock.settimeout(self.timeout)
     sock.connect(self.address)
     self._stream = SocketStream(sock, capture=self._capture)
     self._writer = StreamWriter(self._stream)
     self._reader = StreamReader(self._stream)