Beispiel #1
0
    def _handle_events(self, fd, events):
        if self._is_connecting:
            err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
            if err != 0:
                logger.debug('connecting error in _handle_events')
                return self.close()

            if self._connect_callback:
                self._connect_callback()
            self._is_connecting = False

        if events & READ_EVENT:
            try:
                _buf = self.socket.recv(self._read_size)
                if _buf:
                    self._handle_read(_buf)
                else:
                    return self.close()

            except socket.error as e:
                if errno_from_exception(e) not in _ERRNO_WOULDBLOCK:
                    logger.debug(str(e))
                    return self.close()

        if events & WRITE_EVENT:
            while self._write_buffer:
                _send_buf = self._write_buffer.popleft()
                try:
                    sent = self.socket.send(_send_buf)
                except (socket.error, IOError, OSError) as e:
                    if errno_from_exception(e) in _ERRNO_WOULDBLOCK:
                        logger.debug("write would block")
                        self._write_buffer.appendleft(_send_buf)
                        break
                    else:
                        return self.close()
                else:
                    if sent < len(_send_buf):
                        self._write_buffer.appendleft(_send_buf)

            # Buf if not calling _update_event_handler?
            if not self._write_buffer:
                self._update_event_handler(write=False)

        if events & ERROR_EVENT:
            err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
            logger.debug("Error in event loop!: %s" % str(err))
            self.close()
Beispiel #2
0
 def _handle_connection(self, connection, address):
     if self.ssl_options is not None:
         assert ssl, "Python 2.6+ and OpenSSL required for SSL"
         try:
             connection = ssl_wrap_socket(connection,
                                          self.ssl_options,
                                          server_side=True,
                                          do_handshake_on_connect=False)
         except ssl.SSLError as err:
             if err.args[0] == ssl.SSL_ERROR_EOF:
                 return connection.close()
             else:
                 raise
         except socket.error as err:
             if errno_from_exception(err) in (errno.ECONNABORTED, errno.EINVAL):
                 return connection.close()
             else:
                 raise
     try:
         if self.ssl_options is not None:
             stream = SSLIOStream(connection, io_loop=self.io_loop,
                                  max_buffer_size=self.max_buffer_size,
                                  read_chunk_size=self.read_chunk_size)
         else:
             stream = IOStream(connection, io_loop=self.io_loop,
                               max_buffer_size=self.max_buffer_size,
                               read_chunk_size=self.read_chunk_size)
         self.handle_stream(stream, address)
     except Exception:
         self.handle_exception(sys.exc_info())
Beispiel #3
0
 def _handle_connection(self, connection, address):
     if self.ssl_options is not None:
         assert ssl, "Python 2.6+ and OpenSSL required for SSL"
         try:
             connection = ssl_wrap_socket(connection,
                                          self.ssl_options,
                                          server_side=True,
                                          do_handshake_on_connect=False)
         except ssl.SSLError as err:
             if err.args[0] == ssl.SSL_ERROR_EOF:
                 return connection.close()
             else:
                 raise
         except socket.error as err:
             if errno_from_exception(err) in (errno.ECONNABORTED, errno.EINVAL):
                 return connection.close()
             else:
                 raise
     try:
         if self.ssl_options is not None:
             stream = SSLIOStream(connection,
                                  max_buffer_size=self.max_buffer_size,
                                  read_chunk_size=self.read_chunk_size)
         else:
             stream = IOStream(connection,
                               max_buffer_size=self.max_buffer_size,
                               read_chunk_size=self.read_chunk_size)
         self.handle_stream(stream, address)
     except Exception:
         self.handle_exception(sys.exc_info())
Beispiel #4
0
    def _accept_handler(self, sock, fd, events):
        while True:
            try:
                connection, address = sock.accept()
            except socket.error as e:
                if errno_from_exception(e) not in _ERRNO_WOULDBLOCK:
                    raise
                return

            self._handle_connect(connection)
Beispiel #5
0
    def connect(self, address, callback=None):
        self._is_connecting = True
        if callback is not None:
            self._connect_callback = callback

        try:
            self.socket.connect(address)
        except socket.error as e:
            err = errno_from_exception(e)
            if err not in _ERRNO_WOULDBLOCK and err not in _ERRNO_INPROGRESS:
                logger.debug(str(e))
                self.close()

        self._update_event_handler()