private void OnPacketCaptured(IntPtr iptrParams, PcapPacketHeader pHeader, IntPtr pPacketData) { if (BytesCaptured != null) { DateTime dtCaptureDate = DateTime.Now; byte[] bBuffer = new byte[pHeader.PacketLength]; Marshal.Copy(pPacketData, bBuffer, 0, (int)pHeader.PacketLength); WinPcapCaptureHeader wpcHeader = new WinPcapCaptureHeader(dtCaptureDate, (int)pHeader.CaptureLength, (int)pHeader.PacketLength); if (BytesCaptured != null) { foreach (Delegate dDelgate in BytesCaptured.GetInvocationList()) { if (dDelgate.Target != null && dDelgate.Target is EthernetInterface) { ((EthernetInterface)dDelgate.Target).OnBytesCaptured(wpcHeader, bBuffer, this); } else if (dDelgate.Target != null && dDelgate.Target is System.ComponentModel.ISynchronizeInvoke && ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).InvokeRequired) { ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).BeginInvoke(dDelgate, new object[] { wpcHeader, bBuffer, this }); } else { ((ByteCapturedHandler)dDelgate)(wpcHeader, bBuffer, this); } } } } }
/// <summary> /// Rises the BytesCaptured event /// </summary> /// <param name="bBuffer">The bytes which were captured</param> protected void InvokeBytesCaptured(byte[] bBuffer) { if (BytesCaptured != null) { foreach (Delegate dDelgate in BytesCaptured.GetInvocationList()) { if (dDelgate.Target != null && dDelgate.Target is System.ComponentModel.ISynchronizeInvoke && ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).InvokeRequired) { ((System.ComponentModel.ISynchronizeInvoke)(dDelgate.Target)).BeginInvoke(dDelgate, new object[] { bBuffer, this }); } else { ((BytesCapturedHandler)dDelgate)(bBuffer, this); } } } }