当前位置: 首页>>代码示例>>Python>>正文


Python BufferedByteStream.seek方法代码示例

本文整理汇总了Python中pyamf.util.BufferedByteStream.seek方法的典型用法代码示例。如果您正苦于以下问题:Python BufferedByteStream.seek方法的具体用法?Python BufferedByteStream.seek怎么用?Python BufferedByteStream.seek使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyamf.util.BufferedByteStream的用法示例。


在下文中一共展示了BufferedByteStream.seek方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: write

# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import seek [as 别名]
    def write(self, previous=None):
        """
        Write (encoder) header to byte string.

        @param previous: previous header (used to compress header)
        @type previous: L{RTMPHeader}
        @return: encoded header
        @rtype: C{str}
        """
        if previous is None:
            diff = 3
        else:
            diff = self.diff(previous)

        first = self.object_id & 0x3F | ((diff ^ 3) << 6)

        if diff == 0:
            return chr(first)

        buf = BufferedByteStream()
        buf.write_uchar(first)

        buf.write_24bit_uint(self.timestamp)

        if diff > 1:
            buf.write_24bit_uint(self.length)
            buf.write_uchar(self.type)

            if diff > 2:
                buf.write_ulong(self.stream_id)

        buf.seek(0, 0)
        return buf.read()
开发者ID:smira,项目名称:fmspy,代码行数:35,代码来源:header.py

示例2: test_assemble

# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import seek [as 别名]
    def test_assemble(self):
        for fixture in self.data:
            buf = BufferedByteStream()
            a = RTMPAssembler(128, buf)

            for packet in fixture['packets']:
                a.push_packet(packet)

            buf.seek(0, 0)

            self.failUnlessEqual(struct.pack("B" * len(fixture['data']), *fixture['data']), buf.read())
开发者ID:Xkeeper,项目名称:fmspy,代码行数:13,代码来源:test_assembly.py

示例3: write

# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import seek [as 别名]
    def write(self):
        """
        Encode packet into bytes.

        @return: representation of packet
        @rtype: C{str}
        """
        buf = BufferedByteStream()
        buf.write_ulong(self.bytes)
        self.header.length = len(buf)
        buf.seek(0, 0)
        return buf.read()
开发者ID:smira,项目名称:fmspy,代码行数:14,代码来源:packets.py

示例4: test_assemble_chunks

# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import seek [as 别名]
    def test_assemble_chunks(self):
        chunkSizes = (32, 64, 128, 256)
        header = RTMPHeader(object_id=2, timestamp=9504486, length=10, type=0x04, stream_id=0)

        for chunkSize in chunkSizes:
            for l in xrange(1, 258):
                data = ''.join([chr(random.randint(0, 255)) for x in xrange(l)])

                buf = BufferedByteStream()
                a = RTMPAssembler(chunkSize, buf)
                a.push_packet(DataPacket(header=header, data=data))

                buf.seek(0, 0)
                self.failUnlessEqual(''.join(self.gen_packet("\x02\x91\x06\xe6\x00\x00\x01\x04\x00\x00\x00\x00", ["\xc2"], data, l, chunkSize)), buf.read())
开发者ID:Xkeeper,项目名称:fmspy,代码行数:16,代码来源:test_assembly.py

示例5: ProducingChannel

# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import seek [as 别名]
class ProducingChannel(BaseChannel):
    """
    Writes RTMP frames.

    @ivar buffer: Any data waiting to be written to the underlying stream.
    @type buffer: L{BufferedByteStream}
    @ivar acquired: Whether this channel is acquired. See L{ChannelMuxer.
        acquireChannel}
    """


    def __init__(self, channelId, stream, frameSize):
        BaseChannel.__init__(self, channelId, stream, frameSize)

        self.buffer = BufferedByteStream()
        self.acquired = False
        self.callback = None


    def setCallback(self, cb):
        """
        Sets the callback that will be fired once this channel has been completely
        encoded.
        """
        self.callback = cb


    def reset(self):
        """
        Called when the channel has completed writing the buffer.
        """
        BaseChannel.reset(self)

        self.buffer.seek(0)
        self.buffer.truncate()
        self.header = None


    def append(self, data):
        """
        Appends data to the buffer in preparation of encoding in RTMP.
        """
        self.buffer.append(data)


    def marshallFrame(self, size):
        """
        Writes a section of the buffer as part of the RTMP frame.
        """
        self.stream.write(self.buffer.read(size))
开发者ID:Flumotion,项目名称:rtmpy,代码行数:52,代码来源:codec.py

示例6: Packet

# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import seek [as 别名]
class Packet(object):
    """
    """


    format = None
    challengeKey = None


    def __init__(self):
        self.buffer = BufferedByteStream()


    def computeOffset(self, start, modulus, increment):
        """
        An offset is 4 consecutive bytes encoded at C{start}.

        s = sum of bytes

        offset = (s % modulus) + increment
        """
        self.buffer.seek(start)

        offset = (
            self.buffer.read_uchar() +
            self.buffer.read_uchar() +
            self.buffer.read_uchar() +
            self.buffer.read_uchar())

        offset %= modulus
        offset += increment

        return offset


    def getDigestAndPayload(self, offset, length):
        """
        Returns the C{digest} and C{payload} components.
        """
        self.buffer.seek(0)

        payload = self.buffer.read(offset)
        digest = self.buffer.read(length)
        payload += self.buffer.read()

        return digest, payload


    def setFormat(self, format):
        self.format = format


    def setChallengeKey(self, key):
        self.challengeKey = key


    def setChallengeDigest(self, digest):
        self.challengeDigest = digest


    def decode(self, data):
        """
        Decodes the data bytes into this packet.
        """
        raise NotImplementedError
开发者ID:njoyce,项目名称:rtmpy,代码行数:67,代码来源:__init__.py

示例7: RTMPBaseProtocol

# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import seek [as 别名]

#.........这里部分代码省略.........
    def _handshakeSendReceived(self):
        """
        Data received in HANDSHAKE_SEND state.

        Implemented in descendants.
        """
        raise NotImplementedError

    def _handshakeVerifyReceived(self):
        """
        Data received in HANDSHAKE_VERIFY state.

        Implemented in descendants.
        """
        raise NotImplementedError

    def _handshakeComplete(self):
        """
        Handshake complete, clear timeouts.
        """
        if self.handshakeTimeout is not None:
            self.handshakeTimeout.cancel()
            self.handshakeTimeout = None
        self.state = self.State.RUNNING
        self._regularInput(self.handshakeBuf.read())
        del self.handshakeBuf

    def _handshakeTimedout(self):
        """
        Handshake not completed in timeout.
        """
        self.handshakeTimeout = None
        self.transport.loseConnection()

    def connectionLost(self, reason):
        """
        Connection with peer was lost for some reason.
        """
        if self.handshakeTimeout is not None:
            self.handshakeTimeout.cancel()
            self.handshakeTimeout = None

    def _regularInput(self, data):
        """
        Regular RTMP dataflow: stream of RTMP packets.

        Some bytes (L{data}) was received.

        @param data: bytes received
        @type data: C{str}
        """
        self.input.push_data(data)

        while True:
            packet = self.input.disassemble()
            if packet is None:
                return

            self._handlePacket(packet)

    def dataReceived(self, data):
        """
        Some data was received from peer.

        @param data: bytes received
        @type data: C{str}
        """
        if self.state == self.State.RUNNING:
            self._regularInput(data)
        else: # handshake
            self.handshakeBuf.seek(0, 2)
            self.handshakeBuf.write(data)
            if self.state == self.State.HANDSHAKE_SEND:
                self._handshakeSendReceived()
            elif self.state == self.State.HANDSHAKE_VERIFY:
                self._handshakeVerifyReceived()

    def _handlePacket(self, packet):
        """
        Dispatch received packet to some handler.

        @param packet: packet
        @type packet: L{Packet}
        """
        log.msg("<- %r" % packet)
        handler = 'handle' + packet.__class__.__name__
        try:
            getattr(self, handler)(packet)
        except AttributeError:
            log.msg("Unhandled packet: %r" % packet)

    def pushPacket(self, packet):
        """
        Push outgoing RTMP packet.

        @param packet: outgoing packet
        @type packet: L{Packet}.
        """
        log.msg("-> %r" % packet)
        self.output.push_packet(packet)
开发者ID:Xkeeper,项目名称:fmspy,代码行数:104,代码来源:base.py

示例8: RTMPDisassembler

# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import seek [as 别名]
class RTMPDisassembler(object):
    """
    Disassembling bytestream into RTMP packets.

    RTMP stream slices packets into chunks of L{chunkSize}. This class
    processes incoming stream of RTMP protocol data (after initial handshake)
    and decodes RTMP packets.

    Communication goes independently for each object_id. Last received
    headers are stored for each object_id in L{lastHeaders}. L{pool} holds
    incomplete packet contents also for each object_id.

    @ivar lastHeaders: last received header for object_id
    @type lastHeaders: C{dict}, object_id -> L{RTMPHeader}
    @ivar pool: incomplete packet data for object_id
    @type pool: C{dict}, object_id -> L{BufferedByteStream}
    @ivar chunkSize: size of chunk for this stream
    @type chunkSize: C{int}
    @ivar buffer: incoming buffer with data received from protocol
    @type buffer: L{BufferedByteStream}
    """

    def __init__(self, chunkSize):
        """
        Constructor.

        @param chunkSize: initial size of chunk
        @type chunkSize: C{int}
        """
        self.lastHeaders = {}
        self.pool = {}
        self.chunkSize = chunkSize
        self.buffer = BufferedByteStream()

    def push_data(self, data):
        """
        Push more incoming data.

        @param data: data received
        @type data: C{str}
        """
        self.buffer.seek(0, 2)
        self.buffer.write(data)

        return self

    def disassemble(self):
        """
        Disassemble L{buffer} into packets.

        Returns first decoded packet or None, if no packet could
        be decoded at the moment.

        @return: decoded packet
        @rtype: L{Packet}
        """
        self.buffer.seek(0)

        while self.buffer.remaining() > 0:
            try:
                # try to parse header from stream
                header = RTMPHeader.read(self.buffer)
            except NeedBytes, (bytes,):
                # not enough bytes, return what we've already parsed
                return None

            # fill header with extra data from previous headers received
            # with same object_id
            header.fill(self.lastHeaders.get(header.object_id, RTMPHeader()))

            # get buffer for data of this packet
            buf = self.pool.get(header.object_id, BufferedByteStream())

            # this chunk size is minimum of regular chunk size in this
            # disassembler and what we have left here
            thisChunk = min(header.length - len(buf), self.chunkSize)
            if self.buffer.remaining() < thisChunk:
                # we have not enough bytes to read this chunk of data
                return None

            # we got complete chunk
            buf.write(self.buffer.read(thisChunk))

            # store packet header for this object_id
            self.lastHeaders[header.object_id] = header

            # skip data left in input buffer
            self.buffer.consume()

            # this chunk completes full packet?
            if len(buf) < header.length:
                # no, store buffer for further chunks
                self.pool[header.object_id] = buf
            else:
                # parse packet from header and data
                buf.seek(0, 0)

                # delete stored data for this packet
                if header.object_id in self.pool:
                    del self.pool[header.object_id]
#.........这里部分代码省略.........
开发者ID:smira,项目名称:fmspy,代码行数:103,代码来源:assembly.py


注:本文中的pyamf.util.BufferedByteStream.seek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。