Beispiel #1
0
class Chuck(object):
    def __init__(self):
        self.meggage_parser = ChunkMsg()
        self.clean()

    def clean(self):
        self.parse_error = ""

    def data_from_raw(self, data):
        self.clean()
        offset = 0
        if not data:
            self.self.parse_error += 'No chunk data'
            return (False, 0)

        flags_with_size, size_seq = struct.unpack_from("!BB", data, offset=offset)
        offset += 2
        flags = (flags_with_size>>6) & 0x03
        size = ((flags_with_size&0x3F)<<4) | (size_seq&0b1111)
        sequence = -1
        if flags & MSGFLAG_VITAL:
            seq = struct.unpack_from("!B", data, offset=offset)[0]
            offset += 1
            sequence = ((size_seq&0xC0)<<2) | seq

        if len(data[offset:]) < size:
            self.parse_error += 'ERROR! data < chuck_size\n'
            self.parse_error += bin(flags_with_size)+ ' ' + bin(size_seq) + '<--size' + str(len(data[offset:])) + ' '+ bin(len(data[offset:])) + '  ' + str(sequence)
            return (False, 0)

        raw_data = data[offset:size+offset]
        offset += size
        chunk_data = {
            'flags': flags,
            'sequence': sequence,
            # 'raw_data': raw_data,
            'message': self.meggage_parser.load_from_raw(raw_data),
        }
        return (chunk_data, offset)

    def build_raw_from_data(self, data):
        self.clean()
        flags = data['flags']
        sequence = data['sequence']
        # raw_data = data['raw_data']
        raw_data = self.meggage_parser.build_raw_from_data(data['message'])
        size = len(raw_data)

        flags_with_size = ((flags&0x03)<<6) | ((size>>4)&0x3F)
        size_seq = size & 0b1111
        if flags & MSGFLAG_VITAL:
            size_seq |= (sequence>>2)&0xC0
            sequence = sequence&0xFF
            header = struct.pack('!BBB', flags_with_size, size_seq, sequence)
        else:
            header = struct.pack('!BB', flags_with_size, size_seq)
        return header+raw_data
Beispiel #2
0
 def __init__(self):
     self.meggage_parser = ChunkMsg()
     self.clean()