Example #1
0
    def data_received(self, data: bytes):
        if not self.transport or self.transport.is_closing():
            log.warning("transport closing, but got more bytes from %s:%i\n%s", self.peer_address, self.peer_port,
                        binascii.hexlify(data))
            if self._response_fut and not self._response_fut.done():
                self._response_fut.cancel()
            return
        if self._blob_bytes_received and not self.writer.closed():
            return self._write(data)

        response = BlobResponse.deserialize(data)

        if response.responses and self.blob:
            blob_response = response.get_blob_response()
            if blob_response and not blob_response.error and blob_response.blob_hash == self.blob.blob_hash:
                # set the expected length for the incoming blob if we didn't know it
                self.blob.set_length(blob_response.length)
            elif blob_response and not blob_response.error and self.blob.blob_hash != blob_response.blob_hash:
                # the server started sending a blob we didn't request
                log.warning("mismatch with self.blob %s", self.blob.blob_hash)
                return
        if response.responses:
            log.debug("got response from %s:%i <- %s", self.peer_address, self.peer_port, response.to_dict())
            # fire the Future with the response to our request
            self._response_fut.set_result(response)
        if response.blob_data and self.writer and not self.writer.closed():
            # log.debug("got %i blob bytes from %s:%i", len(response.blob_data), self.peer_address, self.peer_port)
            # write blob bytes if we're writing a blob and have blob bytes to write
            self._write(response.blob_data)
Example #2
0
 def send_response(self, responses: typing.List[blob_response_types]):
     to_send = []
     while responses:
         to_send.append(responses.pop())
     serialized = BlobResponse(to_send).serialize()
     self.transport.write(serialized)
     self.blob_manager.connection_manager.sent_data(self.peer_address_and_port, len(serialized))
Example #3
0
    def data_received(self, data: bytes):
        if not self.transport or self.transport.is_closing():
            log.warning("transport closing, but got more bytes from %s:%i\n%s",
                        self.peer_address, self.peer_port,
                        binascii.hexlify(data))
            if self._response_fut and not self._response_fut.done():
                self._response_fut.cancel()
            return

        response = BlobResponse.deserialize(data)

        if response.responses and self.blob:
            blob_response = response.get_blob_response()
            if blob_response and not blob_response.error and blob_response.blob_hash == self.blob.blob_hash:
                # set the expected length for the incoming blob if we didn't know it
                self.blob.set_length(blob_response.length)
            elif blob_response and not blob_response.error and self.blob.blob_hash != blob_response.blob_hash:
                # the server started sending a blob we didn't request
                log.warning("mismatch with self.blob %s", self.blob.blob_hash)
                return
        if response.responses:
            log.debug("got response from %s:%i <- %s", self.peer_address,
                      self.peer_port, response.to_dict())
            # fire the Future with the response to our request
            self._response_fut.set_result(response)
        if response.blob_data and self.writer and not self.writer.closed():
            log.debug("got %i blob bytes from %s:%i", len(response.blob_data),
                      self.peer_address, self.peer_port)
            # write blob bytes if we're writing a blob and have blob bytes to write
            if len(response.blob_data) > (self.blob.get_length() -
                                          self._blob_bytes_received):
                data = response.blob_data[:(self.blob.get_length() -
                                            self._blob_bytes_received)]
                log.warning(
                    "got more than asked from %s:%d, probable sendfile bug",
                    self.peer_address, self.peer_port)
            else:
                data = response.blob_data
            self._blob_bytes_received += len(data)
            try:
                self.writer.write(data)
                return
            except IOError as err:
                log.error("error downloading blob from %s:%i: %s",
                          self.peer_address, self.peer_port, err)
                if self._response_fut and not self._response_fut.done():
                    self._response_fut.set_exception(err)
            except (asyncio.CancelledError,
                    asyncio.TimeoutError) as err:  # TODO: is this needed?
                log.error("%s downloading blob from %s:%i", str(err),
                          self.peer_address, self.peer_port)
                if self._response_fut and not self._response_fut.done():
                    self._response_fut.set_exception(err)
Example #4
0
    def data_received(self, data: bytes):
        if self.connection_manager:
            if not self.peer_address:
                addr_info = self.transport.get_extra_info('peername')
                self.peer_address, self.peer_port = addr_info
            # assert self.peer_address is not None
            self.connection_manager.received_data(
                f"{self.peer_address}:{self.peer_port}", len(data))
        #log.debug("%s:%d -- got %s bytes -- %s bytes on buffer -- %s blob bytes received",
        #          self.peer_address, self.peer_port, len(data), len(self.buf), self._blob_bytes_received)
        if not self.transport or self.transport.is_closing():
            log.warning("transport closing, but got more bytes from %s:%i\n%s",
                        self.peer_address, self.peer_port,
                        binascii.hexlify(data))
            if self._response_fut and not self._response_fut.done():
                self._response_fut.cancel()
            return
        if not self._response_fut:
            log.warning(
                "Protocol received data before expected, probable race on keep alive. Closing transport."
            )
            return self.close()
        if self._blob_bytes_received and not self.writer.closed():
            return self._write(data)

        response = BlobResponse.deserialize(self.buf + data)
        if not response.responses and not self._response_fut.done():
            self.buf += data
            return
        else:
            self.buf = b''

        if response.responses and self.blob:
            blob_response = response.get_blob_response()
            if blob_response and not blob_response.error and blob_response.blob_hash == self.blob.blob_hash:
                # set the expected length for the incoming blob if we didn't know it
                self.blob.set_length(blob_response.length)
            elif blob_response and not blob_response.error and self.blob.blob_hash != blob_response.blob_hash:
                # the server started sending a blob we didn't request
                log.warning("mismatch with self.blob %s", self.blob.blob_hash)
                return
        if response.responses:
            log.debug("got response from %s:%i <- %s", self.peer_address,
                      self.peer_port, response.to_dict())
            # fire the Future with the response to our request
            self._response_fut.set_result(response)
        if response.blob_data and self.writer and not self.writer.closed():
            # log.debug("got %i blob bytes from %s:%i", len(response.blob_data), self.peer_address, self.peer_port)
            # write blob bytes if we're writing a blob and have blob bytes to write
            self._write(response.blob_data)
Example #5
0
 def send_response(self, responses: typing.List[blob_response_types]):
     to_send = []
     while responses:
         to_send.append(responses.pop())
     self.transport.write(BlobResponse(to_send).serialize())
Example #6
0
 def send_response(self, responses):
     to_send = []
     while responses:
         to_send.append(responses.pop())
     for byte in BlobResponse(to_send).serialize():
         self.transport.write(bytes([byte]))