将部分节点基类与表达式工具类从nodeflow迁移到library,重写了环境与工作台的交互,解耦节点的获取,下一部分将尝试远程登录环境编辑流程。

This commit is contained in:
fengjiayi
2024-10-15 10:55:41 +08:00
parent 4338554384
commit dbbde4f03e
48 changed files with 3687 additions and 292 deletions

View File

@@ -15,14 +15,17 @@ using System.Threading.Tasks;
namespace Serein.Library.Network.WebSocketCommunication.Handle
{
public class WebSocketHandleConfig
/// <summary>
///
/// </summary>
public class JsonMsgHandleConfig
{
private readonly Delegate EmitDelegate;
private readonly EmitHelper.EmitMethodType EmitMethodType;
private Action<Exception, Action<object>> OnExceptionTracking;
internal WebSocketHandleConfig(SocketHandleModel model,ISocketHandleModule instance, MethodInfo methodInfo, Action<Exception, Action<object>> onExceptionTracking)
internal JsonMsgHandleConfig(SocketHandleModel model,ISocketHandleModule instance, MethodInfo methodInfo, Action<Exception, Action<object>> onExceptionTracking)
{
EmitMethodType = EmitHelper.CreateDynamicMethod(methodInfo,out EmitDelegate);
this.Model = model;

View File

@@ -8,30 +8,51 @@ using System.Threading.Tasks;
namespace Serein.Library.Network.WebSocketCommunication.Handle
{
/// <summary>
/// Json消息处理模块
/// </summary>
public class WebSocketHandleModule
{
/// <summary>
/// Json消息处理模块
/// </summary>
public WebSocketHandleModule(string ThemeJsonKey, string DataJsonKey)
{
this.ThemeJsonKey = ThemeJsonKey;
this.DataJsonKey = DataJsonKey;
}
/// <summary>
/// 指示处理模块该使用json中的哪个key作为业务区别字段
/// </summary>
public string ThemeJsonKey { get; }
/// <summary>
/// 指示处理模块该使用json中的哪个key作为业务数据字段
/// </summary>
public string DataJsonKey { get; }
/// <summary>
/// 存储处理数据的配置
/// </summary>
public ConcurrentDictionary<string, JsonMsgHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, JsonMsgHandleConfig>();
public ConcurrentDictionary<string, WebSocketHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, WebSocketHandleConfig>();
internal void AddHandleConfigs(SocketHandleModel model, ISocketHandleModule instance, MethodInfo methodInfo
, Action<Exception, Action<object>> onExceptionTracking)
{
if (!MyHandleConfigs.ContainsKey(model.ThemeValue))
{
var myHandleConfig = new WebSocketHandleConfig(model,instance, methodInfo, onExceptionTracking);
MyHandleConfigs[model.ThemeValue] = myHandleConfig;
var jsonMsgHandleConfig = new JsonMsgHandleConfig(model,instance, methodInfo, onExceptionTracking);
MyHandleConfigs[model.ThemeValue] = jsonMsgHandleConfig;
}
}
public bool ResetConfig(ISocketHandleModule socketControlBase)
/// <summary>
/// 移除某个处理模块
/// </summary>
/// <param name="socketControlBase"></param>
/// <returns></returns>
public bool RemoveConfig(ISocketHandleModule socketControlBase)
{
foreach (var kv in MyHandleConfigs.ToArray())
{
@@ -44,7 +65,10 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
return MyHandleConfigs.Count == 0;
}
public void ResetConfig()
/// <summary>
/// 卸载当前模块的所有配置
/// </summary>
public void UnloadConfig()
{
var temp = MyHandleConfigs.Values;
MyHandleConfigs.Clear();
@@ -54,7 +78,11 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
}
}
/// <summary>
/// 处理JSON数据
/// </summary>
/// <param name="RecoverAsync"></param>
/// <param name="jsonObject"></param>
public void HandleSocketMsg(Func<string, Task> RecoverAsync, JObject jsonObject)
{
// 获取到消息

View File

@@ -19,7 +19,9 @@ using System.Security.Cryptography;
namespace Serein.Library.Network.WebSocketCommunication.Handle
{
/// <summary>
/// 适用于Json数据格式的WebSocket消息处理类
/// </summary>
public class WebSocketMsgHandleHelper
{
/// <summary>
@@ -68,7 +70,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
var key = (themeKeyName, dataKeyName);
if (MyHandleModuleDict.TryGetValue(key, out var myHandleModules))
{
var isRemote = myHandleModules.ResetConfig(socketControlBase);
var isRemote = myHandleModules.RemoveConfig(socketControlBase);
if (isRemote) MyHandleModuleDict.TryGetValue(key, out _);
}
@@ -132,6 +134,12 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
}
/// <summary>
/// 异步处理消息
/// </summary>
/// <param name="RecoverAsync"></param>
/// <param name="message"></param>
/// <returns></returns>
public async Task HandleMsgAsync(Func<string, Task> RecoverAsync, string message)
{
JObject json = JObject.Parse(message);

View File

@@ -14,12 +14,25 @@ using System.Threading.Tasks;
namespace Serein.Library.Network.WebSocketCommunication
{
/// <summary>
/// WebSocket服务类
/// </summary>
[AutoRegister]
public class WebSocketServer
{
/// <summary>
/// 消息处理
/// </summary>
public WebSocketMsgHandleHelper MsgHandleHelper { get; } = new WebSocketMsgHandleHelper();
private HttpListener listener;
/// <summary>
/// 进行监听服务
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public async Task StartAsync(string url)
{
listener = new HttpListener();
@@ -58,6 +71,9 @@ namespace Serein.Library.Network.WebSocketCommunication
}
}
/// <summary>
/// 停止监听服务
/// </summary>
public void Stop()
{
listener?.Stop();
@@ -96,6 +112,12 @@ namespace Serein.Library.Network.WebSocketCommunication
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="webSocket"></param>
/// <param name="message"></param>
/// <returns></returns>
public static async Task SendAsync(WebSocket webSocket, string message)
{
var buffer = Encoding.UTF8.GetBytes(message);