Skip to content

Added IConnectionSocketFeature for exposing underlying Socket! #30803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The socket for the connection.
/// </summary>
public interface IConnectionSocketFeature
{
/// <summary>
/// Gets the underlying socket
/// </summary>
Socket? Socket { get; }
}
}
Original file line number Diff line number Diff line change
@@ -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.ConnectionContext!>
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.ConnectionContext?>
Microsoft.AspNetCore.Connections.Experimental.IMultiplexedConnectionBuilder
Microsoft.AspNetCore.Connections.Experimental.IMultiplexedConnectionBuilder.ApplicationServices.get -> System.IServiceProvider!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -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()."));
}
}
22 changes: 22 additions & 0 deletions src/Servers/Kestrel/shared/TransportConnection.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal partial class TransportConnection : IFeatureCollection
private object? _currentIConnectionItemsFeature;
private object? _currentIMemoryPoolFeature;
private object? _currentIConnectionLifetimeFeature;
private object? _currentIConnectionSocketFeature;

private int _featureRevision;

Expand All @@ -31,6 +32,7 @@ private void FastReset()
_currentIConnectionItemsFeature = this;
_currentIMemoryPoolFeature = this;
_currentIConnectionLifetimeFeature = this;
_currentIConnectionSocketFeature = this;

}

Expand Down Expand Up @@ -123,6 +125,10 @@ private void ExtraFeatureSet(Type key, object? value)
{
feature = _currentIConnectionLifetimeFeature;
}
else if (key == typeof(IConnectionSocketFeature))
{
feature = _currentIConnectionSocketFeature;
}
else if (MaybeExtra != null)
{
feature = ExtraFeatureGet(key);
Expand Down Expand Up @@ -155,6 +161,10 @@ private void ExtraFeatureSet(Type key, object? value)
{
_currentIConnectionLifetimeFeature = value;
}
else if (key == typeof(IConnectionSocketFeature))
{
_currentIConnectionSocketFeature = value;
}
else
{
ExtraFeatureSet(key, value);
Expand Down Expand Up @@ -185,6 +195,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)));
Expand Down Expand Up @@ -216,6 +230,10 @@ private void ExtraFeatureSet(Type key, object? value)
{
_currentIConnectionLifetimeFeature = feature;
}
else if (typeof(TFeature) == typeof(IConnectionSocketFeature))
{
_currentIConnectionSocketFeature = feature;
}
else
{
ExtraFeatureSet(typeof(TFeature), feature);
Expand Down Expand Up @@ -244,6 +262,10 @@ private IEnumerable<KeyValuePair<Type, object>> FastEnumerable()
{
yield return new KeyValuePair<Type, object>(typeof(IConnectionLifetimeFeature), _currentIConnectionLifetimeFeature);
}
if (_currentIConnectionSocketFeature != null)
{
yield return new KeyValuePair<Type, object>(typeof(IConnectionSocketFeature), _currentIConnectionSocketFeature);
}

if (MaybeExtra != null)
{
Expand Down
1 change: 0 additions & 1 deletion src/Servers/Kestrel/shared/TransportConnection.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static string GenerateFile()
"IConnectionTransportFeature",
"IConnectionItemsFeature",
"IMemoryPoolFeature",
"IConnectionLifetimeFeature"
"IConnectionLifetimeFeature",
"IConnectionSocketFeature"
};

var usings = $@"
Expand Down