From df0640e6ee41e8bc19c77a48eebb851e185f1e3d Mon Sep 17 00:00:00 2001 From: Cameron Elliott Date: Thu, 16 Jun 2016 17:16:29 -0700 Subject: [PATCH] Added PcapDevice.GetSequence for Linq, foreach, and other extension method usage --- SharpPcap/LibPcap/PcapDevice.cs | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/SharpPcap/LibPcap/PcapDevice.cs b/SharpPcap/LibPcap/PcapDevice.cs index 25314274..463fe484 100755 --- a/SharpPcap/LibPcap/PcapDevice.cs +++ b/SharpPcap/LibPcap/PcapDevice.cs @@ -19,6 +19,7 @@ You should have received a copy of the GNU Lesser General Public License */ using System; +using System.Collections.Generic; using System.Runtime.InteropServices; namespace SharpPcap.LibPcap @@ -593,5 +594,40 @@ public override string ToString () { return "interface: " + m_pcapIf.ToString() + "\n"; } + + + /// + /// IEnumerable helper allows for easy foreach usage, extension method and Linq usage + /// + /// + public static IEnumerable GetSequence(ICaptureDevice dev, bool maskExceptions = true) + { + try + { + dev.Open(); + + while (true) + { + RawCapture packet = null; + try + { + packet = dev.GetNextPacket(); + } + catch (PcapException pe) + { + if (!maskExceptions) + throw pe; + } + + if (packet == null) + break; + yield return packet; + } + } + finally + { + dev.Close(); + } + } } }