/// <summary> /// UDPs the writer. /// </summary> /// <param name="packet">The args.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public bool UDPPingWriter(UdpPingPacket packet) { // CDFMonitor.LogOutputHandler("DEBUG:UDP.UDPPingWriter:packet:enter"); Debug.Print("UDP.UDPPingWriter:packet:enter"); try { if (!WriterEnabled) { return(false); } IFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, packet); byte[] sendbuf = new byte[stream.Length + 1]; // set first byte to '1' to denote its an udp object sendbuf[0] = (byte)UdpTraceType.Unknown; stream.ToArray().CopyTo(sendbuf, 1); stream.Close(); _udpPingClient.Send(sendbuf, sendbuf.Length); return(true); } catch (Exception e) { Debug.Print("UDP.UDPWriter:packet: Exception:" + e.ToString()); return(false); } }
/// <summary> /// Sends the ping. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="ElapsedEventArgs" /> instance containing the event /// data.</param> public void SendPing(object sender, ElapsedEventArgs e) { TimeSpan currentProcessTimeSpan = Process.GetCurrentProcess().TotalProcessorTime - _lastProcessorTime; TimeSpan currentDuration = DateTime.Now - _lastGetStatTime; TimeSpan processProcessorTime = Process.GetCurrentProcess().TotalProcessorTime; if (_machineCpuCounter == null) { _machineCpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); } TimeSpan processDuration = DateTime.Now.Subtract(_cdfm.StartTime); UdpPingPacket packet = new UdpPingPacket() { UdpTraceType = Udp.UdpTraceType.Ping, ClientName = Environment.MachineName, UdpCounter = UdpCounter++, ClientPingTime = DateTime.Now.ToString("o"), ClientActivity = _cdfm.Config.Activity, TracesPerSecond = (_cdfm.ProcessedEvents / processDuration.TotalSeconds).ToString("F"), MatchedEvents = _cdfm.MatchedEvents, MissedMatchedEvents = _cdfm.MissedMatchedEvents, MaxQueueMissedEvents = CDFMonitor.Instance.Config.MissedControllerEvents, ParserQueue = _cdfm.RegexParserThread.QueueLength(), ProcessedEvents = _cdfm.ProcessedEvents, AvgProcessCpu = (((processProcessorTime.TotalMilliseconds / Environment.ProcessorCount) / processDuration.TotalMilliseconds) * 100).ToString("F"), CurrentProcessCpu = (((currentProcessTimeSpan.TotalMilliseconds / Environment.ProcessorCount) / currentDuration.TotalMilliseconds) * 100).ToString("F"), CurrentMachineCpu = _machineCpuCounter.NextValue(), // AvgMachineCpu = _machineCpuCounter.NextValue(), Duration = processDuration, FormattedTraceString = string.Empty }; UDPPingWriter(packet); _lastProcessorTime = Process.GetCurrentProcess().TotalProcessorTime; _lastGetStatTime = DateTime.Now; }
/// <summary> /// Receives the callback. /// </summary> /// <param name="ar">The ar.</param> public void ReceiveCallback(IAsyncResult ar) { try { UdpClient udpClient = (UdpClient)((UdpState)(ar.AsyncState)).listener; IPEndPoint ipEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).groupEP; UdpPingPacket packet; // using index 0 to determine if packet bytes are just a string (0) or UdpPingPacket // (1) // todo: keeping this way may be more efficient network packet size (smaller) but // need to verify List <byte> receiveBytes = new List <byte>(udpClient.EndReceive(ar, ref ipEndPoint)); UdpTraceType packetType = (UdpTraceType)receiveBytes[0]; receiveBytes.RemoveAt(0); _listenerCallback.Set(); if (packetType == UdpTraceType.TraceString) { // trace string and not UdpPingPacket so create one packet = new UdpPingPacket() { UdpTraceType = UdpTraceType.TraceString, FormattedTraceString = Encoding.ASCII.GetString(receiveBytes.ToArray()) }; } else { // not a tracestring so UdpPingPacket of some type IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(receiveBytes.ToArray()); packet = (UdpPingPacket)formatter.Deserialize(stream); stream.Close(); } // determine udp trace type switch (packet.UdpTraceType) { case UdpTraceType.PingReply: // to do: send response? case UdpTraceType.Ping: // update list in gui Config.Configuration.UdpClientsListViewItem item = new Config.Configuration.UdpClientsListViewItem(packet); CDFMonitorGui.Instance.Dispatcher.Invoke(DispatcherPriority.Background, new Action <Config.Configuration.UdpClientsListViewItem>((listItem) => { try { if (_cdfm.Config.UdpClientsListViewCollection.Any(i => i.ClientName == listItem.ClientName)) { int index = -1; Config.Configuration.UdpClientsListViewItem lvi = _cdfm.Config.UdpClientsListViewCollection.First(i => i.ClientName == listItem.ClientName); index = _cdfm.Config.UdpClientsListViewCollection.IndexOf(lvi); _cdfm.Config.UdpClientsListViewCollection.RemoveAt(index); _cdfm.Config.UdpClientsListViewCollection.Insert(index, listItem); _cdfm.Config.UdpClientsListViewCollection.Remove(lvi); } else { // new entry _cdfm.Config.UdpClientsListViewCollection.Add(listItem); } } catch (Exception ex) { CDFMonitor.LogOutputHandler("Udp.ReceiveCallback:exception:" + ex.ToString()); } }), item); packet.FormattedTraceString = string.Format("{0},{1},{2},ping: Activity:{3} TPS:{4} Matched Events:{5} Missed Events:{6} Max Queue Missed Events:{7} Parser Queue:{8} Processed Events:{9} Avg Cpu:{10} Current Cpu:{11} Total Cpu:{12} Duration:{13}", packet.ClientName, packet.UdpCounter, packet.ClientPingTime, packet.UdpTraceType, packet.TracesPerSecond, packet.MatchedEvents, packet.MissedMatchedEvents, packet.MaxQueueMissedEvents, packet.ParserQueue, packet.ProcessedEvents, packet.AvgProcessCpu, packet.CurrentMachineCpu, packet.CurrentProcessCpu, packet.Duration ); _cdfm.ServerRegexParserThread.Queue(packet.FormattedTraceString); break; case UdpTraceType.TraceEtl: case UdpTraceType.TraceString: _cdfm.ServerRegexParserThread.Queue(packet.FormattedTraceString); break; case UdpTraceType.Unknown: default: break; } } catch (ObjectDisposedException) { // usually gets exception when stopping server return; } catch (Exception e) { CDFMonitor.LogOutputHandler(string.Format("DEBUG:ReceiveCallback:exception:{0}", e.ToString())); } }