diff --git a/FastTunnel.Core/FastTunnel.Core.csproj b/FastTunnel.Core/FastTunnel.Core.csproj
index e33ca86..edbc1a6 100644
--- a/FastTunnel.Core/FastTunnel.Core.csproj
+++ b/FastTunnel.Core/FastTunnel.Core.csproj
@@ -18,6 +18,11 @@
true
FastTunnel.Core
+
+
+
+
+
diff --git a/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs b/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs
index 119ee56..01d9263 100644
--- a/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs
+++ b/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs
@@ -7,6 +7,7 @@
using FastTunnel.Core.Client;
using FastTunnel.Core.Exceptions;
using FastTunnel.Core.Extensions;
+using FastTunnel.Core.Listener;
using FastTunnel.Core.Models;
using FastTunnel.Core.Sockets;
using Microsoft.AspNetCore.Hosting.Server;
@@ -41,7 +42,7 @@ namespace FastTunnel.Core.Handlers.Server
/// 用户请求
/// FastTunnel客户端
///
- public async Task DispatchAsync(Socket _socket, WebSocket client)
+ public async Task DispatchAsync(Socket _socket, WebSocket client, PortProxyListener listener)
{
var msgId = Guid.NewGuid().ToString().Replace("-", "");
@@ -51,7 +52,6 @@ namespace FastTunnel.Core.Handlers.Server
logger.LogDebug($"[Forward]Swap开始 {msgId}|{_config.RemotePort}=>{_config.LocalIp}:{_config.LocalPort}");
var tcs = new TaskCompletionSource();
- tcs.SetTimeOut(10000, () => { logger.LogDebug($"[Dispatch TimeOut]:{msgId}"); });
_server.ResponseTasks.TryAdd(msgId, (tcs, CancellationToken.None));
@@ -76,13 +76,11 @@ namespace FastTunnel.Core.Handlers.Server
return;
}
- using (var stream1 = await tcs.Task)
+ using (var stream1 = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(10)))
using (var stream2 = new NetworkStream(_socket, true) { ReadTimeout = 1000 * 60 * 10 })
{
await Task.WhenAny(stream1.CopyToAsync(stream2), stream2.CopyToAsync(stream1));
}
-
- logger.LogDebug($"[Forward]Swap OK {msgId}");
}
catch (Exception ex)
{
@@ -90,7 +88,9 @@ namespace FastTunnel.Core.Handlers.Server
}
finally
{
+ logger.LogDebug($"[Forward]Swap结束 {msgId}");
_server.ResponseTasks.TryRemove(msgId, out _);
+ listener.DecrementClients();
}
}
diff --git a/FastTunnel.Core/Listener/PortProxyListener.cs b/FastTunnel.Core/Listener/PortProxyListener.cs
index 1cf33c5..3fef3bd 100644
--- a/FastTunnel.Core/Listener/PortProxyListener.cs
+++ b/FastTunnel.Core/Listener/PortProxyListener.cs
@@ -74,19 +74,16 @@ namespace FastTunnel.Core.Listener
}
}
- private void ProcessAcceptAsync(SocketAsyncEventArgs e)
+ private async void ProcessAcceptAsync(SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
var accept = e.AcceptSocket;
- Interlocked.Increment(ref m_numConnectedSockets);
-
- _logerr.LogInformation($"【{ListenIp}:{ListenPort}】Accepted. There are {{0}} clients connected to the port",
- m_numConnectedSockets);
+ IncrementClients();
// 将此客户端交由Dispatcher进行管理
- _requestDispatcher.DispatchAsync(accept, client);
+ _requestDispatcher.DispatchAsync(accept, client, this);
// Accept the next connection request
StartAccept(e);
@@ -123,5 +120,19 @@ namespace FastTunnel.Core.Listener
listenSocket.Close();
}
}
+
+ internal void IncrementClients()
+ {
+ Interlocked.Increment(ref m_numConnectedSockets);
+ _logerr.LogInformation($"[Listener:{ListenPort}] Accepted. There are {{0}} clients connected", m_numConnectedSockets);
+
+ }
+
+ internal void DecrementClients()
+ {
+ Interlocked.Decrement(ref m_numConnectedSockets);
+ _logerr.LogInformation($"[Listener:{ListenPort}] DisConnet. There are {{0}} clients connecting", m_numConnectedSockets);
+
+ }
}
}