Skip to content

Commit

Permalink
Added PcapDevice.GetSequence for Linq, foreach, and other extension m…
Browse files Browse the repository at this point in the history
…ethod usage
  • Loading branch information
Cameron Elliott committed Jun 17, 2016
1 parent 34ed6f6 commit df0640e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions SharpPcap/LibPcap/PcapDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -593,5 +594,40 @@ public override string ToString ()
{
return "interface: " + m_pcapIf.ToString() + "\n";
}


/// <summary>
/// IEnumerable helper allows for easy foreach usage, extension method and Linq usage
/// </summary>
/// <returns></returns>
public static IEnumerable<RawCapture> 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();
}
}
}
}

0 comments on commit df0640e

Please sign in to comment.