Ejemplo n.º 1
0
        private void ProcessFrame(object argument)
        {
            lock (this)
            {
                FrameWithTicks frame = (FrameWithTicks)argument;

                this.receivedBytes = this.receivedBytes + (long)frame.frame.Length;
                ++receivedFrames;

                // Verify the frame fits in a buffer.  If not, write it to the database directly.
                if (frame.frame.Length < Constants.BufferSize)
                {
                    // if there is no room in the current buffer
                    if (!currentBuffer.HasRoom(frame.frame.Length))
                    {
                        // With bufferAvailable we try to prevent writing the dropped frame event to the EventLog too often
                        //  Incidentally, that means we'll be writing once per every time we use up the buffer pool.  If we
                        //  have a sudden flood of data, that means it'll only get written once or twice, which helps perf.
                        bool bufferAvailable = false;

                        if (currentBuffer.Available)
                        {
                            currentBuffer.EnableWrite();
                            ++writtenBuffers;
                            bufferAvailable = true;
                        }

                        if (!IncrementBuffer())
                        {
                            // Discard the packet by doing nothing with it

                            if (bufferAvailable)
                            {
                                // Warn the user
                                eventLog.WriteEntry("Dropping a frame in StreamManager::OnFrameReceived due to no buffers being available.",
                                                    EventLogEntryType.Error, ArchiveServiceEventLog.ID.FrameDropped);
                            }

                            ++droppedFrames;
                            return;
                        }
                    }

                    currentBuffer.AddFrame(frame);
                }
                else
                {
                    Trace.WriteLine("Writing frame directly to disk.  Frame size: " + frame.frame.Length);

                    // Force pending data to be written to disk
                    if (currentBuffer.Available)
                    {
                        currentBuffer.EnableWrite();
                        ++writtenBuffers;
                    }

                    // Get our new buffer
                    IncrementBuffer();

                    // And write this frame directly to disk
                    BufferManager.DirectWrite(frame, this.streamID);
                }

                ++writtenFrames;
            }
        }