OSCBundle.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. return timestamp;
  61. }
  62. override public void Append(object value)
  63. {
  64. if( value is OSCPacket)
  65. {
  66. values.Add(value);
  67. }
  68. else
  69. {
  70. // TODO: exception
  71. }
  72. }
  73. override public bool IsBundle() { return true; }
  74. }
  75. }