Skip to content

Commit

Permalink
Merge pull request #54 from joreg/feature/RemoveUnadvertisedServices
Browse files Browse the repository at this point in the history
  • Loading branch information
momo-the-monster authored Feb 26, 2024
2 parents 3002945 + 5e75d88 commit 139a021
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions vrc-oscquery-lib/Zeroconf/MeaModDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public void RefreshServices()

public event Action<OSCQueryServiceProfile> OnOscServiceAdded;
public event Action<OSCQueryServiceProfile> OnOscQueryServiceAdded;

public event Action<string> OnOscServiceRemoved;
public event Action<string> OnOscQueryServiceRemoved;

private Dictionary<OSCQueryServiceProfile, ServiceProfile> _profiles = new Dictionary<OSCQueryServiceProfile, ServiceProfile>();
public void Advertise(OSCQueryServiceProfile profile)
{
Expand Down Expand Up @@ -95,13 +97,16 @@ private void OnRemoteServiceInfo(object sender, MessageEventArgs eventArgs)
return;
}

foreach (ResourceRecord answer in response.Answers.Where(a=>OSCQueryService.MatchedNames.Contains(a.CanonicalName)))
if (response.Answers.Any(a => OSCQueryService.MatchedNames.Contains(a.CanonicalName)))
{
try
{
foreach (SRVRecord record in response.AdditionalRecords.OfType<SRVRecord>())
{
AddMatchedService(response, record);
if (record.TTL == TimeSpan.Zero)
RemoveMatchedService(record);
else
AddMatchedService(response, record);
}
}
catch (Exception)
Expand Down Expand Up @@ -153,7 +158,25 @@ private void AddMatchedService(Message response, SRVRecord srvRecord)
}
}
}

private void RemoveMatchedService(SRVRecord srvRecord)
{
var domainName = srvRecord.Name.Labels;
var instanceName = domainName[0];
var serviceName = string.Join(".", domainName.Skip(1));

// If this is an OSC service, remove it from the OSC collection
if (string.Compare(serviceName, OSCQueryService._localOscUdpServiceName, StringComparison.Ordinal) == 0)
{
_oscServices.RemoveWhere(p => p.name == instanceName);
OnOscServiceRemoved?.Invoke(instanceName);
}
// If this is an OSCQuery service, remove it from the OSCQuery collection
else if (string.Compare(serviceName, OSCQueryService._localOscJsonServiceName, StringComparison.Ordinal) == 0)
{
_oscQueryServices.RemoveWhere(p => p.name == instanceName);
OnOscQueryServiceRemoved?.Invoke(instanceName);
}
}
}


}

0 comments on commit 139a021

Please sign in to comment.