Exemplo n.º 1
0
 public virtual void Reset()
 {
     this.writedLength = 0;
     this.readedLength = 0;
     base.RunningIndex = 0;
     this.ChildSegment = null;
 }
Exemplo n.º 2
0
        public void WriterAdvance()
        {
            if (awaitWriteSegment == null)
            {
                throw new NullReferenceException(nameof(awaitWriteSegment));
            }

            lock (this.sync)
            {
                var segment = this.awaitWriteSegment;
                this.awaitWriteSegment = null;

                if (this.headBufferSegment == null &&
                    this.tailBufferSegment == null)
                {
                    this.headBufferSegment = segment;
                    this.tailBufferSegment = segment;
                }
                else
                {
                    this.tailBufferSegment.SetNext(segment);
                    this.tailBufferSegment = segment;
                }
            }
        }
Exemplo n.º 3
0
        public virtual void Dispose()
        {
            this.length          = 0;
            this.writedLength    = 0;
            this.readedLength    = 0;
            this.availableMemory = default;

            base.Memory       = default;
            base.RunningIndex = 0;

            this.ChildSegment = null;
        }
Exemplo n.º 4
0
        public void SetNext(ASegment segment)
        {
            this.ChildSegment = segment;

            segment = this;

            while (segment.Next != null)
            {
                segment.ChildSegment.RunningIndex = segment.RunningIndex + segment.writedLength;

                segment = segment.ChildSegment;
            }
        }
Exemplo n.º 5
0
        public void Complete()
        {
            if (this.isDispose)
            {
                return;
            }

            this.isDispose = true;

            lock (this.sync)
            {
                if (this.awaitWriteSegment != null)
                {
                    this.awaitWriteSegment.Dispose();
#if DEBUG
                    Interlocked.Increment(ref d_count);
#endif
                }

                ASegment segment = this.headBufferSegment;
                while (segment != null)
                {
                    ASegment returnSegment = segment;
                    segment = segment.ChildSegment;

                    returnSegment.Dispose();
#if DEBUG
                    Interlocked.Increment(ref d_count);
#endif
                }
            }

            this.memoryPool = null;

            while (this.segmentStack.TryPop(out var item))
            {
                item.Dispose();
#if DEBUG
                Interlocked.Increment(ref d_count);
#endif
            }

            this.segmentStack.Clear();


#if DEBUG
            Logger.Info($"create buffer count:{this.c_count},    recycle buffer count:{this.d_count}");
#endif
        }
Exemplo n.º 6
0
        public MemorySegment GetSegment()
        {
            if (this.awaitWriteSegment != null)
            {
                return(this.awaitWriteSegment as MemorySegment);
            }

            MemorySegment segment;

            if (!this.segmentStack.TryPop(out segment))
            {
                segment = new MemorySegment(this.segmentStack);
                segment.SetMemoryBlock(this.memoryPool.Rent());

#if DEBUG
                Interlocked.Increment(ref c_count);
#endif
            }

            this.awaitWriteSegment = segment;
            return(segment);
        }
Exemplo n.º 7
0
        public void ReaderAdvance(int count)
        {
            lock (this.sync)
            {
                while (count > 0)
                {
                    if (this.headBufferSegment == null)
                    {
                        throw new ArgumentOutOfRangeException("The Segment is not enough");
                    }

                    var segment = this.headBufferSegment.ChildSegment;
                    this.headBufferSegment.Reset();
                    this.headBufferSegment = segment;
                    count--;

                    if (this.headBufferSegment == null)
                    {
                        this.headBufferSegment = null;
                        this.tailBufferSegment = null;
                    }
                }
            }
        }
Exemplo n.º 8
0
 internal static long GetLength(long startPosition, ASegment endSegment, int endIndex)
 {
     return((endSegment.RunningIndex + (uint)endIndex) - startPosition);
 }
Exemplo n.º 9
0
 internal static long GetLength(ASegment startSegment, int startIndex, ASegment endSegment, int endIndex)
 {
     return((endSegment.RunningIndex + (uint)endIndex) - (startSegment.RunningIndex + (uint)startIndex));
 }