唐山轨道机控制端TCP服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.8 KiB

using System;
using System.Collections.Generic;
using System.Threading;
namespace EGFramework{
/// <summary>
/// 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.
/// </summary>
public partial class EGProtocolSchedule : IModule,IEGFramework
{
public Dictionary<Type,IProtocolReceived> ProtocolTools = new Dictionary<Type, IProtocolReceived>();
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<EGMessage>().OnDataReceived.Invoke(tool.GetReceivedMsg().Dequeue());
}
}
}
public void EnabledAllTools(){
ProtocolTools.Add(typeof(EGTCPClient),this.GetModule<EGTCPClient>());
ProtocolTools.Add(typeof(EGTCPServer),this.GetModule<EGTCPServer>());
ProtocolTools.Add(typeof(EGUDP),this.GetModule<EGUDP>());
ProtocolTools.Add(typeof(EGSerialPort),this.GetModule<EGSerialPort>());
}
public void EnabledTool<TProtocolReceived>() where TProtocolReceived : class, IModule,IProtocolReceived,new(){
ProtocolTools.Add(typeof(TProtocolReceived),this.GetModule<TProtocolReceived>());
}
public void DisabledAllTools(){
ProtocolTools.Clear();
}
public void DisabledTool<TProtocolReceived>() 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<EGProtocolSchedule>();
}
public static void EGEnabledProtocolTools(this IEGFramework self){
self.GetModule<EGProtocolSchedule>().EnabledAllTools();
}
public static void EGEnabledProtocolTool<TProtocolReceived>(this IEGFramework self) where TProtocolReceived : class, IModule,IProtocolReceived,new(){
self.GetModule<EGProtocolSchedule>().EnabledTool<TProtocolReceived>();
}
}
}