mirror of
https://github.com/2dust/v2rayN.git
synced 2026-09-22 22:38:10 +03:00
Optimize download and speedtest (#10147)
* Optimize RunMixedTestAsync * Use stopCts instead of exitLoopKey * Refactor
This commit is contained in:
@@ -11,7 +11,7 @@ public class Socks5UdpChannel(string socks5Host, int socks5TcpPort) : IDisposabl
|
||||
/// <summary>
|
||||
/// Send UDP data to a remote endpoint (IP address)
|
||||
/// </summary>
|
||||
public async Task SendAsync(IPEndPoint remote, byte[] data)
|
||||
public async Task SendAsync(IPEndPoint remote, byte[] data, CancellationToken ct = default)
|
||||
{
|
||||
var addrData = new Socks5AddressData
|
||||
{
|
||||
@@ -22,7 +22,7 @@ public class Socks5UdpChannel(string socks5Host, int socks5TcpPort) : IDisposabl
|
||||
Port = (ushort)remote.Port
|
||||
};
|
||||
var packet = BuildSocks5UdpPacket(addrData, data);
|
||||
await _udpClient.SendAsync(packet, packet.Length, _relayEndPoint);
|
||||
await _udpClient.SendAsync(packet.AsMemory(), _relayEndPoint, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -31,7 +31,8 @@ public class Socks5UdpChannel(string socks5Host, int socks5TcpPort) : IDisposabl
|
||||
/// <param name="host">Domain name or IP address</param>
|
||||
/// <param name="port">Port number</param>
|
||||
/// <param name="data">Data to send</param>
|
||||
public async Task SendAsync(string host, ushort port, byte[] data)
|
||||
/// <param name="ct">Cancellation token</param>
|
||||
public async Task SendAsync(string host, ushort port, byte[] data, CancellationToken ct = default)
|
||||
{
|
||||
var addrData = new Socks5AddressData();
|
||||
|
||||
@@ -53,7 +54,8 @@ public class Socks5UdpChannel(string socks5Host, int socks5TcpPort) : IDisposabl
|
||||
addrData.Port = port;
|
||||
|
||||
var packet = BuildSocks5UdpPacket(addrData, data);
|
||||
await _udpClient.SendAsync(packet, packet.Length, _relayEndPoint);
|
||||
//await _udpClient.SendAsync(packet, packet.Length, _relayEndPoint);
|
||||
await _udpClient.SendAsync(packet.AsMemory(), _relayEndPoint, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -88,17 +88,18 @@ public class UdpTestService
|
||||
return (targetServerHost, _udpTest.GetDefaultTargetPort());
|
||||
}
|
||||
|
||||
public async Task<TimeSpan> SendUdpRequestAsync(string targetServerHost, int socks5Port, TimeSpan operationTimeout)
|
||||
public async Task<TimeSpan> SendUdpRequestAsync(string targetServerHost, int socks5Port, CancellationToken ct = default)
|
||||
{
|
||||
using var cts = new CancellationTokenSource(operationTimeout);
|
||||
var cancellationToken = cts.Token;
|
||||
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, timeoutCts.Token);
|
||||
var linkedCt = linkedCts.Token;
|
||||
var udpRequestPacket = _udpTest.BuildUdpRequestPacket();
|
||||
if (udpRequestPacket == null || udpRequestPacket.Length == 0)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to build UDP request packet.");
|
||||
}
|
||||
using var channel = new Socks5UdpChannel("127.0.0.1", socks5Port);
|
||||
if (!await channel.EstablishUdpAssociationAsync(cancellationToken).ConfigureAwait(false))
|
||||
if (!await channel.EstablishUdpAssociationAsync(linkedCt).ConfigureAwait(false))
|
||||
{
|
||||
throw new Exception("Failed to establish UDP association with SOCKS5 proxy.");
|
||||
}
|
||||
@@ -116,8 +117,8 @@ public class UdpTestService
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
await channel.SendAsync(targetHost, targetPort, udpRequestPacket).ConfigureAwait(false);
|
||||
var (_, receiveResult) = await channel.ReceiveAsync(cancellationToken).ConfigureAwait(false);
|
||||
await channel.SendAsync(targetHost, targetPort, udpRequestPacket, linkedCt).ConfigureAwait(false);
|
||||
var (_, receiveResult) = await channel.ReceiveAsync(linkedCt).ConfigureAwait(false);
|
||||
stopwatch.Stop();
|
||||
|
||||
udpReceiveResult = receiveResult;
|
||||
|
||||
Reference in New Issue
Block a user