public async Task ScanAllAsync() { _scannedHosts = 0; List <HostData> result = new List <HostData>(); await Task.Run(() => { lock (_lockObject) { Parallel.ForEach(Hosts, _parallelOptions, host => { if (!_isAborted) { host.Exists = HostChecker.CheckHost(host.Host); } else { host.Exists = false; } result.Add(host); _scannedHosts++; OnOneHostWasScanned?.Invoke(HostsCount, _scannedHosts, host); }); } }); OnScanEnding?.Invoke(result.ToArray()); _isAborted = false; }
/// <summary>Scan all of ports in host.</summary> /// <exception cref="ArgumentNullException">Thrown when <see cref="Host"/> or <see cref="Ports"/> was equals null.</exception> public async Task ScanAllAsync() { if (Host == null) { throw new ArgumentNullException(nameof(Host), "Host value was equals null."); } if (Ports == null) { throw new ArgumentNullException(nameof(Ports), "Ports value was equals null."); } _scannedPortsCount = 0; try { await _checker.HostIsValidAsync(Host); Port[] results = (await IteratePortsAsync()).OrderBy(result => result.Value).ToArray(); OnScanEnding?.Invoke(new ScanResult(Host, DateTime.Now, results)); } catch (HostNotValidException) { OnScanEnding?.Invoke(new ScanResult(Host, DateTime.Now, new Port[] { }, false)); } _scannedPortsCount = 0; _aborted = false; }
/// <summary>Scan all of ports in host.</summary> /// <exception cref="ArgumentNullException">Throws when <see cref="Host"/> or <see cref="Ports"/> was equals null.</exception> /// <exception cref="RangeOfPortsException">Throws when <see cref="Ports"/> parameter not looks like a range.</exception> public async Task ScanAllAsync() { if (Host == null) { throw new ArgumentNullException(nameof(Host), "Host value was equals null."); } if (Ports == null) { throw new ArgumentNullException(nameof(Ports), "Ports value was equals null."); } if (!RangeOfPorts.CheckArrayOnRangeCorrectness(Ports)) { throw new RangeOfPortsException("Ports array are not correct."); } _scannedPortsCount = 0; try { await _checker.HostIsValidAsync(Host); List <Port> results = new List <Port>(); if (Ports.Length == 2 || Ports.Length == 4) { Port[] scannedPorts = await ScanRange(Ports[0], Ports[1]); results.AddRange(scannedPorts); } if (Ports.Length == 4) { Port[] scannedPorts = await ScanRange(Ports[2], Ports[3]); results.AddRange(scannedPorts); } Port[] sortedResults = results.OrderBy(result => result.Value).ToArray(); OnScanEnding?.Invoke(new ScanResult(Host, DateTime.Now, sortedResults)); } catch (HostNotValidException) { OnScanEnding?.Invoke(new ScanResult(Host, DateTime.Now, new Port[] { }, false)); } catch (Exception ex) { new Error(ex).HandleError(); } _scannedPortsCount = 0; _aborted = false; }