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{ @@ -19,17 +19,18 @@ namespace EGFramework.Examples.Test{
this.EGEnabledProtocolTool<EGSsh>();
this.EGEnabledProtocolTool<EGTCPClient>();
this.EGEnabledProtocolTool<EGTCPServer>();
this.EGOnMessage<EasyMessage>();
// this.EGRegisterMessageEvent<EasyMessage>((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{ @@ -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);

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

@ -9,12 +9,10 @@ using System.Threading.Tasks; @@ -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<int,TcpListener> TcpServerDevices { set; get; } = new Dictionary<int, TcpListener>();
public Dictionary<int,bool> IsListening { set; get; } = new Dictionary<int, bool>();
public Dictionary<string, TcpClient> LinkedClients { set; get; } = new Dictionary<string, TcpClient>();
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> OnClientDisconnect { set; get; } = new EasyEvent<string>();
@ -44,25 +42,34 @@ namespace EGFramework{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -133,6 +138,9 @@ namespace EGFramework{
public static void EGTCPServerListen(this IEGFramework self ,int port){
self.GetModule<EGTCPServer>().StartServer(port);
}
public static void EGTCPServerEndListen(this IEGFramework self ,int port){
self.GetModule<EGTCPServer>().EndServer(port);
}
}
}

Loading…
Cancel
Save