Skip to content

Commit 64a12f7

Browse files
committed
feat: for #637, ping with ipv6 detect.
1 parent d99630c commit 64a12f7

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Ui/Utils/TcpHelper.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net;
45
using System.Net.Sockets;
56
using System.Text;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Shawn.Utils;
10+
using System.Net.NetworkInformation;
911

1012
namespace _1RM.Utils
1113
{
1214
public static class TcpHelper
1315
{
16+
public static (bool, IPAddress?) IsIPv6(string address)
17+
{
18+
if (IPAddress.TryParse(address, out var ipAddress))
19+
{
20+
return (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6, ipAddress);
21+
}
22+
return (false, null);
23+
}
24+
1425
/// <summary>
1526
/// return true if connected, false if not connected, null if timeout or cancelled.
1627
/// </summary>
1728
public static async Task<bool?> TestConnectionAsync(string address, int port, CancellationToken? cancellationToken = null, int timeOutMillisecond = 0)
1829
{
19-
using var client = new TcpClient();
30+
var (isIPv6, iPv6) = IsIPv6(address);
31+
using var client = isIPv6 ? new TcpClient(iPv6!.AddressFamily) : new TcpClient();
2032
try
2133
{
2234
var cts = new CancellationTokenSource();
2335
cancellationToken ??= cts.Token;
2436
#if NETCOREAPP
2537
var connectTask = client.ConnectAsync(address, port, (CancellationToken)cancellationToken).AsTask();
2638
#else
27-
var connectTask = client.ConnectAsync(address, port);
39+
var connectTask = isIPv6 ? client.ConnectAsync(iPv6, port) : client.ConnectAsync(address, port);
2840
#endif
2941
if (timeOutMillisecond <= 0)
3042
timeOutMillisecond = 30 * 1000;

0 commit comments

Comments
 (0)