OSCMessage.cs 3.7 KB

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