OSCBundle.cs 2.3 KB

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