示例#1
0
    def SerializeInstructions(self, ops, packing_instructions, huff, stream_id,
                              group_id, end_of_frame, verbose):
        """ Serializes a set of instructions possibly containing many different
    type of opcodes into SPDY4 wire format, discovers the resultant length,
    computes the appropriate SPDY4 boilerplate, and then returns this
    in a new BitBucket
    """
        #print 'SerializeInstructions\n', ops
        if verbose >= 5:
            print
            print "stream_id: %s group_id: %s" % (stream_id, group_id)
            print "Ops to be serialied:"
            print FormatOps(ops)
            print
            descr = deque()

        payload_bb = BitBucket()
        for opcode, oplist in ops.iteritems():
            self.OutputOps(packing_instructions, huff, payload_bb, oplist,
                           opcode)
            #if verbose >= 5:
            #  tmp_bb = BitBucket()
            #  self.OutputOps(packing_instructions, huff, tmp_bb, oplist, opcode)
            #  descr.append(FormatHex(tmp_bb.GetAllBits()[0]))
            #  descr.append("\n")

        (payload, payload_len) = payload_bb.GetAllBits()
        payload_len = (payload_len +
                       7) / 8  # partial bytes are counted as full
        frame_bb = BitBucket()
        self.WriteControlFrameBoilerplate(frame_bb, 0, 0, 0, group_id, 0)
        boilerplate_length = frame_bb.BytesOfStorage()
        frame_bb = BitBucket()
        overall_bb = BitBucket()
        bytes_allowed = 2**16 - boilerplate_length
        while True:
            #print 'payload_len: ', payload_len
            bytes_to_consume = min(payload_len, bytes_allowed)
            #print 'bytes_to_consume: ', bytes_to_consume
            end_of_frame = (bytes_to_consume <= payload_len)
            #print 'end_of_Frame: ', end_of_frame
            self.WriteControlFrameBoilerplate(overall_bb, bytes_to_consume,
                                              end_of_frame, stream_id,
                                              group_id, 0x8)
            if verbose >= 5:
                descr.appendleft(FormatHex(overall_bb.GetAllBits()[0]))
            overall_bb.StoreBits((payload, bytes_to_consume * 8))
            payload = payload[bytes_to_consume:]
            payload_len -= bytes_allowed
            if payload_len <= 0:
                break
        if verbose >= 5:
            PrintHex(overall_bb.GetAllBits()[0])
            print ''.join(descr)

        return overall_bb.GetAllBits()
示例#2
0
    def SerializeInstructions(self, ops, packing_instructions, huff, stream_id,
                              group_id, end_of_frame):
        """ Serializes a set of instructions possibly containing many different
    type of opcodes into SPDY4 wire format, discovers the resultant length,
    computes the appropriate SPDY4 boilerplate, and then returns this
    in a new BitBucket
    """
        #print 'SerializeInstructions\n', ops
        (ot, otr) = self.MutateTogglesToToggleRanges(ops)
        #print FormatOps({'toggl': ot, 'trang': otr, 'clone': ops['clone'],
        #                 'kvsto': ops['kvsto'], 'eref': ops['eref']})

        payload_bb = BitBucket()
        self.OutputOps(packing_instructions, huff, payload_bb, ot, 'toggl')
        self.OutputOps(packing_instructions, huff, payload_bb, otr, 'trang')
        self.OutputOps(packing_instructions, huff, payload_bb, ops['clone'],
                       'clone')
        self.OutputOps(packing_instructions, huff, payload_bb, ops['kvsto'],
                       'kvsto')
        self.OutputOps(packing_instructions, huff, payload_bb, ops['eref'],
                       'eref')

        (payload, payload_len) = payload_bb.GetAllBits()
        payload_len = (payload_len +
                       7) / 8  # partial bytes are counted as full
        frame_bb = BitBucket()
        self.WriteControlFrameBoilerplate(frame_bb, 0, 0, 0, group_id, 0)
        boilerplate_length = frame_bb.BytesOfStorage()
        frame_bb = BitBucket()
        overall_bb = BitBucket()
        bytes_allowed = 2**16 - boilerplate_length
        while True:
            #print 'payload_len: ', payload_len
            bytes_to_consume = min(payload_len, bytes_allowed)
            #print 'bytes_to_consume: ', bytes_to_consume
            end_of_frame = (bytes_to_consume <= payload_len)
            #print 'end_of_Frame: ', end_of_frame
            self.WriteControlFrameBoilerplate(overall_bb, bytes_to_consume,
                                              end_of_frame, stream_id,
                                              group_id, 0x8)
            overall_bb.StoreBits((payload, bytes_to_consume * 8))
            payload = payload[bytes_to_consume:]
            payload_len -= bytes_allowed
            if payload_len <= 0:
                break
        return overall_bb.GetAllBits()