Use a HashSet for the services

This commit is contained in:
Santiago Lo Coco 2024-11-03 16:23:29 +01:00
parent 916e250fa8
commit 84bc24f5cf
2 changed files with 25 additions and 4 deletions

View File

@ -19,7 +19,7 @@ public class EndpointLoader : MonoBehaviour
private const string defaultEndpoint2 = "http://windows.local:8200/mystream/"; private const string defaultEndpoint2 = "http://windows.local:8200/mystream/";
private bool defaultEndpoint1Loaded = false; private bool defaultEndpoint1Loaded = false;
private bool defaultEndpoint2Loaded = false; private bool defaultEndpoint2Loaded = false;
private List<MdnsService> availableServices = new List<MdnsService>(); private HashSet<MdnsService> availableServices = new HashSet<MdnsService>();
private void Start() private void Start()
{ {
@ -112,9 +112,11 @@ public class EndpointLoader : MonoBehaviour
triedMulticast = true; triedMulticast = true;
serviceDiscovery.StartListening((service) => serviceDiscovery.StartListening((service) =>
{ {
Debug.Log($"Received multicast message: {service.Host}"); bool wasAdded = availableServices.Add(service);
availableServices.Add(service); if (wasAdded)
AddServiceToTable(service); {
AddServiceToTable(service);
}
}); });
} }

View File

@ -20,4 +20,23 @@ public class MdnsService
{ {
return $"IpAddress: {IpAddress}, Port: {Port}, Path: {Path}, Host: {Host}"; return $"IpAddress: {IpAddress}, Port: {Port}, Path: {Path}, Host: {Host}";
} }
public override bool Equals(object obj)
{
return obj is MdnsService service &&
IpAddress == service.IpAddress &&
Host == service.Host &&
Port == service.Port &&
Path == service.Path;
}
public override int GetHashCode()
{
int hash = 17;
hash = hash * 31 + (IpAddress?.GetHashCode() ?? 0);
hash = hash * 31 + Port.GetHashCode();
hash = hash * 31 + (Path?.GetHashCode() ?? 0);
hash = hash * 31 + (Host?.GetHashCode() ?? 0);
return hash;
}
} }