示例#1
0
        public static void ScanNetwork(CancellationToken userToken, DnsResolverBase resolver, IPAddressRange addresses, INetworkServiceCore[] networkServices, IProgress <ScanNetworkBatch> progress)
        {
            if (resolver == null)
            {
                throw new ArgumentNullException("resolver");
            }
            if (addresses == null)
            {
                throw new ArgumentNullException("addresses");
            }
            if (networkServices == null)
            {
                throw new ArgumentNullException("networkServices");
            }
            if (progress == null)
            {
                throw new ArgumentNullException("progress");
            }

            var nestedTasks = new List <Task>();
            var chunkSize   = GetChunkSize(addresses);

            foreach (var addressChunk in addresses.Chunk(chunkSize))
            {
                userToken.ThrowIfCancellationRequested();

                var nestedTask = Task.Factory.StartNew(() =>
                {
                    foreach (var address in addressChunk)
                    {
                        var results = new List <ScanNetworkResult>();
                        foreach (var networkService in networkServices)
                        {
                            userToken.ThrowIfCancellationRequested();

                            var endpoint = new IPEndpoint(address, networkService.Port);
                            var result   = ScanEndpoint(userToken, resolver, networkService.Protocol, endpoint);
                            results.Add(result);
                        }
                        progress.Report(new ScanNetworkBatch(results.ToArray()));
                    }
                });
                nestedTasks.Add(nestedTask);
            }
            Task.WaitAll(nestedTasks.ToArray(), userToken);
        }
示例#2
0
        public static void ScanNetwork(CancellationToken userToken, DnsResolverBase resolver, IPAddressRange addresses, INetworkServiceCore[] networkServices, IProgress<ScanNetworkBatch> progress)
        {
            if (resolver == null)
            {
                throw new ArgumentNullException("resolver");
            }
            if (addresses == null)
            {
                throw new ArgumentNullException("addresses");
            }
            if (networkServices == null)
            {
                throw new ArgumentNullException("networkServices");
            }
            if (progress == null)
            {
                throw new ArgumentNullException("progress");
            }

            var nestedTasks = new List<Task>();
            var chunkSize = GetChunkSize(addresses);
            foreach (var addressChunk in addresses.Chunk(chunkSize))
            {
                userToken.ThrowIfCancellationRequested();

                var nestedTask = Task.Factory.StartNew(() =>
                {
                    foreach (var address in addressChunk)
                    {
                        var results = new List<ScanNetworkResult>();
                        foreach (var networkService in networkServices)
                        {
                            userToken.ThrowIfCancellationRequested();

                            var endpoint = new IPEndpoint(address, networkService.Port);
                            var result = ScanEndpoint(userToken, resolver, networkService.Protocol, endpoint);
                            results.Add(result);
                        }
                        progress.Report(new ScanNetworkBatch(results.ToArray()));
                    }
                });
                nestedTasks.Add(nestedTask);
            }
            Task.WaitAll(nestedTasks.ToArray(), userToken);
        }