From 8ea181f088f9022338b258e8f1f7ca70b2851fdb Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 29 Jan 2018 14:27:47 -0800 Subject: [PATCH] Use the OnWriterCompleted and OnReaderCompleted events directly. (#2280) --- .../Internal/HttpConnectionMiddleware.cs | 44 ++++--------------- 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs b/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs index 8018438fa4..6514768ab5 100644 --- a/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs +++ b/src/Kestrel.Core/Internal/HttpConnectionMiddleware.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO.Pipelines; +using System.Collections.Generic; using System.Net; using System.Threading; using System.Threading.Tasks; @@ -9,15 +7,11 @@ using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Protocols; using Microsoft.AspNetCore.Protocols.Features; using Microsoft.AspNetCore.Server.Kestrel.Core.Adapter.Internal; -using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http; -using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2; namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal { public class HttpConnectionMiddleware { - private static Action _completeTcs = CompleteTcs; - private static long _lastHttpConnectionId = long.MinValue; private readonly IList _connectionAdapters; @@ -74,42 +68,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal var connection = new HttpConnection(httpConnectionContext); var processingTask = connection.StartRequestProcessing(_application); - - var inputTcs = new TaskCompletionSource(); - var outputTcs = new TaskCompletionSource(); - - // The reason we don't fire events directly from these callbacks is because it seems - // like the transport callbacks root the state object (even after it fires) - connectionContext.Transport.Input.OnWriterCompleted(_completeTcs, inputTcs); - connectionContext.Transport.Output.OnReaderCompleted(_completeTcs, outputTcs); - - inputTcs.Task.ContinueWith((task, state) => + + connectionContext.Transport.Input.OnWriterCompleted((error, state) => { - ((HttpConnection)state).Abort(task.Exception?.InnerException); + ((HttpConnection)state).Abort(error); }, - connection, TaskContinuationOptions.ExecuteSynchronously); + connection); - outputTcs.Task.ContinueWith((task, state) => + connectionContext.Transport.Output.OnReaderCompleted((error, state) => { - ((HttpConnection)state).OnConnectionClosed(task.Exception?.InnerException); + ((HttpConnection)state).OnConnectionClosed(error); }, - connection, TaskContinuationOptions.ExecuteSynchronously); + connection); return processingTask; } - - private static void CompleteTcs(Exception error, object state) - { - var tcs = (TaskCompletionSource)state; - - if (error != null) - { - tcs.TrySetException(error); - } - else - { - tcs.TrySetResult(null); - } - } } }