Пример #1
0
 def piece(self, payload):
     from pubsub import pub
     from libs import utils
     piece_index = utils.convertBytesToDecimal(payload[:4])
     piece_offset = utils.convertBytesToDecimal(payload[4:8])
     piece_data = payload[8:]
     pub.sendMessage('PiecesManager.Piece',
                     piece=(piece_index, piece_offset, piece_data))
    def manageMessageReceived(self, peer):
        while len(peer.readBuffer) > 0:
            if peer.hasHandshaked == False:
                peer.checkHandshake(peer.readBuffer)
                return

            msgLength = utils.convertBytesToDecimal(peer.readBuffer[0:4])

            # handle keep alive
            if peer.keep_alive(peer.readBuffer):
                return

            # len 0
            try:
                msgCode = int(ord(peer.readBuffer[4:5]))
                payload = peer.readBuffer[5:4 + msgLength]
            except Exception as e:
                logging.info(e)
                return

            # Message is not complete. Return
            if len(payload) < msgLength - 1:
                return

            peer.readBuffer = peer.readBuffer[msgLength + 4:]

            try:
                peer.idFunction[msgCode](payload)
            except Exception, e:
                logging.debug("error id:", msgCode, " ->", e)
                return
Пример #3
0
    def manageMessageReceived(self, peer):
        while len(peer.readBuffer) > 0:
            if peer.hasHandshaked == False:
                peer.checkHandshake(peer.readBuffer)
                return

            msgLength = utils.convertBytesToDecimal(peer.readBuffer[0:4])

            # handle keep alive
            if peer.keep_alive(peer.readBuffer):
                return

            # len 0
            try:
                msgCode = int(ord(peer.readBuffer[4:5]))
                payload = peer.readBuffer[5:4 + msgLength]
            except Exception as e:
                logging.info(e)
                return

            # Message is not complete. Return
            if len(payload) < msgLength - 1:
                return

            peer.readBuffer = peer.readBuffer[msgLength + 4:]

            try:
                peer.idFunction[msgCode](payload)
            except Exception, e:
                logging.debug("error id:", msgCode, " ->", e)
                return
Пример #4
0
 def have(self, payload):
     from pubsub import pub
     from libs import utils
     index = utils.convertBytesToDecimal(payload)
     self.bitField[index] = True
     pub.sendMessage('RarestPiece.updatePeersBitfield',
                     bitfield=self.bitField,
                     peer=self)