Migrate to new pipe APIs (#454)

This commit is contained in:
Pavel Krymets 2017-11-13 15:06:22 -08:00 committed by GitHub
parent adec0c6fbc
commit b8c3d26975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 26 deletions

View File

@ -12,4 +12,10 @@
<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<!-- This is an experimental version of the compiler. See https://github.com/dotnet/csharplang/issues/666 for more details. -->
<PackageReference Include="Microsoft.NETCore.Compilers" Version="$(MicrosoftNETCoreCompilersPackageVersion)" PrivateAssets="All" />
</ItemGroup>
</Project>

View File

@ -26,14 +26,15 @@
<MicrosoftExtensionsOptionsPackageVersion>2.1.0-preview1-27496</MicrosoftExtensionsOptionsPackageVersion>
<MicrosoftExtensionsSecurityHelperSourcesPackageVersion>2.1.0-preview1-27496</MicrosoftExtensionsSecurityHelperSourcesPackageVersion>
<MicrosoftNETCoreApp20PackageVersion>2.0.0</MicrosoftNETCoreApp20PackageVersion>
<MicrosoftNETCoreCompilersPackageVersion>2.6.0-beta2-62211-02</MicrosoftNETCoreCompilersPackageVersion>
<MicrosoftNETTestSdkPackageVersion>15.3.0</MicrosoftNETTestSdkPackageVersion>
<SystemBuffersPackageVersion>4.4.0</SystemBuffersPackageVersion>
<SystemIOPipelinesPackageVersion>0.1.0-e170811-6</SystemIOPipelinesPackageVersion>
<SystemMemoryPackageVersion>4.4.0-preview3-25519-03</SystemMemoryPackageVersion>
<SystemNumericsVectorsPackageVersion>4.4.0</SystemNumericsVectorsPackageVersion>
<SystemRuntimeCompilerServicesUnsafePackageVersion>4.4.0</SystemRuntimeCompilerServicesUnsafePackageVersion>
<SystemIOPipelinesPackageVersion>0.1.0-alpha-002</SystemIOPipelinesPackageVersion>
<SystemMemoryPackageVersion>4.5.0-preview1-25902-08</SystemMemoryPackageVersion>
<SystemNumericsVectorsPackageVersion>4.5.0-preview1-25902-08</SystemNumericsVectorsPackageVersion>
<SystemRuntimeCompilerServicesUnsafePackageVersion>4.5.0-preview1-25902-08</SystemRuntimeCompilerServicesUnsafePackageVersion>
<SystemSecurityPrincipalWindowsPackageVersion>4.4.0</SystemSecurityPrincipalWindowsPackageVersion>
<SystemTextEncodingsWebUtf8PackageVersion>0.1.0-e170811-6</SystemTextEncodingsWebUtf8PackageVersion>
<SystemTextEncodingsWebUtf8PackageVersion>0.1.0-alpha-002</SystemTextEncodingsWebUtf8PackageVersion>
<XunitPackageVersion>2.3.0</XunitPackageVersion>
<XunitRunnerVisualStudioPackageVersion>2.3.0</XunitRunnerVisualStudioPackageVersion>
</PropertyGroup>

View File

@ -41,10 +41,10 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
protected Stack<KeyValuePair<Func<object, Task>, object>> _onCompleted;
protected Exception _applicationException;
private readonly PipeFactory _pipeFactory;
private readonly BufferPool _bufferPool;
private GCHandle _thisHandle;
private BufferHandle _inputHandle;
private MemoryHandle _inputHandle;
private IISAwaitable _operation = new IISAwaitable();
private IISAwaitable _readWebSocketsOperation;
@ -64,12 +64,12 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
private const string NegotiateString = "Negotiate";
private const string BasicString = "Basic";
internal unsafe IISHttpContext(PipeFactory pipeFactory, IntPtr pHttpContext, IISOptions options)
internal unsafe IISHttpContext(BufferPool bufferPool, IntPtr pHttpContext, IISOptions options)
: base((HttpApiTypes.HTTP_REQUEST*)NativeMethods.http_get_raw_request(pHttpContext))
{
_thisHandle = GCHandle.Alloc(this);
_pipeFactory = pipeFactory;
_bufferPool = bufferPool;
_pHttpContext = pHttpContext;
NativeMethods.http_set_managed_context(_pHttpContext, (IntPtr)_thisHandle);
@ -142,8 +142,8 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
RequestBody = new IISHttpRequestBody(this);
ResponseBody = new IISHttpResponseBody(this);
Input = _pipeFactory.Create(new PipeOptions { ReaderScheduler = TaskRunScheduler.Default });
var pipe = _pipeFactory.Create(new PipeOptions { ReaderScheduler = TaskRunScheduler.Default });
Input = new Pipe(new PipeOptions(_bufferPool, readerScheduler: TaskRunScheduler.Default));
var pipe = new Pipe(new PipeOptions(_bufferPool, readerScheduler: TaskRunScheduler.Default));
Output = new OutputProducer(pipe);
}
@ -609,7 +609,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
// REVIEW: We don't really need this list since the memory is already pinned with the default pool,
// but shouldn't assume the pool implementation right now. Unfortunately, this causes a heap allocation...
var handles = new BufferHandle[nChunks];
var handles = new MemoryHandle[nChunks];
foreach (var b in buffer)
{
@ -620,7 +620,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
chunk.DataChunkType = HttpApiTypes.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
chunk.fromMemory.BufferLength = (uint)b.Length;
chunk.fromMemory.pBuffer = (IntPtr)handle.PinnedPointer;
chunk.fromMemory.pBuffer = (IntPtr)handle.Pointer;
currentChunk++;
}
@ -661,7 +661,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{
var hr = NativeMethods.http_read_request_bytes(
_pHttpContext,
(byte*)_inputHandle.PinnedPointer,
(byte*)_inputHandle.Pointer,
length,
out var dwReceivedBytes,
out bool fCompletionExpected);
@ -679,7 +679,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
bool fCompletionExpected;
hr = NativeMethods.http_websockets_read_bytes(
_pHttpContext,
(byte*)_inputHandle.PinnedPointer,
(byte*)_inputHandle.Pointer,
length,
IISAwaitable.ReadCallback,
(IntPtr)_thisHandle,

View File

@ -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.Threading.Tasks;
using System.Threading;
using System.IO.Pipelines;
@ -14,8 +15,8 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{
private readonly IHttpApplication<TContext> _application;
public IISHttpContextOfT(PipeFactory pipeFactory, IHttpApplication<TContext> application, IntPtr pHttpContext, IISOptions options)
: base(pipeFactory, pHttpContext, options)
public IISHttpContextOfT(BufferPool bufferPool, IHttpApplication<TContext> application, IntPtr pHttpContext, IISOptions options)
: base(bufferPool, pHttpContext, options)
{
_application = application;
}

View File

@ -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.Runtime.InteropServices;
using System.Threading;
@ -22,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
private static NativeMethods.PFN_ASYNC_COMPLETION _onAsyncCompletion = OnAsyncCompletion;
private IISContextFactory _iisContextFactory;
private PipeFactory _pipeFactory = new PipeFactory();
private readonly BufferPool _bufferPool = new MemoryPool();
private GCHandle _httpServerHandle;
private readonly IApplicationLifetime _applicationLifetime;
private readonly IAuthenticationSchemeProvider _authentication;
@ -45,7 +46,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
{
_httpServerHandle = GCHandle.Alloc(this);
_iisContextFactory = new IISContextFactory<TContext>(_pipeFactory, application, _options);
_iisContextFactory = new IISContextFactory<TContext>(_bufferPool, application, _options);
// Start the server by registering the callback
NativeMethods.register_callbacks(_requestHandler, _shutdownHandler, _onAsyncCompletion, (IntPtr)_httpServerHandle, (IntPtr)_httpServerHandle);
@ -69,7 +70,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
_httpServerHandle.Free();
}
_pipeFactory.Dispose();
_bufferPool.Dispose();
}
private static NativeMethods.REQUEST_NOTIFICATION_STATUS HandleRequest(IntPtr pHttpContext, IntPtr pvRequestContext)
@ -125,19 +126,19 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
private class IISContextFactory<T> : IISContextFactory
{
private readonly IHttpApplication<T> _application;
private readonly PipeFactory _pipeFactory;
private readonly BufferPool _bufferPool;
private readonly IISOptions _options;
public IISContextFactory(PipeFactory pipeFactory, IHttpApplication<T> application, IISOptions options)
public IISContextFactory(BufferPool bufferPool, IHttpApplication<T> application, IISOptions options)
{
_application = application;
_pipeFactory = pipeFactory;
_bufferPool = bufferPool;
_options = options;
}
public IISHttpContext CreateHttpContext(IntPtr pHttpContext)
{
return new IISHttpContextOfT<T>(_pipeFactory, _application, pHttpContext, _options);
return new IISHttpContextOfT<T>(_bufferPool, _application, pHttpContext, _options);
}
}
}

View File

@ -107,10 +107,10 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
// The flush task can't fail today
return Task.CompletedTask;
}
return FlushAsyncAwaited(awaitable, writableBuffer.BytesWritten, cancellationToken);
return FlushAsyncAwaited(awaitable, cancellationToken);
}
private async Task FlushAsyncAwaited(WritableBufferAwaitable awaitable, long count, CancellationToken cancellationToken)
private async Task FlushAsyncAwaited(WritableBufferAwaitable awaitable, CancellationToken cancellationToken)
{
// https://github.com/dotnet/corefxlab/issues/1334
// Since the flush awaitable doesn't currently support multiple awaiters