Exemplo n.º 1
0
        public void WriteFrame(PcapFrame frame, bool flush)
        {
            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);

            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), 0, 4);
            //actual length of packet
            fileStream.Write(ToByteArray((uint)frame.Data.Length), 0, 4);
            //data
            fileStream.Write(frame.Data, 0, frame.Data.Length);
            if (flush)
            {
                fileStream.Flush();
            }
            framesWritten++;
        }
Exemplo n.º 2
0
 public void WriteFrame(PcapFrame frame)
 {
     WriteFrame(frame, false);
 }
Exemplo n.º 3
0
        void backgroundStreamReader_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            DateTime firstFrameTimestamp = DateTime.MinValue;
            DateTime lastFrameTimestamp  = DateTime.MinValue;
            int      framesCount         = 0;

            try {
                int sleepMilliSecs = 20;
                while (!this.backgroundStreamReader.CancellationPending && !this.EndOfStream())
                {
                    if (this.packetQueue.Count < this.packetQueueSize)
                    {
                        //PcapPacket packet=ReadPcapPacket();
                        //PcapPacket packet = ReadPcapPacketBlocking();
                        PcapFrame packet = this.pcapParser.ReadPcapPacketBlocking();
                        if (firstFrameTimestamp == DateTime.MinValue)
                        {
                            firstFrameTimestamp = packet.Timestamp;
                        }
                        lastFrameTimestamp = packet.Timestamp;
                        framesCount++;
                        lock (this.packetQueue) {
                            this.packetQueue.Enqueue(packet);
                        }
                        this.enqueuedByteCount += packet.Data.Length;
                        sleepMilliSecs          = 20;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(sleepMilliSecs);
                        if (sleepMilliSecs < 1000)
                        {
                            sleepMilliSecs += 10;
                        }
                    }
                }
            }
            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)
                {
                    this.pcapStream.Close();
                }
                //this.pcapStream = null;
            }

#if !DEBUG
            catch (Exception ex) {
                this.pcapStream = null;
                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);
            }
        }