mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
1. 重新设计了Generate项目及相关特性的命名,避免与其他类型混淆。
2. 补充了部分注释。 3. 修改了删除容器节点时,容器内子节点未正确删除的问题。
This commit is contained in:
@@ -21,15 +21,15 @@
|
||||
/// <summary>
|
||||
/// 业务标识
|
||||
/// </summary>
|
||||
public string ThemeKey;
|
||||
public string ThemeKey = string.Empty;
|
||||
/// <summary>
|
||||
/// 数据标识
|
||||
/// </summary>
|
||||
public string DataKey;
|
||||
public string DataKey = string.Empty;
|
||||
/// <summary>
|
||||
/// ID标识
|
||||
/// </summary>
|
||||
public string MsgIdKey;
|
||||
public string MsgIdKey = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// 指示应答数据回复方法返回值
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Serein.Proto.WebSocket
|
||||
{
|
||||
return await _msgChannel.Reader.ReadAsync();
|
||||
}
|
||||
return null; // 若通道关闭,则返回null
|
||||
return string.Empty; // 若通道关闭,则返回null
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -52,13 +52,17 @@ namespace Serein.Proto.WebSocket
|
||||
/// <param name="message"></param>
|
||||
public async Task<bool> HandleAuthorized(string message)
|
||||
{
|
||||
if (InspectionAuthorizedFunc is null)
|
||||
{
|
||||
return false; // 如果没有授权方法,则默认解决
|
||||
}
|
||||
await semaphoreSlim.WaitAsync(1);
|
||||
bool isAuthorized = false;
|
||||
IJsonToken json = JsonHelper.Parse(message);
|
||||
if(json.TryGetValue(TokenKey,out var token))
|
||||
{
|
||||
// 交给之前定义的授权方法进行判断
|
||||
isAuthorized = await InspectionAuthorizedFunc?.Invoke(token);
|
||||
isAuthorized = await InspectionAuthorizedFunc.Invoke(token);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -81,7 +85,7 @@ namespace Serein.Proto.WebSocket
|
||||
/// </summary>
|
||||
public WebSocketMsgHandleHelper MsgHandleHelper { get; } = new WebSocketMsgHandleHelper();
|
||||
|
||||
private HttpListener listener;
|
||||
private HttpListener? listener;
|
||||
|
||||
/// <summary>
|
||||
/// 创建无须授权验证的WebSocket服务端
|
||||
@@ -110,9 +114,11 @@ namespace Serein.Proto.WebSocket
|
||||
/// 授权
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<string, WebSocketAuthorizedHelper> AuthorizedClients;
|
||||
private readonly string TokenKey;
|
||||
private readonly string TokenKey = string.Empty;
|
||||
private readonly Func<dynamic, Task<bool>> InspectionAuthorizedFunc;
|
||||
private bool IsCheckToken = false;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 进行监听服务
|
||||
/// </summary>
|
||||
@@ -138,13 +144,14 @@ namespace Serein.Proto.WebSocket
|
||||
try
|
||||
{
|
||||
var context = await listener.GetContextAsync();
|
||||
string clientPoint = context.Request.RemoteEndPoint?.ToString();
|
||||
string clientPoint = context.Request.RemoteEndPoint.ToString();
|
||||
|
||||
await Console.Out.WriteLineAsync($"新的连接加入:{clientPoint}");
|
||||
|
||||
if (context.Request.IsWebSocketRequest)
|
||||
{
|
||||
WebSocketAuthorizedHelper authorizedHelper = null;
|
||||
#error "需要重写 WebSocket 服务"
|
||||
WebSocketAuthorizedHelper? authorizedHelper = null;
|
||||
if (IsCheckToken)
|
||||
{
|
||||
if (AuthorizedClients.TryAdd(clientPoint, new WebSocketAuthorizedHelper(clientPoint, TokenKey, InspectionAuthorizedFunc)))
|
||||
@@ -154,7 +161,7 @@ namespace Serein.Proto.WebSocket
|
||||
}
|
||||
|
||||
var webSocketContext = await context.AcceptWebSocketAsync(null); //新连接
|
||||
_ = HandleWebSocketAsync(webSocketContext.WebSocket, authorizedHelper); // 处理消息
|
||||
_ = HandleWebSocketAsync(webSocketContext.WebSocket, authorizedHelper!); // 处理消息
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user