changeset 1759:81923b3f8b14

plugin XEP-0065: better handling of finished Deferred
author Goffi <goffi@goffi.org>
date Thu, 17 Dec 2015 22:38:00 +0100
parents a66d34353f34
children 30efe084471c
files src/plugins/plugin_xep_0065.py
diffstat 1 files changed, 9 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0065.py	Thu Dec 17 22:37:59 2015 +0100
+++ b/src/plugins/plugin_xep_0065.py	Thu Dec 17 22:38:00 2015 +0100
@@ -318,6 +318,8 @@
         self.addressType = 0
         self.requestType = 0
         self._file_obj = None
+        self.active = False # set to True when protocol is actually used for transfer
+                            # used by factories to know when the finished Deferred can be triggered
 
     @property
     def file_obj(self):
@@ -495,6 +497,7 @@
 
         @param chunk_size(None, int): size of the buffer, or None for default
         """
+        self.active = True
         if chunk_size is not None:
             self.CHUNK_SIZE = chunk_size
         log.debug(u"Starting file transfer")
@@ -526,6 +529,8 @@
         if self.state == STATE_READY:
             # Everything is set, we just have to write the incoming data
             self.file_obj.write(buf)
+            if not self.active:
+                self.active = True
             return
 
         self.buf = self.buf + buf
@@ -606,8 +611,8 @@
         except (KeyError, ValueError):
             log.error(u"Protocol not found in session while it should be there")
         else:
-            if not protocols:
-                # The last protocol has been removed, session is finished
+            if protocol.active:
+                # The active protocol has been removed, session is finished
                 if reason.check(internet_error.ConnectionDone):
                     self.getSession(session_hash)[DEFER_KEY].callback(None)
                 else:
@@ -631,7 +636,6 @@
         self.connection = defer.Deferred()
         self._protocol_instance = None
         self.connector = None
-        self._discarded = False
 
     def discard(self):
         """Disconnect the client
@@ -639,7 +643,6 @@
         Also set a discarded flag, which avoid to call the session Deferred
         """
         self.connector.disconnect()
-        self._discarded = True
 
     def getSession(self):
         return self.session
@@ -654,14 +657,14 @@
 
     def clientConnectionLost(self, connector, reason):
         log.debug(_(u"Socks 5 client connection lost (reason: %s)") % reason.value)
-        self._protocol_instance = None
-        if not self._discarded:
+        if self._protocol_instance.active:
             # This one was used for the transfer, than mean that
             # the Socks5 session is finished
             if reason.check(internet_error.ConnectionDone):
                 self.getSession()[DEFER_KEY].callback(None)
             else:
                 self.getSession()[DEFER_KEY].errback(reason)
+        self._protocol_instance = None
 
     def buildProtocol(self, addr):
         log.debug(("Socks 5 client connection started"))