From bc1a60704b2cb5fb1effebe11fcefee82fcaa856 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 29 Jan 2018 14:35:22 -0800 Subject: [PATCH] React to pipeline changes (#570) --- build/dependencies.props | 16 +++++----- .../Server/IISHttpContext.cs | 30 +++++++++---------- .../Server/OutputProducer.cs | 27 +++++------------ 3 files changed, 31 insertions(+), 42 deletions(-) diff --git a/build/dependencies.props b/build/dependencies.props index 78b9ac5b95..861df10b37 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -28,18 +28,18 @@ 1.1.0 2.1.0-preview1-28153 2.0.0 - 2.1.0-preview1-26115-03 + 2.1.0-preview1-26126-02 2.1.0-preview1-28153 15.3.0 7.0.0 - 4.5.0-preview1-26112-01 - 0.1.0-e180104-2 + 4.5.0-preview1-26126-05 + 0.1.0-preview1-180129-1 6.1.7601.17515 - 4.5.0-preview1-26112-01 - 4.5.0-preview1-26112-01 - 4.5.0-preview1-26112-01 - 4.5.0-preview1-26112-01 - 0.1.0-e180104-2 + 4.5.0-preview1-26126-05 + 4.5.0-preview1-26126-05 + 4.5.0-preview1-26126-05 + 4.5.0-preview1-26126-05 + 0.1.0-preview1-180129-1 2.3.1 2.3.1 diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs index 5536833474..6690bc21a0 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/IISHttpContext.cs @@ -141,8 +141,8 @@ namespace Microsoft.AspNetCore.Server.IISIntegration RequestBody = new IISHttpRequestBody(this); ResponseBody = new IISHttpResponseBody(this); - Input = new Pipe(new PipeOptions(_memoryPool, readerScheduler: Scheduler.TaskRun)); - var pipe = new Pipe(new PipeOptions(_memoryPool, readerScheduler: Scheduler.TaskRun)); + Input = new Pipe(new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.ThreadPool)); + var pipe = new Pipe(new PipeOptions(_memoryPool, readerScheduler: PipeScheduler.ThreadPool)); Output = new OutputProducer(pipe); } @@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration internal WindowsPrincipal WindowsUser { get; set; } public Stream RequestBody { get; set; } public Stream ResponseBody { get; set; } - public IPipe Input { get; set; } + public Pipe Input { get; set; } public OutputProducer Output { get; set; } public IHeaderDictionary RequestHeaders { get; set; } @@ -253,7 +253,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration } finally { - Input.Reader.Advance(readableBuffer.End, readableBuffer.End); + Input.Reader.AdvanceTo(readableBuffer.End, readableBuffer.End); } } } @@ -438,22 +438,22 @@ namespace Microsoft.AspNetCore.Server.IISIntegration while (true) { // These buffers are pinned - var wb = Input.Writer.Alloc(MinAllocBufferSize); - _inputHandle = wb.Buffer.Retain(true); + var wb = Input.Writer.GetMemory(MinAllocBufferSize); + _inputHandle = wb.Retain(true); try { int read = 0; if (_wasUpgraded) { - read = await ReadWebSocketsAsync(wb.Buffer.Length); + read = await ReadWebSocketsAsync(wb.Length); } else { _currentOperation = _currentOperation.ContinueWith(async (t) => { _currentOperationType = CurrentOperationType.Read; - read = await ReadAsync(wb.Buffer.Length); + read = await ReadAsync(wb.Length); }).Unwrap(); await _currentOperation; } @@ -463,15 +463,15 @@ namespace Microsoft.AspNetCore.Server.IISIntegration break; } - wb.Advance(read); + Input.Writer.Advance(read); } finally { - wb.Commit(); + Input.Writer.Commit(); _inputHandle.Dispose(); } - var result = await wb.FlushAsync(); + var result = await Input.Writer.FlushAsync(); if (result.IsCompleted || result.IsCancelled) { @@ -555,19 +555,19 @@ namespace Microsoft.AspNetCore.Server.IISIntegration } finally { - Output.Reader.Advance(consumed); + Output.Reader.AdvanceTo(consumed); } } Output.Reader.Complete(); } - private unsafe IISAwaitable WriteAsync(ReadOnlyBuffer buffer) + private unsafe IISAwaitable WriteAsync(ReadOnlyBuffer buffer) { var fCompletionExpected = false; var hr = 0; var nChunks = 0; - if (buffer.IsSingleSpan) + if (buffer.IsSingleSegment) { nChunks = 1; } @@ -579,7 +579,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration } } - if (buffer.IsSingleSpan) + if (buffer.IsSingleSegment) { var pDataChunks = stackalloc HttpApiTypes.HTTP_DATA_CHUNK[1]; diff --git a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/OutputProducer.cs b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/OutputProducer.cs index 91c664c5b3..28c2dc856b 100644 --- a/src/Microsoft.AspNetCore.Server.IISIntegration/Server/OutputProducer.cs +++ b/src/Microsoft.AspNetCore.Server.IISIntegration/Server/OutputProducer.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Buffers; using System.IO.Pipelines; using System.Threading; using System.Threading.Tasks; @@ -17,7 +18,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration private bool _completed = false; - private readonly IPipe _pipe; + private readonly Pipe _pipe; // https://github.com/dotnet/corefxlab/issues/1334 // Pipelines don't support multiple awaiters on flush @@ -26,13 +27,13 @@ namespace Microsoft.AspNetCore.Server.IISIntegration private readonly object _flushLock = new object(); private Action _flushCompleted; - public OutputProducer(IPipe pipe) + public OutputProducer(Pipe pipe) { _pipe = pipe; _flushCompleted = OnFlushCompleted; } - public IPipeReader Reader => _pipe.Reader; + public PipeReader Reader => _pipe.Reader; public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken)) { @@ -73,8 +74,6 @@ namespace Microsoft.AspNetCore.Server.IISIntegration ArraySegment buffer, CancellationToken cancellationToken) { - var writableBuffer = default(WritableBuffer); - lock (_contextLock) { if (_completed) @@ -82,26 +81,16 @@ namespace Microsoft.AspNetCore.Server.IISIntegration throw new ObjectDisposedException("Response is already completed"); } - writableBuffer = _pipe.Writer.Alloc(1); - // TODO obsolete -#pragma warning disable CS0618 // Type or member is obsolete - var writer = new WritableBufferWriter(writableBuffer); -#pragma warning restore CS0618 // Type or member is obsolete - if (buffer.Count > 0) - { - writer.Write(buffer.Array, buffer.Offset, buffer.Count); - } - - writableBuffer.Commit(); + _pipe.Writer.Write(new ReadOnlySpan(buffer.Array, buffer.Offset, buffer.Count)); } - return FlushAsync(writableBuffer, cancellationToken); + return FlushAsync(_pipe.Writer, cancellationToken); } - private Task FlushAsync(WritableBuffer writableBuffer, + private Task FlushAsync(PipeWriter pipeWriter, CancellationToken cancellationToken) { - var awaitable = writableBuffer.FlushAsync(cancellationToken); + var awaitable = pipeWriter.FlushAsync(cancellationToken); if (awaitable.IsCompleted) { // The flush task can't fail today