OSCMessage.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections;
  3. namespace OSC.NET
  4. {
  5. /// <summary>
  6. /// OSCMessage
  7. /// </summary>
  8. public class OSCMessage : OSCPacket
  9. {
  10. protected const char DOUBLE = 'd';
  11. protected const char FLOAT = 'f';
  12. protected const char INTEGER = 'i';
  13. protected const char LONG = 'h';
  14. protected const char STRING = 's';
  15. protected const char SYMBOL = 'S';
  16. // protected const char BLOB = 'b';
  17. // protected const char ALL = '*';
  18. protected string typeTag;
  19. public OSCMessage(string address)
  20. {
  21. this.typeTag = ",";
  22. this.Address = address;
  23. }
  24. public OSCMessage(string address, object value)
  25. {
  26. this.typeTag = ",";
  27. this.Address = address;
  28. Append(value);
  29. }
  30. public static OSCMessage Unpack(byte[] bytes, ref int start)
  31. {
  32. string address = unpackString(bytes, ref start);
  33. //Console.WriteLine("address: " + address);
  34. OSCMessage msg = new OSCMessage(address);
  35. char[] tags = unpackString(bytes, ref start).ToCharArray();
  36. //Console.WriteLine("tags: " + new string(tags));
  37. foreach (char tag in tags)
  38. {
  39. //Console.WriteLine("tag: " + tag + " @ "+start);
  40. if (tag == ',') continue;
  41. else if (tag == INTEGER) msg.Append(unpackInt(bytes, ref start));
  42. else if (tag == LONG) msg.Append(unpackLong(bytes, ref start));
  43. else if (tag == DOUBLE) msg.Append(unpackDouble(bytes, ref start));
  44. else if (tag == FLOAT) msg.Append(unpackFloat(bytes, ref start));
  45. else if (tag == STRING || tag == SYMBOL) msg.Append(unpackString(bytes, ref start));
  46. else Console.WriteLine("unknown tag: " + tag);
  47. }
  48. return msg;
  49. }
  50. override public void Append(object value)
  51. {
  52. if (value is int)
  53. {
  54. AppendTag(INTEGER);
  55. }
  56. else if (value is long)
  57. {
  58. AppendTag(LONG);
  59. }
  60. else if (value is float)
  61. {
  62. AppendTag(FLOAT);
  63. }
  64. else if (value is double)
  65. {
  66. AppendTag(DOUBLE);
  67. }
  68. else if (value is string)
  69. {
  70. AppendTag(STRING);
  71. }
  72. else
  73. {
  74. // TODO: exception
  75. }
  76. values.Add(value);
  77. }
  78. override public bool IsBundle()
  79. {
  80. return false;
  81. }
  82. protected void AppendTag(char type)
  83. {
  84. typeTag += type;
  85. }
  86. override protected void pack()
  87. {
  88. ArrayList data = new ArrayList();
  89. addBytes(data, packString(this.address));
  90. padNull(data);
  91. addBytes(data, packString(this.typeTag));
  92. padNull(data);
  93. foreach (object value in this.Values)
  94. {
  95. if (value is int) addBytes(data, packInt((int)value));
  96. else if (value is long) addBytes(data, packLong((long)value));
  97. else if (value is float) addBytes(data, packFloat((float)value));
  98. else if (value is double) addBytes(data, packDouble((double)value));
  99. else if (value is string)
  100. {
  101. addBytes(data, packString((string)value));
  102. padNull(data);
  103. }
  104. else
  105. {
  106. // TODO
  107. }
  108. }
  109. this.binaryData = (byte[])data.ToArray(typeof(byte));
  110. }
  111. }
  112. }