1 # -*- test-case-name: twisted.test.test_stateful -*-
3 # Copyright (c) Twisted Matrix Laboratories.
4 # See LICENSE for details.
7 from twisted.internet import protocol
10 from cStringIO import StringIO
12 from StringIO import StringIO
14 class StatefulProtocol(protocol.Protocol):
15 """A Protocol that stores state for you.
17 state is a pair (function, num_bytes). When num_bytes bytes of data arrives
18 from the network, function is called. It is expected to return the next
19 state or None to keep same state. Initial state is returned by
20 getInitialState (override it).
22 _sful_data = None, None, 0
24 def makeConnection(self, transport):
25 protocol.Protocol.makeConnection(self, transport)
26 self._sful_data = self.getInitialState(), StringIO(), 0
28 def getInitialState(self):
29 raise NotImplementedError
31 def dataReceived(self, data):
32 state, buffer, offset = self._sful_data
35 blen = buffer.tell() # how many bytes total is in the buffer
37 while blen - offset >= state[1]:
38 d = buffer.read(state[1])
41 if self.transport.disconnecting: # XXX: argh stupid hack borrowed right from LineReceiver
42 return # dataReceived won't be called again, so who cares about consistent state
51 self._sful_data = state, buffer, offset