Skip to content

Commit c232fd1

Browse files
Added IConnectionSocketFeature for exposing underlying Socket!
1 parent c646e20 commit c232fd1

File tree

7 files changed

+49
-3
lines changed

7 files changed

+49
-3
lines changed

src/Servers/Connections.Abstractions/src/BaseConnectionContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Net;
7+
using System.Net.Sockets;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using Microsoft.AspNetCore.Http.Features;
@@ -45,6 +46,11 @@ public abstract class BaseConnectionContext : IAsyncDisposable
4546
/// </summary>
4647
public virtual EndPoint? RemoteEndPoint { get; set; }
4748

49+
/// <summary>
50+
/// Gets or sets the socket for this connection.
51+
/// </summary>
52+
public virtual Socket? Socket { get; set; }
53+
4854
/// <summary>
4955
/// Aborts the underlying connection.
5056
/// </summary>

src/Servers/Connections.Abstractions/src/DefaultConnectionContext.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO.Pipelines;
77
using System.Net;
8+
using System.Net.Sockets;
89
using System.Security.Claims;
910
using System.Threading;
1011
using System.Threading.Tasks;
@@ -22,7 +23,8 @@ public class DefaultConnectionContext : ConnectionContext,
2223
IConnectionTransportFeature,
2324
IConnectionUserFeature,
2425
IConnectionLifetimeFeature,
25-
IConnectionEndPointFeature
26+
IConnectionEndPointFeature,
27+
IConnectionSocketFeature
2628
{
2729
private CancellationTokenSource _connectionClosedTokenSource = new CancellationTokenSource();
2830

@@ -51,6 +53,7 @@ public DefaultConnectionContext(string id)
5153
Features.Set<IConnectionTransportFeature>(this);
5254
Features.Set<IConnectionLifetimeFeature>(this);
5355
Features.Set<IConnectionEndPointFeature>(this);
56+
Features.Set<IConnectionSocketFeature>(this);
5457

5558
ConnectionClosed = _connectionClosedTokenSource.Token;
5659
}
@@ -96,6 +99,9 @@ public DefaultConnectionContext(string id, IDuplexPipe transport, IDuplexPipe ap
9699
/// <inheritdoc />
97100
public override EndPoint? RemoteEndPoint { get; set; }
98101

102+
/// <inheritdoc />
103+
public override Socket? Socket { get; set; }
104+
99105
/// <inheritdoc />
100106
public override void Abort(ConnectionAbortedException abortReason)
101107
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Net.Sockets;
5+
6+
namespace Microsoft.AspNetCore.Connections.Features
7+
{
8+
/// <summary>
9+
/// The socket for the connection.
10+
/// </summary>
11+
public interface IConnectionSocketFeature
12+
{
13+
/// <summary>
14+
/// Gets the underlying socket
15+
/// </summary>
16+
Socket? Socket { get; }
17+
}
18+
}

src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#nullable enable
22
*REMOVED*Microsoft.AspNetCore.Connections.IConnectionListener.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<Microsoft.AspNetCore.Connections.ConnectionContext!>
3+
Microsoft.AspNetCore.Connections.Features.IConnectionSocketFeature
4+
Microsoft.AspNetCore.Connections.Features.IConnectionSocketFeature.Socket.get -> System.Net.Sockets.Socket?
35
Microsoft.AspNetCore.Connections.IConnectionListener.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<Microsoft.AspNetCore.Connections.ConnectionContext?>
46
Microsoft.AspNetCore.Connections.Experimental.IMultiplexedConnectionBuilder
57
Microsoft.AspNetCore.Connections.Experimental.IMultiplexedConnectionBuilder.ApplicationServices.get -> System.IServiceProvider!
@@ -23,3 +25,7 @@ Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.Multi
2325
Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionDelegate
2426
abstract Microsoft.AspNetCore.Connections.Experimental.MultiplexedConnectionContext.AcceptAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask<Microsoft.AspNetCore.Connections.ConnectionContext?>
2527
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<Microsoft.AspNetCore.Connections.ConnectionContext!>
28+
override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.get -> System.Net.Sockets.Socket?
29+
override Microsoft.AspNetCore.Connections.DefaultConnectionContext.Socket.set -> void
30+
virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.get -> System.Net.Sockets.Socket?
31+
virtual Microsoft.AspNetCore.Connections.BaseConnectionContext.Socket.set -> void

src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ internal SocketConnection(Socket socket,
4848
MemoryPool = memoryPool;
4949
_trace = trace;
5050
_waitForData = waitForData;
51+
Socket = _socket;
5152

5253
LocalEndPoint = _socket.LocalEndPoint;
5354
RemoteEndPoint = _socket.RemoteEndPoint;

src/Servers/Kestrel/shared/TransportConnection.FeatureCollection.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Buffers;
55
using System.Collections.Generic;
66
using System.IO.Pipelines;
7+
using System.Net.Sockets;
78
using System.Threading;
89
using Microsoft.AspNetCore.Connections.Features;
910

@@ -15,7 +16,8 @@ internal partial class TransportConnection : IConnectionIdFeature,
1516
IConnectionTransportFeature,
1617
IConnectionItemsFeature,
1718
IMemoryPoolFeature,
18-
IConnectionLifetimeFeature
19+
IConnectionLifetimeFeature,
20+
IConnectionSocketFeature
1921
{
2022
// NOTE: When feature interfaces are added to or removed from this TransportConnection class implementation,
2123
// then the list of `features` in the generated code project MUST also be updated.
@@ -41,6 +43,11 @@ CancellationToken IConnectionLifetimeFeature.ConnectionClosed
4143
set => ConnectionClosed = value;
4244
}
4345

46+
Socket? IConnectionSocketFeature.Socket
47+
{
48+
get => Socket;
49+
}
50+
4451
void IConnectionLifetimeFeature.Abort() => Abort(new ConnectionAbortedException("The connection was aborted by the application via IConnectionLifetimeFeature.Abort()."));
4552
}
4653
}

src/Servers/Kestrel/shared/TransportConnection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Buffers;
65
using System.Collections.Generic;
76
using System.Diagnostics;
87
using System.IO.Pipelines;
98
using System.Net;
9+
using System.Net.Sockets;
1010
using System.Threading;
1111
using Microsoft.AspNetCore.Http.Features;
1212

@@ -65,6 +65,8 @@ public override string ConnectionId
6565
}
6666
}
6767

68+
public override Socket? Socket { get; set; }
69+
6870
public override CancellationToken ConnectionClosed { get; set; }
6971

7072
// DO NOT remove this override to ConnectionContext.Abort. Doing so would cause

0 commit comments

Comments
 (0)