msmith - in flumotion/trunk: . flumotion/twisted

flumotion-commit at lists.fluendo.com flumotion-commit at lists.fluendo.com
Tue Feb 27 17:40:16 CET 2007


Author: msmith
Date: Tue Feb 27 17:40:13 2007
New Revision: 4531

Modified:
   flumotion/trunk/ChangeLog
   flumotion/trunk/flumotion/twisted/fdserver.py
Log:
        * flumotion/twisted/fdserver.py:
          Stream sockets don't guarantee us message boundaries. We can't
          switch to datagram sockets as they're unreliable. So, provide
          protocol-level message boundaries for our FD-passing messsages, and
          strip them out before passing on to PB.
          Prevents protocol errors when connecting to the feed server under
          load.



Modified: flumotion/trunk/ChangeLog
==============================================================================
--- flumotion/trunk/ChangeLog	(original)
+++ flumotion/trunk/ChangeLog	Tue Feb 27 17:40:13 2007
@@ -1,5 +1,15 @@
 2007-02-27  Michael Smith  <msmith at fluendo.com>
 
+	* flumotion/twisted/fdserver.py:
+	  Stream sockets don't guarantee us message boundaries. We can't
+	  switch to datagram sockets as they're unreliable. So, provide
+	  protocol-level message boundaries for our FD-passing messsages, and
+	  strip them out before passing on to PB.
+	  Prevents protocol errors when connecting to the feed server under
+	  load.
+
+2007-02-27  Michael Smith  <msmith at fluendo.com>
+
 	* flumotion/component/consumers/httpstreamer/http.py:
 	  Ensure we chain up to our parent when stopping!
 	  Possibly fixes some shutdown races.

Modified: flumotion/trunk/flumotion/twisted/fdserver.py
==============================================================================
--- flumotion/trunk/flumotion/twisted/fdserver.py	(original)
+++ flumotion/trunk/flumotion/twisted/fdserver.py	Tue Feb 27 17:40:13 2007
@@ -28,15 +28,21 @@
 import errno
 import os
 import socket
+import struct
 
 # Heavily based on 
 # http://twistedmatrix.com/trac/browser/sandbox/exarkun/copyover/server.py
 # and client.py
 # Thanks for the inspiration!
 
+# 16 byte long randomly-generated magic signature
+MAGIC_SIGNATURE = "\xfd\xfc\x8e\x7f\x07\x47\xb9\xea" \
+                  "\xa1\x75\xee\xd8\xdc\x36\xc8\xa3"
+
 class FDServer(unix.Server):
-    def sendFileDescriptor(self, fileno, data="\0"):
-        return fdpass.writefds(self.fileno(), [fileno], data)
+    def sendFileDescriptor(self, fileno, data=""):
+        message = struct.pack("@16sI", MAGIC_SIGNATURE, len(data)) + data
+        return fdpass.writefds(self.fileno(), [fileno], message)
 
 class FDPort(unix.Port):
     transport = FDServer
@@ -57,7 +63,24 @@
                 return main.CONNECTION_DONE
 
             if len(fds) > 0:
-                return self.protocol.fileDescriptorsReceived(fds, message)
+                offset = message.find(MAGIC_SIGNATURE)
+                if offset < 0:
+                    raise TypeError("Bad signature")
+                elif offset > 0:
+                    ret = self.protocol.dataReceived(message[0:offset])
+                    if ret:
+                        return ret
+
+                msglen = struct.unpack("@I", message[offset+16:offset+20])[0]
+                offset += 20
+                ret = self.protocol.fileDescriptorsReceived(fds, 
+                    message[offset:offset+msglen])
+                if ret:
+                    return ret
+
+                if offset+msglen < len(message):
+                    return self.protocol.dataReceived(message[offset+msglen:])
+                return ret
             else:
                 return self.protocol.dataReceived(message)
 


More information about the flumotion-commit mailing list