public void WriteFrame(PcapPacket frame, bool flush) { lock (this) { TimeSpan delta = frame.Timestamp.Subtract(this.referenceTime); //The smallest unit of time is the tick, which is equal to 100 nanoseconds. A tick can be negative or positive. long totalMicroseconds = delta.Ticks / 10; uint seconds = (uint)(totalMicroseconds / 1000000); uint microseconds = (uint)(totalMicroseconds % 1000000); byte[] headerData = ToByteArray(frame.Header); fileStream.Write(ToByteArray(seconds), 0, 4); fileStream.Write(ToByteArray(microseconds), 0, 4); //number of octets of packet saved in file fileStream.Write(ToByteArray((uint)frame.Data.Length + headerData.Length), 0, 4); //actual length of packet fileStream.Write(ToByteArray((uint)frame.Data.Length + headerData.Length), 0, 4); //data fileStream.Write(headerData, 0, headerData.Length); fileStream.Write(frame.Data, 0, frame.Data.Length); if (flush) { fileStream.Flush(); } } }
void backgroundFileReader_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { DateTime firstFrameTimestamp = DateTime.MinValue; DateTime lastFrameTimestamp = DateTime.MinValue; int framesCount = 0; try { while (!this.backgroundFileReader.CancellationPending && fileStream.Position + 1 < fileStream.Length) { if (this.packetQueue.Count < this.packetQueueSize) { PcapPacket packet = ReadPcapPacket(); if (firstFrameTimestamp == DateTime.MinValue) { firstFrameTimestamp = packet.Timestamp; } lastFrameTimestamp = packet.Timestamp; framesCount++; lock (this.packetQueue) { this.packetQueue.Enqueue(packet); } this.enqueuedByteCount += packet.Data.Length; } else { System.Threading.Thread.Sleep(20); } } } catch (Exception ex) { e.Cancel = true; e.Result = ex.Message; this.AbortFileRead(); } //do a callback with this.filename as well as first and last timestamp if (this.readCompletedCallback != null && firstFrameTimestamp != DateTime.MinValue && lastFrameTimestamp != DateTime.MinValue) { this.readCompletedCallback(this.filename, framesCount, firstFrameTimestamp, lastFrameTimestamp); } }
public void WriteFrame(PcapPacket frame) { WriteFrame(frame, false); }