1
0

Add optional Callback delegates to endpoints

This commit is contained in:
glax 2024-09-06 00:23:05 +02:00
parent 47981d31e5
commit 7b06587b33
2 changed files with 33 additions and 6 deletions

View File

@ -20,7 +20,7 @@ public class OSC : IDisposable
private const int UpdateInterval = 10;
internal readonly Dictionary<string, object?> ParameterValues = new();
public OSC(string serviceName, Dictionary<string, Type> endpoints, string ipSend = "127.0.0.1", int portSend = 9000, int? portReceive = null, ILogger? logger = null)
public OSC(string serviceName, IEnumerable<OSCEndpoint> 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<string, Type> 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<string, Type> 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<Type, string> oscTypeLookup = new Dictionary<Type, string>()
@ -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]}");
}

15
GlaxOSC/OSCEndpoint.cs Normal file
View File

@ -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;
}
}