Пример #1
0
        /// <summary>
        /// Browse domains
        /// </summary>
        static async Task <List <string> > BrowseDomainsAsync(int period)
        {
            Console.WriteLine($"Browsing for domains for {period} ms...");
            var cts     = new CancellationTokenSource(period);
            var domains = new List <string>();

            try {
                using (var browser = await Dns.BrowseAsync(cts.Token)) {
                    cts.Token.ThrowIfCancellationRequested();
                    while (await browser.MoveNextAsync(cts.Token))
                    {
                        cts.Token.ThrowIfCancellationRequested();
                        Console.WriteLine($"{DateTime.Now}: Domain {browser.Current.Domain} on {browser.Current.Interface} " +
                                          (browser.Current.Removed ? "removed" : "found"));
                        if (browser.Current.Removed)
                        {
                            domains.Remove(browser.Current.Domain);
                        }
                        else
                        {
                            domains.Add(browser.Current.Domain);
                        }
                    }
                }
            }
            catch (OperationCanceledException) { }
            return(domains);
        }
Пример #2
0
        /// <summary>
        /// Browse service names or types
        /// </summary>
        static async Task <IEnumerable <DnsServiceEntry> > ResolveServiceAsync(DnsServiceRecord record,
                                                                               int period, bool fromCache)
        {
            Console.WriteLine($"Resolving {record} for {period} ms...");
            var entries = new HashSet <DnsServiceEntry>();
            var cts     = new CancellationTokenSource(period);

            try {
                using (var browser = await Dns.BrowseAsync(record, fromCache, cts.Token)) {
                    cts.Token.ThrowIfCancellationRequested();
                    while (await browser.MoveNextAsync(cts.Token))
                    {
                        cts.Token.ThrowIfCancellationRequested();
                        Console.WriteLine($"{DateTime.Now}: {browser.Current}");
                        entries.Add(browser.Current);
                    }
                }
            }
            catch (OperationCanceledException) { }
            return(entries);
        }
Пример #3
0
        /// <summary>
        /// Browse service names or types
        /// </summary>
        static async Task <IEnumerable <DnsServiceRecord> > BrowseServicesAsync(string type, string domain,
                                                                                int period, bool fromCache)
        {
            if (type != null)
            {
                Console.WriteLine($"Browsing for service names for type {type} in {domain ?? "local."} for {period} ms...");
            }
            else
            {
                Console.WriteLine($"Browsing for service types in {domain ?? "local."} for {period} ms...");
            }
            var records = new HashSet <DnsServiceRecord>();
            var cts     = new CancellationTokenSource(period);

            try {
                using (var browser = await Dns.BrowseAsync(type, domain, fromCache, cts.Token)) {
                    cts.Token.ThrowIfCancellationRequested();
                    while (await browser.MoveNextAsync(cts.Token))
                    {
                        cts.Token.ThrowIfCancellationRequested();
                        Console.WriteLine($"{DateTime.Now}:    {browser.Current} " +
                                          (browser.Current.Removed ? "removed" : "found"));
                        if (browser.Current.Removed)
                        {
                            records.Remove(browser.Current);
                        }
                        else
                        {
                            records.Add(browser.Current);
                        }
                    }
                }
            }
            catch (OperationCanceledException) { }
            return(records);
        }