From 4d19930da620ee9cf5688d549e8bae96a4851dc4 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 3 Nov 2024 10:25:04 +0100 Subject: [PATCH] Support multiple services --- Assets/Scripts/ServiceDiscovery.cs | 95 ++++++++++++------------------ 1 file changed, 38 insertions(+), 57 deletions(-) diff --git a/Assets/Scripts/ServiceDiscovery.cs b/Assets/Scripts/ServiceDiscovery.cs index c5c231f..3483ece 100644 --- a/Assets/Scripts/ServiceDiscovery.cs +++ b/Assets/Scripts/ServiceDiscovery.cs @@ -4,6 +4,7 @@ using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Threading; +using System.Collections.Generic; using UnityEngine; public class ServiceDiscovery : MonoBehaviour @@ -13,17 +14,14 @@ public class ServiceDiscovery : MonoBehaviour private string receivedIp; private string receivedPort; + private string receivedPath; + private string receivedHost; private bool messageReceived = false; private const string multicastAddress = "224.0.0.251"; private const int multicastPort = 5353; - /* - private void Start() - { - StartListening((ip, port) => Debug.Log($"Service found at {ip}:{port}")); - } - */ + public List discoveredServices = new List(); public void StartListening(Action action) { @@ -53,17 +51,6 @@ public class ServiceDiscovery : MonoBehaviour byte[] query = CreateMdnsQuery(serviceName); Debug.Log($"Sending mDNS query for {serviceName}"); - /* - string hex = ""; - foreach (byte b in query) - { - //hex += b.ToString("X2"); - hex += $"{b:X2}"; - hex += " "; - } - Debug.Log($"Sending message: {hex}"); - */ - udpClient.Send(query, query.Length, new IPEndPoint(IPAddress.Parse(multicastAddress), multicastPort)); } @@ -133,13 +120,31 @@ public class ServiceDiscovery : MonoBehaviour if (receivedIp != null && receivedPort != null) { - messageReceived = true; - StopListening(); + // messageReceived = true; + // StopListening(); + MdnsService currentService = new MdnsService + { + IpAddress = receivedIp, + Port = int.Parse(receivedPort), + Path = receivedPath, + Host = receivedHost + }; + discoveredServices.Add(currentService); + Debug.Log($"Added service: {currentService.IpAddress}:{currentService.Port}, " + + $"{currentService.Path}, {currentService.Host}"); + receivedIp = null; + receivedPort = null; + receivedPath = null; + receivedHost = null; + // receivedIp = receivedHost = receivedPort = receivedPath = null; } + /* else { udpClient?.BeginReceive(OnReceive, null); } + */ + udpClient?.BeginReceive(OnReceive, null); } catch (Exception ex) { @@ -189,6 +194,7 @@ public class ServiceDiscovery : MonoBehaviour IPAddress ipAddress = new IPAddress(new ArraySegment(data, offset, dataLength).ToArray()); Debug.Log($"A Record: {name} -> {ipAddress}"); receivedIp = ipAddress.ToString(); + receivedHost = name; } else if (recordType == 12) // PTR Record { @@ -210,6 +216,11 @@ public class ServiceDiscovery : MonoBehaviour { string txtData = Encoding.UTF8.GetString(data, offset, dataLength); Debug.Log($"TXT Record: {name} -> {txtData}"); + if (txtData.Contains("path")) + { + receivedPath = txtData.Split('=')[1]; + Debug.Log($"Received path: {receivedPath}"); + } } else if (recordType == 47) // NSEC Record { @@ -264,44 +275,6 @@ public class ServiceDiscovery : MonoBehaviour return offset + 1; } - /* - private void OnReceive(IAsyncResult result) - { - if (udpClient == null) - { - return; - } - - try - { - IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, multicastPort); - byte[] receivedBytes = udpClient.EndReceive(result, ref remoteEndPoint); - string receivedMessage = Encoding.UTF8.GetString(receivedBytes); - - Debug.Log($"Received message: {receivedMessage} from {remoteEndPoint}"); - - string[] parts = receivedMessage.Split(':'); - if (parts.Length == 3 && IPAddress.TryParse(parts[1], out IPAddress ip)) - { - int port = int.Parse(parts[2]); - receivedIp = ip.ToString(); - receivedPort = port.ToString(); - messageReceived = true; - - StopListening(); - } - else - { - udpClient?.BeginReceive(OnReceive, null); - } - } - catch (Exception ex) - { - Debug.LogError($"Error receiving UDP message: {ex.Message}"); - } - } - */ - private void Update() { if (messageReceived) @@ -325,5 +298,13 @@ public class ServiceDiscovery : MonoBehaviour udpClient?.Close(); udpClient = null; } + + public class MdnsService + { + public string IpAddress { get; set; } + public int Port { get; set; } + public string Path { get; set; } + public string Host { get; set; } + } }