Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fd9e51
commit a452e99
Showing
76 changed files
with
1,093 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections; | ||
|
||
public class jsOSCBundle {} | ||
|
||
namespace OSC.NET | ||
{ | ||
/// <summary> | ||
/// OSCBundle | ||
/// </summary> | ||
public class OSCBundle : OSCPacket | ||
{ | ||
protected const string BUNDLE = "#bundle"; | ||
private long timestamp = 0; | ||
|
||
public OSCBundle(long ts) | ||
{ | ||
this.address = BUNDLE; | ||
this.timestamp = ts; | ||
} | ||
|
||
public OSCBundle() | ||
{ | ||
this.address = BUNDLE; | ||
this.timestamp = 0; | ||
} | ||
|
||
override protected void pack() | ||
{ | ||
ArrayList data = new ArrayList(); | ||
|
||
addBytes(data, packString(this.Address)); | ||
padNull(data); | ||
addBytes(data, packLong(0)); // TODO | ||
|
||
foreach(object value in this.Values) | ||
{ | ||
if(value is OSCPacket) | ||
{ | ||
byte[] bs = ((OSCPacket)value).BinaryData; | ||
addBytes(data, packInt(bs.Length)); | ||
addBytes(data, bs); | ||
} | ||
else | ||
{ | ||
// TODO | ||
} | ||
} | ||
|
||
this.binaryData = (byte[])data.ToArray(typeof(byte)); | ||
} | ||
|
||
public static new OSCBundle Unpack(byte[] bytes, ref int start, int end) | ||
{ | ||
|
||
string address = unpackString(bytes, ref start); | ||
//Console.WriteLine("bundle: " + address); | ||
if(!address.Equals(BUNDLE)) return null; // TODO | ||
|
||
long timestamp = unpackLong(bytes, ref start); | ||
OSCBundle bundle = new OSCBundle(timestamp); | ||
|
||
while(start < end) | ||
{ | ||
int length = unpackInt(bytes, ref start); | ||
int sub_end = start + length; | ||
//Console.WriteLine(bytes.Length +" "+ start+" "+length+" "+sub_end); | ||
bundle.Append(OSCPacket.Unpack(bytes, ref start, sub_end)); | ||
|
||
} | ||
|
||
return bundle; | ||
} | ||
|
||
public long getTimeStamp() { | ||
return timestamp; | ||
} | ||
|
||
override public void Append(object value) | ||
{ | ||
if( value is OSCPacket) | ||
{ | ||
values.Add(value); | ||
} | ||
else | ||
{ | ||
// TODO: exception | ||
} | ||
} | ||
|
||
override public bool IsBundle() { return true; } | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Text; | ||
|
||
public class jsOSCMessage {} | ||
|
||
namespace OSC.NET | ||
{ | ||
/// <summary> | ||
/// OSCMessage | ||
/// </summary> | ||
public class OSCMessage : OSCPacket | ||
{ | ||
protected const char INTEGER = 'i'; | ||
protected const char FLOAT = 'f'; | ||
protected const char LONG = 'h'; | ||
protected const char DOUBLE = 'd'; | ||
protected const char STRING = 's'; | ||
protected const char SYMBOL = 'S'; | ||
//protected const char BLOB = 'b'; | ||
//protected const char ALL = '*'; | ||
|
||
public OSCMessage(string address) | ||
{ | ||
this.typeTag = ","; | ||
this.Address = address; | ||
} | ||
public OSCMessage(string address, object value) | ||
{ | ||
this.typeTag = ","; | ||
this.Address = address; | ||
Append(value); | ||
} | ||
|
||
override protected void pack() | ||
{ | ||
ArrayList data = new ArrayList(); | ||
|
||
addBytes(data, packString(this.address)); | ||
padNull(data); | ||
addBytes(data, packString(this.typeTag)); | ||
padNull(data); | ||
|
||
foreach(object value in this.Values) | ||
{ | ||
if(value is int) addBytes(data, packInt((int)value)); | ||
else if(value is long) addBytes(data, packLong((long)value)); | ||
else if(value is float) addBytes(data, packFloat((float)value)); | ||
else if(value is double) addBytes(data, packDouble((double)value)); | ||
else if(value is string) | ||
{ | ||
addBytes(data, packString((string)value)); | ||
padNull(data); | ||
} | ||
else | ||
{ | ||
// TODO | ||
} | ||
} | ||
|
||
this.binaryData = (byte[])data.ToArray(typeof(byte)); | ||
} | ||
|
||
|
||
public static OSCMessage Unpack(byte[] bytes, ref int start) | ||
{ | ||
string address = unpackString(bytes, ref start); | ||
//Console.WriteLine("address: " + address); | ||
OSCMessage msg = new OSCMessage(address); | ||
|
||
char[] tags = unpackString(bytes, ref start).ToCharArray(); | ||
//Console.WriteLine("tags: " + new string(tags)); | ||
foreach(char tag in tags) | ||
{ | ||
//Console.WriteLine("tag: " + tag + " @ "+start); | ||
if(tag == ',') continue; | ||
else if(tag == INTEGER) msg.Append(unpackInt(bytes, ref start)); | ||
else if(tag == LONG) msg.Append(unpackLong(bytes, ref start)); | ||
else if(tag == DOUBLE) msg.Append(unpackDouble(bytes, ref start)); | ||
else if(tag == FLOAT) msg.Append(unpackFloat(bytes, ref start)); | ||
else if(tag == STRING || tag == SYMBOL) msg.Append(unpackString(bytes, ref start)); | ||
else Console.WriteLine("unknown tag: "+tag); | ||
} | ||
|
||
return msg; | ||
} | ||
|
||
override public void Append(object value) | ||
{ | ||
if(value is int) | ||
{ | ||
AppendTag(INTEGER); | ||
} | ||
else if(value is long) | ||
{ | ||
AppendTag(LONG); | ||
} | ||
else if(value is float) | ||
{ | ||
AppendTag(FLOAT); | ||
} | ||
else if(value is double) | ||
{ | ||
AppendTag(DOUBLE); | ||
} | ||
else if(value is string) | ||
{ | ||
AppendTag(STRING); | ||
} | ||
else | ||
{ | ||
// TODO: exception | ||
} | ||
values.Add(value); | ||
} | ||
|
||
protected string typeTag; | ||
protected void AppendTag(char type) | ||
{ | ||
typeTag += type; | ||
} | ||
public string TypeTag {get {return typeTag;}} | ||
|
||
override public bool IsBundle() { return false; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Text; | ||
|
||
public class jsOSCPacket {} | ||
|
||
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(); | ||
} | ||
} |
Oops, something went wrong.