优化了节点连接

This commit is contained in:
fengjiayi
2024-10-28 15:21:08 +08:00
parent f20cfb755c
commit 561b6d764f
28 changed files with 295 additions and 165 deletions

View File

@@ -517,13 +517,12 @@ namespace Serein.Library.Api
/// <para>表示是否正在控制远程</para>
/// <para>Local control remote env</para>
/// </summary>
bool IsLcR { get; }
bool IsControlRemoteEnv { get; }
/// <summary>
/// <para>表示是否受到远程控制</para>
/// <para>Remote control local env</para>
/// 是否运行在控制台上
/// </summary>
bool IsRcL { get; }
// bool IsRuningOnConsole { get; }
/// <summary>
/// 流程运行状态

View File

@@ -2,6 +2,7 @@
using Serein.Library.Utils;
using System;
using System.Linq;
using System.Text;
namespace Serein.Library
{
@@ -12,7 +13,7 @@ namespace Serein.Library
[NodeProperty(ValuePath = NodeValuePath.Method)]
public partial class MethodDetails
{
private readonly IFlowEnvironment env;
// private readonly IFlowEnvironment env;
/// <summary>
/// 对应的节点
@@ -58,10 +59,10 @@ namespace Serein.Library
/// <summary>
/// 方法说明
/// 方法别名
/// </summary>
[PropertyInfo]
private string _methodTips;
private string _methodAnotherName;
/// <summary>
@@ -109,7 +110,7 @@ namespace Serein.Library
throw new ArgumentException("无效的节点类型");
}
MethodName = Info.MethodName;
MethodTips = Info.MethodTips;
MethodAnotherName = Info.MethodAnotherName;
MethodDynamicType = nodeType;
ReturnType = Type.GetType(Info.ReturnTypeFullName);
ParameterDetailss = Info.ParameterDetailsInfos.Select(pinfo => new ParameterDetails(pinfo)).ToArray();
@@ -121,10 +122,12 @@ namespace Serein.Library
/// <returns></returns>
public MethodDetailsInfo ToInfo()
{
return new MethodDetailsInfo
{
MethodName = MethodName,
MethodTips = MethodTips,
MethodAnotherName = MethodAnotherName,
NodeType = MethodDynamicType.ToString(),
ParameterDetailsInfos = ParameterDetailss.Select(p => p.ToInfo()).ToArray(),
ReturnTypeFullName = ReturnType.FullName,
@@ -142,7 +145,7 @@ namespace Serein.Library
ActingInstance = this.ActingInstance,
ActingInstanceType = this.ActingInstanceType,
MethodDynamicType = this.MethodDynamicType,
MethodTips = this.MethodTips,
MethodAnotherName = this.MethodAnotherName,
ReturnType = this.ReturnType,
MethodName = this.MethodName,
MethodLockName = this.MethodLockName,
@@ -152,9 +155,23 @@ namespace Serein.Library
return md;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"方法别名:{this.MethodAnotherName}");
sb.AppendLine($"方法名称:{this.MethodName}");
sb.AppendLine($"需要实例:{this.ActingInstanceType.FullName}");
sb.AppendLine($"");
sb.AppendLine($"入参参数信息:");
foreach (var arg in this.ParameterDetailss)
{
sb.AppendLine($" {arg.ToString()}");
}
sb.AppendLine($"");
sb.AppendLine($"返回值信息:");
sb.AppendLine($" {this.ReturnType.FullName}");
return sb.ToString();
}
///// <summary>
///// 每个节点有独自的MethodDetails实例

View File

@@ -29,7 +29,7 @@ namespace Serein.Library
/// <summary>
/// 方法说明
/// </summary>
public string MethodTips { get; set; }
public string MethodAnotherName { get; set; }
/// <summary>
/// 参数内容

View File

@@ -12,12 +12,6 @@ namespace Serein.Library
[NodeProperty(ValuePath = NodeValuePath.DebugSetting)]
public partial class NodeDebugSetting
{
/// <summary>
/// 对应的节点
/// </summary>
[PropertyInfo(IsProtection = true)]
private NodeModelBase _nodeModel;
/// <summary>
/// 创建属于某个节点的调试设置
/// </summary>
@@ -26,6 +20,13 @@ namespace Serein.Library
{
NodeModel = nodeModel;
}
/// <summary>
/// 对应的节点
/// </summary>
[PropertyInfo(IsProtection = true)]
private NodeModelBase _nodeModel;
/// <summary>
/// 是否使能
/// </summary>
@@ -41,11 +42,9 @@ namespace Serein.Library
/// <summary>
/// 中断级别,暂时停止继续执行后继分支。
/// </summary>
[PropertyInfo(IsNotification = true, CustomCode = "NodeModel?.Env?.SetNodeInterruptAsync(NodeModel?.Guid, value);")]
[PropertyInfo(IsNotification = true)] // CustomCode = "NodeModel?.Env?.SetNodeInterruptAsync(NodeModel?.Guid, value);"
private bool _isInterrupt = false;
//private const string MyInteruptCode = "NodeModel?.Env?.SetNodeInterruptAsync(NodeModel?.Guid, value);"; // 添加到中断的自定义代码
/// <summary>
/// 取消中断的回调函数
/// </summary>

View File

@@ -81,7 +81,6 @@ namespace Serein.Library
[PropertyInfo]
private Exception _runingException ;
}

View File

@@ -53,7 +53,7 @@ namespace Serein.Library
{
Guid = Guid,
MethodName = MethodDetails?.MethodName,
Label = DisplayName ?? "",
Label = MethodDetails?.MethodAnotherName,
Type = this.GetType().ToString(),
TrueNodes = trueNodes.ToArray(),
FalseNodes = falseNodes.ToArray(),

View File

@@ -68,7 +68,6 @@ namespace Serein.Library
[PropertyInfo]
private string _argDataSourceNodeGuid;
/// <summary>
/// 方法入参需要的类型。
/// </summary>
@@ -128,7 +127,6 @@ namespace Serein.Library
ExplicitType = Type.GetType(info.ExplicitTypeFullName);
ExplicitTypeName = info.ExplicitTypeName;
Items = info.Items;
}
/// <summary>
@@ -171,6 +169,19 @@ namespace Serein.Library
};
return pd;
}
public override string ToString()
{
if(_convertor is null)
{
return $"[{this.Index}] {this.Name} : {this.DataType.FullName}";
}
else
{
}
return $"[{this.Index}] {this.Name} : {this.ExplicitType.FullName} -> {this.DataType.FullName}";
}
}

View File

@@ -123,7 +123,10 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
catch (Exception ex)
{
Console.WriteLine($"error in ws : {ex.Message}{Environment.NewLine}json value:{jsonObject}");
return;
}
finally
{
context.Handle = true;
}
}

View File

@@ -11,7 +11,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
/// <summary>
/// 消息处理上下文
/// </summary>
public class WebSocketMsgContext : IDisposable
public class WebSocketMsgContext /*: IDisposable*/
{
public WebSocketMsgContext(Func<string, Task> sendAsync)
{
@@ -31,7 +31,14 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
/// <summary>
/// 标记是否已经处理,如果是,则提前退出
/// </summary>
public bool Handle { get; set; }
public bool Handle { get => _handle; set{
if(value)
{
Dispose();
_handle = value;
}
} }
public bool _handle = false;
/// <summary>
/// 消息本体JObject

View File

@@ -201,13 +201,18 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
/// </summary>
/// <param name="context">此次请求的上下文</param>
/// <returns></returns>
public async Task HandleAsync(WebSocketMsgContext context)
public void Handle(WebSocketMsgContext context)
{
foreach (var module in MyHandleModuleDict.Values)
{
await module.HandleAsync(context);
if (context.Handle)
{
return;
}
_ = module.HandleAsync(context);
}
}

View File

@@ -11,23 +11,25 @@ using System.Threading.Tasks;
namespace Serein.Library.Network.WebSocketCommunication
{
public class MsgQueueUtil
/// <summary>
/// 消息处理工具
/// </summary>
public class MsgHandleUtil
{
public ConcurrentQueue<string> Msgs = new ConcurrentQueue<string>();
private readonly Channel<string> _msgChannel;
public MsgQueueUtil()
{
_msgChannel = CreateChannel();
}
private Channel<string> CreateChannel()
/// <summary>
/// 初始化优先容器
/// </summary>
/// <param name="capacity"></param>
public MsgHandleUtil(int capacity = 100)
{
return Channel.CreateBounded<string>(new BoundedChannelOptions(100)
_msgChannel = Channel.CreateBounded<string>(new BoundedChannelOptions(capacity)
{
FullMode = BoundedChannelFullMode.Wait
});
}
/// <summary>
/// 等待消息
@@ -35,27 +37,45 @@ namespace Serein.Library.Network.WebSocketCommunication
/// <returns></returns>
public async Task<string> WaitMsgAsync()
{
var state = await _msgChannel.Reader.ReadAsync();
return state;
}
public void WriteMsg(string msg)
{
//Msgs.Enqueue(msg);
Console.WriteLine($"{DateTime.Now}{msg}{Environment.NewLine}");
_ = _msgChannel.Writer.WriteAsync(msg);
// 检查是否可以读取消息
if (await _msgChannel.Reader.WaitToReadAsync())
{
return await _msgChannel.Reader.ReadAsync();
}
return null; // 若通道关闭则返回null
}
public bool TryGetMsg(out string msg)
/// <summary>
/// 写入消息
/// </summary>
/// <param name="msg">消息内容</param>
/// <returns>是否写入成功</returns>
public async Task<bool> WriteMsgAsync(string msg)
{
return Msgs.TryDequeue(out msg);
try
{
await _msgChannel.Writer.WriteAsync(msg);
return true;
}
catch (ChannelClosedException)
{
// Channel 已关闭
return false;
}
}
/// <summary>
/// 尝试关闭通道,停止写入消息
/// </summary>
public void CloseChannel()
{
_msgChannel.Writer.Complete();
}
}
public class SocketExtension
{
/// <summary>

View File

@@ -76,7 +76,7 @@ namespace Serein.Library.Network.WebSocketCommunication
private async Task ReceiveAsync()
{
var msgQueueUtil = new MsgQueueUtil();
var msgQueueUtil = new MsgHandleUtil();
_ = Task.Run(async () =>
{
await HandleMsgAsync(_client, msgQueueUtil);
@@ -102,7 +102,7 @@ namespace Serein.Library.Network.WebSocketCommunication
} while (!result.EndOfMessage); // 判断是否已经收到完整消息
var message = receivedMessage.ToString();
msgQueueUtil.WriteMsg(message);
await msgQueueUtil.WriteMsgAsync(message);
receivedMessage.Clear(); // 清空 StringBuilder 为下一条消息做准备
// 处理收到的完整消息
if (result.MessageType == WebSocketMessageType.Close)
@@ -126,8 +126,7 @@ namespace Serein.Library.Network.WebSocketCommunication
}
public async Task HandleMsgAsync(WebSocket webSocket,
MsgQueueUtil msgQueueUtil)
public async Task HandleMsgAsync(WebSocket webSocket, MsgHandleUtil msgQueueUtil)
{
async Task sendasync(string text)
{
@@ -136,11 +135,15 @@ namespace Serein.Library.Network.WebSocketCommunication
while (true)
{
var message = await msgQueueUtil.WaitMsgAsync(); // 有消息时通知
using (var context = new WebSocketMsgContext(sendasync))
{
context.JsonObject = JObject.Parse(message);
await MsgHandleHelper.HandleAsync(context); // 处理消息
}
var context = new WebSocketMsgContext(sendasync);
context.JsonObject = JObject.Parse(message);
MsgHandleHelper.Handle(context); // 处理消息
//using (var context = new WebSocketMsgContext(sendasync))
//{
// context.JsonObject = JObject.Parse(message);
// await MsgHandleHelper.HandleAsync(context); // 处理消息
//}
//_ = Task.Run(() => {
// JObject json = JObject.Parse(message);

View File

@@ -180,7 +180,7 @@ namespace Serein.Library.Network.WebSocketCommunication
return;
}
var msgQueueUtil = new MsgQueueUtil();
var msgQueueUtil = new MsgHandleUtil();
_ = Task.Run(async () =>
{
await HandleMsgAsync(webSocket,msgQueueUtil, authorizedHelper);
@@ -219,7 +219,7 @@ namespace Serein.Library.Network.WebSocketCommunication
// 完整消息已经接收到,准备处理
var message = receivedMessage.ToString(); // 获取消息文本
receivedMessage.Clear(); // 清空 StringBuilder 为下一条消息做准备
msgQueueUtil.WriteMsg(message); // 处理消息
await msgQueueUtil.WriteMsgAsync(message); // 处理消息
}
catch (Exception ex)
{
@@ -231,7 +231,7 @@ namespace Serein.Library.Network.WebSocketCommunication
public async Task HandleMsgAsync(WebSocket webSocket,
MsgQueueUtil msgQueueUtil,
MsgHandleUtil msgQueueUtil,
WebSocketAuthorizedHelper authorizedHelper)
{
async Task sendasync(string text)
@@ -254,18 +254,21 @@ namespace Serein.Library.Network.WebSocketCommunication
return;
}
}
var context = new WebSocketMsgContext(sendasync);
context.JsonObject = JObject.Parse(message);
MsgHandleHelper.Handle(context); // 处理消息
using (var context = new WebSocketMsgContext(sendasync))
{
context.JsonObject = JObject.Parse(message);
await MsgHandleHelper.HandleAsync(context); // 处理消息
}
//using (var context = new WebSocketMsgContext(sendasync))
//{
// context.JsonObject = JObject.Parse(message);
// await MsgHandleHelper.Handle(context); // 处理消息
//}
//_ = Task.Run(() => {
//});
//});
}
}

View File

@@ -91,7 +91,7 @@ namespace Serein.Library
{
Scan = scan;
MethodDynamicType = methodDynamicType;
MethodTips = methodTips;
AnotherName = methodTips;
LockName = lockName;
}
/// <summary>
@@ -101,7 +101,7 @@ namespace Serein.Library
/// <summary>
/// 类似于注释的效果
/// </summary>
public string MethodTips;
public string AnotherName;
/// <summary>
/// 标记节点行为
/// </summary>