OSCPacket.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. namespace OSC.NET
  5. {
  6. /// <summary>
  7. /// OSCPacket
  8. /// </summary>
  9. abstract public class OSCPacket
  10. {
  11. protected string address;
  12. protected byte[] binaryData;
  13. protected ArrayList values;
  14. public string Address
  15. {
  16. get
  17. {
  18. return address;
  19. }
  20. set
  21. {
  22. // TODO: validate
  23. address = value;
  24. }
  25. }
  26. public byte[] BinaryData
  27. {
  28. get
  29. {
  30. pack();
  31. return binaryData;
  32. }
  33. }
  34. public ArrayList Values
  35. {
  36. get { return (ArrayList)values.Clone(); }
  37. }
  38. public OSCPacket()
  39. {
  40. this.values = new ArrayList();
  41. }
  42. public static OSCPacket Unpack(byte[] bytes)
  43. {
  44. int start = 0;
  45. return Unpack(bytes, ref start, bytes.Length);
  46. }
  47. public static OSCPacket Unpack(byte[] bytes, ref int start, int end)
  48. {
  49. if (bytes[start] == '#') return OSCBundle.Unpack(bytes, ref start, end);
  50. else return OSCMessage.Unpack(bytes, ref start);
  51. }
  52. abstract public void Append(object value);
  53. abstract public bool IsBundle();
  54. protected static void addBytes(ArrayList data, byte[] bytes)
  55. {
  56. foreach (byte b in bytes)
  57. {
  58. data.Add(b);
  59. }
  60. }
  61. protected static byte[] packDouble(double value)
  62. {
  63. byte[] data = BitConverter.GetBytes(value);
  64. if (BitConverter.IsLittleEndian) data = swapEndian(data);
  65. return data;
  66. }
  67. protected static byte[] packFloat(float value)
  68. {
  69. byte[] data = BitConverter.GetBytes(value);
  70. if (BitConverter.IsLittleEndian) data = swapEndian(data);
  71. return data;
  72. }
  73. protected static byte[] packInt(int value)
  74. {
  75. byte[] data = BitConverter.GetBytes(value);
  76. if (BitConverter.IsLittleEndian) data = swapEndian(data);
  77. return data;
  78. }
  79. protected static byte[] packLong(long value)
  80. {
  81. byte[] data = BitConverter.GetBytes(value);
  82. if (BitConverter.IsLittleEndian) data = swapEndian(data);
  83. return data;
  84. }
  85. protected static byte[] packString(string value)
  86. {
  87. return System.Text.Encoding.ASCII.GetBytes(value);
  88. }
  89. protected static void padNull(ArrayList data)
  90. {
  91. byte zero = 0;
  92. int pad = 4 - (data.Count % 4);
  93. for (int i = 0; i < pad; i++)
  94. {
  95. data.Add(zero);
  96. }
  97. }
  98. protected static byte[] swapEndian(byte[] data)
  99. {
  100. byte[] swapped = new byte[data.Length];
  101. for (int i = data.Length - 1, j = 0; i >= 0; i--, j++)
  102. {
  103. swapped[j] = data[i];
  104. }
  105. return swapped;
  106. }
  107. protected static double unpackDouble(byte[] bytes, ref int start)
  108. {
  109. byte[] data = new byte[8];
  110. for (int i = 0; i < 8; i++, start++) data[i] = bytes[start];
  111. if (BitConverter.IsLittleEndian) data = swapEndian(data);
  112. return BitConverter.ToDouble(data, 0);
  113. }
  114. protected static float unpackFloat(byte[] bytes, ref int start)
  115. {
  116. byte[] data = new byte[4];
  117. for (int i = 0; i < 4; i++, start++) data[i] = bytes[start];
  118. if (BitConverter.IsLittleEndian) data = swapEndian(data);
  119. return BitConverter.ToSingle(data, 0);
  120. }
  121. protected static int unpackInt(byte[] bytes, ref int start)
  122. {
  123. byte[] data = new byte[4];
  124. for (int i = 0; i < 4; i++, start++) data[i] = bytes[start];
  125. if (BitConverter.IsLittleEndian) data = swapEndian(data);
  126. return BitConverter.ToInt32(data, 0);
  127. }
  128. protected static long unpackLong(byte[] bytes, ref int start)
  129. {
  130. byte[] data = new byte[8];
  131. for (int i = 0; i < 8; i++, start++) data[i] = bytes[start];
  132. if (BitConverter.IsLittleEndian) data = swapEndian(data);
  133. return BitConverter.ToInt64(data, 0);
  134. }
  135. protected static string unpackString(byte[] bytes, ref int start)
  136. {
  137. int count = 0;
  138. for (int index = start; bytes[index] != 0; index++, count++) ;
  139. string s = Encoding.ASCII.GetString(bytes, start, count);
  140. start += count + 1;
  141. start = (start + 3) / 4 * 4;
  142. return s;
  143. }
  144. abstract protected void pack();
  145. }
  146. }