mirror of
https://github.com/snltty/linker.git
synced 2025-10-20 07:54:36 +08:00
sync
This commit is contained in:
@@ -118,10 +118,8 @@ namespace linker.tunnel
|
||||
/// <returns></returns>
|
||||
public async Task<ITunnelConnection> ConnectAsync(string remoteMachineId, string transactionId)
|
||||
{
|
||||
if (connectingDic.TryAdd(remoteMachineId, true) == false)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (connectingDic.TryAdd(remoteMachineId, true) == false) return null;
|
||||
if (IsBackground(remoteMachineId, transactionId)) return null;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -221,7 +219,6 @@ namespace linker.tunnel
|
||||
{
|
||||
connectingDic.TryRemove(remoteMachineId, out _);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
/// <summary>
|
||||
@@ -393,5 +390,54 @@ namespace linker.tunnel
|
||||
|
||||
tunnelTransportInfo.RemoteEndPoints = eps;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private ConcurrentDictionary<string, bool> backgroundDic = new ConcurrentDictionary<string, bool>();
|
||||
public void StartBackground(string remoteMachineId, string transactionId)
|
||||
{
|
||||
if (IsBackground(remoteMachineId, transactionId)) return;
|
||||
AddBackground(remoteMachineId, transactionId);
|
||||
Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
await Task.Delay(3000);
|
||||
|
||||
ITunnelConnection connection = await ConnectAsync(remoteMachineId, transactionId);
|
||||
if (connection != null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
RemoveBackground(remoteMachineId, transactionId);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
private void AddBackground(string remoteMachineId, string transactionId)
|
||||
{
|
||||
backgroundDic.TryAdd(GetBackgroundKey(remoteMachineId, transactionId), true);
|
||||
}
|
||||
private void RemoveBackground(string remoteMachineId, string transactionId)
|
||||
{
|
||||
backgroundDic.TryRemove(GetBackgroundKey(remoteMachineId, transactionId), out _);
|
||||
}
|
||||
private bool IsBackground(string remoteMachineId, string transactionId)
|
||||
{
|
||||
return backgroundDic.ContainsKey(GetBackgroundKey(remoteMachineId, transactionId));
|
||||
}
|
||||
private string GetBackgroundKey(string remoteMachineId, string transactionId)
|
||||
{
|
||||
return $"{remoteMachineId}@{transactionId}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user