replaced all loseConnection calls with abortConnection, hopefully addressing some...
[p2pool.git] / p2pool / util / p2protocol.py
index 433d79a..8255886 100644 (file)
@@ -19,21 +19,8 @@ class Protocol(protocol.Protocol):
         self._message_prefix = message_prefix
         self._max_payload_length = max_payload_length
         self.dataReceived2 = datachunker.DataChunker(self.dataReceiver())
-        self.paused_var = variable.Variable(False)
         self.traffic_happened = traffic_happened
     
-    def connectionMade(self):
-        self.transport.registerProducer(self, True)
-    
-    def pauseProducing(self):
-        self.paused_var.set(True)
-    
-    def resumeProducing(self):
-        self.paused_var.set(False)
-    
-    def stopProducing(self):
-        pass
-    
     def dataReceived(self, data):
         self.traffic_happened.happened('p2p/in', len(data))
         self.dataReceived2(data)
@@ -54,6 +41,7 @@ class Protocol(protocol.Protocol):
             
             if hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] != checksum:
                 print 'invalid hash for', self.transport.getPeer().host, repr(command), length, checksum.encode('hex'), hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4].encode('hex'), payload.encode('hex')
+                self.badPeerHappened()
                 continue
             
             type_ = getattr(self, 'message_' + command, None)
@@ -67,7 +55,7 @@ class Protocol(protocol.Protocol):
             except:
                 print 'RECV', command, payload[:100].encode('hex') + ('...' if len(payload) > 100 else '')
                 log.err(None, 'Error handling message: (see RECV line)')
-                self.badPeerHappened()
+                self.disconnect()
     
     def packetReceived(self, command, payload2):
         handler = getattr(self, 'handle_' + command, None)
@@ -76,10 +64,19 @@ class Protocol(protocol.Protocol):
                 print 'no handler for', repr(command)
             return
         
-        handler(**payload2)
+        if getattr(self, 'connected', True) and not getattr(self, 'disconnecting', False):
+            handler(**payload2)
+    
+    def disconnect(self):
+        if hasattr(self.transport, 'abortConnection'):
+            # Available since Twisted 11.1
+            self.transport.abortConnection()
+        else:
+            # This doesn't always close timed out connections! warned about in main
+            self.transport.loseConnection()
     
     def badPeerHappened(self):
-        self.transport.loseConnection()
+        self.disconnect()
     
     def sendPacket(self, command, payload2):
         if len(command) >= 12:
@@ -94,7 +91,6 @@ class Protocol(protocol.Protocol):
         data = self._message_prefix + struct.pack('<12sI', command, len(payload)) + hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] + payload
         self.traffic_happened.happened('p2p/out', len(data))
         self.transport.write(data)
-        return self.paused_var.get_when_satisfies(lambda paused: not paused)
     
     def __getattr__(self, attr):
         prefix = 'send_'