From 5fde48f35333771ee44425aa6fc7513199b2a3de Mon Sep 17 00:00:00 2001
From: Anders Blomdell <anders.blomdell@control.lth.se>
Date: Mon, 13 May 2013 19:42:22 +0200
Subject: [PATCH] lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs:  
 corrected encodeBoolean   changed encodePacked32   added a flush to end()
 method

lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs:
  corrected struct handling
  corrected readInt
  changed decodePacked32
---
 lib/csharp/Makefile                           | 19 +++++++++++++++++++
 .../se/lth/control/labcomm/LabCommDecoder.cs  |  2 ++
 .../control/labcomm/LabCommDecoderChannel.cs  | 12 +++++-------
 .../se/lth/control/labcomm/LabCommEncoder.cs  |  1 +
 .../control/labcomm/LabCommEncoderChannel.cs  | 18 +++++++++++-------
 5 files changed, 38 insertions(+), 14 deletions(-)
 create mode 100644 lib/csharp/Makefile

diff --git a/lib/csharp/Makefile b/lib/csharp/Makefile
new file mode 100644
index 0000000..5ea79f9
--- /dev/null
+++ b/lib/csharp/Makefile
@@ -0,0 +1,19 @@
+MODULES=LabCommDispatcher \
+	LabCommDecoderRegistry \
+	LabComm \
+	LabCommSample \
+	LabCommHandler \
+	LabCommEncoderRegistry \
+	LabCommDecoder \
+	LabCommType \
+	LabCommEncoderChannel \
+	LabCommEncoder \
+	LabCommDecoderChannel \
+
+all: labcomm.dll
+
+labcomm.dll: $(MODULES:%=se/lth/control/labcomm/%.cs) Makefile
+	mcs -out:$@ -target:library $(filter %.cs, $^)
+
+clean:
+	rm labcomm.dll
diff --git a/lib/csharp/se/lth/control/labcomm/LabCommDecoder.cs b/lib/csharp/se/lth/control/labcomm/LabCommDecoder.cs
index c01517c..bcb9dea 100644
--- a/lib/csharp/se/lth/control/labcomm/LabCommDecoder.cs
+++ b/lib/csharp/se/lth/control/labcomm/LabCommDecoder.cs
@@ -6,6 +6,7 @@ namespace se.lth.control.labcomm {
 
     void register(LabCommDispatcher dispatcher, 
 		  LabCommHandler handler);
+
     bool decodeBoolean();
     byte decodeByte();
     short decodeShort();
@@ -14,6 +15,7 @@ namespace se.lth.control.labcomm {
     float decodeFloat();
     double decodeDouble();
     String decodeString();
+    int decodePacked32();
 
   }
 
diff --git a/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs b/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs
index aec8b0a..953c910 100644
--- a/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs
+++ b/lib/csharp/se/lth/control/labcomm/LabCommDecoderChannel.cs
@@ -68,7 +68,7 @@ namespace se.lth.control.labcomm {
       } break;
       case LabComm.STRUCT: {
         int fields = decodePacked32();
-        e.encodeInt(fields);
+        e.encodePacked32(fields);
         for (int i = 0 ; i < fields ; i++) {
           e.encodeString(decodeString());
           collectFlatSignature(e);
@@ -108,10 +108,11 @@ namespace se.lth.control.labcomm {
     }
 
     private Int64 ReadInt(int length) {
-      int result = 0;
+      Int64 result = 0;
       ReadBytes(buf, length);
       for (int i = 0 ; i < length ; i++) {
 	result = (result << 8) + buf[i];
+
       }
       return result;
     }
@@ -163,16 +164,13 @@ namespace se.lth.control.labcomm {
     }
 
     public int decodePacked32() {
-      TODO: Correct byteorder
       Int64 res = 0;
-      byte i = 0;
       bool cont = true; 
 
       do {
-        byte c = decodeByte();
-	res |= (uint) ((c & 0x7f) << 7*i);
+        Int64 c = decodeByte();
+	res = (res << 7) | (c & 0x7f);
         cont = (c & 0x80) != 0;
-        i++;
       } while(cont);
 
       return (int) (res & 0xffffffff);
diff --git a/lib/csharp/se/lth/control/labcomm/LabCommEncoder.cs b/lib/csharp/se/lth/control/labcomm/LabCommEncoder.cs
index 1ddadd2..aadc98e 100644
--- a/lib/csharp/se/lth/control/labcomm/LabCommEncoder.cs
+++ b/lib/csharp/se/lth/control/labcomm/LabCommEncoder.cs
@@ -7,6 +7,7 @@ namespace se.lth.control.labcomm {
     void register(LabCommDispatcher dispatcher);
     void begin(Type c);
     void end(Type c);
+
     void encodeBoolean(bool value);
     void encodeByte(byte value);
     void encodeShort(short value);
diff --git a/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs b/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs
index 62d72f0..d13a236 100644
--- a/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs
+++ b/lib/csharp/se/lth/control/labcomm/LabCommEncoderChannel.cs
@@ -35,6 +35,7 @@ namespace se.lth.control.labcomm {
     public void end(Type c) {
       bytes.WriteTo(writer);
       bytes.SetLength(0);
+      writer.Flush();
     }
 
     private void WriteInt(Int64 value, int length) {
@@ -46,7 +47,7 @@ namespace se.lth.control.labcomm {
     }
 
     public void encodeBoolean(bool value) {
-      WriteInt(value ? 0 : 1, 1);
+      WriteInt(value ? 1 : 0, 1);
     }
 
     public void encodeByte(byte value) {
@@ -89,13 +90,16 @@ namespace se.lth.control.labcomm {
     }
 
     public void encodePacked32(Int64 value) {
-      Int64 tmp = value;
-      TODO: Correct byteorder
-      while(tmp >= 0x80) {
-        encodeByte( (byte) ((tmp & 0x7f) | 0x80 ) );
-        tmp >>= 7;           
+      byte[] tmp = new byte[5];
+      Int64 v = value & 0xffffffff;
+      int i;
+  
+      for (i = 0 ; i == 0 || v != 0 ; i++, v = (v >> 7)) {
+        tmp[i] = (byte)(v & 0x7f);
+      }
+      for (i = i - 1 ; i >= 0 ; i--) {
+        encodeByte((byte)(tmp[i] | (i!=0?0x80:0x00)));
       }
-      encodeByte( (byte) (tmp & 0x7f) );
     }
   }
 }
-- 
GitLab