public void StartService() { if (ServiceStarted) { return; } ServiceStarted = true; MDNS.Start(); SD.ServiceInstanceDiscovered += (s, e) => MDNS.SendQuery(e.ServiceInstanceName, type: DnsType.SRV); MDNS.AnswerReceived += (s, e) => { var servers = e.Message.Answers.OfType <SRVRecord>(); foreach (var server in servers) { var target = server.Target.ToString(); var name = string.Join(".", server.Name.Labels.TakeLast(3)); lock (_dictLock) { if (TargetDictionary.ContainsKey(target)) { if (!TargetDictionary[target].Contains(name)) { TargetDictionary[target].Add(name); } } else { TargetDictionary.Add(target, new HashSet <string> { name }); } } Logger.LogInformation($"Receive service record: {name} = {target}"); MDNS.SendQuery(server.Target, type: DnsType.A); } var addresses = e.Message.Answers.OfType <AddressRecord>(); foreach (var address in addresses) { if (!DomainNameDictionary.TryAdd(address.Name.ToString(), address.Address)) { DomainNameDictionary[address.Name.ToString()] = address.Address; } Logger.LogInformation($"Receive address record: {address.Name} = {address.Address}"); if (TargetDictionary.ContainsKey(address.Name.ToString())) { TargetDictionary[address.Name.ToString()] .Where(t => EventDictionary.ContainsKey(t)) .Foreach(t => EventDictionary[t].Invoke(this, new IPAddressEventArgs(address.Address))); } } }; Logger.LogInformation("Service discovery started"); }
private static T GetDef <T>(Dictionary <int, T> TargetDictionary, string assetUrl, int ID) where T : ScriptableObject { if (TargetDictionary == null) { TargetDictionary = new Dictionary <int, T>(); } if (TargetDictionary.ContainsKey(ID)) { return(TargetDictionary[ID]); } else { var res = Resources.Load <T>(Path.Combine(assetUrl, ID.ToString())); if (res == null) { Debug.LogError("the res is null:" + assetUrl + " :" + ID); return(null); } TargetDictionary.Add(ID, GameObject.Instantiate <T>(res)); return(TargetDictionary[ID]); } }