mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-26 09:47:54 +08:00
1. 重新设计了Generate项目及相关特性的命名,避免与其他类型混淆。
2. 补充了部分注释。 3. 修改了删除容器节点时,容器内子节点未正确删除的问题。
This commit is contained in:
@@ -13,17 +13,17 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
/// <summary>
|
||||
/// Emit委托
|
||||
/// </summary>
|
||||
public DelegateDetails DelegateDetails { get; set; }
|
||||
public DelegateDetails? DelegateDetails { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 未捕获的异常跟踪
|
||||
/// </summary>
|
||||
public Action<Exception, Action<object>> OnExceptionTracking { get; set; }
|
||||
public Action<Exception, Action<object>>? OnExceptionTracking { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 所使用的实例
|
||||
/// </summary>
|
||||
public Func<ISocketHandleModule> InstanceFactory { get; set; }
|
||||
public Func<ISocketHandleModule>? InstanceFactory { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否需要返回
|
||||
@@ -38,32 +38,32 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
/// <summary>
|
||||
/// 是否使用Data整体内容作为入参参数
|
||||
/// </summary>
|
||||
public bool[] UseData { get; set; }
|
||||
public bool[] UseData { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用Request整体内容作为入参参数
|
||||
/// </summary>
|
||||
public bool[] UseRequest { get; set; }
|
||||
public bool[] UseRequest { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用消息ID作为入参参数
|
||||
/// </summary>
|
||||
public bool[] UseMsgId { get; set; }
|
||||
public bool[] UseMsgId { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 参数名称
|
||||
/// </summary>
|
||||
public string[] ParameterName { get; set; }
|
||||
public string[] ParameterName { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 参数类型
|
||||
/// </summary>
|
||||
public Type[] ParameterType { get; set; }
|
||||
public Type[] ParameterType { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 是否检查变量为空
|
||||
/// </summary>
|
||||
public bool[] IsCheckArgNotNull { get; set; }
|
||||
public bool[] IsCheckArgNotNull { get; set; } = [];
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -86,33 +86,62 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
public async Task HandleAsync(WebSocketMsgContext context)
|
||||
{
|
||||
var jsonObject = context.MsgRequest; // 获取到消息
|
||||
string theme = jsonObject.GetValue(moduleConfig.ThemeJsonKey)?.ToString();
|
||||
if (!MyHandleConfigs.TryGetValue(theme, out var handldConfig))
|
||||
|
||||
if (jsonObject is null)
|
||||
{
|
||||
// SereinEnv.WriteLine(InfoType.WARN, "没有获取到消息");
|
||||
return; // 没有获取到消息
|
||||
}
|
||||
|
||||
// 验证主题
|
||||
if (!jsonObject.TryGetValue(moduleConfig.ThemeJsonKey, out var themeToken)
|
||||
|| themeToken.ToString() is not string theme
|
||||
|| !MyHandleConfigs.TryGetValue(theme, out var handldConfig))
|
||||
{
|
||||
// SereinEnv.WriteLine(InfoType.WARN, $"{theme} 主题不存在");
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证消息ID
|
||||
if (!jsonObject.TryGetValue(moduleConfig.MsgIdJsonKey, out var msgIdToken)
|
||||
|| msgIdToken.ToString() is not string msgId)
|
||||
{
|
||||
// SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 没有消息Id");
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证消息ID是否重复
|
||||
if (!_myMsgIdHash.Add(msgId))
|
||||
{
|
||||
// SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 消息重复");
|
||||
return; // 消息重复
|
||||
}
|
||||
|
||||
// 验证数据
|
||||
if (!jsonObject.TryGetValue(moduleConfig.DataJsonKey, out var dataToken))
|
||||
{
|
||||
// SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 消息重复");
|
||||
return; // 没有主题
|
||||
}
|
||||
context.MsgTheme = theme; // 添加主题
|
||||
string msgId = jsonObject.GetValue(moduleConfig.MsgIdJsonKey)?.ToString();
|
||||
if (_myMsgIdHash.Contains(msgId))
|
||||
{
|
||||
SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 消息重复");
|
||||
return;
|
||||
}
|
||||
context.MsgId = msgId; // 添加 ID
|
||||
_myMsgIdHash.Add(msgId);
|
||||
|
||||
context.MsgTheme = theme; // 添加主题
|
||||
context.MsgId = msgId; // 添加 ID
|
||||
context.MsgData = dataToken; // 添加消息
|
||||
context.MsgRequest = jsonObject; // 添加原始消息
|
||||
try
|
||||
{
|
||||
var dataObj = jsonObject.GetValue(moduleConfig.DataJsonKey);
|
||||
context.MsgData = dataObj; // 添加消息
|
||||
if (TryGetParameters(handldConfig, context, out var args))
|
||||
{
|
||||
var result = await HandleAsync(handldConfig, args);
|
||||
var result = await HandleAsync(handldConfig, args);
|
||||
if (handldConfig.IsReturnValue)
|
||||
{
|
||||
await context.RepliedAsync(moduleConfig, context, result);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 参数获取失败");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -131,9 +160,13 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
/// <param name="config"></param>
|
||||
/// <param name="args"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<object> HandleAsync(HandleConfiguration config, object[] args)
|
||||
public static async Task<object> HandleAsync(HandleConfiguration config, object?[] args)
|
||||
{
|
||||
var instance = config.InstanceFactory.Invoke();
|
||||
if (config.DelegateDetails is null)
|
||||
{
|
||||
throw new InvalidOperationException("DelegateDetails 为 null, 无法进行调用.");
|
||||
}
|
||||
var instance = config.InstanceFactory?.Invoke();
|
||||
var result = await config.DelegateDetails.InvokeAsync(instance, args);
|
||||
return result;
|
||||
}
|
||||
@@ -146,7 +179,7 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
/// <param name="context">处理上下文</param>
|
||||
/// <param name="args">返回的入参参数</param>
|
||||
/// <returns></returns>
|
||||
internal static bool TryGetParameters(HandleConfiguration config, WebSocketMsgContext context, out object[] args)
|
||||
internal static bool TryGetParameters(HandleConfiguration config, WebSocketMsgContext context, out object?[] args)
|
||||
{
|
||||
args = new object[config.ParameterType.Length];
|
||||
bool isCanInvoke = true; ; // 表示是否可以调用方法
|
||||
@@ -165,20 +198,20 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
#region DATA JSON数据
|
||||
else if (config.UseRequest[i])
|
||||
{
|
||||
args[i] = context.MsgRequest.ToObject(type);
|
||||
args[i] = context.MsgRequest?.ToObject(type);
|
||||
}
|
||||
#endregion
|
||||
#region DATA JSON数据
|
||||
else if (config.UseData[i])
|
||||
{
|
||||
args[i] = context.MsgData.ToObject(type);
|
||||
args[i] = context.MsgData?.ToObject(type);
|
||||
}
|
||||
#endregion
|
||||
#region 值类型参数
|
||||
else if (type.IsValueType)
|
||||
{
|
||||
var jsonValue = context.MsgData.GetValue(argName);
|
||||
if (!(jsonValue is null))
|
||||
var jsonValue = context.MsgData?.GetValue(argName);
|
||||
if (jsonValue is not null)
|
||||
{
|
||||
args[i] = jsonValue.ToObject(type);
|
||||
}
|
||||
@@ -200,14 +233,14 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
#region 引用类型参数
|
||||
else if (type.IsClass)
|
||||
{
|
||||
var jsonValue = context.MsgData.GetValue(argName);
|
||||
var jsonValue = context.MsgData?.GetValue(argName);
|
||||
if (!(jsonValue is null))
|
||||
{
|
||||
args[i] = jsonValue.ToObject(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (config.ArgNotNull && !config.IsCheckArgNotNull[i])
|
||||
if (!config.ArgNotNull && !config.IsCheckArgNotNull[i])
|
||||
{
|
||||
|
||||
args[i] = null; // 引用类型返回null
|
||||
|
||||
@@ -8,15 +8,15 @@
|
||||
/// <summary>
|
||||
/// 有关消息ID的 Json Key
|
||||
/// </summary>
|
||||
public string MsgIdJsonKey { get; set; }
|
||||
public string MsgIdJsonKey { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 有关消息主题的 Json Key
|
||||
/// </summary>
|
||||
public string ThemeJsonKey { get; set; }
|
||||
public string ThemeJsonKey { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 有关数据的 Json Key
|
||||
/// </summary>
|
||||
public string DataJsonKey { get; set; }
|
||||
public string DataJsonKey { get; set; } = string.Empty;
|
||||
/// <summary>
|
||||
/// 使用怎么样的数据
|
||||
/// </summary>
|
||||
|
||||
@@ -19,8 +19,8 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
public void Dispose()
|
||||
{
|
||||
MsgRequest = null;
|
||||
MsgTheme = null;
|
||||
MsgId = null;
|
||||
MsgTheme = string.Empty;
|
||||
MsgId = string.Empty;
|
||||
MsgData = null;
|
||||
MsgData = null;
|
||||
_sendAsync = null;
|
||||
@@ -42,25 +42,25 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
/// <summary>
|
||||
/// 消息本体(IJsonToken)
|
||||
/// </summary>
|
||||
public IJsonToken MsgRequest { get; set; }
|
||||
public IJsonToken? MsgRequest { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息请求的主题
|
||||
/// </summary>
|
||||
public string MsgTheme { get; set; }
|
||||
public string MsgTheme { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息附带的ID
|
||||
/// </summary>
|
||||
public string MsgId { get; set; }
|
||||
public string MsgId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息的数据
|
||||
/// </summary>
|
||||
public IJsonToken MsgData { get; set; }
|
||||
public IJsonToken? MsgData { get; set; }
|
||||
|
||||
|
||||
private Func<string, Task> _sendAsync;
|
||||
private Func<string, Task>? _sendAsync;
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
@@ -69,6 +69,7 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
/// <returns></returns>
|
||||
public async Task SendAsync(string msg)
|
||||
{
|
||||
if (_sendAsync is null) return;
|
||||
await _sendAsync.Invoke(msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,11 +17,10 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
public ConcurrentDictionary<(string, string), WebSocketHandleModule> MyHandleModuleDict
|
||||
= new ConcurrentDictionary<(string, string), WebSocketHandleModule>();
|
||||
|
||||
private Action<Exception, Action<object>> _onExceptionTracking;
|
||||
/// <summary>
|
||||
/// 异常跟踪
|
||||
/// </summary>
|
||||
public event Action<Exception, Action<object>> OnExceptionTracking;
|
||||
public Action<Exception, Action<object>>? OnExceptionTracking;
|
||||
|
||||
/// <summary>
|
||||
/// 添加消息处理与异常处理
|
||||
@@ -92,12 +91,12 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
|
||||
var handleModule = AddMyHandleModule(moduleConfig);
|
||||
var configs = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Select<MethodInfo, WebSocketHandleConfiguration>(methodInfo =>
|
||||
.Select<MethodInfo, WebSocketHandleConfiguration?>(methodInfo =>
|
||||
{
|
||||
var methodsAttribute = methodInfo.GetCustomAttribute<AutoSocketHandleAttribute>();
|
||||
if (methodsAttribute is null)
|
||||
{
|
||||
return null;
|
||||
return default;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -135,7 +134,7 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
config.InstanceFactory = instanceFactory; // 调用emit委托时的实例
|
||||
config.OnExceptionTracking = onExceptionTracking; // 异常追踪
|
||||
config.ParameterType = parameterInfos.Select(t => t.ParameterType).ToArray(); // 入参参数类型
|
||||
config.ParameterName = parameterInfos.Select(t => t.Name).ToArray(); // 入参参数名称
|
||||
config.ParameterName = parameterInfos.Select(t => $"{t.Name}").ToArray(); // 入参参数名称
|
||||
config.UseRequest = parameterInfos.Select(p => p.GetCustomAttribute<UseRequestAttribute>() != null).ToArray(); // 是否使用整体data数据
|
||||
config.UseData = parameterInfos.Select(p => p.GetCustomAttribute<UseDataAttribute>() != null).ToArray(); // 是否使用整体data数据
|
||||
config.UseMsgId = parameterInfos.Select(p => p.GetCustomAttribute<UseMsgIdAttribute>() != null).ToArray(); // 是否使用消息ID
|
||||
@@ -179,7 +178,7 @@ namespace Serein.Proto.WebSocket.Handle
|
||||
SereinEnv.WriteLine(InfoType.INFO, $"theme key, data key : {themeKey}, {dataKey}");
|
||||
foreach (var config in configs)
|
||||
{
|
||||
SereinEnv.WriteLine(InfoType.INFO, $"theme value : {config.ThemeValue} ");
|
||||
SereinEnv.WriteLine(InfoType.INFO, $"theme value : {config!.ThemeValue} ");
|
||||
var result = handleModule.AddHandleConfigs(config);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user