changeset 1581:8cc7d83141a4

plugin XEP-0065: chunk size optimization: 64Kio is used except for proxy were it is set to 4Kio, to avoid wild disconnection by Prosody's proxy
author Goffi <goffi@goffi.org>
date Wed, 11 Nov 2015 22:28:48 +0100
parents 641cfd2faefe
children a41cc0849008
files src/plugins/plugin_xep_0065.py
diffstat 1 files changed, 17 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0065.py	Wed Nov 11 18:32:59 2015 +0100
+++ b/src/plugins/plugin_xep_0065.py	Wed Nov 11 22:28:48 2015 +0100
@@ -274,7 +274,11 @@
         return iq_elt.send()
 
     def startTransfer(self, session_hash=None):
-        self.factory.startTransfer(session_hash)
+        if self.type == XEP_0065.TYPE_PROXY:
+            chunk_size = 4096 # Prosody's proxy reject bigger chunks by default
+        else:
+            chunk_size = None
+        self.factory.startTransfer(session_hash, chunk_size=chunk_size)
 
 
 def getSessionHash(from_jid, to_jid, sid):
@@ -289,7 +293,7 @@
 
 
 class SOCKSv5(protocol.Protocol, FileSender):
-    CHUNK_SIZE = 4096
+    CHUNK_SIZE = 2**16
 
     def __init__(self, session_hash=None):
         """
@@ -484,8 +488,13 @@
         self._session_hash = addr
         self.connectCompleted(addr, 0)
 
-    def startTransfer(self):
-        """Callback called when the result iq is received"""
+    def startTransfer(self, chunk_size):
+        """Callback called when the result iq is received
+
+        @param chunk_size(None, int): size of the buffer, or None for default
+        """
+        if chunk_size is not None:
+            self.CHUNK_SIZE = chunk_size
         log.debug(u"Starting file transfer")
         d = self.beginFileTransfer(self.file_obj, self.transport)
         d.addCallback(self.fileTransfered)
@@ -554,14 +563,14 @@
     def getSession(self, session_hash):
         return self.parent.getSession(session_hash, None)
 
-    def startTransfer(self, session_hash):
+    def startTransfer(self, session_hash, chunk_size=None):
         session = self.getSession(session_hash)
         try:
             protocol = session['protocols'][0]
         except (KeyError, IndexError):
             log.error(u"Can't start file transfer, can't find protocol")
         else:
-            protocol.startTransfer()
+            protocol.startTransfer(chunk_size)
 
     def addToSession(self, session_hash, protocol):
         """Check is session_hash is valid, and associate protocol with it
@@ -631,8 +640,8 @@
     def getSession(self):
         return self.session
 
-    def startTransfer(self, dummy=None):
-        self._protocol_instance.startTransfer()
+    def startTransfer(self, dummy=None, chunk_size=None):
+        self._protocol_instance.startTransfer(chunk_size)
 
     def clientConnectionFailed(self, connector, reason):
         log.debug(u"Connection failed")