diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs index b59315b..e56cb1c 100644 --- a/Example/UsingTest/Script/EGSaveTest.cs +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -19,17 +19,18 @@ namespace EGFramework.Examples.Test{ this.EGEnabledProtocolTool(); this.EGEnabledProtocolTool(); + this.EGEnabledProtocolTool(); this.EGOnMessage(); // this.EGRegisterMessageEvent((e,sender)=>{ // }); //TestSsh(); - TestTCPSend(); + //TestTCPSend(); + TestTCPServer(); } public async void TestSsh(){ 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); } 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); } + public void TestTCPServer(){ + this.EGTCPServer().StartServer(5555); + this.EGTCPServer().StartServer(6666); + } + public async void TestThread(){ await Task.Run(()=>{ //this.ExecuteInMainThread(TestMainThreadFunc); diff --git a/addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs b/addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs index 70075e6..23f1058 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs @@ -9,12 +9,10 @@ using System.Threading.Tasks; namespace EGFramework{ public class EGTCPServer : IModule, IEGFramework,IProtocolReceived { - public TcpListener TcpServer { set; get; } - - public bool IsListening { set; get; } + public Dictionary TcpServerDevices { set; get; } = new Dictionary(); + public Dictionary IsListening { set; get; } = new Dictionary(); public Dictionary LinkedClients { set; get; } = new Dictionary(); public Encoding StringEncoding { set; get; } = Encoding.UTF8; - public List ClientNames = new List(); public EasyEvent OnClientConnect { set; get; } = new EasyEvent(); public EasyEvent OnClientDisconnect { set; get; } = new EasyEvent(); @@ -44,25 +42,34 @@ namespace EGFramework{ public async void StartServer(int 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 { - TcpServer.Start(); - IsListening = true; - while (IsListening) + while (IsListening[port]) { - TcpClient client = await TcpServer.AcceptTcpClientAsync(); - ClientNames.Add(client.Client.RemoteEndPoint.ToString()); + TcpClient client = await TcpServerDevices[port].AcceptTcpClientAsync(); LinkedClients.Add(client.Client.RemoteEndPoint.ToString(), client); OnClientConnect.Invoke(client.Client.RemoteEndPoint.ToString()); _ = 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) { @@ -70,7 +77,7 @@ namespace EGFramework{ { NetworkStream stream = client.GetStream(); string ClientName = client.Client.RemoteEndPoint.ToString(); - while (true) + while (client.Connected) { byte[] buffer = new byte[1024]; int bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length); @@ -85,6 +92,7 @@ namespace EGFramework{ ResponseMsgs.Enqueue(receivedMsgs); } //await Task.Run(() => DeleteClient(client)).ConfigureAwait(false); + EG.Print("[EGTCPServer] Client Disconnected: " + client.Client.LocalEndPoint.ToString() +"--"+ client.Client.RemoteEndPoint.ToString()); DeleteClient(client); client.Close(); } @@ -109,9 +117,6 @@ namespace EGFramework{ public void DeleteClient(TcpClient client) { string clientName = client.Client.RemoteEndPoint.ToString(); - if (ClientNames.Contains(clientName)) { - ClientNames.Remove(clientName); - } if (LinkedClients.ContainsKey(clientName)) { LinkedClients.Remove(clientName); } @@ -133,6 +138,9 @@ namespace EGFramework{ public static void EGTCPServerListen(this IEGFramework self ,int port){ self.GetModule().StartServer(port); } + public static void EGTCPServerEndListen(this IEGFramework self ,int port){ + self.GetModule().EndServer(port); + } } }