This commit is contained in:
parent
76f2e55883
commit
e20683f718
1 changed files with 204 additions and 1 deletions
|
|
@ -13,6 +13,179 @@ namespace DMX2
|
||||||
Thread pollThread = null;
|
Thread pollThread = null;
|
||||||
bool running=true;
|
bool running=true;
|
||||||
|
|
||||||
|
|
||||||
|
enum OSCType {
|
||||||
|
Int32,
|
||||||
|
Float32,
|
||||||
|
String,
|
||||||
|
Blob
|
||||||
|
}
|
||||||
|
|
||||||
|
class OSCMessage {
|
||||||
|
|
||||||
|
public abstract class OSCArg{
|
||||||
|
protected OSCArg(){}
|
||||||
|
public abstract OSCType Type{ get; }
|
||||||
|
public virtual string GetString(){ throw new System.NotImplementedException (); }
|
||||||
|
public virtual float GetFloat (){throw new System.NotImplementedException (); }
|
||||||
|
public virtual int GetInt(){throw new System.NotImplementedException (); }
|
||||||
|
public virtual byte[] GetBlob(){throw new System.NotImplementedException (); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private class OSCStringArg : OSCArg{
|
||||||
|
string _s;
|
||||||
|
public override OSCType Type {
|
||||||
|
get {
|
||||||
|
return OSCType.String;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public OSCStringArg(string s){
|
||||||
|
_s=s;
|
||||||
|
}
|
||||||
|
public override string GetString ()
|
||||||
|
{
|
||||||
|
return _s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private class OSCIntArg : OSCArg{
|
||||||
|
int _i;
|
||||||
|
public override OSCType Type {
|
||||||
|
get {
|
||||||
|
return OSCType.Int32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public OSCIntArg(int i){
|
||||||
|
_i=i;
|
||||||
|
}
|
||||||
|
public override int GetInt ()
|
||||||
|
{
|
||||||
|
return _i;
|
||||||
|
}
|
||||||
|
public override float GetFloat ()
|
||||||
|
{
|
||||||
|
return (float)_i;
|
||||||
|
}
|
||||||
|
public override string GetString ()
|
||||||
|
{
|
||||||
|
return _i.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
private class OSCFloatArg : OSCArg{
|
||||||
|
float _f;
|
||||||
|
public override OSCType Type {
|
||||||
|
get {
|
||||||
|
return OSCType.Float32;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public OSCFloatArg(float f){
|
||||||
|
_f=f;
|
||||||
|
}
|
||||||
|
public override int GetInt ()
|
||||||
|
{
|
||||||
|
return (int)(_f);
|
||||||
|
}
|
||||||
|
public override float GetFloat ()
|
||||||
|
{
|
||||||
|
return _f;
|
||||||
|
}
|
||||||
|
public override string GetString ()
|
||||||
|
{
|
||||||
|
return _f.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
private class OSCBlobArg : OSCArg{
|
||||||
|
byte[] _b;
|
||||||
|
public override OSCType Type {
|
||||||
|
get {
|
||||||
|
return OSCType.Blob;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public OSCBlobArg(byte[] b){
|
||||||
|
_b=b;
|
||||||
|
}
|
||||||
|
public override byte[] GetBlob ()
|
||||||
|
{
|
||||||
|
return _b;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static string DecodeString (byte[] b, ref int pos)
|
||||||
|
{
|
||||||
|
int end = Array.IndexOf<byte>(b,0,pos);
|
||||||
|
if(end==-1) end = b.Length;
|
||||||
|
string ret = System.Text.Encoding.ASCII.GetString(b,pos,end-pos);
|
||||||
|
pos = (end/4+1)*4;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static float DecodeFloat (byte[] b, ref int pos)
|
||||||
|
{
|
||||||
|
if(BitConverter.IsLittleEndian)
|
||||||
|
Array.Reverse(b,pos,4);
|
||||||
|
float ret = BitConverter.ToSingle(b,pos);
|
||||||
|
pos+=4;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int DecodeInt (byte[] b, ref int pos)
|
||||||
|
{
|
||||||
|
if(BitConverter.IsLittleEndian)
|
||||||
|
Array.Reverse(b,pos,4);
|
||||||
|
int ret = BitConverter.ToInt32(b, pos);
|
||||||
|
pos+=4;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OSCMessage(byte[] bmsg){
|
||||||
|
int pos = 0;
|
||||||
|
Address = DecodeString(bmsg,ref pos);
|
||||||
|
string typestring = DecodeString(bmsg,ref pos);
|
||||||
|
|
||||||
|
args = new OSCArg[typestring.Length-1];
|
||||||
|
|
||||||
|
int idx=0;
|
||||||
|
foreach(char c in typestring){
|
||||||
|
switch(c){
|
||||||
|
case 'f':
|
||||||
|
args[idx++] = new OSCFloatArg(DecodeFloat(bmsg,ref pos));
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
args[idx++] = new OSCStringArg(DecodeString(bmsg,ref pos));
|
||||||
|
break;
|
||||||
|
case 'i':
|
||||||
|
args[idx++] = new OSCIntArg(DecodeInt(bmsg,ref pos));
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
int len=DecodeInt(bmsg,ref pos)*4;
|
||||||
|
byte[] b = new byte[len];
|
||||||
|
bmsg.CopyTo(b,0);
|
||||||
|
pos+=len;
|
||||||
|
args[idx++] = new OSCBlobArg(b);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
public string Address{
|
||||||
|
get;
|
||||||
|
private set;
|
||||||
|
}
|
||||||
|
|
||||||
|
OSCArg[] args;
|
||||||
|
|
||||||
|
public OSCArg[] Args {
|
||||||
|
get {
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public OSCServer ()
|
public OSCServer ()
|
||||||
{
|
{
|
||||||
pollThread = new Thread(new ThreadStart(Loop));
|
pollThread = new Thread(new ThreadStart(Loop));
|
||||||
|
|
@ -29,6 +202,12 @@ namespace DMX2
|
||||||
try {
|
try {
|
||||||
while (running) {
|
while (running) {
|
||||||
recv = udpCli.Receive (ref remep);
|
recv = udpCli.Receive (ref remep);
|
||||||
|
Console.WriteLine(remep);
|
||||||
|
OSCMessage msg = new OSCMessage(recv);
|
||||||
|
Console.WriteLine(msg.Address);
|
||||||
|
foreach(var arg in msg.Args)
|
||||||
|
Console.WriteLine(arg.GetString());
|
||||||
|
ProcessMessage(msg);
|
||||||
}
|
}
|
||||||
} catch (SocketException ex) {
|
} catch (SocketException ex) {
|
||||||
}
|
}
|
||||||
|
|
@ -36,8 +215,32 @@ namespace DMX2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Flush ()
|
|
||||||
|
/// <summary>
|
||||||
|
/// /master
|
||||||
|
/// /masterseq/
|
||||||
|
/// go
|
||||||
|
/// goback
|
||||||
|
/// goto
|
||||||
|
///
|
||||||
|
/// /seq/X/
|
||||||
|
/// go
|
||||||
|
/// goback
|
||||||
|
/// goto
|
||||||
|
/// master
|
||||||
|
/// circuit
|
||||||
|
///
|
||||||
|
/// /universe/X/
|
||||||
|
/// D/on
|
||||||
|
/// reset
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
|
||||||
|
void ProcessMessage (OSCMessage msg)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue