Esempio n. 1
0
        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");
        }
Esempio n. 2
0
        bool Handle(TemplateTypeParameter p, ISemantic arg)
        {
            // if no argument given, try to handle default arguments
            if (arg == null)
            {
                if (p.Default == null)
                {
                    return(false);
                }
                else
                {
                    IStatement stmt = null;
                    ctxt.PushNewScope(DResolver.SearchBlockAt(ctxt.ScopedBlock.NodeRoot as IBlockNode, p.Default.Location, out stmt));
                    ctxt.ScopedStatement = stmt;
                    var  defaultTypeRes = TypeDeclarationResolver.Resolve(p.Default, ctxt);
                    bool b = false;
                    if (defaultTypeRes != null)
                    {
                        b = Set(p, defaultTypeRes.First());
                    }
                    ctxt.Pop();
                    return(b);
                }
            }

            // If no spezialization given, assign argument immediately
            if (p.Specialization == null)
            {
                return(Set(p, arg));
            }

            bool handleResult = HandleDecl(p, p.Specialization, arg);

            if (!handleResult)
            {
                return(false);
            }

            // Apply the entire argument to parameter p if there hasn't been no explicit association yet
            if (!TargetDictionary.ContainsKey(p.Name) || TargetDictionary[p.Name] == null)
            {
                TargetDictionary[p.Name] = new TemplateParameterSymbol(p, arg);
            }

            return(true);
        }
Esempio n. 3
0
 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]);
     }
 }