LabCommEncoderChannel.cs 2.43 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
namespace se.lth.control.labcomm {

  using System;
  using System.IO;
  using System.Text;
  using System.Runtime.InteropServices;

  public class LabCommEncoderChannel : LabCommEncoder {

    private Stream writer;
    private MemoryStream bytes = new MemoryStream();
    private LabCommEncoderRegistry registry = new LabCommEncoderRegistry();
    byte[] buf = new byte[8];

    public LabCommEncoderChannel(Stream writer) {
      this.writer = writer;
    }

    public void register(LabCommDispatcher dispatcher) {
      int index = registry.add(dispatcher);
21
22
      encodePacked32(LabComm.SAMPLE);
      encodePacked32(index);
Anders Nilsson's avatar
Anders Nilsson committed
23
24
25
26
27
28
29
30
31
      encodeString(dispatcher.getName());
      byte[] signature = dispatcher.getSignature();
      for (int i = 0 ; i < signature.Length ; i++) {
	encodeByte(signature[i]);
      }
      end(null);
    }

    public void begin(Type c) {
32
      encodePacked32(registry.getTag(c));
Anders Nilsson's avatar
Anders Nilsson committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    }

    public void end(Type c) {
      bytes.WriteTo(writer);
      bytes.SetLength(0);
    }

    private void WriteInt(Int64 value, int length) {
      for (int i = length - 1 ; i >= 0 ; i--) {
	buf[i] = (byte)(value & 0xff);
	value = value >> 8;
      }
      bytes.Write(buf, 0, length);
    }

    public void encodeBoolean(bool value) {
      WriteInt(value ? 0 : 1, 1);
    }

    public void encodeByte(byte value) {
      WriteInt(value, 1);
    }

    public void encodeShort(short value) {
      WriteInt(value, 2);
    }

    public void encodeInt(int value) {
      WriteInt(value, 4);
    }

    public void encodeLong(long value) {
      WriteInt(value, 8);
    }

    [StructLayout(LayoutKind.Explicit)]
    private struct Int32SingleUnion {
      [FieldOffset(0)] public int AsInt;
      [FieldOffset(0)] public float AsFloat;
    };

    public void encodeFloat(float value) {
      Int32SingleUnion u;
      u.AsInt = 0; // Avoid error messge
      u.AsFloat = value;
      WriteInt(u.AsInt, 4);
    }

    public void encodeDouble(double value) {
      WriteInt(BitConverter.DoubleToInt64Bits(value), 8);
    }

    public void encodeString(String value) {
      byte[] buf = Encoding.UTF8.GetBytes(value);
87
      encodePacked32(buf.Length);
Anders Nilsson's avatar
Anders Nilsson committed
88
89
90
      bytes.Write(buf, 0, buf.Length);
    }

91
92
    public void encodePacked32(Int64 value) {
      Int64 tmp = value;
93
      TODO: Correct byteorder
94
95
96
97
98
99
      while(tmp >= 0x80) {
        encodeByte( (byte) ((tmp & 0x7f) | 0x80 ) );
        tmp >>= 7;           
      }
      encodeByte( (byte) (tmp & 0x7f) );
    }
Anders Nilsson's avatar
Anders Nilsson committed
100
  }
101
}