using System; using System.Collections.Generic; using System.Threading; namespace EGFramework{ /// /// In Godot engine, the async method return cannot operate the godot main thread such as Screen Trees Node. /// The protocol schedule provide a main thread to check the message in protocol tools 's response message. /// If you have more idea ,please issue to me, thanks. /// public partial class EGProtocolSchedule : IModule,IEGFramework { public Dictionary ProtocolTools = new Dictionary(); public void Init() { int frameRate = 30; // 帧率 int interval = 1000 / frameRate; // 计算每帧间隔时间 Timer timer = new Timer(_Process, null, 0, interval); } public void _Process(object? state) { foreach(IProtocolReceived tool in ProtocolTools.Values){ if(tool.GetReceivedMsg().Count>0){ this.GetModule().OnDataReceived.Invoke(tool.GetReceivedMsg().Dequeue()); } } } public void EnabledAllTools(){ ProtocolTools.Add(typeof(EGTCPClient),this.GetModule()); ProtocolTools.Add(typeof(EGTCPServer),this.GetModule()); ProtocolTools.Add(typeof(EGUDP),this.GetModule()); ProtocolTools.Add(typeof(EGSerialPort),this.GetModule()); } public void EnabledTool() where TProtocolReceived : class, IModule,IProtocolReceived,new(){ ProtocolTools.Add(typeof(TProtocolReceived),this.GetModule()); } public void DisabledAllTools(){ ProtocolTools.Clear(); } public void DisabledTool() where TProtocolReceived : class, IModule,IProtocolReceived,new(){ if(ProtocolTools.ContainsKey(typeof(TProtocolReceived))){ ProtocolTools.Remove(typeof(TProtocolReceived)); } } public IArchitecture GetArchitecture() { return EGArchitectureImplement.Interface; } } public static class CanGetEGProtocolExtension{ public static EGProtocolSchedule EGProtocolSchedule(this IEGFramework self){ return self.GetModule(); } public static void EGEnabledProtocolTools(this IEGFramework self){ self.GetModule().EnabledAllTools(); } public static void EGEnabledProtocolTool(this IEGFramework self) where TProtocolReceived : class, IModule,IProtocolReceived,new(){ self.GetModule().EnabledTool(); } } }