Browse Source

tcp server support multi-server

master
jkpete 4 months ago
parent
commit
de24126549
  1. 10
      Example/UsingTest/Script/EGSaveTest.cs
  2. 40
      addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs

10
Example/UsingTest/Script/EGSaveTest.cs

@ -19,17 +19,18 @@ namespace EGFramework.Examples.Test{
this.EGEnabledProtocolTool<EGSsh>(); this.EGEnabledProtocolTool<EGSsh>();
this.EGEnabledProtocolTool<EGTCPClient>(); this.EGEnabledProtocolTool<EGTCPClient>();
this.EGEnabledProtocolTool<EGTCPServer>();
this.EGOnMessage<EasyMessage>(); this.EGOnMessage<EasyMessage>();
// this.EGRegisterMessageEvent<EasyMessage>((e,sender)=>{ // this.EGRegisterMessageEvent<EasyMessage>((e,sender)=>{
// }); // });
//TestSsh(); //TestSsh();
TestTCPSend(); //TestTCPSend();
TestTCPServer();
} }
public async void TestSsh(){ public async void TestSsh(){
await this.EGSsh().ConnectSsh("127.0.0.1","jkpete",new PrivateKeyFile("../../../.ssh/id_ed25519")); await this.EGSsh().ConnectSsh("127.0.0.1","jkpete",new PrivateKeyFile("../../../.ssh/id_ed25519"));
// await this.EGSsh().ConnectSsh("byserver","bytech","bytech");
this.EGSendMessage(new EasyMessage(){sendString = "ls -la"},"127.0.0.1",ProtocolType.SSHClient); this.EGSendMessage(new EasyMessage(){sendString = "ls -la"},"127.0.0.1",ProtocolType.SSHClient);
} }
public async void TestTCPSend(){ public async void TestTCPSend(){
@ -43,6 +44,11 @@ namespace EGFramework.Examples.Test{
this.EGSendMessage(new EasyMessage(){sendString = "ls -la"},"127.0.0.1:5555",ProtocolType.TCPClient); this.EGSendMessage(new EasyMessage(){sendString = "ls -la"},"127.0.0.1:5555",ProtocolType.TCPClient);
} }
public void TestTCPServer(){
this.EGTCPServer().StartServer(5555);
this.EGTCPServer().StartServer(6666);
}
public async void TestThread(){ public async void TestThread(){
await Task.Run(()=>{ await Task.Run(()=>{
//this.ExecuteInMainThread(TestMainThreadFunc); //this.ExecuteInMainThread(TestMainThreadFunc);

40
addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs

@ -9,12 +9,10 @@ using System.Threading.Tasks;
namespace EGFramework{ namespace EGFramework{
public class EGTCPServer : IModule, IEGFramework,IProtocolReceived public class EGTCPServer : IModule, IEGFramework,IProtocolReceived
{ {
public TcpListener TcpServer { set; get; } public Dictionary<int,TcpListener> TcpServerDevices { set; get; } = new Dictionary<int, TcpListener>();
public Dictionary<int,bool> IsListening { set; get; } = new Dictionary<int, bool>();
public bool IsListening { set; get; }
public Dictionary<string, TcpClient> LinkedClients { set; get; } = new Dictionary<string, TcpClient>(); public Dictionary<string, TcpClient> LinkedClients { set; get; } = new Dictionary<string, TcpClient>();
public Encoding StringEncoding { set; get; } = Encoding.UTF8; public Encoding StringEncoding { set; get; } = Encoding.UTF8;
public List<string> ClientNames = new List<string>();
public EasyEvent<string> OnClientConnect { set; get; } = new EasyEvent<string>(); public EasyEvent<string> OnClientConnect { set; get; } = new EasyEvent<string>();
public EasyEvent<string> OnClientDisconnect { set; get; } = new EasyEvent<string>(); public EasyEvent<string> OnClientDisconnect { set; get; } = new EasyEvent<string>();
@ -44,25 +42,34 @@ namespace EGFramework{
public async void StartServer(int port) public async void StartServer(int port)
{ {
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, port); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, port);
TcpServer = new(ipEndPoint); TcpListener tcpServer = new TcpListener(ipEndPoint);
if(TcpServerDevices.ContainsKey(port)){
return;
}else{
TcpServerDevices.Add(port,tcpServer);
IsListening.Add(port,true);
}
TcpServerDevices[port].Start();
try try
{ {
TcpServer.Start(); while (IsListening[port])
IsListening = true;
while (IsListening)
{ {
TcpClient client = await TcpServer.AcceptTcpClientAsync(); TcpClient client = await TcpServerDevices[port].AcceptTcpClientAsync();
ClientNames.Add(client.Client.RemoteEndPoint.ToString());
LinkedClients.Add(client.Client.RemoteEndPoint.ToString(), client); LinkedClients.Add(client.Client.RemoteEndPoint.ToString(), client);
OnClientConnect.Invoke(client.Client.RemoteEndPoint.ToString()); OnClientConnect.Invoke(client.Client.RemoteEndPoint.ToString());
_ = HandleClientAsync(client); _ = HandleClientAsync(client);
EG.Print("[EGTCPServer]"+port+" Client connected: " + client.Client.RemoteEndPoint.ToString());
} }
TcpServer.Stop(); TcpServerDevices[port].Stop();
} }
catch (Exception) catch (Exception e)
{ {
EG.Print("[EGTCPServer]"+port+" Error: " + e.ToString());
} }
} }
public void EndServer(int port){
IsListening[port] = false;
}
public async Task HandleClientAsync(TcpClient client) public async Task HandleClientAsync(TcpClient client)
{ {
@ -70,7 +77,7 @@ namespace EGFramework{
{ {
NetworkStream stream = client.GetStream(); NetworkStream stream = client.GetStream();
string ClientName = client.Client.RemoteEndPoint.ToString(); string ClientName = client.Client.RemoteEndPoint.ToString();
while (true) while (client.Connected)
{ {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length); int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
@ -85,6 +92,7 @@ namespace EGFramework{
ResponseMsgs.Enqueue(receivedMsgs); ResponseMsgs.Enqueue(receivedMsgs);
} }
//await Task.Run(() => DeleteClient(client)).ConfigureAwait(false); //await Task.Run(() => DeleteClient(client)).ConfigureAwait(false);
EG.Print("[EGTCPServer] Client Disconnected: " + client.Client.LocalEndPoint.ToString() +"--"+ client.Client.RemoteEndPoint.ToString());
DeleteClient(client); DeleteClient(client);
client.Close(); client.Close();
} }
@ -109,9 +117,6 @@ namespace EGFramework{
public void DeleteClient(TcpClient client) public void DeleteClient(TcpClient client)
{ {
string clientName = client.Client.RemoteEndPoint.ToString(); string clientName = client.Client.RemoteEndPoint.ToString();
if (ClientNames.Contains(clientName)) {
ClientNames.Remove(clientName);
}
if (LinkedClients.ContainsKey(clientName)) { if (LinkedClients.ContainsKey(clientName)) {
LinkedClients.Remove(clientName); LinkedClients.Remove(clientName);
} }
@ -133,6 +138,9 @@ namespace EGFramework{
public static void EGTCPServerListen(this IEGFramework self ,int port){ public static void EGTCPServerListen(this IEGFramework self ,int port){
self.GetModule<EGTCPServer>().StartServer(port); self.GetModule<EGTCPServer>().StartServer(port);
} }
public static void EGTCPServerEndListen(this IEGFramework self ,int port){
self.GetModule<EGTCPServer>().EndServer(port);
}
} }
} }

Loading…
Cancel
Save