Skip to content

Commit

Permalink
OSC should not crash randomly anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
KimihikoAkayasaki committed Dec 29, 2023
1 parent fd608d6 commit dc9fc32
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions plugin_OSC/OSC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Osc : IServiceEndpoint

private static OscConfig _sOscConfig = new("127.0.0.1");
private static readonly Vector3 HeadOffset = new(0, 0, 0.2f);
private readonly List<OscClientPlus> _receivers = new();
private readonly SortedDictionary<string, OscClientPlus> _receivers = new();

private Exception _lastInitException;
[Import(typeof(IAmethystHost))] private IAmethystHost Host { get; set; }
Expand Down Expand Up @@ -145,15 +145,23 @@ public void DisplayToast((string Title, string Text) message)

public void Heartbeat()
{
// If the service hasn't started yet
if (!_receivers.Any()) return;
try
{
// If the service hasn't started yet
if (!_receivers.Any()) return;

var headJoint = Host?.GetHookJointPose();
if (!headJoint.HasValue) return;
var headJoint = Host?.GetHookJointPose();
if (!headJoint.HasValue) return;

// Vector3 eulerAngles = NumericExtensions.ToEulerAngles(headJoint.Value.Orientation);
foreach (var receiver in _receivers)
SendTrackerDataToReceiver("head", new OscPosition(headJoint.Value.Position + HeadOffset), receiver);
// Vector3 eulerAngles = NumericExtensions.ToEulerAngles(headJoint.Value.Orientation);
foreach (var receiver in _receivers.Values.ToList())
SendTrackerDataToReceiver("head", new OscPosition(headJoint.Value.Position + HeadOffset), receiver);
}
catch (Exception ex)
{
Host?.Log($"Unhandled Exception: {ex.GetType().Name} " +
$"in {ex.Source}: {ex.Message}\n{ex.StackTrace}", LogSeverity.Fatal);
}
}

public int Initialize()
Expand Down Expand Up @@ -400,7 +408,7 @@ public void Shutdown()
}

var trackers = trackerBases.ToList();
foreach (var receiver in _receivers)
foreach (var receiver in _receivers.Values.ToList())
try
{
foreach (var tracker in trackers)
Expand Down Expand Up @@ -432,7 +440,7 @@ public void Shutdown()
}

var trackers = trackerBases.ToList();
foreach (var receiver in _receivers)
foreach (var receiver in _receivers.Values.ToList())
try
{
foreach (var tracker in trackers)
Expand Down Expand Up @@ -472,10 +480,19 @@ public TrackerBase GetTrackerPose(string contains, bool canBeFromAmethyst = true
private async void OnOscQueryServiceFound(OSCQueryServiceProfile profile)
{
Host?.Log($"Found service {profile.name} at {profile.address}!");

if (!await ServiceSupportsTracking(profile)) return;

var hostInfo = await GetHostInfo(profile.address, profile.port);
AddTrackingReceiver(profile.address, hostInfo.oscPort);
if (_receivers.TryGetValue(profile.name, out var value) &&
value.Destination.Address.Equals(profile.address) &&
value.Destination.Port == hostInfo.oscPort)
{
Host?.Log($"Service with key \"{profile.name}\" at " +
$"{profile.address}:{hostInfo.oscPort} already registered, skipping");
return;
}

AddTrackingReceiver(profile.name, profile.address, hostInfo.oscPort);

Host?.Log($"Set up {profile.name} at {profile.address}:{hostInfo.oscPort}");
ServiceStatus = (int)OscStatusEnum.Success;
Expand All @@ -490,10 +507,10 @@ async Task<bool> ServiceSupportsTracking(OSCQueryServiceProfile profile)
}

// Does the actual construction of the OSC Client
void AddTrackingReceiver(IPAddress address, int port)
void AddTrackingReceiver(string key, IPAddress address, int port)
{
var receiver = new OscClientPlus(address.ToString(), port);
_receivers.Add(receiver);
_receivers[key] = receiver;
}
}

Expand Down

0 comments on commit dc9fc32

Please sign in to comment.