Skip to content

Commit ff8363a

Browse files
authored
Add SocketConnectionFactory and http2cat (#14582)
1 parent 53a54d9 commit ff8363a

File tree

8 files changed

+1277
-0
lines changed

8 files changed

+1277
-0
lines changed

src/Servers/Kestrel/Core/src/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel.Performance, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
1515
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
1616
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
17+
[assembly: InternalsVisibleTo("http2cat, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

src/Servers/Kestrel/Kestrel.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Hostin
8282
EndProject
8383
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.WebUtilities", "..\..\Http\WebUtilities\src\Microsoft.AspNetCore.WebUtilities.csproj", "{EE45763C-753D-4228-8E5D-A71F8BDB3D89}"
8484
EndProject
85+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "http2cat", "samples\http2cat\http2cat.csproj", "{3D6821F5-F242-4828-8DDE-89488E85512D}"
86+
EndProject
8587
Global
8688
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8789
Debug|Any CPU = Debug|Any CPU
@@ -452,6 +454,18 @@ Global
452454
{EE45763C-753D-4228-8E5D-A71F8BDB3D89}.Release|x64.Build.0 = Release|Any CPU
453455
{EE45763C-753D-4228-8E5D-A71F8BDB3D89}.Release|x86.ActiveCfg = Release|Any CPU
454456
{EE45763C-753D-4228-8E5D-A71F8BDB3D89}.Release|x86.Build.0 = Release|Any CPU
457+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
458+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Debug|Any CPU.Build.0 = Debug|Any CPU
459+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Debug|x64.ActiveCfg = Debug|Any CPU
460+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Debug|x64.Build.0 = Debug|Any CPU
461+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Debug|x86.ActiveCfg = Debug|Any CPU
462+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Debug|x86.Build.0 = Debug|Any CPU
463+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|Any CPU.ActiveCfg = Release|Any CPU
464+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|Any CPU.Build.0 = Release|Any CPU
465+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|x64.ActiveCfg = Release|Any CPU
466+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|x64.Build.0 = Release|Any CPU
467+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|x86.ActiveCfg = Release|Any CPU
468+
{3D6821F5-F242-4828-8DDE-89488E85512D}.Release|x86.Build.0 = Release|Any CPU
455469
EndGlobalSection
456470
GlobalSection(SolutionProperties) = preSolution
457471
HideSolutionNode = FALSE
@@ -488,6 +502,7 @@ Global
488502
{D9872E91-EF1D-4181-82C9-584224ADE368} = {F0A1281A-B512-49D2-8362-21EE32B3674F}
489503
{E0AD50A3-2518-4060-8BB9-5649B04B3A6D} = {F0A1281A-B512-49D2-8362-21EE32B3674F}
490504
{EE45763C-753D-4228-8E5D-A71F8BDB3D89} = {F0A1281A-B512-49D2-8362-21EE32B3674F}
505+
{3D6821F5-F242-4828-8DDE-89488E85512D} = {F826BA61-60A9-45B6-AF29-FD1A6E313EF0}
491506
EndGlobalSection
492507
GlobalSection(ExtensibilityGlobals) = postSolution
493508
SolutionGuid = {48207B50-7D05-4B10-B585-890FE0F4FCE1}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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;
5+
using System.Buffers;
6+
using System.IO.Pipelines;
7+
using System.Net;
8+
using System.Net.Sockets;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
using Microsoft.AspNetCore.Connections;
12+
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal;
13+
using Microsoft.Extensions.Logging;
14+
using Microsoft.Extensions.Options;
15+
16+
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Client
17+
{
18+
internal class SocketConnectionFactory : IConnectionFactory, IAsyncDisposable
19+
{
20+
private readonly SocketTransportOptions _options;
21+
private readonly MemoryPool<byte> _memoryPool;
22+
private readonly SocketsTrace _trace;
23+
24+
public SocketConnectionFactory(IOptions<SocketTransportOptions> options, ILoggerFactory loggerFactory)
25+
{
26+
if (options == null)
27+
{
28+
throw new ArgumentNullException(nameof(options));
29+
}
30+
31+
if (loggerFactory == null)
32+
{
33+
throw new ArgumentNullException(nameof(loggerFactory));
34+
}
35+
36+
_options = options.Value;
37+
_memoryPool = options.Value.MemoryPoolFactory();
38+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Client");
39+
_trace = new SocketsTrace(logger);
40+
}
41+
42+
public async ValueTask<ConnectionContext> ConnectAsync(EndPoint endpoint, CancellationToken cancellationToken = default)
43+
{
44+
var ipEndPoint = endpoint as IPEndPoint;
45+
46+
if (ipEndPoint is null)
47+
{
48+
throw new NotSupportedException("The SocketConnectionFactory only supports IPEndPoints for now.");
49+
}
50+
51+
var socket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
52+
{
53+
NoDelay = _options.NoDelay
54+
};
55+
56+
await socket.ConnectAsync(ipEndPoint);
57+
58+
var socketConnection = new SocketConnection(
59+
socket,
60+
_memoryPool,
61+
PipeScheduler.ThreadPool,
62+
_trace,
63+
_options.MaxReadBufferSize,
64+
_options.MaxWriteBufferSize);
65+
66+
socketConnection.Start();
67+
return socketConnection;
68+
}
69+
70+
public ValueTask DisposeAsync()
71+
{
72+
_memoryPool.Dispose();
73+
return default;
74+
}
75+
}
76+
}

src/Servers/Kestrel/Transport.Sockets/src/Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
<Reference Include="Microsoft.Extensions.Options" />
2727
</ItemGroup>
2828

29+
<ItemGroup>
30+
<InternalsVisibleTo Include="http2cat" />
31+
</ItemGroup>
32+
2933
<ItemGroup>
3034
<EmbeddedResource Update="SocketsStrings.resx">
3135
<Generator></Generator>

0 commit comments

Comments
 (0)