/// <summary> /// Internal representation of the job logic. /// </summary> private async Task Run() { var runs = 0; var shouldRun = true; var overallWatch = new Stopwatch(); var portWatch = new Stopwatch(); overallWatch.Start(); while (!_tokenSource.IsCancellationRequested && shouldRun) { runs++; State = JobStateEnum.Running; JobDefinition.TargetPorts.ToList().ForEach( port => { try { var runResult = new JobSingleRunModel { Tcp = JobDefinition.Tcp, Udp = JobDefinition.Udp, Port = port }; portWatch.Restart(); runResult.PortReached = NetworkUtil.IsPortOpened(JobDefinition.TargetAddess, port, (int)JobDefinition.Timeout.TotalSeconds, JobDefinition.Udp); portWatch.Stop(); runResult.Duration = portWatch.Elapsed; if (JobDefinition.ResolveAddress) { // job should try to resolve ip address Dns.BeginGetHostEntry( JobDefinition.TargetAddess, ar => { IPHostEntry firstNetworkAddress = null; try { firstNetworkAddress = Dns.EndGetHostEntry(ar); } catch { // empty catch } if (firstNetworkAddress == null || !firstNetworkAddress.AddressList.Any()) { return; } runResult.ResolvedAddress = firstNetworkAddress.AddressList[0].ToString(); }, null); } DispatcherHelper.BeginInvoke(() => Result.Runs.Add(runResult)); ResultReceived?.Invoke(this, new JobResultEventArgs(runResult)); if (runResult.PortReached && JobDefinition.AutoStop) { // the port is reached and autostop is on shouldRun = false; return; } if (JobDefinition.MaxTries.HasValue && JobDefinition.MaxTries.Value <= runs) { // the maximum amount of tries is reached shouldRun = false; return; } if (JobDefinition.MaxRuntime.HasValue && JobDefinition.MaxRuntime.Value <= overallWatch.Elapsed) { // maximum runtime is reached shouldRun = false; return; } // inform callers that there is a new result // ReSharper disable once ExplicitCallerInfoArgument OnPropertyChanged(nameof(Result)); } catch (Exception ex) { // TODO } }); State = JobStateEnum.Waiting; await Task.Delay(JobDefinition.WaitTime, CancellationToken.None); } overallWatch.Stop(); }
public JobResultEventArgs(JobSingleRunModel result) { Result = result; }