private void OnResultReceived(ResponseMessage message) { RequestMessage reqMsg; int msgId = message.DetermineMessageId().GetValueOrDefault(); if (sentMessages.TryGetValue(msgId, out reqMsg)) { sentMessages.Remove(msgId); reqMsg.Complete(message, null); } ResultReceived?.Invoke(this, new ResultReceivedEventArgs(message)); }
private void _ws_OnMessage(object sender, MessageEventArgs e) { var m = new Message().ToObject(e.Data); if (m == null) { var r = JsonConvert.DeserializeObject <Result>(e.Data); if (r?.Namespace == "result") { ResultReceived?.Invoke(this, r); } //else //DEBUG, checking for messages we don't know about //Console.WriteLine(e.Data); return; } if (m is Connect c) { if (c.Payload == RESPONSE_CODE_REQUIRED) { ConnectReceived.Invoke(this, RESPONSE_CODE_REQUIRED); } else if (Guid.TryParse(c.Payload, out Guid g)) { ConnectReceived.Invoke(this, c.Payload); } return; } MessageReceived?.Invoke(this, m); Type t = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x == m.GetType() && x.IsSubclassOf(typeof(Message))); if (t != null) { dynamic nm = Convert.ChangeType(m, t); var field = this.GetType().GetField($"{t.Name}Received", BindingFlags.Instance | BindingFlags.NonPublic); var em = field?.GetValue(this); em?.GetType().GetMethod("Invoke").Invoke(em, new[] { this, nm.Payload }); } else { OnError?.Invoke(this, $"Invalid Message Received: {e.Data}"); } }
public void OnResultReceived(List <ScannedResult> args) { ResultReceived?.Invoke(this, args); }
/// <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(); }
protected void InvokeResultReceived(NameValueItem nameValueItem) { ResultReceived?.Invoke(_updateProperty, nameValueItem); }