/// <summary> /// Closes this adapter /// </summary> public virtual void Close() { if (PcapHandle == IntPtr.Zero) { return; } if (Started) { StopCapture(); } SafeNativeMethods.pcap_close(PcapHandle); PcapHandle = IntPtr.Zero; //Remove event handlers if (OnPacketArrival != null) { foreach (Pcap.PacketArrivalEvent pa in OnPacketArrival.GetInvocationList()) { OnPacketArrival -= pa; } } if (OnPcapStatistics != null) { foreach (Pcap.PcapStatisticsEvent pse in OnPcapStatistics.GetInvocationList()) { OnPcapStatistics -= pse; } } }
/// <summary> /// Notify the OnPacketArrival delegates about a newly captured packet /// </summary> /// <param name="p"> /// A <see cref="RawCapture"/> /// </param> override protected void SendPacketArrivalEvent(RawCapture p) { if (Mode == CaptureMode.Packets) { base.SendPacketArrivalEvent(p); } else if (Mode == CaptureMode.Statistics) { OnPcapStatistics?.Invoke(this, new StatisticsModeEventArgs(p, this)); } }
/// <summary> /// Close the device /// </summary> public override void Close() { if (OnPcapStatistics != null) { foreach (StatisticsModeEventHandler pse in OnPcapStatistics.GetInvocationList()) { OnPcapStatistics -= pse; } } // call the base method base.Close(); }
private void LiveDevice_OnPacketArrival(object sender, CaptureEventArgs e) { if (IsWindows) { ReceivedPackets += BitConverter.ToInt64(e.Packet.Data, 0); ReceivedBytes += BitConverter.ToInt64(e.Packet.Data, 8); } else { ReceivedPackets++; ReceivedBytes += e.Packet.PacketLength; } var args = new StatisticsEventArgs( this, e.Packet.Timeval, ReceivedPackets, ReceivedBytes ); OnPcapStatistics?.Invoke(this, args); }