changeset 1163:65cce3c355a0

Rewrote sendData and stringRecived to use a simpler format. The new format includes the length of the data as well for easier unpacking.
author Martin Geisler <mg@cs.au.dk>
date Wed, 15 Apr 2009 00:07:07 +0200
parents 83282f6e20b9
children a9119cc75e11
files viff/runtime.py
diffstat 1 files changed, 9 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/viff/runtime.py	Tue Apr 14 23:30:34 2009 +0200
+++ b/viff/runtime.py	Wed Apr 15 00:07:07 2009 +0200
@@ -302,17 +302,13 @@
                     self.transport.loseConnection()
             self.factory.identify_peer(self)
         else:
-            # TODO: we cannot handle the empty string
-            # also note that we cannot handle pcs longer than 256
-            pc_size = ord(string[0])
-            fmt = "%di" % (pc_size + 1)
-            predata_size = struct.calcsize(fmt) + 1
-            fmt = "%s%is" % (fmt, len(string)-predata_size)
-
-            unpacked = struct.unpack(fmt, string[1:])
+            # TODO: We cannot handle the empty string.
+            pc_size, data_size, data_type = struct.unpack("!HHB", string[:5])
+            fmt = "!%dI%ds" % (pc_size, data_size)
+            unpacked = struct.unpack(fmt, string[5:])
 
             program_counter = unpacked[:pc_size]
-            data_type, data = unpacked[-2:]
+            data = unpacked[-1]
 
             key = (program_counter, data_type)
 
@@ -327,10 +323,10 @@
 
     def sendData(self, program_counter, data_type, data):
         pc_size = len(program_counter)
-        fmt = "%di%ds" % (pc_size + 1, len(data))
-        data_tuple = program_counter + (data_type, data)
-
-        self.sendString(chr(pc_size) + struct.pack(fmt, *data_tuple))
+        data_size = len(data)
+        fmt = "!HHB%dI%ds" % (pc_size, data_size)
+        t = (pc_size, data_size, data_type) + program_counter + (data,)
+        self.sendString(struct.pack(fmt, *t))
 
     def sendShare(self, program_counter, share):
         """Send a share.