-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Replace SocketsHttpConnectionFactory with SocketsConnectionFactory #40506
Merged
Merged
Changes from all commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
9b84cf7
add NetworkException
5b5779d
Update src/libraries/System.Net.Primitives/src/Resources/Strings.resx
geoffkizer 78b6b4d
add proper serialization logic
6b83c4e
add OperationAborted and better XML comments and error strings
298f96d
use NetworkException in SocketsHttpConnectionFactory
b4e10b7
add the API manually for now
antonfirsov 8ab151c
add skeletons
antonfirsov bffcabb
Update src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpH…
geoffkizer b3cfa89
fix casing of constant in test
563f86a
Update src/libraries/System.Net.Primitives/src/Resources/Strings.resx
geoffkizer 11fc432
Update src/libraries/System.Net.Primitives/src/Resources/Strings.resx
geoffkizer 1f8d94b
Address -> EndPoint in enum values
6d7872a
move MapSocketError to a common file and fix some review issues
b2a7b45
add test skeletons
antonfirsov 2185ee5
update NetworkStream to use NetworkException
a120d07
remove SocketConnectionNetworkStream and use NetworkStream directly
0cf0a1a
basic tests
antonfirsov ff9597b
Merge branch 'master' into af/SocketsConnectionFactory
antonfirsov 7785efe
remove InvalidEndPoint
9a17607
remove factory method and update constructor arguments
db769f5
Update src/libraries/System.Net.Primitives/src/System/Net/NetworkExce…
geoffkizer c266425
add OperationAborted to GetExceptionMessage
728d575
WIP
antonfirsov a90566b
compiles
antonfirsov 5f35b31
Merge branch 'master' into af/SocketsConnectionFactory-2
antonfirsov 515f224
revert NetworkException stuff for now
antonfirsov 84cde84
fixes
antonfirsov 3f37507
socket tests succeeding
antonfirsov fa5d17f
Merge branch 'networkexception' into af/SocketsConnectionFactory-2
antonfirsov 9e4ed04
adapt NetworkException
antonfirsov d76bb55
simplify SocketConnection
antonfirsov f3d6b00
adapt SocketsConnectionFactory in SocketsHttpHandler
antonfirsov 1cc6271
Use output from GenerateReferenceSource
antonfirsov 60fbcc1
added docs
antonfirsov edb8332
better comments
antonfirsov 4e3f828
improve cancellation
antonfirsov 93a32a8
Merge branch 'master' into af/SocketsConnectionFactory-2
antonfirsov 24d7b0f
bring back the fix from #40565
antonfirsov 2cd861d
fixing tests
antonfirsov 1b5dcf0
address review findings
antonfirsov 697e816
implement DerivedFactory_CanShimPipe
antonfirsov ec04da2
CustomConnectionFactory_ConnectAsync_CanCaptureHttpRequestMessage
antonfirsov a2dd16d
CloseAsync: call synchronous Stream.Dispose to make sure catching exc…
antonfirsov 6b144a9
prefer OperationCanceledException
antonfirsov 54f1919
Move SocketConnectionFactory to System.Net.Connections
antonfirsov 88a7449
refactor
antonfirsov b8634d0
fix ConnectHelper
antonfirsov 953b65b
address remaining findings
antonfirsov 10b19f2
run Connections.Tests on same platforms as Sockets.Tests
antonfirsov 611e71c
change SocketsConnectionFactory arg nullability
antonfirsov 99a38a1
disable tests on mono for now
antonfirsov c15dec3
address review findings
antonfirsov 9c25a93
tests
antonfirsov 3fc17d0
Connection_Pipe_ReadWrite_Integration
antonfirsov 9091689
better disposal
antonfirsov 0b0dde2
remove Stream and Pipe factory methods from SocketsConnectionFactory
antonfirsov 72d3a21
try to fix test flakyness
antonfirsov 1c9980c
simplify SocketConection
antonfirsov 5e5917b
move Connection_Pipe_ReadWrite_Integration to OuterLoop
antonfirsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
24 changes: 24 additions & 0 deletions
24
src/libraries/System.Net.Connections/src/System/Net/Connections/DuplexStreamPipe.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,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO; | ||
using System.IO.Pipelines; | ||
|
||
namespace System.Net.Connections | ||
{ | ||
internal sealed class DuplexStreamPipe : IDuplexPipe | ||
{ | ||
private static readonly StreamPipeReaderOptions s_readerOpts = new StreamPipeReaderOptions(leaveOpen: true); | ||
private static readonly StreamPipeWriterOptions s_writerOpts = new StreamPipeWriterOptions(leaveOpen: true); | ||
|
||
public DuplexStreamPipe(Stream stream) | ||
{ | ||
Input = PipeReader.Create(stream, s_readerOpts); | ||
Output = PipeWriter.Create(stream, s_writerOpts); | ||
} | ||
|
||
public PipeReader Input { get; } | ||
|
||
public PipeWriter Output { get; } | ||
} | ||
} |
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
137 changes: 137 additions & 0 deletions
137
...ies/System.Net.Connections/src/System/Net/Connections/Sockets/SocketsConnectionFactory.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,137 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO; | ||
using System.IO.Pipelines; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Net.Connections | ||
{ | ||
/// <summary> | ||
/// A <see cref="ConnectionFactory"/> to establish socket-based connections. | ||
/// </summary> | ||
/// <remarks> | ||
/// When constructed with <see cref="ProtocolType.Tcp"/>, this factory will create connections with <see cref="Socket.NoDelay"/> enabled. | ||
/// In case of IPv6 sockets <see cref="Socket.DualMode"/> is also enabled. | ||
/// </remarks> | ||
public class SocketsConnectionFactory : ConnectionFactory | ||
{ | ||
private readonly AddressFamily _addressFamily; | ||
private readonly SocketType _socketType; | ||
private readonly ProtocolType _protocolType; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SocketsConnectionFactory"/> class. | ||
/// </summary> | ||
/// <param name="addressFamily">The <see cref="AddressFamily"/> to forward to the socket.</param> | ||
/// <param name="socketType">The <see cref="SocketType"/> to forward to the socket.</param> | ||
/// <param name="protocolType">The <see cref="ProtocolType"/> to forward to the socket.</param> | ||
public SocketsConnectionFactory( | ||
AddressFamily addressFamily, | ||
SocketType socketType, | ||
ProtocolType protocolType) | ||
{ | ||
_addressFamily = addressFamily; | ||
_socketType = socketType; | ||
_protocolType = protocolType; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SocketsConnectionFactory"/> class | ||
/// that will forward <see cref="AddressFamily.InterNetworkV6"/> to the Socket constructor. | ||
/// </summary> | ||
/// <param name="socketType">The <see cref="SocketType"/> to forward to the socket.</param> | ||
/// <param name="protocolType">The <see cref="ProtocolType"/> to forward to the socket.</param> | ||
/// <remarks>The created socket will be an IPv6 socket with <see cref="Socket.DualMode"/> enabled.</remarks> | ||
public SocketsConnectionFactory(SocketType socketType, ProtocolType protocolType) | ||
: this(AddressFamily.InterNetworkV6, socketType, protocolType) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <exception cref="ArgumentNullException">When <paramref name="endPoint"/> is <see langword="null"/>.</exception> | ||
public override async ValueTask<Connection> ConnectAsync( | ||
EndPoint? endPoint, | ||
IConnectionProperties? options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (endPoint == null) throw new ArgumentNullException(nameof(endPoint)); | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
Socket socket = CreateSocket(_addressFamily, _socketType, _protocolType, endPoint, options); | ||
|
||
try | ||
{ | ||
using var args = new TaskSocketAsyncEventArgs(); | ||
args.RemoteEndPoint = endPoint; | ||
|
||
if (socket.ConnectAsync(args)) | ||
{ | ||
using (cancellationToken.UnsafeRegister(static o => Socket.CancelConnectAsync((SocketAsyncEventArgs)o!), args)) | ||
{ | ||
await args.Task.ConfigureAwait(false); | ||
} | ||
} | ||
|
||
if (args.SocketError != SocketError.Success) | ||
{ | ||
if (args.SocketError == SocketError.OperationAborted) | ||
{ | ||
antonfirsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cancellationToken.ThrowIfCancellationRequested(); | ||
} | ||
|
||
throw NetworkErrorHelper.MapSocketException(new SocketException((int)args.SocketError)); | ||
} | ||
|
||
return new SocketConnection(socket); | ||
} | ||
catch (SocketException socketException) | ||
{ | ||
socket.Dispose(); | ||
throw NetworkErrorHelper.MapSocketException(socketException); | ||
} | ||
catch | ||
{ | ||
socket.Dispose(); | ||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates the socket that shall be used with the connection. | ||
/// </summary> | ||
/// <param name="addressFamily">The <see cref="AddressFamily"/> to forward to the socket.</param> | ||
/// <param name="socketType">The <see cref="SocketType"/> to forward to the socket.</param> | ||
/// <param name="protocolType">The <see cref="ProtocolType"/> to forward to the socket.</param> | ||
/// <param name="endPoint">The <see cref="EndPoint"/> this socket will be connected to.</param> | ||
/// <param name="options">Properties, if any, that might change how the socket is initialized.</param> | ||
/// <returns>A new unconnected <see cref="Socket"/>.</returns> | ||
/// <remarks> | ||
/// In case of TCP sockets, the default implementation of this method will create a socket with <see cref="Socket.NoDelay"/> enabled. | ||
/// In case of IPv6 sockets <see cref="Socket.DualMode"/> is also be enabled. | ||
/// </remarks> | ||
protected virtual Socket CreateSocket( | ||
AddressFamily addressFamily, | ||
SocketType socketType, | ||
ProtocolType protocolType, | ||
EndPoint? endPoint, | ||
IConnectionProperties? options) | ||
{ | ||
Socket socket = new Socket(addressFamily, socketType, protocolType); | ||
|
||
if (protocolType == ProtocolType.Tcp) | ||
{ | ||
socket.NoDelay = true; | ||
} | ||
|
||
if (addressFamily == AddressFamily.InterNetworkV6) | ||
{ | ||
socket.DualMode = true; | ||
} | ||
|
||
return socket; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this needed for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It contains
Win32Exception
which is the subclass ofSocketException
and also somehow referenced byNetworkException
. Nothing builds without it, IDK why isn't the reference transitive throughSystem.Net.Primitives
.