OSCPacket.cs 4.7 KB

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