Skip to content
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

add support for parallel Connect #106374

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -102,6 +102,9 @@ internal class PortBlocker : IDisposable
private const int MaxAttempts = 16;
private Socket _shadowSocket;
public Socket MainSocket { get; }
public Socket SecondarySocket => _shadowSocket;

public int Port;

public PortBlocker(Func<Socket> socketFactory)
{
Expand All @@ -126,7 +129,11 @@ public PortBlocker(Func<Socket> socketFactory)
_shadowSocket = new Socket(shadowAddress.AddressFamily, MainSocket.SocketType, MainSocket.ProtocolType);
success = TryBindWithoutReuseAddress(_shadowSocket, shadowEndPoint, out _);

if (success) break;
if (success)
{
Port = port;
break;
}
}
catch (SocketException)
{
Expand Down
6 changes: 6 additions & 0 deletions src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

namespace System.Net.Sockets
{
public enum ConnectAlgorithm
{
Default = 0,
Parallel = 1,
}
public enum IOControlCode : long
{
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
Expand Down Expand Up @@ -343,6 +348,7 @@ public void Connect(string host, int port) { }
public System.Threading.Tasks.ValueTask ConnectAsync(System.Net.IPAddress[] addresses, int port, System.Threading.CancellationToken cancellationToken) { throw null; }
public bool ConnectAsync(System.Net.Sockets.SocketAsyncEventArgs e) { throw null; }
public static bool ConnectAsync(System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType, System.Net.Sockets.SocketAsyncEventArgs e) { throw null; }
public static bool ConnectAsync(System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType, System.Net.Sockets.SocketAsyncEventArgs e, System.Net.Sockets.ConnectAlgorithm connectAlgorithm) { throw null; }
public System.Threading.Tasks.Task ConnectAsync(string host, int port) { throw null; }
public System.Threading.Tasks.ValueTask ConnectAsync(string host, int port, System.Threading.CancellationToken cancellationToken) { throw null; }
public void Disconnect(bool reuseSocket) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<!-- All configurations -->
<Compile Include="System\Net\Sockets\ConnectAlgorithm.cs" />
<Compile Include="System\Net\Sockets\SocketReceiveFromResult.cs" />
<Compile Include="System\Net\Sockets\SocketReceiveMessageFromResult.cs" />
<Compile Include="System\Net\Sockets\SocketsTelemetry.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Net.Sockets
{
// Defines constants used by the Socket.Shutdown method.
public enum ConnectAlgorithm
{
// defaul mechanism e.g. sequential processing
Default = 0,

// use a Happy Eyeballs-like algorithm to connect.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Please use XML comments

Parallel = 1,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2823,7 +2823,9 @@ internal bool ConnectAsync(SocketAsyncEventArgs e, bool userSocket, bool saeaCan
return pending;
}

public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e)
public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) =>
ConnectAsync(socketType, protocolType, e, ConnectAlgorithm.Default);
public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e, ConnectAlgorithm connectAlgorithm)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validate that connectAlgorithm is one of the two known values?

{
ArgumentNullException.ThrowIfNull(e);

Expand All @@ -2847,7 +2849,7 @@ public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType
e.StartOperationConnect(saeaMultiConnectCancelable: true, userSocket: false);
try
{
pending = e.DnsConnectAsync(dnsEP, socketType, protocolType);
pending = e.DnsConnectAsync(dnsEP, socketType, protocolType, connectAlgorithm);
}
catch
{
Expand Down
Loading
Loading