Пример #1
0
        /// <summary>
        /// Initializes the <see cref="Pipe"/> with the specified <see cref="PipeOptions"/>.
        /// </summary>
        public Pipe(PipeOptions options)
        {
            if (options == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.options);
            }

            _bufferSegmentPool = new BufferSegment[SegmentPoolSize];

            _operationState   = default;
            _readerCompletion = default;
            _writerCompletion = default;

            _pool = options.Pool;
            _minimumSegmentSize        = options.MinimumSegmentSize;
            _pauseWriterThreshold      = options.PauseWriterThreshold;
            _resumeWriterThreshold     = options.ResumeWriterThreshold;
            _readerScheduler           = options.ReaderScheduler;
            _writerScheduler           = options.WriterScheduler;
            _useSynchronizationContext = options.UseSynchronizationContext;
            _readerAwaitable           = new PipeAwaitable(completed: false, _useSynchronizationContext);
            _writerAwaitable           = new PipeAwaitable(completed: true, _useSynchronizationContext);
            _reader = new DefaultPipeReader(this);
            _writer = new DefaultPipeWriter(this);
        }
Пример #2
0
        /// <summary>
        /// Initializes the <see cref="Pipe"/> with the specified <see cref="PipeOptions"/>.
        /// </summary>
        public Pipe(PipeOptions options)
        {
            if (options == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.options);
            }

            _bufferSegmentPool = new BufferSegmentStack(InitialSegmentPoolSize);

            _operationState   = default;
            _readerCompletion = default;
            _writerCompletion = default;

            // If we're using the default pool then mark it as null since we're just going to use the
            // array pool under the covers
            _pool = options.Pool == MemoryPool <byte> .Shared ? null : options.Pool;
            _maxPooledBufferSize       = _pool?.MaxBufferSize ?? 0;
            _minimumSegmentSize        = options.MinimumSegmentSize;
            _pauseWriterThreshold      = options.PauseWriterThreshold;
            _resumeWriterThreshold     = options.ResumeWriterThreshold;
            _readerScheduler           = options.ReaderScheduler;
            _writerScheduler           = options.WriterScheduler;
            _useSynchronizationContext = options.UseSynchronizationContext;
            _readerAwaitable           = new PipeAwaitable(completed: false, _useSynchronizationContext);
            _writerAwaitable           = new PipeAwaitable(completed: true, _useSynchronizationContext);
            _reader = new DefaultPipeReader(this);
            _writer = new DefaultPipeWriter(this);
        }
Пример #3
0
        /// <summary>
        /// Initializes the <see cref="ResetablePipe"/> with the specifed <see cref="IBufferPool"/>.
        /// </summary>
        /// <param name="pool"></param>
        /// <param name="options"></param>
        public ResetablePipe(PipeOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.MaximumSizeLow < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options.MaximumSizeLow));
            }

            if (options.MaximumSizeHigh < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options.MaximumSizeHigh));
            }

            if (options.MaximumSizeLow > options.MaximumSizeHigh)
            {
                throw new ArgumentException(nameof(options.MaximumSizeHigh) + " should be greater or equal to " + nameof(options.MaximumSizeLow), nameof(options.MaximumSizeHigh));
            }

            _bufferSegmentPool = new BufferSegment[SegmentPoolSize];

            _pool = options.Pool;
            _minimumSegmentSize = options.MinimumSegmentSize;
            _maximumSizeHigh    = options.MaximumSizeHigh;
            _maximumSizeLow     = options.MaximumSizeLow;
            _readerScheduler    = options.ReaderScheduler ?? Scheduler.Inline;
            _writerScheduler    = options.WriterScheduler ?? Scheduler.Inline;
            _readerAwaitable    = new PipeAwaitable(completed: false);
            _writerAwaitable    = new PipeAwaitable(completed: true);
            Reader = new ResetablePipeReader(this);
            Writer = new ResetablePipeWriter(this);
        }
Пример #4
0
        /// <summary>
        /// Initializes the <see cref="Pipe"/> with the specifed <see cref="IBufferPool"/>.
        /// </summary>
        /// <param name="pool"></param>
        /// <param name="options"></param>
        public Pipe(IBufferPool pool, PipeOptions options = null)
        {
            if (pool == null)
            {
                throw new ArgumentNullException(nameof(pool));
            }

            options = options ?? new PipeOptions();

            if (options.MaximumSizeLow < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options.MaximumSizeLow));
            }

            if (options.MaximumSizeHigh < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(options.MaximumSizeHigh));
            }

            if (options.MaximumSizeLow > options.MaximumSizeHigh)
            {
                throw new ArgumentException(nameof(options.MaximumSizeHigh) + " should be greater or equal to " + nameof(options.MaximumSizeLow), nameof(options.MaximumSizeHigh));
            }

            _pool            = pool;
            _maximumSizeHigh = options.MaximumSizeHigh;
            _maximumSizeLow  = options.MaximumSizeLow;
            _readerScheduler = options.ReaderScheduler ?? InlineScheduler.Default;
            _writerScheduler = options.WriterScheduler ?? InlineScheduler.Default;
            _readerAwaitable = new PipeAwaitable(completed: false);
            _writerAwaitable = new PipeAwaitable(completed: true);
        }
Пример #5
0
 private void ResetState()
 {
     _readerCompletion.Reset();
     _writerCompletion.Reset();
     _readerAwaitable    = new PipeAwaitable(completed: false, _useSynchronizationContext);
     _writerAwaitable    = new PipeAwaitable(completed: true, _useSynchronizationContext);
     _commitHeadIndex    = 0;
     _currentWriteLength = 0;
     _length             = 0;
 }
Пример #6
0
 private void ResetState()
 {
     _readerAwaitable    = new PipeAwaitable(completed: false);
     _writerAwaitable    = new PipeAwaitable(completed: true);
     _readerCompletion   = default;
     _writerCompletion   = default;
     _commitHeadIndex    = 0;
     _currentWriteLength = 0;
     _length             = 0;
 }
Пример #7
0
 private void ResetState()
 {
     _readerCompletion.Reset();
     _writerCompletion.Reset();
     _readerAwaitable   = new PipeAwaitable(completed: false, _useSynchronizationContext);
     _writerAwaitable   = new PipeAwaitable(completed: true, _useSynchronizationContext);
     _readTailIndex     = 0;
     _readHeadIndex     = 0;
     _lastExaminedIndex = -1;
     _unflushedBytes    = 0;
     _unconsumedBytes   = 0;
 }
Пример #8
0
        private static void GetResult(ref PipeAwaitable awaitableState,
                                      ref PipeCompletion completion,
                                      out bool isCancelled,
                                      out bool isCompleted)
        {
            if (!awaitableState.IsCompleted)
            {
                ThrowHelper.ThrowInvalidOperationException(ExceptionResource.GetResultNotCompleted);
            }

            // Change the state from to be cancelled -> observed
            isCancelled = awaitableState.ObserveCancelation();
            isCompleted = completion.IsCompleted;
            completion.ThrowIfFailed();
        }
Пример #9
0
        /// <summary>Initializes a new instance of the <see cref="System.IO.Pipelines.Pipe" /> class with the specified options.</summary>
        /// <param name="options">The set of options for this pipe.</param>
        public Pipe(PipeOptions options)
        {
            if (options == null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.options);
            }

            _bufferSegmentPool = new BufferSegmentStack(options.InitialSegmentPoolSize);

            _operationState   = default;
            _readerCompletion = default;
            _writerCompletion = default;

            _options         = options;
            _readerAwaitable = new PipeAwaitable(completed: false, UseSynchronizationContext);
            _writerAwaitable = new PipeAwaitable(completed: true, UseSynchronizationContext);
            _reader          = new DefaultPipeReader(this);
            _writer          = new DefaultPipeWriter(this);
        }