本文整理汇总了Python中pyamf.util.BufferedByteStream.read方法的典型用法代码示例。如果您正苦于以下问题:Python BufferedByteStream.read方法的具体用法?Python BufferedByteStream.read怎么用?Python BufferedByteStream.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyamf.util.BufferedByteStream
的用法示例。
在下文中一共展示了BufferedByteStream.read方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [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()
示例2: write
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [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()
示例3: ProducingChannel
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [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))
示例4: BaseNegotiator
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [as 别名]
class BaseNegotiator(object):
"""
Base functionality for negotiating an RTMP handshake.
Call L{start} to begin negotiations.
@ivar observer: An observer for handshake negotiations.
@type observer: L{IHandshakeObserver}
@ivar started: Whether negotiations have begun.
@type started: C{bool}
@ivar _buffer: Any data that has been received but not yet been consumed.
@type _buffer: L{BufferedByteStream}
"""
started = False
nearRequest = None
nearResponse = None
farRequest = None
farResponse = None
protocolVersion = 3
farProtocolVersion = None
def __init__(self, observer, output):
self.observer = observer
self.output = output
def start(self, uptime=0, version=0):
"""
Called to start the handshaking negotiations.
"""
if self.started:
raise AlreadyStarted('Handshake negotiator cannot be restarted')
self.started = True
self.uptime = uptime
self.version = version
self._buffer = BufferedByteStream()
def readPacket(self):
if self._buffer.remaining() < HANDSHAKE_LENGTH:
# we're expecting more data
return
packet = self._buffer.read(HANDSHAKE_LENGTH)
self._buffer.consume()
return packet
def dataReceived(self, data):
"""
Called when handshake data has been received.
"""
if not self.started:
raise HandshakeError('Data received, but negotiator not started')
self._buffer.append(data)
if self.farProtocolVersion is None:
self.farProtocolVersion = self._buffer.read_uchar()
packet = self.readPacket()
if not packet:
return
if not self.farRequest:
self.farRequest = self.buildFarRequest()
self.farRequest.decode(packet)
self.farRequestReceived(self.farRequest)
packet = self.readPacket()
if not packet:
return
if not self.farResponse:
self.farResponse = self.buildFarResponse()
self.farResponse.decode(packet)
self.farResponseReceived(self.farResponse)
def buildFarRequest(self):
"""
"""
return RequestPacket()
#.........这里部分代码省略.........
示例5: Packet
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [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
示例6: test_assemble_chunks
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [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())
示例7: test_assemble
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [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())
示例8: RTMPBaseProtocol
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [as 别名]
class RTMPBaseProtocol(protocol.Protocol):
"""
Basis RTMP protocol implementation.
@ivar state: internal state of protocol
@ivar input: input packet disassebmbler
@type input: L{RTMPDisassembler}
@ivar output: output packet assembler
@type output: L{RTMPAssembler}
@ivar handshakeBuf: buffer, holding input data during handshake
@type handshakeBuf: C{BufferedByteStream}
"""
class State:
CONNECTING = 'connecting'
"""
Connection in progress
"""
HANDSHAKE_SEND = 'handshake-send'
"""
Handshake, 1st phase.
"""
HANDSHAKE_VERIFY = 'handshake-verify'
"""
Handshake, 2nd phase.
"""
RUNNING = 'running'
"""
Usual state of protocol: receiving-sending RTMP packets.
"""
def __init__(self):
"""
Constructor.
"""
self.state = self.State.CONNECTING
self.handshakeTimeout = None
def connectionMade(self):
"""
Successfully connected to peer.
"""
self.input = RTMPDisassembler(constants.DEFAULT_CHUNK_SIZE)
self.output = RTMPAssembler(constants.DEFAULT_CHUNK_SIZE, self.transport)
self.state = self.State.HANDSHAKE_SEND
self.handshakeTimeout = reactor.callLater(config.getint('RTMP', 'handshakeTimeout'), self._handshakeTimedout)
self.handshakeBuf = BufferedByteStream()
self._beginHandshake()
def _beginHandshake(self):
"""
Begin handshake procedures.
Implemented in descendants.
"""
raise NotImplementedError
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
#.........这里部分代码省略.........
示例9: StateEngine
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [as 别名]
#.........这里部分代码省略.........
elif self.state == self.STATE_HANDSHAKE:
self.handshake_dataReceived(data)
elif self.state == self.STATE_STREAM:
BaseStreamer.dataReceived(self, data)
else:
raise RuntimeError('Invalid state!')
def startVersioning(self):
"""
Start protocol version negotiations.
"""
self.buffer = BufferedByteStream()
def stopVersioning(self, reason=None):
"""
Stop protocol version negotiations.
@param reason: A L{failure.Failure} object if protocol version
negotiations failed. C{None} means success.
"""
del self.buffer
def version_dataReceived(self, data):
"""
"""
if not data:
return
self.buffer.append(data)
self.peerProtocolVersion = self.buffer.read_uchar()
self.versionReceived(self.peerProtocolVersion)
def versionReceived(self, version):
"""
Called when the peers' protocol version has been received.
The default behaviour is to accept any known version. It is the
responsibility for any overriding subclass to call L{versionSuccess} for
negotiations to proceed.
"""
if version == self.protocolVersion:
self.versionSuccess()
return
raise UnknownProtocolVersion(
'Unhandled protocol version %d' % (version,))
def versionSuccess(self):
"""
Protocol version negotiations have been successful, now on to
handshaking.
"""
try:
data = self.buffer.read()
except IOError:
data = None
self.stopVersioning()
示例10: RTMPDisassembler
# 需要导入模块: from pyamf.util import BufferedByteStream [as 别名]
# 或者: from pyamf.util.BufferedByteStream import read [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]
#.........这里部分代码省略.........