Beispiel #1
0
        public ServiceBrowser(Zeroconf zc, string type,
                              List <Delegates.HandlerDelegate> handlers = null,
                              IListener listener = null)
        {
            if (handlers == null && listener == null)
            {
                throw new Exception("You need to specify at least one handler");
            }

            if (!type.EndsWith(Utilities.ServiceTypeName(type), StringComparison.CurrentCulture))
            {
                throw new BadTypeInNameException("");
            }

            this.t = new Thread(Run)
            {
                Name         = "zeroconf-ServiceBrowser_" + type,
                IsBackground = true
            };

            this.zc       = zc;
            this.type     = type;
            this.services = new Dictionary <string, DNSRecord>();
            this.nextTime = Utilities.CurrentTimeMilliseconds();
            this.delay    = Timing.Browser;

            this.done = false;

            if (handlers != null)
            {
                foreach (Delegates.HandlerDelegate h in handlers)
                {
                    this._handlersToCall += h;
                }
            }

            if (listener != null)
            {
                Delegates.HandlerDelegate OnChange = (sender, e) => {
                    Zeroconf _zc = sender as Zeroconf;
                    if (e.StateChange == ServiceStateChange.Added)
                    {
                        listener.AddService(_zc, e.Type, e.Name);
                    }
                    else if (e.StateChange == ServiceStateChange.Removed)
                    {
                        listener.RemoveService(_zc, e.Type, e.Name);
                    }
                    else
                    {
                        throw new NotImplementedException(e.StateChange.ToString());
                    }
                };

                this._handlersToCall += OnChange;
            }
            this.eventArgs = new Queue <Delegates.OnChangeEventArgs>();

            this.t.Start();
        }