2024-10-20 21:59:42 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Serein.Library;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 客户端的消息管理(用于处理服务端的响应)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey, DataKey = FlowEnvironment.DataKey)]
|
|
|
|
|
|
public class MsgControllerOfClient : ISocketHandleModule
|
|
|
|
|
|
{
|
|
|
|
|
|
public Guid HandleGuid => new Guid();
|
|
|
|
|
|
private readonly Func<string, object?, Task> SendCommandAsync;
|
|
|
|
|
|
private readonly RemoteFlowEnvironment remoteFlowEnvironment;
|
|
|
|
|
|
|
|
|
|
|
|
public MsgControllerOfClient(RemoteFlowEnvironment remoteFlowEnvironment, Func<string, object?, Task> func)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.remoteFlowEnvironment = remoteFlowEnvironment;
|
|
|
|
|
|
SendCommandAsync = func;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送请求并等待远程环境响应
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException">超时触发</exception>
|
2024-10-20 21:59:42 +08:00
|
|
|
|
public async Task SendAsync(string signal, object? sendData = null, int overtimeInMs = 100)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-20 21:59:42 +08:00
|
|
|
|
//Console.WriteLine($"指令[{signal}],value:{JsonConvert.SerializeObject(sendData)}");
|
|
|
|
|
|
if (!DebounceHelper.CanExecute(signal, overtimeInMs))
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-10-20 21:59:42 +08:00
|
|
|
|
await SendCommandAsync.Invoke(signal, sendData);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送请求并等待远程环境响应
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException">超时触发</exception>
|
2024-10-20 21:59:42 +08:00
|
|
|
|
public async Task<TResult> SendAndWaitDataAsync<TResult>(string signal, object? sendData = null, int overtimeInMs = 50)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-20 21:59:42 +08:00
|
|
|
|
//Console.WriteLine($"指令[{signal}],value:{JsonConvert.SerializeObject(sendData)}");
|
|
|
|
|
|
_ = SendCommandAsync.Invoke(signal, sendData);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
return await remoteFlowEnvironment.WaitData<TResult>(signal);
|
|
|
|
|
|
|
2024-10-20 21:59:42 +08:00
|
|
|
|
//if (DebounceHelper.CanExecute(signal, overtimeInMs))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// _ = SendCommandAsync.Invoke(signal, sendData);
|
|
|
|
|
|
// return await remoteFlowEnvironment.WaitData<TResult>(signal);
|
|
|
|
|
|
|
|
|
|
|
|
// //(var type, var result) = await remoteFlowEnvironment.WaitDataWithTimeoutAsync<TResult>(signal, TimeSpan.FromSeconds(150));
|
|
|
|
|
|
// //if (type == TriggerType.Overtime)
|
|
|
|
|
|
// //{
|
|
|
|
|
|
// // throw new NotImplementedException("超时触发");
|
|
|
|
|
|
// //}
|
|
|
|
|
|
// //else
|
|
|
|
|
|
// //{
|
|
|
|
|
|
// // return result;
|
|
|
|
|
|
// //}
|
|
|
|
|
|
//}
|
|
|
|
|
|
//else
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return default;
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 消息接收
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 远程环境发来项目信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="flowEnvInfo"></param>
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo)]
|
|
|
|
|
|
public void GetEnvInfo([UseMsgData] FlowEnvInfo flowEnvInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.GetEnvInfo, flowEnvInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 远程环境发来项目信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sereinProjectData"></param>
|
|
|
|
|
|
[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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 21:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.CreateNode)]
|
|
|
|
|
|
public void CreateNode([UseMsgData] NodeInfo nodeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.CreateNode, nodeInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveNode)]
|
|
|
|
|
|
public void RemoveNode(bool state)
|
|
|
|
|
|
{
|
|
|
|
|
|
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.RemoveNode, state);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.ConnectNode)]
|
|
|
|
|
|
public void ConnectNode(bool state)
|
|
|
|
|
|
{
|
|
|
|
|
|
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.ConnectNode, state);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveConnect)]
|
|
|
|
|
|
public void RemoveConnect(bool state)
|
|
|
|
|
|
{
|
|
|
|
|
|
remoteFlowEnvironment.TriggerSignal(EnvMsgTheme.RemoveConnect, state);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|