Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 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
3 changes: 3 additions & 0 deletions src/libraries/System.Net.Sockets/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,7 @@
<data name="net_sockets_address_small" xml:space="preserve">
<value>Provided SocketAddress is too small for given AddressFamily.</value>
</data>
<data name="net_sockets_invalid_connect_algorithm" xml:space="preserve">
<value>Provided ConnectAlgorithm {0} is not valid.</value>
</data>
</root>
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,21 @@
// 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
{
/// <summary>
/// Specifies the algorithm used to establish a socket connection.
/// </summary>
public enum ConnectAlgorithm
{
/// <summary>
/// The default connection mechanism, typically sequential processing.
/// </summary>
Default = 0,

/// <summary>
/// Uses a Happy Eyeballs-like algorithm to connect, attempting connections in parallel to improve speed and reliability.
/// </summary>
Parallel = 1,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2977,9 +2977,16 @@ 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)
{
ArgumentNullException.ThrowIfNull(e);
if (connectAlgorithm != ConnectAlgorithm.Default &&
connectAlgorithm != ConnectAlgorithm.Parallel)
{
throw new ArgumentException(SR.Format(SR.net_sockets_invalid_connect_algorithm, connectAlgorithm), nameof(connectAlgorithm));
}

if (e.HasMultipleBuffers)
{
Expand All @@ -3001,7 +3008,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