客户端在线计数

Swap增加超时取消等待
This commit is contained in:
ioxygen 2021-08-17 22:26:48 +08:00
parent 2a3a7323eb
commit 56bd3ecf50
7 changed files with 42 additions and 10 deletions

View File

@ -21,6 +21,8 @@ namespace FastTunnel.Core.Client
public ConcurrentDictionary<string, WebInfo> WebList { get; private set; } = new();
public int ConnectedClientCount = 0;
public ConcurrentDictionary<int, ForwardInfo<ForwardHandlerArg>> ForwardList { get; private set; }
= new ConcurrentDictionary<int, ForwardInfo<ForwardHandlerArg>>();

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FastTunnel.Core.Extensions
{
public static class TaskCompletionSourceExtensions
{
public static void SetTimeOut<T>(this TaskCompletionSource<T> tcs, int timeoutMs, Action action)
{
var ct = new CancellationTokenSource(timeoutMs);
ct.Token.Register(() =>
{
tcs.TrySetCanceled();
action.Invoke();
}, useSynchronizationContext: false);
}
}
}

View File

@ -63,11 +63,14 @@ namespace FastTunnel.Core.Forwarder
// 发送指令给客户端,等待建立隧道
await web.Socket.SendCmdAsync(MessageType.SwapMsg, $"{RequestId}|{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}", cancellation);
// TODO:超时处理
TaskCompletionSource<Stream> task = new(cancellation);
_fastTunnelServer.ResponseTasks.TryAdd(RequestId, task);
TaskCompletionSource<Stream> tcs = new(cancellation);
tcs.SetTimeOut(10000, () =>
{
_logger.LogError($"客户端在指定时间内为建立Swap连接 {RequestId}|{host}=>{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}");
});
var res = await task.Task;
_fastTunnelServer.ResponseTasks.TryAdd(RequestId, tcs);
var res = await tcs.Task;
return res;
}
catch (Exception ex)

View File

@ -64,12 +64,14 @@ namespace FastTunnel.Core.MiddleWares
try
{
logger.LogInformation($"客户端连接 {context.TraceIdentifier}:{context.Connection.RemoteIpAddress}");
Interlocked.Increment(ref fastTunnelServer.ConnectedClientCount);
logger.LogInformation($"客户端连接 {context.TraceIdentifier}:{context.Connection.RemoteIpAddress} 当前在线数:{fastTunnelServer.ConnectedClientCount}");
await tunnelClient.ReviceAsync(CancellationToken.None);
}
catch (Exception)
{
logger.LogInformation($"客户端关闭 {context.TraceIdentifier}:{context.Connection.RemoteIpAddress}");
Interlocked.Decrement(ref fastTunnelServer.ConnectedClientCount);
logger.LogInformation($"客户端关闭 {context.TraceIdentifier}:{context.Connection.RemoteIpAddress} 当前在线数:{fastTunnelServer.ConnectedClientCount}");
}
}

View File

@ -38,6 +38,11 @@ namespace FastTunnel.Core.Dispatchers
try
{
if (client.State == WebSocketState.Aborted)
{
logger.LogError("客户端已离线");
return;
}
await client.SendCmdAsync(MessageType.Forward, $"{msgid}|{_config.LocalIp}:{_config.LocalPort}", CancellationToken.None);
}
@ -57,7 +62,7 @@ namespace FastTunnel.Core.Dispatchers
}
catch (Exception ex)
{
logger.LogError(ex);
logger.LogError("Forward Swap Error" + ex.Message);
}
}
}

View File

@ -110,7 +110,6 @@ namespace FastTunnel.Core {
/// <summary>
/// 查找类似 &lt;!DOCTYPE html&gt;
///&lt;html lang=&quot;en&quot;&gt;
///
///&lt;head&gt;
/// &lt;meta charset=&quot;utf-8&quot; /&gt;
/// &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
@ -128,7 +127,7 @@ namespace FastTunnel.Core {
///
/// .btn-primary {
/// color: #fff;
/// background-color [字符串的其余部分被截断]&quot;; 的本地化字符串。
/// background-color: [字符串的其余部分被截断]&quot;; 的本地化字符串。
/// </summary>
internal static string Page_NoSite {
get {

View File

@ -242,7 +242,6 @@
<data name="Page_NoSite" xml:space="preserve">
<value>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8" /&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;