Skip to content

Commit

Permalink
optimization and stability
Browse files Browse the repository at this point in the history
  • Loading branch information
benkuper committed Oct 22, 2020
1 parent 999999d commit bea2cd5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 60 deletions.
149 changes: 90 additions & 59 deletions Runtime/OSCQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public enum FilterMode { All, Include, Exclude }

Dictionary<string, CompInfo> compInfoMap;

Dictionary<string, WSQuery> propQueryMap;
Dictionary<WSQuery, List<string>> propQueryMap;
Dictionary<string, object> propQueryPreviousValues;
bool propQueryMapLock;

RegisterService zeroconfService;
Expand All @@ -89,7 +90,8 @@ void Awake()

private void OnEnable()
{
if (propQueryMap == null) propQueryMap = new Dictionary<string, WSQuery>();
if (propQueryMap == null) propQueryMap = new Dictionary<WSQuery, List<string>>();
if (propQueryPreviousValues == null) propQueryPreviousValues = new Dictionary<string, object>();

if (filteredComponentNames.Count == 0)
{
Expand Down Expand Up @@ -156,12 +158,13 @@ void Update()

//while(propQueryMapLock)
//{
//wait
//wait
//}
propQueryMapLock = true;
foreach (KeyValuePair<string, WSQuery> aqm in propQueryMap)
foreach (KeyValuePair<WSQuery, List<string>> qprops in propQueryMap)
{
sendFeedback(aqm.Key, aqm.Value);
Debug.Log("Feedback for query " + qprops.Key.ID);
foreach (string s in qprops.Value) sendFeedback(s, qprops.Key);
}
propQueryMapLock = false;

Expand Down Expand Up @@ -211,9 +214,28 @@ WSQuery createWSQuery()
WSQuery q = new WSQuery();
q.messageReceived += wsMessageReceived;
q.dataReceived += wsDataReceived;
q.socketOpened += wsSocketOpened;
q.socketClosed += wsSocketClosed;
q.socketError += wsSocketError;
return q;
}

private void wsSocketOpened(WSQuery query)
{
propQueryMap.Add(query, new List<string>());
}

private void wsSocketClosed(WSQuery query)
{
propQueryMap.Remove(query);

}

private void wsSocketError(WSQuery query)
{
propQueryMap.Remove(query);
}

private void wsMessageReceived(WSQuery query, string message)
{
Debug.Log("Message received " + message);
Expand All @@ -232,11 +254,11 @@ private void wsMessageReceived(WSQuery query, string message)

if (command == "LISTEN")
{
propQueryMap.Add(data, query);
propQueryMap[query].Add(data);
}
else if (command == "IGNORE")
{
propQueryMap.Remove(data);
propQueryMap[query].Remove(data);
}

propQueryMapLock = false;
Expand Down Expand Up @@ -665,74 +687,83 @@ void ProcessIncomingMessages()
}



void sendFeedback(string address, WSQuery query)
{
CompInfo info = compInfoMap[address];

object oldData = null;
propQueryPreviousValues.TryGetValue(address, out oldData);

object data = null;
if (info.propInfo != null) data = info.propInfo.GetValue(info.comp);
else if (info.fieldInfo != null) data = info.fieldInfo.GetValue(info.comp);

if (data != null)
{
OSCMessage m = new OSCMessage(address);
if (data == null) return;

string dataType = data.GetType().Name;
switch (dataType)
{
case "Boolean":
{
bool val = (bool)data;
m.Append(val ? 1 : 0);
}
break;
propQueryPreviousValues[address] = data;

case "Vector2":
{
Vector2 v = (Vector2)data;
m.Append(v.x);
m.Append(v.y);
}
break;
OSCMessage m = new OSCMessage(address);

case "Vector3":
{
Vector3 v = (Vector3)data;
m.Append(v.x);
m.Append(v.y);
m.Append(v.z);
}
break;
string dataType = data.GetType().Name;
switch (dataType)
{
case "Boolean":
{
bool val = (bool)data;
if (oldData != null && val == (bool)oldData) return;
m.Append(val ? 1 : 0);
}
break;

case "Color":
{
Color color = (Color)data;
m.Append(color.r);
m.Append(color.g);
m.Append(color.b);
m.Append(color.a);
}
break;
case "Vector2":
{
Vector2 v = (Vector2)data;
if (oldData != null && v == (Vector2)oldData) return;
m.Append(v.x);
m.Append(v.y);
}
break;

case "Quaternion":
{
Vector3 v = ((Quaternion)data).eulerAngles;
m.Append(v.x);
m.Append(v.y);
m.Append(v.z);
}
break;
case "Vector3":
{
Vector3 v = (Vector3)data;
if (oldData != null && v == (Vector3)oldData) return;
m.Append(v.x);
m.Append(v.y);
m.Append(v.z);
}
break;

default:
if(info.type.IsEnum) m.Append(data.ToString());
else m.Append(data);
break;
}
case "Color":
{
Color color = (Color)data;
if (oldData != null && color == (Color)oldData) return;
m.Append(color.r);
m.Append(color.g);
m.Append(color.b);
m.Append(color.a);
}
break;

Debug.Log("Send data here ! "+ m.BinaryData.Length+" bytes");
query.sendData(m.BinaryData);
case "Quaternion":
{
Vector3 v = ((Quaternion)data).eulerAngles;
if (oldData != null && v == ((Quaternion)oldData).eulerAngles) return;

m.Append(v.x);
m.Append(v.y);
m.Append(v.z);
}
break;

default:
if (oldData != null && data.ToString() == oldData.ToString()) return;
if (info.type.IsEnum) m.Append(data.ToString());
else m.Append(data);
break;
}

query.sendData(m.BinaryData);
}
}
}
8 changes: 7 additions & 1 deletion Runtime/WSQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public class WSQuery : WebSocketBehavior
public delegate void DataEvent(WSQuery query, byte[] data);
public DataEvent dataReceived;

public delegate void WSQueryEvent(WSQuery query);
public WSQueryEvent socketOpened;
public WSQueryEvent socketClosed;
public WSQueryEvent socketError;

public void sendMessage(string message)
{
Expand All @@ -25,16 +29,18 @@ public void sendData(byte[] data)
protected override void OnOpen()
{
Debug.Log("Socket Opened ");
socketOpened?.Invoke(this);

}
protected override void OnClose(CloseEventArgs e)
{
Debug.Log("Socket closed " + e.Reason);

socketClosed?.Invoke(this);
}
protected override void OnError(ErrorEventArgs e)
{
Debug.LogWarning("Socket error " + e.Message);
socketError?.Invoke(this);
}

protected override void OnMessage(MessageEventArgs e)
Expand Down

0 comments on commit bea2cd5

Please sign in to comment.