public async Task WriteFrameAsync(PcapFrame frame, bool flush) { Tuple <uint, uint> secondsMicroseconds = this.GetSecondsMicrosecondsTuple(frame.Timestamp); await this.writeLock.WaitAsync(); try { await outputStream.WriteAsync(ToByteArray(secondsMicroseconds.Item1), 0, 4); await outputStream.WriteAsync(ToByteArray(secondsMicroseconds.Item2), 0, 4); //number of octets of packet saved in file await outputStream.WriteAsync(ToByteArray((uint)frame.Data.Length), 0, 4); //actual length of packet await outputStream.WriteAsync(ToByteArray((uint)frame.Data.Length), 0, 4); //data await outputStream.WriteAsync(frame.Data, 0, frame.Data.Length); if (flush) { await outputStream.FlushAsync(); } } finally { this.writeLock.Release(); } FramesWritten++; }
public void WriteFrame(PcapFrame frame, bool flush) { Tuple <uint, uint> secondsMicroseconds = this.GetSecondsMicrosecondsTuple(frame.Timestamp); this.writeLock.Wait(); try { this.outputStream.Write(ToByteArray(secondsMicroseconds.Item1), 0, 4); this.outputStream.Write(ToByteArray(secondsMicroseconds.Item2), 0, 4); //number of octets of packet saved in file this.outputStream.Write(ToByteArray((uint)frame.Data.Length), 0, 4); //actual length of packet this.outputStream.Write(ToByteArray((uint)frame.Data.Length), 0, 4); //data this.outputStream.Write(frame.Data, 0, frame.Data.Length); if (flush) { this.outputStream.Flush(); } } finally { this.writeLock.Release(); } this.FramesWritten++; }
public Task WriteFrameAsync(PcapFrame frame) { return(WriteFrameAsync(frame, this.autoFlush)); }
public void WriteFrame(PcapFrame frame) { WriteFrame(frame, this.autoFlush); }
public PcapStreamReader(System.IO.Stream pcapStream, int packetQueueSize, StreamReadCompletedCallback streamReadCompletedCallback, bool startBackgroundWorkers, long streamMaxLength, int readTimeoutMilliseconds) { this.pcapStream = pcapStream; this.streamLength = streamMaxLength; this.readBytesEstimate = 0; this.readTimeoutMilliseconds = readTimeoutMilliseconds; this.packetQueueMaxSize = packetQueueSize; this.streamReadCompletedCallback = streamReadCompletedCallback; //TODO: Figure out if it is a libpcap or pcapNG stream... this.pcapParser = PcapParserFactory.CreatePcapParser(this);// new PcapParser(pcapStream, this.AbortReadingPcapStream); this.packetQueueBC = new System.Collections.Concurrent.BlockingCollection <PcapFrame>(this.packetQueueMaxSize); //this.packetQueue = new System.Collections.Concurrent.ConcurrentQueue<PcapFrame>(); //this.packetQueueHasRoomEvent = new System.Threading.AutoResetEvent(true); this.enqueuedByteCount = 0; this.dequeuedByteCount = 0; this.backgroundStreamReaderCanceller = new System.Threading.CancellationTokenSource(); System.Threading.CancellationToken cancellationToken = backgroundStreamReaderCanceller.Token; this.readAction = new Action(() => { DateTime firstFrameTimestamp = DateTime.MinValue; DateTime lastFrameTimestamp = DateTime.MinValue; int framesCount = 0; try { //int sleepMilliSecs = 20; while (!cancellationToken.IsCancellationRequested && !this.EndOfStream()) { PcapFrame packet = this.pcapParser.ReadPcapPacketBlocking(); //PcapFrame packet = await this.pcapParser.ReadPcapPacketAsync(cancellationToken); if (firstFrameTimestamp == DateTime.MinValue) { firstFrameTimestamp = packet.Timestamp; } lastFrameTimestamp = packet.Timestamp; framesCount++; this.enqueuedByteCount += packet.Data.Length; while (!this.packetQueueBC.TryAdd(packet, 1000, cancellationToken)) { if (cancellationToken.IsCancellationRequested || this.EndOfStream()) { break; } } //this.packetQueue.Enqueue(packet); } } catch (System.IO.EndOfStreamException) { //Do nothing, just stop reading this.pcapStream = null; } catch (System.IO.IOException) { //probably a socket timout if (!(this.pcapStream is System.IO.FileStream) && this.pcapStream != null) { if (this.pcapStream.CanWrite) { this.pcapStream.Flush(); } this.pcapStream.Dispose(); } //this.pcapStream = null; } catch (OperationCanceledException) { if (!(this.pcapStream is System.IO.FileStream) && this.pcapStream != null) { if (this.pcapStream.CanWrite) { this.pcapStream.Flush(); } this.pcapStream.Dispose(); } } #if !DEBUG catch (Exception ex) { this.pcapStream = null; //this.backgroundStreamReaderCanceller.Cancel(); //e.Cancel = true; //e.Result = ex.Message; this.AbortFileRead(); } #endif //do a callback with this.filename as well as first and last timestamp if (this.streamReadCompletedCallback != null && firstFrameTimestamp != DateTime.MinValue && lastFrameTimestamp != DateTime.MinValue) { this.streamReadCompletedCallback(framesCount, firstFrameTimestamp, lastFrameTimestamp); } }); if (startBackgroundWorkers) { this.StartBackgroundWorkers(); } }