diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs index fc3bc35..b59315b 100644 --- a/Example/UsingTest/Script/EGSaveTest.cs +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -18,17 +18,29 @@ namespace EGFramework.Examples.Test{ //TestCode(); this.EGEnabledProtocolTool(); + this.EGEnabledProtocolTool(); this.EGOnMessage(); // this.EGRegisterMessageEvent((e,sender)=>{ // }); - TestSsh(); + //TestSsh(); + TestTCPSend(); } 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(){ + await this.EGTCPClient().ConnectTCP("127.0.0.1",5555); + await this.EGTCPClient().ConnectTCP("127.0.0.1",6666); + 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:6666",ProtocolType.TCPClient); + this.EGSendMessage(new EasyMessage(){sendString = "ls -la"},"127.0.0.1:6666",ProtocolType.TCPClient); + this.EGSendMessage(new EasyMessage(){sendString = "ls -la"},"127.0.0.1:6666",ProtocolType.TCPClient); + this.EGSendMessage(new EasyMessage(){sendString = "ls -la"},"127.0.0.1:6666",ProtocolType.TCPClient); + this.EGSendMessage(new EasyMessage(){sendString = "ls -la"},"127.0.0.1:5555",ProtocolType.TCPClient); } public async void TestThread(){ diff --git a/addons/EGFramework/Module/EGMessage.cs b/addons/EGFramework/Module/EGMessage.cs index 7a08f76..de9b7dc 100644 --- a/addons/EGFramework/Module/EGMessage.cs +++ b/addons/EGFramework/Module/EGMessage.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using System.Collections.Concurrent; using System.Timers; namespace EGFramework @@ -19,7 +19,7 @@ namespace EGFramework /// /// public int SendDelay { set; get; } = 100; - public Queue RequestCache { set; get; } = new Queue(); + public ConcurrentDictionary> RequestCache { set; get; } = new ConcurrentDictionary>(); private System.Timers.Timer RequestTimer { set; get; } public override void Init() @@ -58,7 +58,10 @@ namespace EGFramework public void SendRequest(TRequest request,string sender,ProtocolType protocolType) where TRequest:IRequest { if(SendDelay>0){ - RequestCache.Enqueue(new RequestMsgEvent(request,sender,protocolType)); + if(!RequestCache.ContainsKey(sender)){ + RequestCache[sender] = new ConcurrentQueue(); + } + RequestCache[sender].Enqueue(new RequestMsgEvent(request,sender,protocolType)); }else{ OnRequest.Invoke(new RequestMsgEvent(request,sender,protocolType)); } @@ -67,8 +70,11 @@ namespace EGFramework } private void ExecuteRequest(object source, ElapsedEventArgs e){ - if(RequestCache.Count>0){ - OnRequest.Invoke(RequestCache.Dequeue()); + foreach(ConcurrentQueue singleCache in RequestCache.Values){ + if(singleCache.Count>0){ + singleCache.TryDequeue(out RequestMsgEvent msg); + OnRequest.Invoke(msg); + } } } diff --git a/addons/EGFramework/Module/ProtocolTools/EGBacnet.cs b/addons/EGFramework/Module/ProtocolTools/EGBacnet.cs index 38f24ca..3040279 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGBacnet.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGBacnet.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO.BACnet; using System.Linq; @@ -14,7 +15,7 @@ namespace EGFramework{ public Dictionary DevicesList = new Dictionary(); public Encoding StringEncoding { set; get; } = Encoding.ASCII; - public Queue ResponseMsgs { set; get; } = new Queue(); + public ConcurrentQueue ResponseMsgs { set; get; } = new ConcurrentQueue(); public void Init() { @@ -283,7 +284,7 @@ namespace EGFramework{ this.StringEncoding = textEncoding; } - public Queue GetReceivedMsg() + public ConcurrentQueue GetReceivedMsg() { return this.ResponseMsgs; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGFileStream.cs b/addons/EGFramework/Module/ProtocolTools/EGFileStream.cs index 670ebc4..55db780 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGFileStream.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGFileStream.cs @@ -1,13 +1,14 @@ using System.Collections.Generic; using System.Text; using System.IO; +using System.Collections.Concurrent; namespace EGFramework{ public class EGFileStream : IEGFramework, IModule,IProtocolSend,IProtocolReceived { public Encoding StringEncoding { set; get; } = Encoding.UTF8; - public Queue ResponseMsgs { set; get; } = new Queue(); + public ConcurrentQueue ResponseMsgs { set; get; } = new ConcurrentQueue(); public void Init() { @@ -83,7 +84,7 @@ namespace EGFramework{ this.StringEncoding = textEncoding; } - public Queue GetReceivedMsg() + public ConcurrentQueue GetReceivedMsg() { return this.ResponseMsgs; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGMQTT.cs b/addons/EGFramework/Module/ProtocolTools/EGMQTT.cs index a69123e..b2d6b0e 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGMQTT.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGMQTT.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Net; using System.Net.Sockets; @@ -20,7 +21,7 @@ namespace EGFramework{ public Encoding StringEncoding { set; get; } = Encoding.UTF8; - public Queue ResponseMsgs { set; get; } = new Queue(); + public ConcurrentQueue ResponseMsgs { set; get; } = new ConcurrentQueue(); public EasyEvent OnMqttConnect { set; get; } = new EasyEvent(); @@ -143,7 +144,7 @@ namespace EGFramework{ { StringEncoding = textEncoding; } - public Queue GetReceivedMsg() + public ConcurrentQueue GetReceivedMsg() { return ResponseMsgs; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGModbus.cs b/addons/EGFramework/Module/ProtocolTools/EGModbus.cs index 6ca1dbe..5a40647 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGModbus.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGModbus.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Threading.Tasks; @@ -8,16 +9,16 @@ namespace EGFramework{ /// public class EGModbus : IEGFramework, IModule { - public Queue RTUCache = new Queue(); - public Queue TCPCache = new Queue(); + public ConcurrentQueue RTUCache = new ConcurrentQueue(); + public ConcurrentQueue TCPCache = new ConcurrentQueue(); public int Delay = 2000; - public Queue WaitForSendRTU = new Queue(); + public ConcurrentQueue WaitForSendRTU = new ConcurrentQueue(); public int NextSendRTU = 0; public int SendPointerRTU = 1; - public Queue WaitForSendTCP = new Queue(); + public ConcurrentQueue WaitForSendTCP = new ConcurrentQueue(); public int NextSendTCP = 0; public int SendPointerTCP = 1; @@ -90,7 +91,7 @@ namespace EGFramework{ timeout+=10; } if(RTUCache.Count>0){ - res = RTUCache.Dequeue(); + RTUCache.TryDequeue(out res); }else{ //Print Error Timeout OnReadTimeOut.Invoke(); @@ -99,7 +100,7 @@ namespace EGFramework{ this.EGSerialPort().ClearReceivedCache(serialPort); IsRequestRTU = false; if(this.WaitForSendRTU.Count>0){ - NextSendRTU = this.WaitForSendRTU.Dequeue(); + this.WaitForSendRTU.TryDequeue(out NextSendRTU); } return res; } @@ -143,7 +144,7 @@ namespace EGFramework{ timeout+=10; } if(RTUCache.Count>0){ - res = RTUCache.Dequeue(); + RTUCache.TryDequeue(out res); }else{ //Print Error Timeout OnReadTimeOut.Invoke(); @@ -152,7 +153,7 @@ namespace EGFramework{ this.EGSerialPort().ClearReceivedCache(serialPort); IsRequestRTU = false; if(this.WaitForSendRTU.Count>0){ - NextSendRTU = this.WaitForSendRTU.Dequeue(); + this.WaitForSendRTU.TryDequeue(out NextSendRTU); } return res; } @@ -195,7 +196,7 @@ namespace EGFramework{ timeout += 10; } if(TCPCache.Count>0){ - res = TCPCache.Dequeue(); + TCPCache.TryDequeue(out res); }else{ //Print Error Timeout OnReadTimeOut.Invoke(); @@ -203,7 +204,7 @@ namespace EGFramework{ }); IsRequestTCP = false; if(this.WaitForSendTCP.Count>0){ - NextSendTCP = this.WaitForSendTCP.Dequeue(); + this.WaitForSendTCP.TryDequeue(out NextSendTCP); } return res; } @@ -250,7 +251,7 @@ namespace EGFramework{ timeout+=10; } if(TCPCache.Count>0){ - res = TCPCache.Dequeue(); + TCPCache.TryDequeue(out res); }else{ //Print Error Timeout OnReadTimeOut.Invoke(); @@ -258,10 +259,10 @@ namespace EGFramework{ }); IsRequestTCP = false; if(this.WaitForSendTCP.Count>0){ - NextSendTCP = this.WaitForSendTCP.Dequeue(); + this.WaitForSendTCP.TryDequeue(out NextSendTCP); } if(this.WaitForSendTCP.Count>0){ - NextSendTCP = this.WaitForSendTCP.Dequeue(); + this.WaitForSendTCP.TryDequeue(out NextSendTCP); } return res; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs b/addons/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs index 4b045ea..7cd2fe1 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGProtocolSchedule.cs @@ -9,7 +9,10 @@ namespace EGFramework{ public void CheckedProcess(){ foreach(IProtocolReceived tool in ProtocolTools.Values){ if(tool.GetReceivedMsg().Count>0){ - this.GetModule().OnDataReceived.Invoke(tool.GetReceivedMsg().Dequeue()); + bool isDequeue = tool.GetReceivedMsg().TryDequeue(out ResponseMsg msg); + if(isDequeue){ + this.GetModule().OnDataReceived.Invoke(msg); + } } } } diff --git a/addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs b/addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs index d27331d..e76e720 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGSerialPort.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Collections.Concurrent; namespace EGFramework{ public class EGSerialPort : IModule,IEGFramework,IProtocolSend,IProtocolReceived @@ -21,7 +22,7 @@ namespace EGFramework{ public Encoding StringEncoding { set; get; } = Encoding.UTF8; - public Queue ResponseMsgs { set; get; } = new Queue(); + public ConcurrentQueue ResponseMsgs { set; get; } = new ConcurrentQueue(); public Dictionary ReceivedCache { set; get; } = new Dictionary(); @@ -39,7 +40,7 @@ namespace EGFramework{ }); } - public Queue GetReceivedMsg() + public ConcurrentQueue GetReceivedMsg() { return this.ResponseMsgs; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGSsh.cs b/addons/EGFramework/Module/ProtocolTools/EGSsh.cs index 04aa99d..573e240 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGSsh.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGSsh.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -14,7 +15,7 @@ namespace EGFramework{ public Encoding StringEncoding { set; get; } = Encoding.UTF8; - public Queue ResponseMsgs { set; get; } = new Queue(); + public ConcurrentQueue ResponseMsgs { set; get; } = new ConcurrentQueue(); public int TimeOutDelay = 5000; public void Init() @@ -124,7 +125,7 @@ namespace EGFramework{ } } - public Queue GetReceivedMsg() + public ConcurrentQueue GetReceivedMsg() { return ResponseMsgs; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGTCPClient.cs b/addons/EGFramework/Module/ProtocolTools/EGTCPClient.cs index 753f54e..e99bbfe 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGTCPClient.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGTCPClient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.Text; using System.Net.Sockets; using System.Threading.Tasks; @@ -14,7 +15,7 @@ namespace EGFramework{ public Encoding StringEncoding { set; get; } = Encoding.UTF8; - public Queue ResponseMsgs { set; get; } = new Queue(); + public ConcurrentQueue ResponseMsgs { set; get; } = new ConcurrentQueue(); public void Init() { @@ -107,7 +108,7 @@ namespace EGFramework{ DisconnectTCP(host,port); } - public Queue GetReceivedMsg() + public ConcurrentQueue GetReceivedMsg() { return ResponseMsgs; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs b/addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs index 5848f8c..70075e6 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGTCPServer.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Concurrent; using System.Net; using System.Net.Sockets; using System.Text; @@ -18,7 +19,7 @@ namespace EGFramework{ public EasyEvent OnClientConnect { set; get; } = new EasyEvent(); public EasyEvent OnClientDisconnect { set; get; } = new EasyEvent(); - public Queue ResponseMsgs { set; get; } = new Queue(); + public ConcurrentQueue ResponseMsgs { set; get; } = new ConcurrentQueue(); public string ErrorLogs { set; get; } public void Init() @@ -35,7 +36,7 @@ namespace EGFramework{ }); } - public Queue GetReceivedMsg() + public ConcurrentQueue GetReceivedMsg() { return ResponseMsgs; } diff --git a/addons/EGFramework/Module/ProtocolTools/EGUDP.cs b/addons/EGFramework/Module/ProtocolTools/EGUDP.cs index 8ff4cea..2939ef9 100644 --- a/addons/EGFramework/Module/ProtocolTools/EGUDP.cs +++ b/addons/EGFramework/Module/ProtocolTools/EGUDP.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; @@ -11,7 +12,7 @@ namespace EGFramework{ public Encoding StringEncoding { set; get; } = Encoding.UTF8; - public Queue ResponseMsgs { set; get; } = new Queue(); + public ConcurrentQueue ResponseMsgs { set; get; } = new ConcurrentQueue(); public void Init() { @@ -114,7 +115,7 @@ namespace EGFramework{ return EGArchitectureImplement.Interface; } - public Queue GetReceivedMsg() + public ConcurrentQueue GetReceivedMsg() { return ResponseMsgs; } diff --git a/addons/EGFramework/Module/ProtocolTools/ProtocolToolsInterface.cs b/addons/EGFramework/Module/ProtocolTools/ProtocolToolsInterface.cs index d4ab078..ed21f06 100644 --- a/addons/EGFramework/Module/ProtocolTools/ProtocolToolsInterface.cs +++ b/addons/EGFramework/Module/ProtocolTools/ProtocolToolsInterface.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Concurrent; using System.Text; namespace EGFramework{ @@ -8,7 +8,7 @@ namespace EGFramework{ public void SetEncoding(Encoding textEncoding); } public interface IProtocolReceived{ - public Queue GetReceivedMsg(); + public ConcurrentQueue GetReceivedMsg(); } public interface IProtocolListener{ diff --git a/project.godot b/project.godot index 5d6eb0b..d93e807 100644 --- a/project.godot +++ b/project.godot @@ -18,8 +18,8 @@ config/icon="res://icon.svg" [display] -window/size/viewport_width=1920 -window/size/viewport_height=1080 +window/size/viewport_width=1600 +window/size/viewport_height=900 [dotnet]