plugin XEP-0065: better handling of finished Deferred

This commit is contained in:
Goffi 2015-12-17 22:38:00 +01:00
commit ea0da04fdf

View file

@ -318,6 +318,8 @@ class SOCKSv5(protocol.Protocol, FileSender):
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 @@ class SOCKSv5(protocol.Protocol, FileSender):
@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 @@ class SOCKSv5(protocol.Protocol, FileSender):
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 @@ class Socks5ServerFactory(protocol.ServerFactory):
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 @@ class Socks5ClientFactory(protocol.ClientFactory):
self.connection = defer.Deferred()
self._protocol_instance = None
self.connector = None
self._discarded = False
def discard(self):
"""Disconnect the client
@ -639,7 +643,6 @@ class Socks5ClientFactory(protocol.ClientFactory):
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 @@ class Socks5ClientFactory(protocol.ClientFactory):
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"))