示例#1
0
        private SequenceSegment GetSegment(int sizeHint)
        {
            Requires.Range(sizeHint >= 0, nameof(sizeHint));
            int?minBufferSize = null;

            if (sizeHint == 0)
            {
                if (this.last == null || this.last.WritableBytes == 0)
                {
                    // We're going to need more memory. Take whatever size the pool wants to give us.
                    minBufferSize = -1;
                }
            }
            else
            {
                sizeHint = Math.Max(this.MinimumSpanLength, sizeHint);
                if (this.last == null || this.last.WritableBytes < sizeHint)
                {
                    minBufferSize = sizeHint;
                }
            }

            if (minBufferSize.HasValue)
            {
                var segment = this.segmentPool.Count > 0 ? this.segmentPool.Pop() : new SequenceSegment();
                if (this.arrayPool != null)
                {
                    segment.Assign(this.arrayPool.Rent(minBufferSize.Value == -1 ? DefaultLengthFromArrayPool : minBufferSize.Value));
                }
                else
                {
                    segment.Assign(this.memoryPool !.Rent(minBufferSize.Value));
                }

                this.Append(segment);
            }

            return(this.last !);
        }
示例#2
0
            private void LocalContentExamined(long bytesExamined)
            {
                Requires.Range(bytesExamined >= 0, nameof(bytesExamined));
                if (bytesExamined == 0 || this.IsDisposed)
                {
                    return;
                }

                if (this.TraceSource !.Switch.ShouldTrace(TraceEventType.Verbose))
                {
                    this.TraceSource.TraceEvent(TraceEventType.Verbose, 0, "Acknowledging processing of {0} bytes.", bytesExamined);
                }

                this.MultiplexingStream.SendFrame(
                    new FrameHeader
                {
                    Code      = ControlCode.ContentProcessed,
                    ChannelId = this.QualifiedId,
                },
                    this.MultiplexingStream.formatter.SerializeContentProcessed(bytesExamined),
                    CancellationToken.None);
            }
示例#3
0
        /// <summary>
        /// Gets writable memory that can be initialized and added to the sequence via a subsequent call to <see cref="Advance(int)"/>.
        /// </summary>
        /// <param name="sizeHint">The size of the memory required, or 0 to just get a convenient (non-empty) buffer.</param>
        /// <returns>The requested memory.</returns>
        public Memory <T> GetMemory(int sizeHint)
        {
            Requires.Range(sizeHint >= 0, nameof(sizeHint));

            if (sizeHint == 0)
            {
                if (this.last?.WritableBytes > 0)
                {
                    sizeHint = this.last.WritableBytes;
                }
                else
                {
                    sizeHint = DefaultBufferSize;
                }
            }

            if (this.last == null || this.last.WritableBytes < sizeHint)
            {
                this.Append(this.memoryPool.Rent(sizeHint));
            }

            return(this.last.TrailingSlack);
        }
示例#4
0
 /// <summary>
 /// Advances the sequence to include the specified number of elements initialized into memory
 /// returned by a prior call to <see cref="GetMemory(int)"/>.
 /// </summary>
 /// <param name="count">The number of elements written into memory.</param>
 public void Advance(int count)
 {
     Requires.Range(count >= 0, nameof(count));
     this.last.End += count;
 }