using Serein.Library; using Serein.Library.Network.WebSocketCommunication; using Serein.Library.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Serein.NodeFlow.Env { /// /// 客户端的消息管理(用于处理服务端的响应) /// [AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey, DataKey = FlowEnvironment.DataKey)] public class MsgControllerOfClient : ISocketHandleModule { public Guid HandleGuid => new Guid(); private readonly Func SendCommandAsync; private readonly RemoteFlowEnvironment remoteFlowEnvironment; public MsgControllerOfClient(RemoteFlowEnvironment remoteFlowEnvironment, Func func) { this.remoteFlowEnvironment = remoteFlowEnvironment; SendCommandAsync = func; } /// /// 发送请求并等待远程环境响应 /// /// /// 超时触发 public async Task SendAsync(string signal, object? senddata = null, int debounceTimeInMs = 100) { if (!DebounceHelper.CanExecute(signal, debounceTimeInMs)) { return; } await SendCommandAsync.Invoke(signal, senddata); } /// /// 发送请求并等待远程环境响应 /// /// /// 超时触发 public async Task SendAndWaitDataAsync(string signal, object? senddata = null, int debounceTimeInMs = 50) { _ = SendCommandAsync.Invoke(signal, senddata); return await remoteFlowEnvironment.WaitData(signal); #if DEBUG if (DebounceHelper.CanExecute(signal, debounceTimeInMs)) { _ = SendCommandAsync.Invoke(signal, senddata); return await remoteFlowEnvironment.WaitData(signal); //(var type, var result) = await remoteFlowEnvironment.WaitDataWithTimeoutAsync(signal, TimeSpan.FromSeconds(150)); //if (type == TriggerType.Overtime) //{ // throw new NotImplementedException("超时触发"); //} //else //{ // return result; //} } else { return default; } #endif } #region 消息接收 /// /// 远程环境发来项目信息 /// /// [AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo)] public void GetEnvInfo([UseMsgData] FlowEnvInfo flowEnvInfo) { remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.GetEnvInfo, flowEnvInfo); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.CreateNode)] public void AddInterruptExpression([UseMsgData] NodeInfo nodeInfo) { remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.CreateNode, nodeInfo); } /// /// 远程环境发来项目信息 /// /// [AutoSocketHandle(ThemeValue = EnvMsgTheme.GetProjectInfo)] public void GetProjectInfo([UseMsgData] SereinProjectData sereinProjectData) { remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.GetProjectInfo, sereinProjectData); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.SetNodeInterrupt)] public void SetNodeInterrupt() { remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.GetProjectInfo, null); } [AutoSocketHandle(ThemeValue = EnvMsgTheme.AddInterruptExpression)] public void AddInterruptExpression() { remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.AddInterruptExpression, null); } #endregion } }