Skip to content

Commit 5e75d88

Browse files
committed
adds RemoveMatchedService(). fixes #41
1 parent 3002945 commit 5e75d88

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

vrc-oscquery-lib/Zeroconf/MeaModDiscovery.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public void RefreshServices()
5656

5757
public event Action<OSCQueryServiceProfile> OnOscServiceAdded;
5858
public event Action<OSCQueryServiceProfile> OnOscQueryServiceAdded;
59-
59+
public event Action<string> OnOscServiceRemoved;
60+
public event Action<string> OnOscQueryServiceRemoved;
61+
6062
private Dictionary<OSCQueryServiceProfile, ServiceProfile> _profiles = new Dictionary<OSCQueryServiceProfile, ServiceProfile>();
6163
public void Advertise(OSCQueryServiceProfile profile)
6264
{
@@ -95,13 +97,16 @@ private void OnRemoteServiceInfo(object sender, MessageEventArgs eventArgs)
9597
return;
9698
}
9799

98-
foreach (ResourceRecord answer in response.Answers.Where(a=>OSCQueryService.MatchedNames.Contains(a.CanonicalName)))
100+
if (response.Answers.Any(a => OSCQueryService.MatchedNames.Contains(a.CanonicalName)))
99101
{
100102
try
101103
{
102104
foreach (SRVRecord record in response.AdditionalRecords.OfType<SRVRecord>())
103105
{
104-
AddMatchedService(response, record);
106+
if (record.TTL == TimeSpan.Zero)
107+
RemoveMatchedService(record);
108+
else
109+
AddMatchedService(response, record);
105110
}
106111
}
107112
catch (Exception)
@@ -153,7 +158,25 @@ private void AddMatchedService(Message response, SRVRecord srvRecord)
153158
}
154159
}
155160
}
161+
162+
private void RemoveMatchedService(SRVRecord srvRecord)
163+
{
164+
var domainName = srvRecord.Name.Labels;
165+
var instanceName = domainName[0];
166+
var serviceName = string.Join(".", domainName.Skip(1));
167+
168+
// If this is an OSC service, remove it from the OSC collection
169+
if (string.Compare(serviceName, OSCQueryService._localOscUdpServiceName, StringComparison.Ordinal) == 0)
170+
{
171+
_oscServices.RemoveWhere(p => p.name == instanceName);
172+
OnOscServiceRemoved?.Invoke(instanceName);
173+
}
174+
// If this is an OSCQuery service, remove it from the OSCQuery collection
175+
else if (string.Compare(serviceName, OSCQueryService._localOscJsonServiceName, StringComparison.Ordinal) == 0)
176+
{
177+
_oscQueryServices.RemoveWhere(p => p.name == instanceName);
178+
OnOscQueryServiceRemoved?.Invoke(instanceName);
179+
}
180+
}
156181
}
157-
158-
159182
}

0 commit comments

Comments
 (0)