diff --git a/GlaxOSC/OSC.cs b/GlaxOSC/OSC.cs index 18f2de2..05545f5 100644 --- a/GlaxOSC/OSC.cs +++ b/GlaxOSC/OSC.cs @@ -20,7 +20,7 @@ public class OSC : IDisposable private const int UpdateInterval = 10; internal readonly Dictionary ParameterValues = new(); - public OSC(string serviceName, Dictionary endpoints, string ipSend = "127.0.0.1", int portSend = 9000, int? portReceive = null, ILogger? logger = null) + public OSC(string serviceName, IEnumerable endpoints, string ipSend = "127.0.0.1", int portSend = 9000, int? portReceive = null, ILogger? logger = null) { this._logger = logger; this.Client = new OscClient(ipSend, portSend); @@ -36,8 +36,8 @@ public class OSC : IDisposable this._oscServer = new OscServer(OSCPort); this.AddEndpoint("/avatar/change", typeof(string)); - foreach (KeyValuePair endpoint in endpoints) - this.AddEndpoint(endpoint.Key, endpoint.Value); + foreach (OSCEndpoint endpoint in endpoints) + this.AddEndpoint(endpoint); this._oscServer.Start(); this._logger?.LogInformation($"TCP: {this._oscQuery.TcpPort} UDP/OSC: {portReceive}"); @@ -46,6 +46,14 @@ public class OSC : IDisposable this._listenThread.Start(); } + public OSC(string serviceName, Dictionary endpoints, string ipSend = "127.0.0.1", int portSend = 9000, + int? portReceive = null, ILogger? logger = null) + : this(serviceName, endpoints.Select(endpoint => new OSCEndpoint(endpoint.Key, endpoint.Value, null)), ipSend, + portSend, portReceive, logger) + { + + } + private void Listen() { while (_keepRunning) @@ -81,7 +89,10 @@ public class OSC : IDisposable public delegate void OnParameterChangeEventHandler(string endpoint, object? oldValue, object? newValue); - internal void AddEndpoint(string endpoint, Type type) + internal void AddEndpoint(OSCEndpoint oscEndpoint) => + AddEndpoint(oscEndpoint.Endpoint, oscEndpoint.Type, oscEndpoint.Callback); + + internal void AddEndpoint(string endpoint, Type type, OnParameterChangeEventHandler? callback = null) { this._logger?.LogDebug($"Adding endpoint {endpoint}"); Dictionary oscTypeLookup = new Dictionary() @@ -99,10 +110,10 @@ public class OSC : IDisposable }; this._oscQuery.AddEndpoint(endpoint, oscTypeLookup[type], Attributes.AccessValues.WriteOnly); this.ParameterValues.Add(endpoint, null); - this._oscServer.TryAddMethod(endpoint, values => HandleParameterChange(endpoint, type, values)); + this._oscServer.TryAddMethod(endpoint, values => HandleParameterChange(endpoint, type, values, callback)); } - private void HandleParameterChange(string endpoint, Type type, OscMessageValues values) + private void HandleParameterChange(string endpoint, Type type, OscMessageValues values, OnParameterChangeEventHandler? callback) { object? oldValue = ParameterValues[endpoint]; if (type == typeof(int)) @@ -120,6 +131,7 @@ public class OSC : IDisposable else if (type == typeof(bool)) ParameterValues[endpoint] = values.ReadBooleanElement(0); OnParameterChangeEvent?.Invoke(endpoint, oldValue, ParameterValues[endpoint]); + callback?.Invoke(endpoint, oldValue, ParameterValues[endpoint]); this._logger?.LogDebug($"{endpoint} {oldValue} -> {ParameterValues[endpoint]}"); } diff --git a/GlaxOSC/OSCEndpoint.cs b/GlaxOSC/OSCEndpoint.cs new file mode 100644 index 0000000..d8d82bd --- /dev/null +++ b/GlaxOSC/OSCEndpoint.cs @@ -0,0 +1,15 @@ +namespace GlaxOSC; + +public struct OSCEndpoint +{ + public string Endpoint; + public Type Type; + public OSC.OnParameterChangeEventHandler? Callback; + + public OSCEndpoint(string endpoint, Type type, OSC.OnParameterChangeEventHandler? callback) + { + this.Endpoint = endpoint; + this.Type = type; + this.Callback = callback; + } +} \ No newline at end of file