[Twisted-Python] loseConnection should stop the int32/int16 stringReceived too

Andrea Arcangeli andrea at cpushare.com
Sun Mar 13 14:57:09 EST 2005


This patch adds a check for transport.disconnecting to the int16/32
protocols, so that they don't keep firing stringReceived callbacks after
calling transport.loseConnection (in line with the linereceiver
protocol). This also adds the methods for pauseproducer in line with the
linereceiver protocol (as per previous patch in this thread).

If these two fixes to int32/16 are correct please apply to SVN.

Thanks.

Index: Twisted/twisted/protocols/basic.py
===================================================================
--- Twisted/twisted/protocols/basic.py	(revision 13177)
+++ Twisted/twisted/protocols/basic.py	(working copy)
@@ -166,9 +166,23 @@
         """
         return error.ConnectionLost('Line length exceeded')
     
+class PauseProducer(object):
+    paused = False
 
+    def pauseProducing(self):
+        self.paused = True
+        self.transport.pauseProducing()
 
-class LineReceiver(protocol.Protocol):
+    def resumeProducing(self):
+        self.paused = False
+        self.transport.resumeProducing()
+        self.dataReceived('')
+
+    def stopProducing(self):
+        self.paused = True
+        self.transport.stopProducing()
+
+class LineReceiver(protocol.Protocol, PauseProducer):
     """A protocol that receives lines and/or raw data, depending on mode.
     
     In line mode, each line that's received becomes a callback to
@@ -188,7 +202,6 @@
     __buffer = ''
     delimiter = '\r\n'
     MAX_LENGTH = 16384
-    paused = False
     
     def clearLineBuffer(self):
         """Clear buffered data."""
@@ -279,21 +292,8 @@
         """
         return self.transport.loseConnection()
 
-    def pauseProducing(self):
-        self.paused = True
-        self.transport.pauseProducing()
 
-    def resumeProducing(self):
-        self.paused = False
-        self.dataReceived('')
-        self.transport.resumeProducing()
-
-    def stopProducing(self):
-        self.paused = True
-        self.transport.stopProducing()
-
-
-class Int32StringReceiver(protocol.Protocol):
+class Int32StringReceiver(protocol.Protocol, PauseProducer):
     """A receiver for int32-prefixed strings.
 
     An int32 string is a string prefixed by 4 bytes, the 32-bit length of
@@ -314,7 +314,7 @@
         """Convert int32 prefixed strings into calls to stringReceived.
         """
         self.recvd = self.recvd + recd
-        while len(self.recvd) > 3:
+        while len(self.recvd) > 3 and not self.paused and not self.transport.disconnecting:
             length ,= struct.unpack("!i",self.recvd[:4])
             if length > self.MAX_LENGTH:
                 self.transport.loseConnection()
@@ -331,7 +331,7 @@
         self.transport.write(struct.pack("!i",len(data))+data)
 
 
-class Int16StringReceiver(protocol.Protocol):
+class Int16StringReceiver(protocol.Protocol, PauseProducer):
     """A receiver for int16-prefixed strings.
 
     An int16 string is a string prefixed by 2 bytes, the 16-bit length of
@@ -351,7 +351,7 @@
         """Convert int16 prefixed strings into calls to stringReceived.
         """
         self.recvd = self.recvd + recd
-        while len(self.recvd) > 1:
+        while len(self.recvd) > 1 and not self.paused and not self.transport.disconnecting:
             length = (ord(self.recvd[0]) * 256) + ord(self.recvd[1])
             if len(self.recvd) < length+2:
                 break




More information about the Twisted-Python mailing list