OSCBundle.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections;
  2. namespace OSC.NET
  3. {
  4. /// <summary>
  5. /// OSCBundle
  6. /// </summary>
  7. public class OSCBundle : OSCPacket
  8. {
  9. protected const string BUNDLE = "#bundle";
  10. private long timestamp = 0;
  11. public OSCBundle(long ts)
  12. {
  13. this.address = BUNDLE;
  14. this.timestamp = ts;
  15. }
  16. public OSCBundle()
  17. {
  18. this.address = BUNDLE;
  19. this.timestamp = 0;
  20. }
  21. public static new OSCBundle Unpack(byte[] bytes, ref int start, int end)
  22. {
  23. string address = unpackString(bytes, ref start);
  24. //Console.WriteLine("bundle: " + address);
  25. if (!address.Equals(BUNDLE)) return null; // TODO
  26. long timestamp = unpackLong(bytes, ref start);
  27. OSCBundle bundle = new OSCBundle(timestamp);
  28. while (start < end)
  29. {
  30. int length = unpackInt(bytes, ref start);
  31. int sub_end = start + length;
  32. //Console.WriteLine(bytes.Length +" "+ start+" "+length+" "+sub_end);
  33. bundle.Append(OSCPacket.Unpack(bytes, ref start, sub_end));
  34. }
  35. return bundle;
  36. }
  37. override public void Append(object value)
  38. {
  39. if (value is OSCPacket)
  40. {
  41. values.Add(value);
  42. }
  43. else
  44. {
  45. // TODO: exception
  46. }
  47. }
  48. public long getTimeStamp()
  49. {
  50. return timestamp;
  51. }
  52. override public bool IsBundle()
  53. {
  54. return true;
  55. }
  56. override protected void pack()
  57. {
  58. ArrayList data = new ArrayList();
  59. addBytes(data, packString(this.Address));
  60. padNull(data);
  61. addBytes(data, packLong(0)); // TODO
  62. foreach (object value in this.Values)
  63. {
  64. if (value is OSCPacket)
  65. {
  66. byte[] bs = ((OSCPacket)value).BinaryData;
  67. addBytes(data, packInt(bs.Length));
  68. addBytes(data, bs);
  69. }
  70. else
  71. {
  72. // TODO
  73. }
  74. }
  75. this.binaryData = (byte[])data.ToArray(typeof(byte));
  76. }
  77. }
  78. }