/// <summary> /// Register a single background thread method for an OSC address /// </summary> /// <param name="address">The OSC address to handle messages for</param> /// <param name="valueReadMethod"> /// The method to execute immediately on the worker thread that reads values from the message /// </param> /// <returns>True if the address was valid, false otherwise</returns> public bool TryAddMethod(string address, Action <OscMessageValues> valueReadMethod) { var pair = new OscActionPair(valueReadMethod); k_SingleCallbackToPair.Add(valueReadMethod, pair); return(AddressSpace.TryAddMethod(address, pair)); }
void HandleCallbacks(OscActionPair pair, OscMessageValues messageValues) { // call the value read method associated with this OSC address pair.ValueRead(messageValues); // if there's a main thread method, queue it if (pair.MainThreadQueued != null) { if (m_MainThreadCount >= m_MainThreadQueue.Length) { Array.Resize(ref m_MainThreadQueue, m_MainThreadCount + 16); } m_MainThreadQueue[m_MainThreadCount++] = pair.MainThreadQueued; } }
public bool TryAddMethod(string address, OscActionPair onReceived) { if (string.IsNullOrEmpty(address) || onReceived == null) { return(false); } switch (OscParser.GetAddressType(address)) { case AddressType.Address: AddressToMethod.Add(address, onReceived); return(true); case AddressType.Pattern: int index; // if a method has already been registered for this pattern, add the new delegate if (PatternStringToIndex.TryGetValue(address, out index)) { PatternMethods[index] += onReceived; return(true); } if (FreedPatternIndices.Count > 0) { index = FreedPatternIndices.Dequeue(); } else { index = PatternCount; if (index >= Patterns.Length) { var newSize = Patterns.Length * 2; Array.Resize(ref Patterns, newSize); Array.Resize(ref PatternMethods, newSize); } } Patterns[index] = new Regex(address); PatternMethods[index] = onReceived; PatternStringToIndex[address] = index; PatternCount++; return(true); default: return(false); } }
void OnEnable() { if (m_Receiver == null) { m_Receiver = GetComponentInParent <OscReceiver>(); } if (m_Registered || string.IsNullOrEmpty(Address)) { return; } if (m_Receiver != null && m_Receiver.Server != null) { m_ActionPair = new OscActionPair(ValueRead, InvokeEvent); Receiver.Server.TryAddMethodPair(Address, m_ActionPair); m_Registered = true; } }
public void Add(string address, OscActionPair callbacks) { if (!SourceToBlob.TryGetValue(address, out var blobStr)) { blobStr = new BlobString(address); HandleToValue[blobStr.Handle] = callbacks; SourceToBlob.Add(address, blobStr); } else { if (HandleToValue.ContainsKey(blobStr.Handle)) { HandleToValue[blobStr.Handle] += callbacks; } else { HandleToValue[blobStr.Handle] = callbacks; } } }
public bool Remove(string address, OscActionPair callbacks) { if (!SourceToBlob.TryGetValue(address, out var blobStr)) { return(false); } if (!HandleToValue.TryGetValue(blobStr.Handle, out var existingPair)) { return(false); } var valueReadMethod = existingPair.ValueRead; if (valueReadMethod.GetInvocationList().Length == 1) { var removed = HandleToValue.Remove(blobStr.Handle) && SourceToBlob.Remove(address); blobStr.Dispose(); return(removed); } HandleToValue[blobStr.Handle] -= callbacks; return(true); }
public bool RemoveMethod(string address, OscActionPair onReceived) { if (string.IsNullOrEmpty(address) || onReceived == null) { return(false); } switch (OscParser.GetAddressType(address)) { case AddressType.Address: return(AddressToMethod.Remove(address, onReceived)); case AddressType.Pattern: if (!PatternStringToIndex.TryGetValue(address, out var patternIndex)) { return(false); } var method = PatternMethods[patternIndex].ValueRead; if (method.GetInvocationList().Length == 1) { Patterns[patternIndex] = null; PatternMethods[patternIndex] = null; } else { PatternMethods[patternIndex] -= onReceived; } PatternCount--; FreedPatternIndices.Enqueue(patternIndex); return(PatternStringToIndex.Remove(address)); default: return(false); } }
/// <summary> /// Remove a background thread read callback and main thread callback associated with an OSC address. /// </summary> /// <param name="address">The OSC address to remove methods from</param> /// <param name="actionPair">The pair of callbacks to remove</param> /// <returns>True if successfully removed, false otherwise</returns> public bool RemoveMethodPair(string address, OscActionPair actionPair) { // if the address space is null, this got called during cleanup / shutdown, // and effectively all addresses are removed by setting it to null return(AddressSpace == null || AddressSpace.RemoveMethod(address, actionPair)); }
/// <summary> /// Add a background thread read callback and main thread callback associated with an OSC address. /// </summary> /// <param name="address">The OSC address to associate a method with</param> /// <param name="actionPair">The pair of callbacks to add</param> /// <returns>True if the address was valid & methods associated with it, false otherwise</returns> public bool TryAddMethodPair(string address, OscActionPair actionPair) => AddressSpace.TryAddMethod(address, actionPair);
public bool TryGetValueFromBytes(byte *ptr, int byteCount, out OscActionPair value) { return(HandleToValue.TryGetValue(new BlobHandle(ptr, byteCount), out value)); }