void InvokeAnyMessageEventRecursively(OscPacket packet) { if (packet is OscBundle) { OscBundle bundle = packet as OscBundle; foreach (OscPacket subPacket in bundle.packets) { InvokeAnyMessageEventRecursively(subPacket); } } else { onAnyMessage.Invoke(packet as OscMessage); _messageCount++; } }
void Update() { if (_mappings == null) { return; } if (!isOpen) { return; } if (_mappingLookup == null || _dirtyMappings) { UpdateMappings(); } if (invokeUnmappedQueue == null) { invokeUnmappedQueue = new Queue <OscMessage>(); } if (invokeMappedQueue == null) { invokeMappedQueue = new Queue <KeyValuePair <OscMapping, OscMessage> >(); } deadAddresses.Clear(); // Lock while we mess with '_messages' lock ( _lock ) { foreach (KeyValuePair <string, List <OscMessage> > pair in _messages) { List <OscMessage> groupedMessages = pair.Value; int gmCount = groupedMessages.Count; // Collect the dead if (gmCount == 0) { deadAddresses.Add(pair.Key); continue; } // Get messages and invoke mapped handlers int gmBegin = _filterDuplicates ? gmCount - 1 : 0; for (int gm = gmBegin; gm < gmCount; gm++) { OscMessage message = groupedMessages[gm]; OscMapping mapping; if (_mappingLookup.TryGetValue(message.address, out mapping)) { // Enqueue mapped call invokeMappedQueue.Enqueue(new KeyValuePair <OscMapping, OscMessage>(mapping, message)); } else { // Enqueue unmapped (catch all) call invokeUnmappedQueue.Enqueue(message); } } // Clear address group groupedMessages.Clear(); } // Remove the dead foreach (string address in deadAddresses) { _messages.Remove(address); } } // Count _messageCount = invokeUnmappedQueue.Count + invokeMappedQueue.Count; // Invoke handlers outside lock while (invokeUnmappedQueue.Count > 0) { _onAnyMessage.Invoke(invokeUnmappedQueue.Dequeue()); } while (invokeMappedQueue.Count > 0) { KeyValuePair <OscMapping, OscMessage> pair = invokeMappedQueue.Dequeue(); OscMapping mapping = pair.Key; OscMessage message = pair.Value; _onAnyMessage.Invoke(message); switch (mapping.type) { case OscMessageType.OscMessage: mapping.OscMessageHandler.Invoke(message); break; case OscMessageType.Float: float floatValue; if (message.TryGet(0, out floatValue)) { mapping.FloatHandler.Invoke(floatValue); } break; case OscMessageType.Double: double doubleValue; if (message.TryGet(0, out doubleValue)) { mapping.DoubleHandler.Invoke(doubleValue); } break; case OscMessageType.Int: int intValue; if (message.TryGet(0, out intValue)) { mapping.IntHandler.Invoke(intValue); } break; case OscMessageType.Long: long longValue; if (message.TryGet(0, out longValue)) { mapping.LongHandler.Invoke(longValue); } break; case OscMessageType.String: string stringValue; if (message.TryGet(0, out stringValue)) { mapping.StringHandler.Invoke(stringValue); } break; case OscMessageType.Char: char charValue; if (message.TryGet(0, out charValue)) { mapping.CharHandler.Invoke(charValue); } break; case OscMessageType.Bool: bool boolValue; if (message.TryGet(0, out boolValue)) { mapping.BoolHandler.Invoke(boolValue); } break; case OscMessageType.Color: Color32 colorValue; if (message.TryGet(0, out colorValue)) { mapping.ColorHandler.Invoke(colorValue); } break; case OscMessageType.Blob: byte[] blobValue; if (message.TryGet(0, out blobValue)) { mapping.BlobHandler.Invoke(blobValue); } break; case OscMessageType.TimeTag: OscTimeTag timeTagValue; if (message.TryGet(0, out timeTagValue)) { mapping.TimeTagHandler.Invoke(timeTagValue); } break; case OscMessageType.ImpulseNullEmpty: mapping.ImpulseNullEmptyHandler.Invoke(); break; } } }