2024-10-27 00:54:10 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Serein.Library;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library.Network.WebSocketCommunication;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
using Serein.Library.Network.WebSocketCommunication.Handle;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Serein.NodeFlow.Env
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 客户端的消息管理(用于处理服务端的响应)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
[AutoSocketModule(ThemeKey = FlowEnvironment.ThemeKey,
|
|
|
|
|
|
DataKey = FlowEnvironment.DataKey,
|
|
|
|
|
|
MsgIdKey = FlowEnvironment.MsgIdKey)]
|
2024-10-20 12:10:57 +08:00
|
|
|
|
public class MsgControllerOfClient : ISocketHandleModule
|
|
|
|
|
|
{
|
|
|
|
|
|
public Guid HandleGuid => new Guid();
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
|
|
|
|
|
// 消息主题,data - task等待
|
|
|
|
|
|
private readonly Func<string, string, object?, Task> SendCommandFunc;
|
2024-10-20 12:10:57 +08:00
|
|
|
|
private readonly RemoteFlowEnvironment remoteFlowEnvironment;
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public MsgControllerOfClient(RemoteFlowEnvironment remoteFlowEnvironment, Func<string, string, object?, Task> func)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
|
|
|
|
|
this.remoteFlowEnvironment = remoteFlowEnvironment;
|
2024-10-22 00:13:13 +08:00
|
|
|
|
SendCommandFunc = func;
|
|
|
|
|
|
}
|
2024-10-27 00:54:10 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理需要返回的消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="msgId"></param>
|
|
|
|
|
|
/// <param name="theme"></param>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
private async Task SendCommandAsync(string msgId, string theme, object? data)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SendCommandFunc.Invoke(msgId, theme, data);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// 发送请求
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException">超时触发</exception>
|
2024-10-27 00:54:10 +08:00
|
|
|
|
public async Task SendAsync(string theme, object? data = null, int overtimeInMs = 100)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-27 00:54:10 +08:00
|
|
|
|
|
2024-10-22 00:13:13 +08:00
|
|
|
|
var msgId = MsgIdHelper.GenerateId().ToString();
|
2024-10-27 00:54:10 +08:00
|
|
|
|
Console.WriteLine($"[{msgId}] => {theme}");
|
|
|
|
|
|
await SendCommandAsync(msgId, theme, data); // 客户端发送消息
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送请求并等待远程环境响应
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotImplementedException">超时触发</exception>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public async Task<TResult> SendAndWaitDataAsync<TResult>(string theme, object? data = null, int overtimeInMs = 50)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
var msgId = MsgIdHelper.GenerateId().ToString();
|
2024-10-27 00:54:10 +08:00
|
|
|
|
//_ = Task.Run(async () =>
|
2024-10-20 21:59:42 +08:00
|
|
|
|
//{
|
2024-10-27 00:54:10 +08:00
|
|
|
|
// await Task.Delay(500);
|
|
|
|
|
|
//});
|
|
|
|
|
|
await SendCommandAsync(msgId, theme, data); // 客户端发送消息
|
|
|
|
|
|
return await remoteFlowEnvironment.WaitData<TResult>(msgId);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 消息接收
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 远程环境发来项目信息
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// <param name="msgId"></param>
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <param name="flowEnvInfo"></param>
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetEnvInfo)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void GetEnvInfo([UseMsgId] string msgId, [UseData] FlowEnvInfo flowEnvInfo)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, flowEnvInfo);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 远程环境发来项目信息
|
|
|
|
|
|
/// </summary>
|
2024-10-22 00:13:13 +08:00
|
|
|
|
/// <param name="msgId"></param>
|
2024-10-20 12:10:57 +08:00
|
|
|
|
/// <param name="sereinProjectData"></param>
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.GetProjectInfo)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void GetProjectInfo([UseMsgId] string msgId, [UseData] SereinProjectData sereinProjectData)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, sereinProjectData);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.SetNodeInterrupt)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void SetNodeInterrupt([UseMsgId] string msgId)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, null);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.AddInterruptExpression)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void AddInterruptExpression([UseMsgId] string msgId)
|
2024-10-20 12:10:57 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, null);
|
2024-10-20 12:10:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 21:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.CreateNode)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void CreateNode([UseMsgId] string msgId, [UseData] NodeInfo nodeInfo)
|
2024-10-20 21:59:42 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, nodeInfo);
|
2024-10-20 21:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveNode)]
|
2024-10-22 00:13:13 +08:00
|
|
|
|
public void RemoveNode([UseMsgId] string msgId, bool state)
|
2024-10-20 21:59:42 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, state);
|
2024-10-20 21:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-27 00:54:10 +08:00
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.ConnectInvokeNode)]
|
|
|
|
|
|
public void ConnectInvokeNode([UseMsgId] string msgId, bool state)
|
|
|
|
|
|
{
|
|
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, state);
|
|
|
|
|
|
}
|
2024-10-20 21:59:42 +08:00
|
|
|
|
|
2024-10-27 00:54:10 +08:00
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveInvokeConnect)]
|
|
|
|
|
|
public void RemoveInvokeConnect([UseMsgId] string msgId, bool state)
|
|
|
|
|
|
{
|
|
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, state);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.ConnectArgSourceNode)]
|
|
|
|
|
|
public void ConnectArgSourceNode([UseMsgId] string msgId, bool state)
|
2024-10-20 21:59:42 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, state);
|
2024-10-20 21:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-27 00:54:10 +08:00
|
|
|
|
[AutoSocketHandle(ThemeValue = EnvMsgTheme.RemoveArgSourceConnect)]
|
|
|
|
|
|
public void RemoveArgSourceConnect([UseMsgId] string msgId, bool state)
|
2024-10-20 21:59:42 +08:00
|
|
|
|
{
|
2024-10-22 00:13:13 +08:00
|
|
|
|
remoteFlowEnvironment.TriggerSignal(msgId, state);
|
2024-10-20 21:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-20 12:10:57 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|