From c232fd1b532adf4f1ee9e15b63598f7009036adb Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Wed, 10 Mar 2021 22:40:09 +0530 Subject: [PATCH 01/12] Added IConnectionSocketFeature for exposing underlying Socket! --- .../src/BaseConnectionContext.cs | 6 ++++++ .../src/DefaultConnectionContext.cs | 8 +++++++- .../src/Features/IConnectionSocketFeature.cs | 18 ++++++++++++++++++ .../src/PublicAPI.Unshipped.txt | 6 ++++++ .../src/Internal/SocketConnection.cs | 1 + .../TransportConnection.FeatureCollection.cs | 9 ++++++++- .../Kestrel/shared/TransportConnection.cs | 4 +++- 7 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs diff --git a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs index 6ac40d175166..c34231cab69d 100644 --- a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http.Features; @@ -45,6 +46,11 @@ public abstract class BaseConnectionContext : IAsyncDisposable /// public virtual EndPoint? RemoteEndPoint { get; set; } + /// + /// Gets or sets the socket for this connection. + /// + public virtual Socket? Socket { get; set; } + /// /// Aborts the underlying connection. /// diff --git a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs index f64e71958783..de5a53254c97 100644 --- a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO.Pipelines; using System.Net; +using System.Net.Sockets; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; @@ -22,7 +23,8 @@ public class DefaultConnectionContext : ConnectionContext, IConnectionTransportFeature, IConnectionUserFeature, IConnectionLifetimeFeature, - IConnectionEndPointFeature + IConnectionEndPointFeature, + IConnectionSocketFeature { private CancellationTokenSource _connectionClosedTokenSource = new CancellationTokenSource(); @@ -51,6 +53,7 @@ public DefaultConnectionContext(string id) Features.Set(this); Features.Set(this); Features.Set(this); + Features.Set(this); ConnectionClosed = _connectionClosedTokenSource.Token; } @@ -96,6 +99,9 @@ public DefaultConnectionContext(string id, IDuplexPipe transport, IDuplexPipe ap /// public override EndPoint? RemoteEndPoint { get; set; } + /// + public override Socket? Socket { get; set; } + /// public override void Abort(ConnectionAbortedException abortReason) { diff --git a/src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs b/src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs new file mode 100644 index 000000000000..21a489012f8e --- /dev/null +++ b/src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs @@ -0,0 +1,18 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Net.Sockets; + +namespace Microsoft.AspNetCore.Connections.Features +{ + /// + /// The socket for the connection. + /// + public interface IConnectionSocketFeature + { + /// + /// Gets the underlying socket + /// + Socket? Socket { get; } + } +} diff --git a/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt b/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt index 575c538f6a9b..17edaebb4b9f 100644 --- a/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt +++ b/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt @@ -1,5 +1,7 @@ #nullable enable *REMOVED*Microsoft.AspNetCore.Connections.IConnectionListener.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +Microsoft.AspNetCore.Connections.Features.IConnectionSocketFeature +Microsoft.AspNetCore.Connections.Features.IConnectionSocketFeature.Socket.get -> System.Net.Sockets.Socket? Microsoft.AspNetCore.Connections.IConnectionListener.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask Microsoft.AspNetCore.Connections.Experimental.IMultiplexedConnectionBuilder Microsoft.AspNetCore.Connections.Experimental.IMultiplexedConnectionBuilder.ApplicationServices.get -> System.IServiceProvider! @@ -23,3 +25,7 @@ Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.Multi Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionDelegate abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.ConnectAsync(Microsoft.AspNetCore.Http.Features.IFeatureCollection? features = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.get -> System.Net.Sockets.Socket? +override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.set -> void +virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.get -> System.Net.Sockets.Socket? +virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.set -> void diff --git a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs index cf81d90e372b..008f7ba74018 100644 --- a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs +++ b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs @@ -48,6 +48,7 @@ internal SocketConnection(Socket socket, MemoryPool = memoryPool; _trace = trace; _waitForData = waitForData; + Socket = _socket; LocalEndPoint = _socket.LocalEndPoint; RemoteEndPoint = _socket.RemoteEndPoint; diff --git a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs index 5d94f6e3c485..2ac3b71d4a85 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs @@ -4,6 +4,7 @@ using System.Buffers; using System.Collections.Generic; using System.IO.Pipelines; +using System.Net.Sockets; using System.Threading; using Microsoft.AspNetCore.Connections.Features; @@ -15,7 +16,8 @@ internal partial class TransportConnection : IConnectionIdFeature, IConnectionTransportFeature, IConnectionItemsFeature, IMemoryPoolFeature, - IConnectionLifetimeFeature + IConnectionLifetimeFeature, + IConnectionSocketFeature { // NOTE: When feature interfaces are added to or removed from this TransportConnection class implementation, // then the list of `features` in the generated code project MUST also be updated. @@ -41,6 +43,11 @@ CancellationToken IConnectionLifetimeFeature.ConnectionClosed set => ConnectionClosed = value; } + Socket? IConnectionSocketFeature.Socket + { + get => Socket; + } + void IConnectionLifetimeFeature.Abort() => Abort(new ConnectionAbortedException("The connection was aborted by the application via IConnectionLifetimeFeature.Abort().")); } } diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index 8a1d2b7f16bf..87623d85b468 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -1,12 +1,12 @@ // Copyright (c) .NET Foundation. All rights reserved. // 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.Collections.Generic; using System.Diagnostics; using System.IO.Pipelines; using System.Net; +using System.Net.Sockets; using System.Threading; using Microsoft.AspNetCore.Http.Features; @@ -65,6 +65,8 @@ public override string ConnectionId } } + public override Socket? Socket { get; set; } + public override CancellationToken ConnectionClosed { get; set; } // DO NOT remove this override to ConnectionContext.Abort. Doing so would cause From 8e7396187d1341a7d9aac518962a161774ce99d8 Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Thu, 11 Mar 2021 10:27:56 +0530 Subject: [PATCH 02/12] Readonly Socket ! --- .../Connections.Abstractions/src/BaseConnectionContext.cs | 4 ++-- .../Connections.Abstractions/src/DefaultConnectionContext.cs | 2 +- .../Connections.Abstractions/src/PublicAPI.Unshipped.txt | 2 -- src/Servers/Kestrel/shared/TransportConnection.cs | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs index c34231cab69d..a5a031497cc1 100644 --- a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs @@ -47,9 +47,9 @@ public abstract class BaseConnectionContext : IAsyncDisposable public virtual EndPoint? RemoteEndPoint { get; set; } /// - /// Gets or sets the socket for this connection. + /// Gets the socket for this connection. /// - public virtual Socket? Socket { get; set; } + public virtual Socket? Socket { get; } /// /// Aborts the underlying connection. diff --git a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs index de5a53254c97..8bbf2dd16680 100644 --- a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs @@ -100,7 +100,7 @@ public DefaultConnectionContext(string id, IDuplexPipe transport, IDuplexPipe ap public override EndPoint? RemoteEndPoint { get; set; } /// - public override Socket? Socket { get; set; } + public override Socket? Socket { get; } /// public override void Abort(ConnectionAbortedException abortReason) diff --git a/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt b/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt index 17edaebb4b9f..db88438fc133 100644 --- a/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt +++ b/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt @@ -26,6 +26,4 @@ Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionDelegate abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.ConnectAsync(Microsoft.AspNetCore.Http.Features.IFeatureCollection? features = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.get -> System.Net.Sockets.Socket? -override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.set -> void virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.get -> System.Net.Sockets.Socket? -virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.set -> void diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index 87623d85b468..5a9e88e2a34f 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -65,7 +65,7 @@ public override string ConnectionId } } - public override Socket? Socket { get; set; } + public override Socket? Socket { get; } public override CancellationToken ConnectionClosed { get; set; } From 09840e5d58ccf5e97b2fda4a9d51527d8df29e20 Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Thu, 11 Mar 2021 16:33:30 +0530 Subject: [PATCH 03/12] Revert "Readonly Socket !" This reverts commit 8e7396187d1341a7d9aac518962a161774ce99d8. --- .../Connections.Abstractions/src/BaseConnectionContext.cs | 4 ++-- .../Connections.Abstractions/src/DefaultConnectionContext.cs | 2 +- .../Connections.Abstractions/src/PublicAPI.Unshipped.txt | 2 ++ src/Servers/Kestrel/shared/TransportConnection.cs | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs index a5a031497cc1..c34231cab69d 100644 --- a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs @@ -47,9 +47,9 @@ public abstract class BaseConnectionContext : IAsyncDisposable public virtual EndPoint? RemoteEndPoint { get; set; } /// - /// Gets the socket for this connection. + /// Gets or sets the socket for this connection. /// - public virtual Socket? Socket { get; } + public virtual Socket? Socket { get; set; } /// /// Aborts the underlying connection. diff --git a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs index 8bbf2dd16680..de5a53254c97 100644 --- a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs @@ -100,7 +100,7 @@ public DefaultConnectionContext(string id, IDuplexPipe transport, IDuplexPipe ap public override EndPoint? RemoteEndPoint { get; set; } /// - public override Socket? Socket { get; } + public override Socket? Socket { get; set; } /// public override void Abort(ConnectionAbortedException abortReason) diff --git a/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt b/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt index db88438fc133..17edaebb4b9f 100644 --- a/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt +++ b/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt @@ -26,4 +26,6 @@ Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionDelegate abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.ConnectAsync(Microsoft.AspNetCore.Http.Features.IFeatureCollection? features = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.get -> System.Net.Sockets.Socket? +override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.set -> void virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.get -> System.Net.Sockets.Socket? +virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.set -> void diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index 5a9e88e2a34f..87623d85b468 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -65,7 +65,7 @@ public override string ConnectionId } } - public override Socket? Socket { get; } + public override Socket? Socket { get; set; } public override CancellationToken ConnectionClosed { get; set; } From b320450d840ab5a5da1f5a9aee6708ad7278be35 Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Fri, 12 Mar 2021 19:57:15 +0530 Subject: [PATCH 04/12] Addressed PR feedback! --- .../src/BaseConnectionContext.cs | 5 ---- .../src/DefaultConnectionContext.cs | 7 +----- .../src/PublicAPI.Unshipped.txt | 4 ---- .../src/Internal/SocketConnection.cs | 1 - .../TransportConnection.FeatureCollection.cs | 8 +------ .../shared/TransportConnection.Generated.cs | 23 ++++++++++++++++++- .../Kestrel/shared/TransportConnection.cs | 2 -- 7 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs index c34231cab69d..4a72b05cec84 100644 --- a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs @@ -46,11 +46,6 @@ public abstract class BaseConnectionContext : IAsyncDisposable /// public virtual EndPoint? RemoteEndPoint { get; set; } - /// - /// Gets or sets the socket for this connection. - /// - public virtual Socket? Socket { get; set; } - /// /// Aborts the underlying connection. /// diff --git a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs index de5a53254c97..f436f7fa784b 100644 --- a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs @@ -23,8 +23,7 @@ public class DefaultConnectionContext : ConnectionContext, IConnectionTransportFeature, IConnectionUserFeature, IConnectionLifetimeFeature, - IConnectionEndPointFeature, - IConnectionSocketFeature + IConnectionEndPointFeature { private CancellationTokenSource _connectionClosedTokenSource = new CancellationTokenSource(); @@ -53,7 +52,6 @@ public DefaultConnectionContext(string id) Features.Set(this); Features.Set(this); Features.Set(this); - Features.Set(this); ConnectionClosed = _connectionClosedTokenSource.Token; } @@ -99,9 +97,6 @@ public DefaultConnectionContext(string id, IDuplexPipe transport, IDuplexPipe ap /// public override EndPoint? RemoteEndPoint { get; set; } - /// - public override Socket? Socket { get; set; } - /// public override void Abort(ConnectionAbortedException abortReason) { diff --git a/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt b/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt index 17edaebb4b9f..f79889680abe 100644 --- a/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt +++ b/src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt @@ -25,7 +25,3 @@ Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.Multi Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionDelegate abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.ConnectAsync(Microsoft.AspNetCore.Http.Features.IFeatureCollection? features = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.get -> System.Net.Sockets.Socket? -override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.set -> void -virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.get -> System.Net.Sockets.Socket? -virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.set -> void diff --git a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs index 008f7ba74018..cf81d90e372b 100644 --- a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs +++ b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs @@ -48,7 +48,6 @@ internal SocketConnection(Socket socket, MemoryPool = memoryPool; _trace = trace; _waitForData = waitForData; - Socket = _socket; LocalEndPoint = _socket.LocalEndPoint; RemoteEndPoint = _socket.RemoteEndPoint; diff --git a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs index 2ac3b71d4a85..fd44cabc06f4 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs @@ -16,8 +16,7 @@ internal partial class TransportConnection : IConnectionIdFeature, IConnectionTransportFeature, IConnectionItemsFeature, IMemoryPoolFeature, - IConnectionLifetimeFeature, - IConnectionSocketFeature + IConnectionLifetimeFeature { // NOTE: When feature interfaces are added to or removed from this TransportConnection class implementation, // then the list of `features` in the generated code project MUST also be updated. @@ -43,11 +42,6 @@ CancellationToken IConnectionLifetimeFeature.ConnectionClosed set => ConnectionClosed = value; } - Socket? IConnectionSocketFeature.Socket - { - get => Socket; - } - void IConnectionLifetimeFeature.Abort() => Abort(new ConnectionAbortedException("The connection was aborted by the application via IConnectionLifetimeFeature.Abort().")); } } diff --git a/src/Servers/Kestrel/shared/TransportConnection.Generated.cs b/src/Servers/Kestrel/shared/TransportConnection.Generated.cs index b35954bb6fa2..5be6257ec8c1 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.Generated.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.Generated.cs @@ -19,6 +19,7 @@ internal partial class TransportConnection : IFeatureCollection private object? _currentIConnectionItemsFeature; private object? _currentIMemoryPoolFeature; private object? _currentIConnectionLifetimeFeature; + private object? _currentIConnectionSocketFeature; private int _featureRevision; @@ -31,7 +32,7 @@ private void FastReset() _currentIConnectionItemsFeature = this; _currentIMemoryPoolFeature = this; _currentIConnectionLifetimeFeature = this; - + _currentIConnectionSocketFeature = this; } // Internal for testing @@ -123,6 +124,10 @@ private void ExtraFeatureSet(Type key, object? value) { feature = _currentIConnectionLifetimeFeature; } + else if (key == typeof(IConnectionSocketFeature)) + { + feature = _currentIConnectionSocketFeature; + } else if (MaybeExtra != null) { feature = ExtraFeatureGet(key); @@ -155,6 +160,10 @@ private void ExtraFeatureSet(Type key, object? value) { _currentIConnectionLifetimeFeature = value; } + else if (key == typeof(IConnectionSocketFeature)) + { + _currentIConnectionSocketFeature = value; + } else { ExtraFeatureSet(key, value); @@ -185,6 +194,10 @@ private void ExtraFeatureSet(Type key, object? value) { feature = (TFeature?)_currentIConnectionLifetimeFeature; } + else if (typeof(TFeature) == typeof(IConnectionSocketFeature)) + { + feature = (TFeature?)_currentIConnectionSocketFeature; + } else if (MaybeExtra != null) { feature = (TFeature?)(ExtraFeatureGet(typeof(TFeature))); @@ -216,6 +229,10 @@ private void ExtraFeatureSet(Type key, object? value) { _currentIConnectionLifetimeFeature = feature; } + else if (typeof(TFeature) == typeof(IConnectionSocketFeature)) + { + _currentIConnectionSocketFeature = feature; + } else { ExtraFeatureSet(typeof(TFeature), feature); @@ -244,6 +261,10 @@ private IEnumerable> FastEnumerable() { yield return new KeyValuePair(typeof(IConnectionLifetimeFeature), _currentIConnectionLifetimeFeature); } + if (_currentIConnectionSocketFeature != null) + { + yield return new KeyValuePair(typeof(IConnectionSocketFeature), _currentIConnectionSocketFeature); + } if (MaybeExtra != null) { diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index 87623d85b468..70a47e7c2eed 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -65,8 +65,6 @@ public override string ConnectionId } } - public override Socket? Socket { get; set; } - public override CancellationToken ConnectionClosed { get; set; } // DO NOT remove this override to ConnectionContext.Abort. Doing so would cause From 703511e188934df1f1c6140480fad14ffde4564a Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Fri, 12 Mar 2021 20:09:29 +0530 Subject: [PATCH 05/12] Cleanup - Unwanted usings --- .../Connections.Abstractions/src/BaseConnectionContext.cs | 1 - .../Connections.Abstractions/src/DefaultConnectionContext.cs | 1 - .../Kestrel/shared/TransportConnection.FeatureCollection.cs | 1 - src/Servers/Kestrel/shared/TransportConnection.cs | 1 - 4 files changed, 4 deletions(-) diff --git a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs index 4a72b05cec84..6ac40d175166 100644 --- a/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Net; -using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http.Features; diff --git a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs index f436f7fa784b..f64e71958783 100644 --- a/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs +++ b/src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO.Pipelines; using System.Net; -using System.Net.Sockets; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; diff --git a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs index fd44cabc06f4..5d94f6e3c485 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs @@ -4,7 +4,6 @@ using System.Buffers; using System.Collections.Generic; using System.IO.Pipelines; -using System.Net.Sockets; using System.Threading; using Microsoft.AspNetCore.Connections.Features; diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index 70a47e7c2eed..f4622aeb76f6 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -6,7 +6,6 @@ using System.Diagnostics; using System.IO.Pipelines; using System.Net; -using System.Net.Sockets; using System.Threading; using Microsoft.AspNetCore.Http.Features; From e7b6a959925da2b29bb753ecd379adaf1cbb5dcd Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Sat, 13 Mar 2021 12:49:26 +0530 Subject: [PATCH 06/12] Addressed PR feedback. Generated TransportConnection file using CodeGenerator --- .../Kestrel/shared/TransportConnection.FeatureCollection.cs | 6 +++++- src/Servers/Kestrel/shared/TransportConnection.Generated.cs | 1 + .../CodeGenerator/TransportConnectionFeatureCollection.cs | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs index 5d94f6e3c485..e6135a746cd7 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs @@ -4,6 +4,7 @@ using System.Buffers; using System.Collections.Generic; using System.IO.Pipelines; +using System.Net.Sockets; using System.Threading; using Microsoft.AspNetCore.Connections.Features; @@ -15,7 +16,8 @@ internal partial class TransportConnection : IConnectionIdFeature, IConnectionTransportFeature, IConnectionItemsFeature, IMemoryPoolFeature, - IConnectionLifetimeFeature + IConnectionLifetimeFeature, + IConnectionSocketFeature { // NOTE: When feature interfaces are added to or removed from this TransportConnection class implementation, // then the list of `features` in the generated code project MUST also be updated. @@ -41,6 +43,8 @@ CancellationToken IConnectionLifetimeFeature.ConnectionClosed set => ConnectionClosed = value; } + Socket? IConnectionSocketFeature.Socket { get; } + void IConnectionLifetimeFeature.Abort() => Abort(new ConnectionAbortedException("The connection was aborted by the application via IConnectionLifetimeFeature.Abort().")); } } diff --git a/src/Servers/Kestrel/shared/TransportConnection.Generated.cs b/src/Servers/Kestrel/shared/TransportConnection.Generated.cs index 5be6257ec8c1..c1ee63177240 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.Generated.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.Generated.cs @@ -33,6 +33,7 @@ private void FastReset() _currentIMemoryPoolFeature = this; _currentIConnectionLifetimeFeature = this; _currentIConnectionSocketFeature = this; + } // Internal for testing diff --git a/src/Servers/Kestrel/tools/CodeGenerator/TransportConnectionFeatureCollection.cs b/src/Servers/Kestrel/tools/CodeGenerator/TransportConnectionFeatureCollection.cs index fdc759e7f31f..bca86278d263 100644 --- a/src/Servers/Kestrel/tools/CodeGenerator/TransportConnectionFeatureCollection.cs +++ b/src/Servers/Kestrel/tools/CodeGenerator/TransportConnectionFeatureCollection.cs @@ -15,7 +15,8 @@ public static string GenerateFile() "IConnectionTransportFeature", "IConnectionItemsFeature", "IMemoryPoolFeature", - "IConnectionLifetimeFeature" + "IConnectionLifetimeFeature", + "IConnectionSocketFeature" }; var usings = $@" From b211ce0be9bea420c837dd97fe8b1972a9fb7085 Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Sun, 14 Mar 2021 10:58:02 +0530 Subject: [PATCH 07/12] Addressed PR feedback. Added virutal socket again to SocketConnection.cs --- .../Kestrel/shared/TransportConnection.FeatureCollection.cs | 2 +- src/Servers/Kestrel/shared/TransportConnection.cs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs index e6135a746cd7..d26f53f505db 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs @@ -43,7 +43,7 @@ CancellationToken IConnectionLifetimeFeature.ConnectionClosed set => ConnectionClosed = value; } - Socket? IConnectionSocketFeature.Socket { get; } + Socket? IConnectionSocketFeature.Socket => Socket; void IConnectionLifetimeFeature.Abort() => Abort(new ConnectionAbortedException("The connection was aborted by the application via IConnectionLifetimeFeature.Abort().")); } diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index f4622aeb76f6..aea2beea4924 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO.Pipelines; using System.Net; +using System.Net.Sockets; using System.Threading; using Microsoft.AspNetCore.Http.Features; @@ -64,6 +65,8 @@ public override string ConnectionId } } + public virtual Socket Socket { get; } = default!; + public override CancellationToken ConnectionClosed { get; set; } // DO NOT remove this override to ConnectionContext.Abort. Doing so would cause From f1f01daeb7adc7eb45c72194985740236a6df74e Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Sun, 14 Mar 2021 10:58:02 +0530 Subject: [PATCH 08/12] Addressed PR feedback. Added virtual socket again to SocketConnection.cs --- .../Kestrel/shared/TransportConnection.FeatureCollection.cs | 2 +- src/Servers/Kestrel/shared/TransportConnection.cs | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs index e6135a746cd7..d26f53f505db 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs @@ -43,7 +43,7 @@ CancellationToken IConnectionLifetimeFeature.ConnectionClosed set => ConnectionClosed = value; } - Socket? IConnectionSocketFeature.Socket { get; } + Socket? IConnectionSocketFeature.Socket => Socket; void IConnectionLifetimeFeature.Abort() => Abort(new ConnectionAbortedException("The connection was aborted by the application via IConnectionLifetimeFeature.Abort().")); } diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index f4622aeb76f6..aea2beea4924 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO.Pipelines; using System.Net; +using System.Net.Sockets; using System.Threading; using Microsoft.AspNetCore.Http.Features; @@ -64,6 +65,8 @@ public override string ConnectionId } } + public virtual Socket Socket { get; } = default!; + public override CancellationToken ConnectionClosed { get; set; } // DO NOT remove this override to ConnectionContext.Abort. Doing so would cause From ea59c5badc9e7dde2496d1cac5400bb4541fe78d Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Mon, 15 Mar 2021 19:23:26 +0530 Subject: [PATCH 09/12] Added socket finally! :) --- .../Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs index cf81d90e372b..df929c6d995c 100644 --- a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs +++ b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs @@ -48,6 +48,7 @@ internal SocketConnection(Socket socket, MemoryPool = memoryPool; _trace = trace; _waitForData = waitForData; + Socket = socket; LocalEndPoint = _socket.LocalEndPoint; RemoteEndPoint = _socket.RemoteEndPoint; @@ -75,6 +76,8 @@ internal SocketConnection(Socket socket, public override MemoryPool MemoryPool { get; } + public override Socket Socket { get; } + public void Start() { _processingTask = StartAsync(); From 4b90693e6fe5e09de163c5a68bfd83aa2875c768 Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Sun, 28 Mar 2021 12:28:30 +0530 Subject: [PATCH 10/12] Addressed PR feedback. --- .../src/Features/IConnectionSocketFeature.cs | 4 ++-- src/Servers/Kestrel/shared/TransportConnection.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs b/src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs index 21a489012f8e..bef71a70b6ed 100644 --- a/src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs +++ b/src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs @@ -6,12 +6,12 @@ namespace Microsoft.AspNetCore.Connections.Features { /// - /// The socket for the connection. + /// Provides access to the connection's underlying if any. /// public interface IConnectionSocketFeature { /// - /// Gets the underlying socket + /// Gets the underlying . /// Socket? Socket { get; } } diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index aea2beea4924..adeb3c7771eb 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -65,7 +65,7 @@ public override string ConnectionId } } - public virtual Socket Socket { get; } = default!; + public Socket? Socket { get; } public override CancellationToken ConnectionClosed { get; set; } From ad3fe2198bccf509aa9114c2967e1bf8960693ba Mon Sep 17 00:00:00 2001 From: shreyas jejurkar Date: Sat, 3 Apr 2021 16:12:14 +0530 Subject: [PATCH 11/12] Addressed PR feedback. --- .../Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs | 2 -- src/Servers/Kestrel/shared/TransportConnection.cs | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs index df929c6d995c..f0ce293936dd 100644 --- a/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs +++ b/src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs @@ -76,8 +76,6 @@ internal SocketConnection(Socket socket, public override MemoryPool MemoryPool { get; } - public override Socket Socket { get; } - public void Start() { _processingTask = StartAsync(); diff --git a/src/Servers/Kestrel/shared/TransportConnection.cs b/src/Servers/Kestrel/shared/TransportConnection.cs index adeb3c7771eb..bef728ee83be 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.cs @@ -65,7 +65,7 @@ public override string ConnectionId } } - public Socket? Socket { get; } + public Socket? Socket { get; protected set; } public override CancellationToken ConnectionClosed { get; set; } From 2c39ec4cf35e3f3b1df48a44f30a18d128781293 Mon Sep 17 00:00:00 2001 From: Shreyas Jejurkar Date: Sat, 3 Apr 2021 16:33:55 +0530 Subject: [PATCH 12/12] Update TransportConnection.Generated.cs --- src/Servers/Kestrel/shared/TransportConnection.Generated.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Servers/Kestrel/shared/TransportConnection.Generated.cs b/src/Servers/Kestrel/shared/TransportConnection.Generated.cs index a3ab306b1183..ca396fbb182b 100644 --- a/src/Servers/Kestrel/shared/TransportConnection.Generated.cs +++ b/src/Servers/Kestrel/shared/TransportConnection.Generated.cs @@ -171,7 +171,7 @@ private void ExtraFeatureSet(Type key, object? value) } else if (key == typeof(IConnectionSocketFeature)) { - _currentIConnectionSocketFeature = value; + _currentIConnectionSocketFeature = (IConnectionSocketFeature?)value; } else { @@ -209,7 +209,7 @@ private void ExtraFeatureSet(Type key, object? value) } else if (typeof(TFeature) == typeof(IConnectionSocketFeature)) { - feature = (TFeature?)_currentIConnectionSocketFeature; + feature = Unsafe.As(ref _currentIConnectionSocketFeature); } else if (MaybeExtra != null) { @@ -248,7 +248,7 @@ private void ExtraFeatureSet(Type key, object? value) } else if (typeof(TFeature) == typeof(IConnectionSocketFeature)) { - _currentIConnectionSocketFeature = feature; + _currentIConnectionSocketFeature = Unsafe.As(ref feature); } else {