コード例 #1
0
    def _react_piece(self, response: bytes):
        piece_index = bytes_to_int(response[1:5])
        begin = bytes_to_int(response[5:9])
        piece = response[9:]

        if piece_index == self._target_piece_index and \
                self._target_begin == begin and \
                self._target_segment_length == len(piece):

            self._storage += piece
            self._target_begin = begin + self._target_segment_length
            self._target_segment_length = min(
                self._target_piece_length - self._target_begin,
                Messages.piece_segment_length)
            self._state = "send_request"

            if self._target_begin == self._target_piece_length:
                _hash = get_sha_1_hash(self._storage)
                expected_hash = self._loader.get_piece_hash(piece_index)
                if _hash == expected_hash:
                    self.log.info("SAVED GOOD PIECE from '%s'" %
                                  str(self.peer_address))
                    self.allocator.save_piece(piece_index, self._storage, self)
                    self._saved_piece = True
                    self._state = "need_target"
                    return
                else:
                    self.log.info("Piece with wrong hash from '%s'" %
                                  str(self.peer_address))
                    self._state = "need_target"
コード例 #2
0
def _parse_announce_response(data):
    action = bytes_to_int(data[0:4])
    transaction_id = data[4:8]
    interval = bytes_to_int(data[8:12])
    peers_addresses = data[20:]
    tracker_answer = {b"interval": interval, b"peers": peers_addresses}
    return transaction_id, action, tracker_answer
コード例 #3
0
    def _react_request(self, response: bytes):
        if len(response) != 13:
            self.log.error("EXCEPTION! len of request peer message is "
                           "incorrect: " + response.decode())

        piece_index = bytes_to_int(response[1:5])
        begin = bytes_to_int(response[5:9])
        length = bytes_to_int(response[9:13])

        piece = self.allocator.try_get_piece_segment(piece_index, begin,
                                                     length)
        # TODO: сделать очередь
        if piece:
            self._sender.send_piece(piece_index, begin, piece)
コード例 #4
0
    def run(self):
        while True:
            if self._connection_closed:
                self._socket.close()
                self.log.fatal("Receiver from peer closed (connection closed)")
                return
            if self._was_closed:
                self._connection.react_receiver_closing()
                self.log.fatal("Receiver from peer closed")
                return
            try:
                response = self._socket.recv(4)
            except Exception as ex:
                self.log.error("Exception during receiving data: '%s' \n%s" %
                               (ex, traceback.format_exc()))
                self.close()
                continue
            if not response:
                self.log.warning("Len-Response was None")
                self.close()
                continue
            response_len = bytes_to_int(response)
            if response_len == 0:
                self.log.warning("Message with zero length was received")

            response = b""
            while response_len > 0:
                try:
                    received = self._socket.recv(min(1024, response_len))
                except Exception as ex:
                    self.log.error(
                        "Exception during receiving data: '%s' \n%s" %
                        (ex, traceback.format_exc()))
                    self.close()
                    break
                if not received:
                    self.log.warning(
                        "No message was received but it was expected")
                    self.close()
                    break
                response_len -= len(received)
                response += received
            else:
                if not response:
                    message_type = "keepalive"
                elif response[0] > 8:
                    self.log.warning("Unknown message type '%d' in '%s'" %
                                     (response[0], response.decode()))
                    continue
                else:
                    message_type = Messages.messages_types[response[0]]
                if not self._connection_closed:
                    self._connection.response_queue.put(
                        (message_type, response))
コード例 #5
0
 def _react_have(self, response: bytes):
     self.allocator.add_have_info(bytes_to_int(response[1:5]), self)
コード例 #6
0
def _parse_connect_response(data):
    action = bytes_to_int(data[0:4])
    recv_transaction_id = data[4:8]
    connection_id = data[8:16]
    return action, recv_transaction_id, connection_id