collector-agent/CollectorAgent/TPackets.py
def parseNetByteStream(view, size):
        '''
        size must > 2
        return type and packet
        :param  memoryview view:
        :param int size:
        :return:
        '''

        if size < 2:
            return

        offset = 0
        while offset < size:
            ### parse type
            type, = struct.unpack("!h", view[offset:offset + Packet.TYPE_LENGTH])
            if type not in PacketType.PTypeBytesLayOut:
                raise Exception("type:%d not found", type)

            ### parse type ->sub header
            body_header_len, sub_header_len, sub_body_len_offset = PacketType.PTypeBytesLayOut[type]
            if size - Packet.TYPE_LENGTH < body_header_len:  ## not enough
                # yield (Packet.READ_AGAIN,size - offset)
                return  ## stop iteration
            offset += 2
            header = 0
            ### get sub header
            if sub_header_len == 4:
                header, = struct.unpack("!i", view[offset:offset + sub_header_len])
                offset += sub_header_len

            ### parse body_length
            body_len = 0
            if sub_body_len_offset == 2:
                body_len, = struct.unpack("!h", view[offset:offset + sub_body_len_offset])
            elif sub_body_len_offset == 4:
                body_len, = struct.unpack("!i", view[offset:offset + sub_body_len_offset])
            elif sub_body_len_offset == 0:
                yield (offset + sub_body_len_offset, type, header, None)
                continue  ## goto parse type

            offset += sub_body_len_offset
            if size - offset < body_len:  ## not enough
                # yield (Packet.READ_AGAIN, size - body_header_len - Packet.TYPE_LENGTH)
                return  ## stop iteration
            else:
                if body_len > 0:
                    yield (offset + body_len, type, header, view[offset:offset + body_len])
                else:
                    yield (offset, type, header, None)
                offset += body_len

        size must > 2
        return type and packet
        :param  memoryview view:
        :param int size:
        :return:
        
size must > 2
return type and packet
size must > 2 return type and packet
def parseNetByteStream(view, size):
        if size < 2:
            return
        offset = 0
        while offset < size:
            type, = struct.unpack("!h", view[offset:offset + Packet.TYPE_LENGTH])
            if type not in PacketType.PTypeBytesLayOut:
                raise Exception("type:%d not found", type)
             ->sub header
            body_header_len, sub_header_len, sub_body_len_offset = PacketType.PTypeBytesLayOut[type]
            if size - Packet.TYPE_LENGTH < body_header_len:  
                return  
            offset += 2
            header = 0
            if sub_header_len == 4:
                header, = struct.unpack("!i", view[offset:offset + sub_header_len])
                offset += sub_header_len
            body_len = 0
            if sub_body_len_offset == 2:
                body_len, = struct.unpack("!h", view[offset:offset + sub_body_len_offset])
            elif sub_body_len_offset == 4:
                body_len, = struct.unpack("!i", view[offset:offset + sub_body_len_offset])
            elif sub_body_len_offset == 0:
                yield (offset + sub_body_len_offset, type, header, None)
                continue  
            offset += sub_body_len_offset
            if size - offset < body_len:  
                return  
            else:
                if body_len > 0:
                    yield (offset + body_len, type, header, view[offset:offset + body_len])
                else:
                    yield (offset, type, header, None)
                offset += body_len
def parseNetByteStream ( view , size ) : if size < 2 : return offset = 0 while offset < size : type , = struct . unpack ( "!h" , view [ offset : offset + Packet . TYPE_LENGTH ] ) if type not in PacketType . PTypeBytesLayOut : raise Exception ( "type:%d not found" , type ) body_header_len , sub_header_len , sub_body_len_offset = PacketType . PTypeBytesLayOut [ type ] if size - Packet . TYPE_LENGTH < body_header_len : return offset += 2 header = 0 if sub_header_len == 4 : header , = struct . unpack ( "!i" , view [ offset : offset + sub_header_len ] ) offset += sub_header_len body_len = 0 if sub_body_len_offset == 2 : body_len , = struct . unpack ( "!h" , view [ offset : offset + sub_body_len_offset ] ) elif sub_body_len_offset == 4 : body_len , = struct . unpack ( "!i" , view [ offset : offset + sub_body_len_offset ] ) elif sub_body_len_offset == 0 : yield ( offset + sub_body_len_offset , type , header , None ) continue offset += sub_body_len_offset if size - offset < body_len : return else : if body_len > 0 : yield ( offset + body_len , type , header , view [ offset : offset + body_len ] ) else : yield ( offset , type , header , None ) offset += body_len
size must > 2
return type and packet
size must > 2 return type and packet
'''
        size must > 2
        return type and packet
        :param  memoryview view:
        :param int size:
        :return:
        ''' ### parse type ### parse type ->sub header ## not enough # yield (Packet.READ_AGAIN,size - offset) ## stop iteration ### get sub header ### parse body_length ## goto parse type ## not enough # yield (Packet.READ_AGAIN, size - body_header_len - Packet.TYPE_LENGTH) ## stop iteration
None
view
None
size
None