Ejemplo n.º 1
0
        /// <summary>
        /// Scan for web ports and their status async.
        /// </summary>
        /// <exception cref="ArgumentException">If min and max port are logically wrong.</exception>
        /// <exception cref="ArgumentOutOfRangeException">If min or max value is outside the port range. </exception>
        /// <exception cref="Exception">If scan of ports can't be started or any other reason.</exception>
        /// <param name="sSettings">Object containing all scan settings.</param>
        /// <param name="cToken">CancellationToken object.</param>
        /// <returns>A collection of type web port statuses.</returns>
        public async Task <IEnumerable <IPrintablePortStatus> > ScanAsync(IScanProperties sSettings, CancellationToken cToken)
        {
            var minPort = sSettings.GetMinPort();
            var maxPort = sSettings.GetMaxPort();

            if (maxPort < minPort)
            {
                throw new ArgumentException("Max port value is less than min port value.");
            }

            if (minPort < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minPort));
            }

            if (maxPort > 65535)
            {
                throw new ArgumentOutOfRangeException(nameof(maxPort));
            }

            var iPHostEntry = Dns.GetHostEntry(Dns.GetHostName());

            if (iPHostEntry.AddressList.Length < 1)
            {
                throw new Exception("Web port scan can't be started.");
            }

            var host = iPHostEntry.AddressList[0];

            var taskArray = Enumerable.Range(minPort, (maxPort - minPort) + 1)
                            .Select(x => Task.FromResult(GetPortStatus(host, x)))
                            .ToArray();

            var masterTask = Task.Run(() => Task.WhenAll(taskArray), cToken);

            var sResult = await masterTask;

            var status          = sSettings.GetSearchStatus();
            var targetStatusStr = status.ToString();

            if (masterTask.Status != TaskStatus.RanToCompletion)
            {
                throw new Exception("Failed to scan web ports.");
            }

            return(status != PortStatus.Any ? sResult.Where(x =>
                                                            x.GetStatusString() == targetStatusStr) : sResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Scans for serial ports and their status.
        /// </summary>
        /// <exception cref="ArgumentException">If min and max port are logically wrong.</exception>
        /// <exception cref="ArgumentOutOfRangeException">If min or max value is outside the port range. </exception>
        /// <exception cref="Exception">If scan of ports can't be started or any other reason.</exception>
        /// <param name="sSettings">Object containing all scan settings.</param>
        /// <param name="cToken">CancellationToken object.</param>
        /// <returns>A collection of type serial port statuses.</returns>
        public async Task <IEnumerable <IPrintablePortStatus> > ScanAsync(IScanProperties sSettings, CancellationToken cToken)
        {
            var minPort = sSettings.GetMinPort();
            var maxPort = sSettings.GetMaxPort();

            if (maxPort < minPort)
            {
                throw new ArgumentException("Max port value is less than min port value.");
            }

            if (minPort < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minPort));
            }

            if (maxPort > 65535)
            {
                throw new ArgumentOutOfRangeException(nameof(maxPort));
            }

            var serialPorts = SerialPort.GetPortNames()
                              .Where(x => int.Parse(x.Substring(3)) >= minPort &&
                                     int.Parse(x.Substring(3)) <= maxPort).ToList();

            if (serialPorts.Count < 1)
            {
                throw new Exception("No serial port found.");
            }

            var sResult = await Task.Run(() => GetPortStatus(serialPorts), cToken);

            var status       = sSettings.GetSearchStatus();
            var targetStatus = status.ToString();

            return(status != PortStatus.Any ? sResult.Where(x =>
                                                            x.GetStatusString() == targetStatus) : sResult);
        }