123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using System;
- using System.Collections;
- using System.Text;
- namespace OSC.NET
- {
- /// <summary>
- /// OSCPacket
- /// </summary>
- abstract public class OSCPacket
- {
- public OSCPacket()
- {
- this.values = new ArrayList();
- }
- protected static void addBytes(ArrayList data, byte[] bytes)
- {
- foreach (byte b in bytes)
- {
- data.Add(b);
- }
- }
- protected static void padNull(ArrayList data)
- {
- byte zero = 0;
- int pad = 4 - (data.Count % 4);
- for (int i = 0; i < pad; i++)
- {
- data.Add(zero);
- }
- }
- protected static byte[] swapEndian(byte[] data)
- {
- byte[] swapped = new byte[data.Length];
- for (int i = data.Length - 1, j = 0; i >= 0; i--, j++)
- {
- swapped[j] = data[i];
- }
- return swapped;
- }
- protected static byte[] packInt(int value)
- {
- byte[] data = BitConverter.GetBytes(value);
- if (BitConverter.IsLittleEndian) data = swapEndian(data);
- return data;
- }
- protected static byte[] packLong(long value)
- {
- byte[] data = BitConverter.GetBytes(value);
- if (BitConverter.IsLittleEndian) data = swapEndian(data);
- return data;
- }
- protected static byte[] packFloat(float value)
- {
- byte[] data = BitConverter.GetBytes(value);
- if (BitConverter.IsLittleEndian) data = swapEndian(data);
- return data;
- }
- protected static byte[] packDouble(double value)
- {
- byte[] data = BitConverter.GetBytes(value);
- if (BitConverter.IsLittleEndian) data = swapEndian(data);
- return data;
- }
- protected static byte[] packString(string value)
- {
- return System.Text.Encoding.ASCII.GetBytes(value);
- }
- abstract protected void pack();
- protected byte[] binaryData;
- public byte[] BinaryData
- {
- get
- {
- pack();
- return binaryData;
- }
- }
- protected static int unpackInt(byte[] bytes, ref int start)
- {
- byte[] data = new byte[4];
- for (int i = 0; i < 4; i++, start++) data[i] = bytes[start];
- if (BitConverter.IsLittleEndian) data = swapEndian(data);
- return BitConverter.ToInt32(data, 0);
- }
- protected static long unpackLong(byte[] bytes, ref int start)
- {
- byte[] data = new byte[8];
- for (int i = 0; i < 8; i++, start++) data[i] = bytes[start];
- if (BitConverter.IsLittleEndian) data = swapEndian(data);
- return BitConverter.ToInt64(data, 0);
- }
- protected static float unpackFloat(byte[] bytes, ref int start)
- {
- byte[] data = new byte[4];
- for (int i = 0; i < 4; i++, start++) data[i] = bytes[start];
- if (BitConverter.IsLittleEndian) data = swapEndian(data);
- return BitConverter.ToSingle(data, 0);
- }
- protected static double unpackDouble(byte[] bytes, ref int start)
- {
- byte[] data = new byte[8];
- for (int i = 0; i < 8; i++, start++) data[i] = bytes[start];
- if (BitConverter.IsLittleEndian) data = swapEndian(data);
- return BitConverter.ToDouble(data, 0);
- }
- protected static string unpackString(byte[] bytes, ref int start)
- {
- int count = 0;
- for (int index = start; bytes[index] != 0; index++, count++) ;
- string s = Encoding.ASCII.GetString(bytes, start, count);
- start += count + 1;
- start = (start + 3) / 4 * 4;
- return s;
- }
- public static OSCPacket Unpack(byte[] bytes)
- {
- int start = 0;
- return Unpack(bytes, ref start, bytes.Length);
- }
- public static OSCPacket Unpack(byte[] bytes, ref int start, int end)
- {
- if (bytes[start] == '#') return OSCBundle.Unpack(bytes, ref start, end);
- else return OSCMessage.Unpack(bytes, ref start);
- }
- protected string address;
- public string Address
- {
- get { return address; }
- set
- {
- // TODO: validate
- address = value;
- }
- }
- protected ArrayList values;
- public ArrayList Values
- {
- get { return (ArrayList)values.Clone(); }
- }
- abstract public void Append(object value);
- abstract public bool IsBundle();
- }
- }
|