-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IConnectionSocketFeature (#31588)
- Removed the nullable from the Socket property as approved in API review - Make the IConnectionSocketFeature an additional feature instead of one implemented on the TransportConnection. - Added a test to verify it works
- Loading branch information
Showing
7 changed files
with
133 additions
and
4 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/Servers/Connections.Abstractions/src/Features/IConnectionSocketFeature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
/// Provides access to the connection's underlying <see cref="Socket"/>. | ||
/// </summary> | ||
public interface IConnectionSocketFeature | ||
{ | ||
/// <summary> | ||
/// Gets the underlying <see cref="Socket"/>. | ||
/// </summary> | ||
Socket Socket { get; } | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Servers/Connections.Abstractions/src/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Servers/Kestrel/Transport.Sockets/src/Internal/SocketConnection.FeatureCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
using Microsoft.AspNetCore.Connections.Features; | ||
|
||
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal | ||
{ | ||
internal sealed partial class SocketConnection : IConnectionSocketFeature | ||
{ | ||
public Socket Socket => _socket; | ||
|
||
private void InitiaizeFeatures() | ||
{ | ||
_currentIConnectionSocketFeature = this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Servers/Kestrel/test/Sockets.FunctionalTests/SocketTranspotTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Sockets; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Connections.Features; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Server.Kestrel.FunctionalTests; | ||
using Microsoft.AspNetCore.Testing; | ||
using Microsoft.Extensions.Hosting; | ||
using Xunit; | ||
|
||
namespace Sockets.FunctionalTests | ||
{ | ||
public class SocketTranspotTests : LoggedTestBase | ||
{ | ||
[Fact] | ||
public async Task SocketTransportExposesSocketsFeature() | ||
{ | ||
var builder = TransportSelector.GetHostBuilder() | ||
.ConfigureWebHost(webHostBuilder => | ||
{ | ||
webHostBuilder | ||
.UseKestrel() | ||
.UseUrls("http://127.0.0.1:0") | ||
.Configure(app => | ||
{ | ||
app.Run(context => | ||
{ | ||
var socket = context.Features.Get<IConnectionSocketFeature>().Socket; | ||
Assert.NotNull(socket); | ||
Assert.Equal(ProtocolType.Tcp, socket.ProtocolType); | ||
var ip = (IPEndPoint)socket.RemoteEndPoint; | ||
Assert.Equal(ip.Address, context.Connection.RemoteIpAddress); | ||
Assert.Equal(ip.Port, context.Connection.RemotePort); | ||
|
||
return Task.CompletedTask; | ||
}); | ||
}); | ||
}) | ||
.ConfigureServices(AddTestLogging); | ||
|
||
using var host = builder.Build(); | ||
using var client = new HttpClient(); | ||
|
||
await host.StartAsync(); | ||
|
||
var response = await client.GetAsync($"http://127.0.0.1:{host.GetPort()}/"); | ||
response.EnsureSuccessStatusCode(); | ||
|
||
await host.StopAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters