replace listener by async

This commit is contained in:
SpringHgui 2020-06-29 11:17:26 +08:00
parent 938f7f1ca2
commit c9f3768797
8 changed files with 67 additions and 133 deletions

View File

@ -20,7 +20,7 @@
"LocalIp": "127.0.0.1", "LocalIp": "127.0.0.1",
// //
"LocalPort": 80, "LocalPort": 8001,
// , 访url http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/ // , 访url http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/
"SubDomain": "test" // test.test.cc "SubDomain": "test" // test.test.cc

View File

@ -4,6 +4,8 @@ using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FastTunnel.Core namespace FastTunnel.Core
{ {
@ -15,10 +17,13 @@ namespace FastTunnel.Core
public int Port { get; set; } public int Port { get; set; }
Action<Socket, T> handler; Action<Socket, T> receiveClient;
Socket listener; Socket listener;
T _data; T _data;
// Thread signal.
public ManualResetEvent allDone = new ManualResetEvent(false);
public AsyncListener(string ip, int port, ILogger<object> logerr, T data) public AsyncListener(string ip, int port, ILogger<object> logerr, T data)
{ {
_logerr = logerr; _logerr = logerr;
@ -37,36 +42,39 @@ namespace FastTunnel.Core
{ {
// example https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example // example https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example
// Bind the socket to the local endpoint and listen for incoming connections. // Bind the socket to the local endpoint and listen for incoming connections.
try this.receiveClient = receiveClient;
{
listener.Listen(100);
while (true) Task.Run(() =>
{
try
{ {
// Set the event to nonsignaled state. listener.Listen(100);
//allDone.Reset();
// Start an asynchronous socket to listen for connections. while (true)
Console.WriteLine("Waiting for a connection..."); {
listener.BeginAccept( // Set the event to nonsignaled state.
new AsyncCallback(AcceptCallback), allDone.Reset();
listener);
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
// Wait until a connection is made before continuing.
//allDone.WaitOne();
} }
catch (Exception e)
} {
catch (Exception e) Console.WriteLine(e.ToString());
{ }
Console.WriteLine(e.ToString()); });
}
} }
void AcceptCallback(IAsyncResult ar) void AcceptCallback(IAsyncResult ar)
{ {
// Signal the main thread to continue. // Signal the main thread to continue.
//allDone.Set(); allDone.Set();
// Get the socket that handles the client request. // Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState; Socket listener = (Socket)ar.AsyncState;
@ -75,7 +83,10 @@ namespace FastTunnel.Core
// Create the state object. // Create the state object.
StateObject state = new StateObject(); StateObject state = new StateObject();
state.workSocket = handler; state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
receiveClient.Invoke(handler, _data);
//handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
} }
void ReadCallback(IAsyncResult ar) void ReadCallback(IAsyncResult ar)
@ -93,8 +104,7 @@ namespace FastTunnel.Core
if (bytesRead > 0) if (bytesRead > 0)
{ {
// There might be more data, so store the data received so far. // There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString( state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read // Check for end-of-file tag. If it is not there, read
// more data. // more data.
@ -106,6 +116,7 @@ namespace FastTunnel.Core
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
content.Length, content); content.Length, content);
// Echo the data back to the client. // Echo the data back to the client.
Send(handler, content); Send(handler, content);
} }
else else
@ -130,22 +141,33 @@ namespace FastTunnel.Core
{ {
try try
{ {
// Retrieve the socket from the state object. // Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState; Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device. // Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar); int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent); Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());
} }
} }
public void ShutdownAndClose()
{
try
{
listener.Shutdown(SocketShutdown.Both);
}
catch (Exception ex)
{
}
finally
{
listener.Close();
}
}
} }
public class StateObject public class StateObject

View File

@ -51,14 +51,14 @@ namespace FastTunnel.Core.Core
private void ListenFastTunnelClient() private void ListenFastTunnelClient()
{ {
IListener<object> listener = new Listener<object>(_serverSettings.BindAddr, _serverSettings.BindPort, _logger, null); IListener<object> listener = new AsyncListener<object>(_serverSettings.BindAddr, _serverSettings.BindPort, _logger, null);
listener.Listen(ReceiveClient); listener.Listen(ReceiveClient);
_logger.LogDebug($"监听客户端 -> {_serverSettings.BindAddr}:{_serverSettings.BindPort}"); _logger.LogDebug($"监听客户端 -> {_serverSettings.BindAddr}:{_serverSettings.BindPort}");
} }
private void ListenCustomer() private void ListenCustomer()
{ {
var listener = new Listener<object>(_serverSettings.BindAddr, _serverSettings.ProxyPort_HTTP, _logger, null); var listener = new AsyncListener<object>(_serverSettings.BindAddr, _serverSettings.ProxyPort_HTTP, _logger, null);
listener.Listen(ReceiveCustomer); listener.Listen(ReceiveCustomer);
_logger.LogDebug($"监听HTTP -> {_serverSettings.BindAddr}:{_serverSettings.ProxyPort_HTTP}"); _logger.LogDebug($"监听HTTP -> {_serverSettings.BindAddr}:{_serverSettings.ProxyPort_HTTP}");

View File

@ -11,6 +11,8 @@
<Compile Remove="Core\SuiDaoServer.cs.BASE.cs" /> <Compile Remove="Core\SuiDaoServer.cs.BASE.cs" />
<Compile Remove="Core\SuiDaoServer.cs.LOCAL.cs" /> <Compile Remove="Core\SuiDaoServer.cs.LOCAL.cs" />
<Compile Remove="Core\SuiDaoServer.cs.REMOTE.cs" /> <Compile Remove="Core\SuiDaoServer.cs.REMOTE.cs" />
<Compile Remove="Listener.cs" />
<Compile Remove="Server.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -25,6 +27,7 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog" Version="4.6.8" /> <PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" /> <PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
<PackageReference Include="System.Private.ServiceModel" Version="4.7.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -91,9 +91,10 @@ namespace FastTunnel.Core.Handlers
server.SSHList.Remove(item.RemotePort); server.SSHList.Remove(item.RemotePort);
} }
var ls = new Listener<SSHHandlerArg>("0.0.0.0", item.RemotePort, _logger, var ls = new AsyncListener<SSHHandlerArg>("0.0.0.0", item.RemotePort, _logger,
new SSHHandlerArg { LocalClient = client, SSHConfig = item }); new SSHHandlerArg { LocalClient = client, SSHConfig = item });
ls.Listen((client, local) => ls.Listen((client, local) =>
{ {
var msgid = Guid.NewGuid().ToString(); var msgid = Guid.NewGuid().ToString();

View File

@ -7,6 +7,12 @@ namespace FastTunnel.Core
{ {
public interface IListener<T> public interface IListener<T>
{ {
string IP { get; set; }
int Port { get; set; }
void Listen(Action<Socket, T> receiveClient); void Listen(Action<Socket, T> receiveClient);
void ShutdownAndClose();
} }
} }

View File

@ -1,98 +0,0 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using FastTunnel.Core.Extensions;
namespace FastTunnel.Core
{
[Obsolete("Replaced by AsyncListener")]
public class Listener<T> : IListener<T>
{
ILogger<object> _logerr;
public string IP { get; set; }
public int Port { get; set; }
Action<Socket, T> handler;
Socket ls;
T _data;
public Listener(string ip, int port, ILogger<object> logerr, T data)
{
_logerr = logerr;
_data = data;
this.IP = ip;
this.Port = port;
IPAddress ipa = IPAddress.Parse(IP);
IPEndPoint ipe = new IPEndPoint(ipa, Port);
ls = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ls.Bind(ipe);
}
public void Listen(Action<Socket, T> receiveClient)
{
this.handler = receiveClient;
ls.Listen(100);
ThreadPool.QueueUserWorkItem((state) =>
{
var _socket = state as Socket;
while (true)
{
try
{
var client = _socket.Accept();
string point = client.RemoteEndPoint.ToString();
ThreadPool.QueueUserWorkItem(ReceiveCustomer, client);
}
catch (SocketException ex)
{
break;
}
catch (Exception ex)
{
_logerr.LogError(ex);
throw;
}
}
}, ls);
}
private void ReceiveCustomer(object state)
{
var client = state as Socket;
try
{
handler.Invoke(client, _data);
}
catch (Exception ex)
{
_logerr.LogError(ex);
}
}
public void ShutdownAndClose()
{
try
{
ls.Shutdown(SocketShutdown.Both);
}
catch (Exception)
{
}
finally
{
ls.Close();
_logerr.LogDebug("Listener closed");
}
}
}
}

View File

@ -11,6 +11,6 @@ namespace FastTunnel.Core.Models
public SSHConfig SSHConfig { get; set; } public SSHConfig SSHConfig { get; set; }
public Listener<T> Listener { get; set; } public IListener<T> Listener { get; set; }
} }
} }