Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}


}