Bend.LogSegmentsHandler.flushPending C# (CSharp) Method

flushPending() private method

private flushPending ( ) : long
return long
        internal long flushPending()
        {
            byte[] cmds;
            long curLWSN;
            BinaryWriter newChunkBuffer = new BinaryWriter(new MemoryStream());
            bool shouldCleanupCheckpoint;

            lock (this) {
                shouldCleanupCheckpoint = checkpointReady;
                checkpointReady = false;
                // grab the current chunkbuffer
                cmds = ((MemoryStream)(nextChunkBuffer.BaseStream)).ToArray();
                curLWSN = _logWaitSequenceNumber;

                // make a clean chunkbuffer
                this._logWaitSequenceNumber++; // increment the wait sequence number
                nextChunkBuffer = newChunkBuffer;
            }

            // construct the raw log packet
            if (cmds.Length != 0) {
                UInt16 checksum = Util.Crc16.Instance.ComputeChecksum(cmds);

                MemoryStream ms = new MemoryStream();
                BinaryWriter logbr = new BinaryWriter(ms);

                LogPacketHeader hdr;
                hdr.magic = LogPacketHeader.LOG_MAGIC;
                hdr.curLWSN = curLWSN;
                hdr.cmddata_length = (uint)cmds.Length;
                hdr.checksum = checksum;
                Util.writeStruct<LogPacketHeader>(hdr, logbr);
                logbr.Write(cmds);
                logbr.Flush();

                // hand the log packet to the log segment handler
                addLogPacket(ms.ToArray());
            }

            if (shouldCleanupCheckpoint) {
                lock (this) {
                    foreach (RootBlockLogSegment seg in checkpoint_log_segments) {
                        active_log_segments.Remove(seg);
                        empty_log_segments.Add(seg);
                    }
                    checkpoint_log_segments = null;
                }
            }

            return curLWSN;
        }