Example #1
0
        /// <summary>Itearte all ports asynchronously.</summary>
        /// <returns>Result of iteration checking.</returns>
        private async Task <Port[]> IteratePortsAsync()
        {
            List <Port> result = new List <Port>();

            await Task.Run(() =>
            {
                lock (_lockObject)
                {
                    // Use all processors
                    Parallel.ForEach(Ports, _parallelOptions, port =>
                    {
                        try
                        {
                            _checker = new PortChecker();
                            _checker.InstallHost(Host);

                            if (!_aborted)
                            {
                                port.ChangeState(_checker.CheckPort(port));
                            }

                            result.Add(port);

                            _scannedPortsCount++;
                            OnOnePortWasScanned?.Invoke(PortsCount, _scannedPortsCount);
                        }
                        catch (ArgumentNullException) // If host not initializes in checker.
                        {
                            result = new List <Port>();
                            return;
                        }
                    });
                }
            });

            return(result.ToArray());
        }
Example #2
0
        /// <summary>Scan range of ports.</summary>
        /// <param name="from">First port.</param>
        /// <param name="to">Last port.</param>
        /// <returns>Array with scan results</returns>
        /// <exception cref="RangeOfPortsException">Throws when from and to parameters not looks like a range.</exception>
        private async Task <Port[]> ScanRange(Port from, Port to)
        {
            if (from.Value >= to.Value && from.Protocol == to.Protocol)
            {
                throw new RangeOfPortsException("Ports array are not correct.");
            }

            List <Port>  result   = new List <Port>();
            ProtocolType protocol = from.Protocol;

            await Task.Run(() =>
            {
                lock (_lockObject)
                {
                    Parallel.For(from.Value, to.Value + 1, _parallelOptions, currentPort =>
                    {
                        _checker = new PortChecker();
                        _checker.InstallHost(Host);

                        Port portToScan = new Port((ushort)currentPort, protocol);
                        if (!_aborted)
                        {
                            portToScan.ChangeState(_checker.CheckPort(portToScan));
                        }

                        result.Add(portToScan);

                        currentPort++;
                        _scannedPortsCount++;
                        OnOnePortWasScanned?.Invoke(PortsCount, _scannedPortsCount);
                    });
                }
            });

            return(result.ToArray());
        }