mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
调整TCP连接数为实时连接数
This commit is contained in:
parent
9ebc8fbcc6
commit
42766d1294
|
@ -18,6 +18,11 @@
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<PackageReleaseNotes>FastTunnel.Core</PackageReleaseNotes>
|
<PackageReleaseNotes>FastTunnel.Core</PackageReleaseNotes>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Remove="Listener\PortProxyListenerV2.cs" />
|
||||||
|
<Compile Remove="Listener\TcpServerHandler.cs" />
|
||||||
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
using FastTunnel.Core.Client;
|
using FastTunnel.Core.Client;
|
||||||
using FastTunnel.Core.Exceptions;
|
using FastTunnel.Core.Exceptions;
|
||||||
using FastTunnel.Core.Extensions;
|
using FastTunnel.Core.Extensions;
|
||||||
|
using FastTunnel.Core.Listener;
|
||||||
using FastTunnel.Core.Models;
|
using FastTunnel.Core.Models;
|
||||||
using FastTunnel.Core.Sockets;
|
using FastTunnel.Core.Sockets;
|
||||||
using Microsoft.AspNetCore.Hosting.Server;
|
using Microsoft.AspNetCore.Hosting.Server;
|
||||||
|
@ -41,7 +42,7 @@ namespace FastTunnel.Core.Handlers.Server
|
||||||
/// <param name="_socket">用户请求</param>
|
/// <param name="_socket">用户请求</param>
|
||||||
/// <param name="client">FastTunnel客户端</param>
|
/// <param name="client">FastTunnel客户端</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task DispatchAsync(Socket _socket, WebSocket client)
|
public async Task DispatchAsync(Socket _socket, WebSocket client, PortProxyListener listener)
|
||||||
{
|
{
|
||||||
var msgId = Guid.NewGuid().ToString().Replace("-", "");
|
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}");
|
logger.LogDebug($"[Forward]Swap开始 {msgId}|{_config.RemotePort}=>{_config.LocalIp}:{_config.LocalPort}");
|
||||||
|
|
||||||
var tcs = new TaskCompletionSource<Stream>();
|
var tcs = new TaskCompletionSource<Stream>();
|
||||||
tcs.SetTimeOut(10000, () => { logger.LogDebug($"[Dispatch TimeOut]:{msgId}"); });
|
|
||||||
|
|
||||||
_server.ResponseTasks.TryAdd(msgId, (tcs, CancellationToken.None));
|
_server.ResponseTasks.TryAdd(msgId, (tcs, CancellationToken.None));
|
||||||
|
|
||||||
|
@ -76,13 +76,11 @@ namespace FastTunnel.Core.Handlers.Server
|
||||||
return;
|
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 })
|
using (var stream2 = new NetworkStream(_socket, true) { ReadTimeout = 1000 * 60 * 10 })
|
||||||
{
|
{
|
||||||
await Task.WhenAny(stream1.CopyToAsync(stream2), stream2.CopyToAsync(stream1));
|
await Task.WhenAny(stream1.CopyToAsync(stream2), stream2.CopyToAsync(stream1));
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.LogDebug($"[Forward]Swap OK {msgId}");
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -90,7 +88,9 @@ namespace FastTunnel.Core.Handlers.Server
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
logger.LogDebug($"[Forward]Swap结束 {msgId}");
|
||||||
_server.ResponseTasks.TryRemove(msgId, out _);
|
_server.ResponseTasks.TryRemove(msgId, out _);
|
||||||
|
listener.DecrementClients();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,19 +74,16 @@ namespace FastTunnel.Core.Listener
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ProcessAcceptAsync(SocketAsyncEventArgs e)
|
private async void ProcessAcceptAsync(SocketAsyncEventArgs e)
|
||||||
{
|
{
|
||||||
if (e.SocketError == SocketError.Success)
|
if (e.SocketError == SocketError.Success)
|
||||||
{
|
{
|
||||||
var accept = e.AcceptSocket;
|
var accept = e.AcceptSocket;
|
||||||
|
|
||||||
Interlocked.Increment(ref m_numConnectedSockets);
|
IncrementClients();
|
||||||
|
|
||||||
_logerr.LogInformation($"【{ListenIp}:{ListenPort}】Accepted. There are {{0}} clients connected to the port",
|
|
||||||
m_numConnectedSockets);
|
|
||||||
|
|
||||||
// 将此客户端交由Dispatcher进行管理
|
// 将此客户端交由Dispatcher进行管理
|
||||||
_requestDispatcher.DispatchAsync(accept, client);
|
_requestDispatcher.DispatchAsync(accept, client, this);
|
||||||
|
|
||||||
// Accept the next connection request
|
// Accept the next connection request
|
||||||
StartAccept(e);
|
StartAccept(e);
|
||||||
|
@ -123,5 +120,19 @@ namespace FastTunnel.Core.Listener
|
||||||
listenSocket.Close();
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user