Exemple #1
0
 def cancelRequestFor(self, piece_index, block_index):
     if ((piece_index, block_index) in self.set_of_blocks_requested):
         self.set_of_blocks_requested.remove((piece_index, block_index))
     begin = block_index * BLOCK_SIZE
     piece_index = Messages.number_to_bytes(piece_index)
     begin = Messages.number_to_bytes(block_index)
     length = Messages.number_to_bytes(BLOCK_SIZE)
     self.transport.write(
         str(Messages.Cancel(index=piece_index, begin=begin,
                             length=length)))
Exemple #2
0
 def parseNonHandshakeMessage(self, data):
     bytestring = data
     if (bytestring[0:4] == '\x00\x00\x00\x00'):
         # Its a Keep Alive message #
         message_obj = Messages.KeepAlive(response=bytestring)
     else:
         message_obj = {
             0: lambda: Messages.Choke(response=bytestring),
             1: lambda: Messages.Unchoke(response=bytestring),
             2: lambda: Messages.Interested(response=bytestring),
             3: lambda: Messages.Interested(response=bytestring),
             4: lambda: Messages.Have(response=bytestring),
             5: lambda: Messages.Bitfield(response=bytestring),
             6: lambda: Messages.Request(response=bytestring),
             7: lambda: Messages.Piece(response=bytestring),
             8: lambda: Messages.Cancel(response=bytestring),
             9: lambda: Messages.Port(response=bytestring),
         }[struct.unpack(
             '!b',
             data[4])[0]]()  # The 5th byte in 'data' is the message type/id
     self.process_message(message_obj)