Example #1
0
    def decodeImage(self, data: bytes, filepath: str):
        if data.startswith(b'\xff\xd8'):  # 这是jiff文件
            with open(filepath + '.jfif', 'wb') as fp:
                fp.write(data)
        elif data.startswith(b'LZW!'):  # 这是BMP文件
            # 测试代码
            # with open(filepath+'_src.bin', 'wb') as sp:
            #     sp.write(data)

            arrInt = self.__loadData(data[IMAGE_DECODE_OFFSET:])
            # print(len(arrInt))
            # print(arrInt)
            arrRaw = self.__decodeRawData(arrInt)

            # BITMAP头部
            # typedef struct tagBITMAPFILEHEADER {
            #         WORD    bfType;
            #         DWORD   bfSize;
            #         WORD    bfReserved1;
            #         WORD    bfReserved2;
            #         DWORD   bfOffBits;
            # } BITMAPFILEHEADER, FAR *LPBITMAPFILEHEADER, *PBITMAPFILEHEADER;
            bytesFileHeader = struct.pack('<HIHHI', 0x4d42, 14 + len(arrRaw), 0, 0, 54)
            bytesData = bytes(arrRaw)
            with open(filepath + '.jfif', 'wb') as fp:
                fp.write(bytesFileHeader)
                fp.write(bytesData)
        else:
            logger.error('未知的图像数据')
            logger.debug(data)
Example #2
0
    def handle(self, msg: Message):
        if IPMSG.IS_CMD_SET(msg.cmd, IPMSG.IPMSG_BR_EXIT):

            self.removeUser(msg.friend)
            logger.debug('好友下线:' + msg.friend.__str__())
            return True

        return False
Example #3
0
    def handle(self,msg:Message):
        return False
        try:
            logger.debug("DebugHandler::%s--->%d--->%s" % (msg.friend.__str__(), msg.cmd, msg.extra.decode(
                IPMSG.ENCODETYPE)))
        except:
            logger.debug("DebugHandler::%s--->%d--->%s" % (msg.friend.__str__(), msg.cmd, msg.extra))

        return False
Example #4
0
 def Execute(self):
     try:
         nowTime = time.time() * 1000
         if self.before_handler is not None and nowTime < self.deadline:
             self.before_handler()
         else:
             self.afert_handler()
     except Exception as ex:
         logger.debug(ex)
Example #5
0
    def handle(self, msg: Message):
        if IPMSG.IS_CMD_SET(msg.cmd, IPMSG.IPMSG_GETPUBKEY):
            strExtra = msg.extra.decode(IPMSG.ENCODETYPE)
            if strExtra.startswith('21003'):
                # 回应消息
                sender = RepRsaSender()
                self.sendSender(sender, msg.friend.ip, msg.friend.port)
            logger.debug('对方请求RSA PUBLIC KEY:' + msg.friend.__str__())
            return True

        return False
Example #6
0
    def thread_func(self):
        logger.debug('任务线程启动')

        while not self.StopThread:
            tk, timeout = self.__PeekTask()
            if tk is not None:
                tk.Execute()
            elif timeout:
                continue
            elif self.condition.acquire():
                #等待一段时间后直接重新开始
                if self.condition.wait(self.ThreadWaitMs / 1000.0):
                    self.condition.release()
        logger.debug('线程结束')
Example #7
0
    def handle(self, msg: Message):
        if IPMSG.IS_CMD_SET(msg.cmd, IPMSG.IPMSG_BR_ENTRY):
            strExtra = msg.extra.decode(IPMSG.ENCODETYPE)
            arrData = strExtra.split(' ')
            if len(arrData) > 2:
                msg.friend.nickname = arrData[0]
                msg.friend.groupname = arrData[1]

            # 回应消息
            sender = CommandSender(IPMSG.IPMSG_ANSENTRY)
            self.sendSender(sender, msg.friend.ip, msg.friend.port)
            self.updateUser(msg.friend)
            logger.debug('好友上线:' + msg.friend.__str__())
            return True

        return False
Example #8
0
    def handle(self, msg: Message):
        if IPMSG.IS_CMD_SET(msg.cmd, IPMSG.IPMSG_ANSPUBKEY):
            strExtra = Util.RemoveLastZero(msg.extra).decode(IPMSG.ENCODETYPE)

            if strExtra.startswith('21003:'):
                strE,strN = strExtra[6:].split('-')

                intE = int(strE, 16)
                intN = int(strN, 16)
                SecurtInstance.addPubKey(msg.friend.getId(), intE, intN)

                #对方发送密钥后需要通知任务队里的任务,设置用户ID类任务全部执行
                self.engine.SetReleateTaskRun('PUBKEY_' + msg.friend.getId())

            logger.debug('对方发送RSA PUBLIC KEY:' + msg.friend.__str__())
            return True

        return False
Example #9
0
    def handle(self, msg: Message):
        if not IPMSG.IS_CMD_SET(msg.cmd, IPMSG.IPMSG_SENDIMAGE):
            return False

        if not IPMSG.IS_OPT_SET(msg.cmd, IPMSG.IPMSG_FILEATTACHOPT):
            return False

        rawData = msg.extra

        bs = BmpSlice()
        # 如果解析成功
        if bs.load(rawData):
            # 回应消息
            sender = ResponseBmpRecvSender(bs.key, bs.slice)
            self.sendSender(sender, msg.friend.ip, msg.friend.port)
            logger.debug('接收到分片报文:%s  %d : %d' %
                         (bs.key, bs.slice, bs.totalSlice))
            # 清除任务队列中的重传请求,用key_slice作为关键字
            self.engine.EraseReleateTask(bs.key + '_' + str(bs.slice))

            SMI.append(bs)

            # 设置报文重传请求
            if SMI.count(bs.key) == 1 and bs.totalSlice != 1:
                # TODO 这里设置报文重传请求
                pass

            # 如果容器中存储的数据还不满,那么不需要后续处理了
            if SMI.count(bs.key) != bs.totalSlice:
                return True

            bmpData = SMI.getData(bs.key)
            if len(bmpData) == 0:
                return True

            lw = ImgLzw()
            lw.decodeImage(bmpData, RECV_IMAGE_PATH + bs.key)

            return True

        logger.error('加载bmp分片报文失败')
        # 如果解析失败,后续不需要再解析了
        return True
Example #10
0
    def handle(self, msg: Message):
        if IPMSG.IS_CMD_SET(msg.cmd, IPMSG.IPMSG_ANSENTRY) and \
                IPMSG.IS_OPT_SET(msg.cmd, IPMSG.FEIQ_EXTEND_CMD):
            strExtra = msg.extra.decode(IPMSG.ENCODETYPE)
            arrData = strExtra.split('\0')
            if len(arrData) >= 2:
                msg.friend.nickname = arrData[0]
                msg.friend.groupname = arrData[1]
            elif len(arrData) >= 1:
                msg.friend.nickname = arrData[0]

            #回应消息
            #sender = GroupSender(IPMSG.IPMSG_ANSENTRY|IPMSG.FEIQ_EXTEND_CMD)
            #self.sendSender(sender, msg.friend.ip, msg.friend.port)
            self.updateUser(msg.friend)
            logger.debug('飞秋的分组信息:%s'%(msg.friend,))
            return True

        return False
Example #11
0
 def handle(self, msg:Message):
     for item in msg.contents:
         logger.debug('内容接收结束处理器---->%s'% item)
         self.engine.onRecvContent(item)
     
     return True
Example #12
0
 def dealPacket(self, address, data):
     logger.debug('Got Msg from ' + str(address))
     packet = UdpPacketData(address, data)
     Instance.putRecvMessage(packet)
Example #13
0
 def Execute(self):
     try:
         if self.execute_handler is not None:
             self.execute_handler()
     except Exception as ex:
         logger.debug(ex)