From 2b80596cba893643410478758a933aa1fe837a0c Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 3 Nov 2024 16:16:29 +0100 Subject: [PATCH] Fix concurrency bugs and optimize code --- Assets/Scripts/ServiceDiscovery.cs | 65 ++++++++++++++++-------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/Assets/Scripts/ServiceDiscovery.cs b/Assets/Scripts/ServiceDiscovery.cs index e529788..1beb03b 100644 --- a/Assets/Scripts/ServiceDiscovery.cs +++ b/Assets/Scripts/ServiceDiscovery.cs @@ -20,7 +20,8 @@ public class ServiceDiscovery : MonoBehaviour private const string multicastAddress = "224.0.0.251"; private const int multicastPort = 5353; - public List discoveredServices = new List(); + // public List discoveredServices = new List(); + private Queue serviceQueue = new Queue(); public void StartListening(Action action) { @@ -117,29 +118,6 @@ public class ServiceDiscovery : MonoBehaviour // Debug.Log($"Received message: {receivedBytes} from {remoteEndPoint}"); ParseMdnsResponse(receivedBytes); - if (receivedIp != null && receivedPort != null) - { - // StopListening(); - MdnsService currentService = new MdnsService(receivedIp, int.Parse(receivedPort), receivedPath, receivedHost); - /* - MdnsService currentService = new MdnsService - { - IpAddress = receivedIp, - Port = int.Parse(receivedPort), - Path = receivedPath, - Host = receivedHost - }; - */ - discoveredServices.Add(currentService); - Debug.Log($"Added service: http://{currentService.Host}:{currentService.Port}{currentService.Path}"); - receivedIp = null; - receivedPort = null; - receivedPath = null; - receivedHost = null; - - // action?.Invoke(currentService); - } - udpClient?.BeginReceive(OnReceive, null); } catch (Exception ex) @@ -148,6 +126,20 @@ public class ServiceDiscovery : MonoBehaviour } } + private void AddMdnsService() + { + if (receivedIp != null && receivedPort != null && receivedHost != null && receivedPath != null) + { + MdnsService currentService = new MdnsService(receivedIp, int.Parse(receivedPort), receivedPath, receivedHost); + serviceQueue.Enqueue(currentService); + Debug.Log($"Added service: {currentService}"); + receivedIp = null; + receivedPort = null; + receivedPath = null; + receivedHost = null; + } + } + private void ParseMdnsResponse(byte[] data) { int offset = 12; @@ -171,6 +163,7 @@ public class ServiceDiscovery : MonoBehaviour for (int i = 0; i < additionalRRs; i++) { offset = ParseRecord(data, offset); + AddMdnsService(); } } @@ -188,7 +181,7 @@ public class ServiceDiscovery : MonoBehaviour if (recordType == 1) // A Record { IPAddress ipAddress = new IPAddress(new ArraySegment(data, offset, dataLength).ToArray()); - Debug.Log($"A Record: {name} -> {ipAddress}"); + // Debug.Log($"A Record: {name} -> {ipAddress}"); receivedIp = ipAddress.ToString(); receivedHost = name; } @@ -196,7 +189,7 @@ public class ServiceDiscovery : MonoBehaviour { string target; (target, _) = ReadName(data, offset); - Debug.Log($"PTR Record: {name} -> {target}"); + // Debug.Log($"PTR Record: {name} -> {target}"); } else if (recordType == 33) // SRV Record { @@ -205,13 +198,13 @@ public class ServiceDiscovery : MonoBehaviour ushort port = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, offset + 4)); string target; (target, _) = ReadName(data, offset + 6); - Debug.Log($"SRV Record: {name} -> {target}:{port} (priority: {priority}, weight: {weight})"); + // Debug.Log($"SRV Record: {name} -> {target}:{port} (priority: {priority}, weight: {weight})"); receivedPort = port.ToString(); } else if (recordType == 16) // TXT Record { string txtData = Encoding.UTF8.GetString(data, offset, dataLength); - Debug.Log($"TXT Record: {name} -> {txtData}"); + // Debug.Log($"TXT Record: {name} -> {txtData}"); if (txtData.Contains("path")) { receivedPath = txtData.Split('=')[1]; @@ -220,7 +213,7 @@ public class ServiceDiscovery : MonoBehaviour } else if (recordType == 47) // NSEC Record { - Debug.Log($"NSEC Record: {name}"); + // Debug.Log($"NSEC Record: {name}"); } else { @@ -273,7 +266,7 @@ public class ServiceDiscovery : MonoBehaviour private void Update() { - if (discoveredServices.Count > 0) +/* if (discoveredServices.Count > 0) { foreach (MdnsService service in discoveredServices) { @@ -282,6 +275,18 @@ public class ServiceDiscovery : MonoBehaviour } discoveredServices.Clear(); } +*/ + if (serviceQueue.Count > 0) + { + Debug.Log($"Processing discovered services... Left: {serviceQueue.Count}"); + + while (serviceQueue.Count > 0) + { + MdnsService service = serviceQueue.Dequeue(); + Debug.Log($"Invoking action with: {service}"); + action?.Invoke(service); + } + } } private void OnDestroy()