def on_new_data(self):
        # Note that we use blocking socket, because it has its own dedicated
        # process. We read only 512 bytes.
        try:
            self._listener.settimeout(self._timeout)
            data, peer = self._listener.recvfrom(constants.DEFAULT_BLKSIZE)
            self._listener.settimeout(None)
        except socket.timeout:
            self._stats.error = {
                'error_code': constants.ERR_UNDEFINED,
                'error_message': 'timeout occurred on socket.recvfrom()',
            }
            self._should_stop = True
            return
        if peer != self._peer:
            logging.error(
                'Unexpected peer: %s, expected %s' % (peer, self._peer)
            )
            self._should_stop = True
            return
        code, block_number = struct.unpack('!HH', data[:4])
        if code == constants.OPCODE_ERROR:
            self._stats.error = {
                'error_code': block_number,
                'error_message': data[4:-1].decode('ascii', 'ignore'),
            }
            # An error was reported by the client which terminates the exchange
            logging.error(
                "Error reported from client: %s" %
                self._stats.error['error_message']
            )
            self._transmit_error()
            self._should_stop = True
            return
        if code != constants.OPCODE_ACK:
            logging.error(
                'Expected an ACK opcode from %s, got: %d' % (self._peer, code)
            )
            self._stats.error = {
                'error_code': constants.ERR_ILLEGAL_OPERATION,
                'error_message': 'I only do reads, really',
            }
            self._transmit_error()
            self._should_stop = True
            return
        self._handle_ack(block_number)
Called when new data is available on the socket.
dataset/ETHPy150Open facebook/fbtftp/fbtftp/base_handler.py/BaseHandler.on_new_data