/// <summary> /// Returns frames from a sequence as long as a specified condition is true. /// </summary> /// <param name="reader">The reader to get elements from.</param> /// <param name="predicate">A function to test each source frame for a condition; /// the second parameter of the function represents the index of the source frame in the scope of current operation.</param> /// <returns>The enumeration that contains elements satisfying the predicate. /// The reader position is on the first frame after this sequence. It is thus /// possible to contiune with reading next frames.</returns> public IEnumerable <RawCapture> TakeWhile(ICaptureFileReader reader, Func <RawCapture, int, bool> predicate) { var index = 0; // read first frame if necessary... if (reader.State == ReadingState.NotStarted) { reader.MoveNext(); } if (reader.State == ReadingState.Finished) { yield break; } if (reader.State == ReadingState.Closed) { throw new InvalidOperationException("Cannot read from closed reader."); } do { if (predicate.Invoke(reader.Current, index)) { yield return(reader.Current); index++; } else { yield break; } } while (reader.MoveNext()); }
public static IObservable <RawCapture> CreateObservable(this ICaptureFileReader captureReader) { return(Observable.Create <RawCapture>((observer, cancellation) => Task.Factory.StartNew( () => { while (!cancellation.IsCancellationRequested && captureReader.GetNextFrame(out var rawFrame)) { observer.OnNext(rawFrame); } observer.OnCompleted(); captureReader.Dispose(); }))); }
/// <summary> /// Reads up to the specified <paramref name="count"/> of frames using the given <paramref name="reader"/> /// </summary> /// <param name="reader">The pcap reader.</param> /// <param name="count">Number of frames to read.</param> /// <returns>A collection of <see cref="RawFrame"/> objects.</returns> public IEnumerable <RawCapture> Take(ICaptureFileReader reader, int count) { if (reader.State == ReadingState.Finished) { yield break; } if (reader.State == ReadingState.Closed) { throw new InvalidOperationException("Cannot read from closed reader."); } for (int i = 0; i < count; i++) { if (reader.GetNextFrame(out var frame, true)) { yield return(frame); } else { yield break; } } }