/usr/lib/python3/dist-packages/twisted/protocols
NameSizeModeActions
haproxy/-0755rm
test/-0755rm
__pycache__/-0755rm
amp.py964910644editdlrm
basic.py316420644editdlrm
dict.py108910644editdlrm
finger.py12230644editdlrm
ftp.py1044200644editdlrm
htb.py94170644editdlrm
ident.py79310644editdlrm
loopback.py119280644editdlrm
memcache.py236510644editdlrm
pcp.py71860644editdlrm
policies.py213710644editdlrm
portforward.py23710644editdlrm
postfix.py36330644editdlrm
shoutcast.py35600644editdlrm
sip.py379600644editdlrm
socks.py79290644editdlrm
stateful.py16760644editdlrm
tls.py324980644editdlrm
wire.py24970644editdlrm
__init__.py3970644editdlrm
Edit: /usr/lib/python3/dist-packages/twisted/protocols/stateful.py (1676B)
# -*- test-case-name: twisted.test.test_stateful -*- # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. from io import BytesIO from twisted.internet import protocol class StatefulProtocol(protocol.Protocol): """A Protocol that stores state for you. state is a pair (function, num_bytes). When num_bytes bytes of data arrives from the network, function is called. It is expected to return the next state or None to keep same state. Initial state is returned by getInitialState (override it). """ _sful_data = None, None, 0 def makeConnection(self, transport): protocol.Protocol.makeConnection(self, transport) self._sful_data = self.getInitialState(), BytesIO(), 0 def getInitialState(self): raise NotImplementedError def dataReceived(self, data): state, buffer, offset = self._sful_data buffer.seek(0, 2) buffer.write(data) blen = buffer.tell() # how many bytes total is in the buffer buffer.seek(offset) while blen - offset >= state[1]: d = buffer.read(state[1]) offset += state[1] next = state[0](d) if ( self.transport.disconnecting ): # XXX: argh stupid hack borrowed right from LineReceiver return # dataReceived won't be called again, so who cares about consistent state if next: state = next if offset != 0: b = buffer.read() buffer.seek(0) buffer.truncate() buffer.write(b) offset = 0 self._sful_data = state, buffer, offset