重新设计了Libray.Json Api以及 WebSocket 的交互处理方式

This commit is contained in:
fengjiayi
2025-08-02 10:48:31 +08:00
parent 1bccccc835
commit 6fc57458a7
29 changed files with 883 additions and 348 deletions

View File

@@ -1,14 +0,0 @@
using System;
namespace Serein.Proto.WebSocket.Handle
{
/// <summary>
/// 表示参数不能为空(Net462不能使用NutNull的情况
/// </summary>
public sealed class NeedfulAttribute : Attribute
{
}
}

View File

@@ -1,4 +1,5 @@
using Serein.Library;
using static Serein.Proto.WebSocket.SereinWebSocketService;
@@ -8,18 +9,13 @@ namespace Serein.Proto.WebSocket.Handle
/// socket模块处理数据配置
/// </summary>
public class HandleConfiguration
public class MethodInvokeConfiguration
{
/// <summary>
/// Emit委托
/// </summary>
public DelegateDetails? DelegateDetails { get; set; }
/// <summary>
/// 未捕获的异常跟踪
/// </summary>
public Action<Exception, Action<object>>? OnExceptionTracking { get; set; }
/// <summary>
/// 所使用的实例
/// </summary>
@@ -30,11 +26,6 @@ namespace Serein.Proto.WebSocket.Handle
/// </summary>
public bool IsReturnValue { get; set; } = true;
/// <summary>
/// 是否要求必须不为null
/// </summary>
public bool ArgNotNull { get; set; } = true;
/// <summary>
/// 是否使用Data整体内容作为入参参数
/// </summary>
@@ -45,6 +36,21 @@ namespace Serein.Proto.WebSocket.Handle
/// </summary>
public bool[] UseRequest { get; set; } = [];
/// <summary>
/// 是否需要发送消息的委托
/// </summary>
public bool[] IsNeedSendDelegate { get; set; } = [];
/// <summary>
/// 发送消息的委托类型
/// </summary>
public SendType[] SendDelegateType { get; set; } = [];
/// <summary>
/// 缓存的发送委托数组
/// </summary>
public Delegate?[] CachedSendDelegates ;
/// <summary>
/// 是否使用消息ID作为入参参数
/// </summary>
@@ -60,11 +66,6 @@ namespace Serein.Proto.WebSocket.Handle
/// </summary>
public Type[] ParameterType { get; set; } = [];
/// <summary>
/// 是否检查变量为空
/// </summary>
public bool[] IsCheckArgNotNull { get; set; } = [];
}

View File

@@ -1,8 +1,10 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
namespace Serein.Proto.WebSocket.Handle
@@ -16,35 +18,38 @@ namespace Serein.Proto.WebSocket.Handle
/// <summary>
/// Json消息处理模块
/// </summary>
public WebSocketHandleModule(WebSocketHandleModuleConfig config)
public WebSocketHandleModule(WebSocketModuleConfig config)
{
moduleConfig = config;
_moduleConfig = config;
_methodInvokeConfigs = new ConcurrentDictionary<string, MethodInvokeConfiguration>();
_myMsgIdHash = new HashSet<string>();
}
/// <summary>
/// 模块的处理配置
/// </summary>
private readonly WebSocketHandleModuleConfig moduleConfig;
private readonly WebSocketModuleConfig _moduleConfig;
/// <summary>
/// 用来判断消息是否重复
/// </summary>
private HashSet<string> _myMsgIdHash = new HashSet<string>();
private readonly HashSet<string> _myMsgIdHash;
/// <summary>
/// 存储处理数据的配置
/// </summary>
public ConcurrentDictionary<string, HandleConfiguration> MyHandleConfigs = new ConcurrentDictionary<string, HandleConfiguration>();
private readonly ConcurrentDictionary<string, MethodInvokeConfiguration> _methodInvokeConfigs ;
/// <summary>
/// 添加处理配置
/// </summary>
/// <param name="config">处理模块</param>
internal bool AddHandleConfigs(WebSocketHandleConfiguration config)
internal bool AddHandleConfigs(WebSocketMethodConfig config)
{
if (!MyHandleConfigs.ContainsKey(config.ThemeValue))
if (!_methodInvokeConfigs.ContainsKey(config.ThemeValue))
{
MyHandleConfigs[config.ThemeValue] = config;
_methodInvokeConfigs[config.ThemeValue] = config;
return true;
}
else
@@ -60,13 +65,13 @@ namespace Serein.Proto.WebSocket.Handle
/// <returns></returns>
public bool RemoveConfig(ISocketHandleModule socketControlBase)
{
foreach (var kv in MyHandleConfigs.ToArray())
foreach (var kv in _methodInvokeConfigs.ToArray())
{
var config = kv.Value;
MyHandleConfigs.TryRemove(kv.Key, out _);
_methodInvokeConfigs.TryRemove(kv.Key, out _);
}
return MyHandleConfigs.Count == 0;
return _methodInvokeConfigs.Count == 0;
}
/// <summary>
@@ -74,8 +79,8 @@ namespace Serein.Proto.WebSocket.Handle
/// </summary>
public void UnloadConfig()
{
var temp = MyHandleConfigs.Values;
MyHandleConfigs.Clear();
var temp = _methodInvokeConfigs.Values;
_methodInvokeConfigs.Clear();
}
@@ -83,46 +88,64 @@ namespace Serein.Proto.WebSocket.Handle
/// <summary>
/// 处理JSON数据
/// </summary>
public async Task HandleAsync(WebSocketMsgContext context)
public async Task HandleAsync(WebSocketHandleContext context)
{
var jsonObject = context.MsgRequest; // 获取到消息
if (jsonObject is null)
{
// SereinEnv.WriteLine(InfoType.WARN, "没有获取到消息");
context.TriggerExceptionTracking($"请求没有获取到消息");
return; // 没有获取到消息
}
if(!jsonObject.TryGetValue(_moduleConfig.ThemeJsonKey, out var themeToken) || themeToken.IsNull)
{
context.TriggerExceptionTracking($"请求没有获取到主题\"{_moduleConfig.ThemeJsonKey}\"");
return; // 没有获取到消息
}
if(themeToken.Type != IJsonToken.TokenType.Value)
{
context.TriggerExceptionTracking($"请求主题需要值类型 \"{_moduleConfig.ThemeJsonKey}\"");
return; // 没有获取到消息
}
var theme = themeToken.ToString(); // 获取主题
// 验证主题
if (!jsonObject.TryGetValue(moduleConfig.ThemeJsonKey, out var themeToken)
|| themeToken.ToString() is not string theme
|| !MyHandleConfigs.TryGetValue(theme, out var handldConfig))
if (!_methodInvokeConfigs.TryGetValue(theme, out var handldConfig))
{
// SereinEnv.WriteLine(InfoType.WARN, $"{theme} 主题不存在");
context.TriggerExceptionTracking($"{_moduleConfig.ThemeJsonKey} 主题不存在");
return;
}
// 验证消息ID
if (!jsonObject.TryGetValue(moduleConfig.MsgIdJsonKey, out var msgIdToken)
|| msgIdToken.ToString() is not string msgId)
if (!jsonObject.TryGetValue(_moduleConfig.MsgIdJsonKey, out var msgIdToken) || themeToken.IsNull)
{
// SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 没有消息Id");
return;
context.TriggerExceptionTracking($"主题 {theme} 没有消息Id");
return; // 没有获取到消息
}
if (themeToken.Type != IJsonToken.TokenType.Value)
{
context.TriggerExceptionTracking($"请求消息Id需要值类型 \"{_moduleConfig.ThemeJsonKey}\"");
return; // 没有获取到消息
}
var msgId = msgIdToken.ToString(); // 获取主题
// 验证消息ID是否重复
if (!_myMsgIdHash.Add(msgId))
{
// SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 消息重复");
context.TriggerExceptionTracking($"主题 {theme} 消息Id {msgId} 消息重复");
return; // 消息重复
}
// 验证数据
if (!jsonObject.TryGetValue(moduleConfig.DataJsonKey, out var dataToken))
if (!jsonObject.TryGetValue(_moduleConfig.DataJsonKey, out var dataToken))
{
// SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 消息重复");
context.TriggerExceptionTracking($"主题 {theme} 消息Id {msgId} 数据提取失败,当前指定键\"{_moduleConfig.DataJsonKey}\"");
return; // 没有主题
}
if(dataToken.Type != IJsonToken.TokenType.Object)
{
context.TriggerExceptionTracking($"主题 {theme} 消息Id {msgId} 数据需要 JSON Object");
}
context.MsgTheme = theme; // 添加主题
context.MsgId = msgId; // 添加 ID
@@ -135,17 +158,17 @@ namespace Serein.Proto.WebSocket.Handle
var result = await HandleAsync(handldConfig, args);
if (handldConfig.IsReturnValue)
{
await context.RepliedAsync(moduleConfig, context, result);
await RepliedAsync(_moduleConfig, context, result);
}
}
else
{
SereinEnv.WriteLine(InfoType.WARN, $"[{msgId}]{theme} 参数获取失败");
context.TriggerExceptionTracking($"主题 {theme} 消息Id {msgId} 参数获取失败");
}
}
catch (Exception ex)
{
SereinEnv.WriteLine(InfoType.ERROR, $"error in ws : {ex.Message}{Environment.NewLine}json value:{jsonObject}");
context.TriggerExceptionTracking(ex);
}
finally
{
@@ -153,6 +176,7 @@ namespace Serein.Proto.WebSocket.Handle
}
}
/// <summary>
/// 调用
@@ -160,7 +184,7 @@ 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(MethodInvokeConfiguration config, object?[] args)
{
if (config.DelegateDetails is null)
{
@@ -179,14 +203,15 @@ 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(MethodInvokeConfiguration config, WebSocketHandleContext context, out object?[] args)
{
args = new object[config.ParameterType.Length];
var theme = context.MsgTheme;
var msgId = context.MsgId;
List<string> exTips = [$"主题 {theme} 消息Id {msgId}"];
bool isCanInvoke = true; ; // 表示是否可以调用方法
for (int i = 0; i < config.ParameterType.Length; i++)
{
var type = config.ParameterType[i]; // 入参变量类型
var argName = config.ParameterName[i]; // 入参参数名称
#region ID
@@ -207,93 +232,123 @@ namespace Serein.Proto.WebSocket.Handle
args[i] = context.MsgData?.ToObject(type);
}
#endregion
#region
else if (type.IsValueType)
#region
else if (!config.IsNeedSendDelegate[i])
{
var jsonValue = context.MsgData?.GetValue(argName);
if (jsonValue is not null)
if(jsonValue is null)
{
args[i] = jsonValue.ToObject(type);
isCanInvoke = false;
exTips.Add($"参数 {argName}({i}) 不存在,请检查参数名称是否正确");
continue;
}
else
var data = jsonValue.ToObject(type);
if (data is null)
{
if (config.ArgNotNull && !config.IsCheckArgNotNull[i]) // 检查不能为空
{
args[i] = Activator.CreateInstance(type); // 值类型返回默认值
}
else
{
isCanInvoke = false; // 参数不能为空,终止调用
break;
}
}
}
#endregion
#region
else if (type.IsClass)
{
var jsonValue = context.MsgData?.GetValue(argName);
if (!(jsonValue is null))
{
args[i] = jsonValue.ToObject(type);
}
else
{
if (!config.ArgNotNull && !config.IsCheckArgNotNull[i])
{
args[i] = null; // 引用类型返回null
}
else
{
isCanInvoke = false; // 参数不能为空,终止调用
break;
}
isCanInvoke = false;
exTips.Add($"参数 {argName}({i}) 解析失败,类型:{type.FullName},值:{jsonValue},请检查参数类型是否正确");
continue;
}
args[i] = data;
}
#endregion
#region
else if (type.IsGenericType) // 传递SendAsync委托
else if (config.IsNeedSendDelegate[i]) // 传递SendAsync委托
{
if (type.IsAssignableFrom(typeof(Func<object, Task>)))
if (config.CachedSendDelegates != null && config.CachedSendDelegates[i] != null)
{
args[i] = new Func<object, Task>(async data =>
{
var jsonText = JsonHelper.Serialize(data);
await context.SendAsync(jsonText);
});
args[i] = config.CachedSendDelegates[i];
continue;
}
else if (type.IsAssignableFrom(typeof(Func<string, Task>)))
Delegate? del = null;
var st = config.SendDelegateType[i];
switch (st)
{
args[i] = new Func<string, Task>(async data =>
{
await context.SendAsync(data);
});
case SereinWebSocketService.SendType.ObjectAsync:
del = new Func<object, Task>(async data =>
{
var jsonText = JsonHelper.Serialize(data);
await context.SendAsync(jsonText);
});
break;
case SereinWebSocketService.SendType.StringAsync:
del = new Func<string, Task>(async data =>
{
await context.SendAsync(data);
});
break;
case SereinWebSocketService.SendType.Object:
del = new Action<object>(data =>
{
var jsonText = JsonHelper.Serialize(data);
_ = context.SendAsync(jsonText);
});
break;
case SereinWebSocketService.SendType.String:
del = new Action<string>(data =>
{
_ = context.SendAsync(data);
});
break;
}
else if (type.IsAssignableFrom(typeof(Action<object>)))
if (del is not null)
{
args[i] = new Action<object>(async data =>
{
var jsonText = JsonHelper.Serialize(data);
await context.SendAsync(jsonText);
});
config.CachedSendDelegates![i] = del;
args[i] = del;
}
else if (type.IsAssignableFrom(typeof(Action<string>)))
else
{
args[i] = new Action<string>(async data =>
{
var jsonText = JsonHelper.Serialize(data);
await context.SendAsync(jsonText);
});
isCanInvoke = false; // 方法要求参数不能为空,终止调用
exTips.Add($"参数 {argName}({i}) 发送委托类型错误");
break;
}
}
#endregion
}
if (!isCanInvoke)
{
string ex = string.Join(Environment.NewLine, exTips);
context.TriggerExceptionTracking(ex);
}
return isCanInvoke;
}
/// <summary>
/// 返回消息
/// </summary>
/// <param name="moduleConfig"></param>
/// <param name="context"></param>
/// <param name="data"></param>
/// <returns></returns>
public async Task RepliedAsync(WebSocketModuleConfig moduleConfig,
WebSocketHandleContext context,
object data)
{
if (moduleConfig.IsResponseUseReturn)
{
var responseContent = JsonHelper.Serialize(data);
await context.SendAsync(responseContent);
}
else
{
IJsonToken jsonData;
jsonData = JsonHelper.Object(obj =>
{
obj[moduleConfig.MsgIdJsonKey] = context.MsgId;
obj[moduleConfig.ThemeJsonKey] = context.MsgTheme;
obj[moduleConfig.DataJsonKey] = data is null ? null : JsonHelper.FromObject(data);
});
var msg = jsonData.ToString();
await context.SendAsync(msg);
}
}
}

View File

@@ -1,6 +1,6 @@
namespace Serein.Proto.WebSocket.Handle
{
internal class WebSocketHandleConfiguration : HandleConfiguration
public class WebSocketMethodConfig : MethodInvokeConfiguration
{
/// <summary>
/// 主题

View File

@@ -3,20 +3,23 @@
/// <summary>
/// 远程环境配置
/// </summary>
public class WebSocketHandleModuleConfig
public class WebSocketModuleConfig
{
/// <summary>
/// 有关消息ID的 Json Key
/// </summary>
public string MsgIdJsonKey { get; set; } = string.Empty;
/// <summary>
/// 有关消息主题的 Json Key
/// </summary>
public string ThemeJsonKey { get; set; } = string.Empty;
/// <summary>
/// 有关数据的 Json Key
/// </summary>
public string DataJsonKey { get; set; } = string.Empty;
/// <summary>
/// 使用怎么样的数据
/// </summary>

View File

@@ -1,116 +0,0 @@
using Serein.Library.Api;
using Serein.Library.Utils;
using System;
using System.Threading.Tasks;
namespace Serein.Proto.WebSocket.Handle
{
/// <summary>
/// 消息处理上下文
/// </summary>
public class WebSocketMsgContext : IDisposable
{
public WebSocketMsgContext(Func<string, Task> sendAsync)
{
_sendAsync = sendAsync;
}
public void Dispose()
{
MsgRequest = null;
MsgTheme = string.Empty;
MsgId = string.Empty;
MsgData = null;
MsgData = null;
_sendAsync = null;
}
/// <summary>
/// 标记是否已经处理,如果是,则提前退出
/// </summary>
public bool Handle { get => _handle; set{
if(value)
{
Dispose();
_handle = value;
}
} }
public bool _handle = false;
/// <summary>
/// 消息本体IJsonToken
/// </summary>
public IJsonToken? MsgRequest { get; set; }
/// <summary>
/// 此次消息请求的主题
/// </summary>
public string MsgTheme { get; set; } = string.Empty;
/// <summary>
/// 此次消息附带的ID
/// </summary>
public string MsgId { get; set; } = string.Empty;
/// <summary>
/// 此次消息的数据
/// </summary>
public IJsonToken? MsgData { get; set; }
private Func<string, Task>? _sendAsync;
/// <summary>
/// 发送消息
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public async Task SendAsync(string msg)
{
if (_sendAsync is null) return;
await _sendAsync.Invoke(msg);
}
/// <summary>
/// 返回消息
/// </summary>
/// <param name="moduleConfig"></param>
/// <param name="context"></param>
/// <param name="data"></param>
/// <returns></returns>
public async Task RepliedAsync(WebSocketHandleModuleConfig moduleConfig,
WebSocketMsgContext context,
object data)
{
if (moduleConfig.IsResponseUseReturn)
{
var responseContent = JsonHelper.Serialize(data);
await SendAsync(responseContent);
}
else
{
IJsonToken jsonData;
jsonData = JsonHelper.Object(obj =>
{
obj[moduleConfig.MsgIdJsonKey] = context.MsgId;
obj[moduleConfig.ThemeJsonKey] = context.MsgTheme;
obj[moduleConfig.DataJsonKey] = data is null ? null
: JsonHelper.FromObject(data);
});
var msg = jsonData.ToString();
//Console.WriteLine($"[{msgId}] => {theme}");
await SendAsync(msg);
}
}
}
}

View File

@@ -3,6 +3,7 @@ using Serein.Proto.WebSocket.Attributes;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Threading.Tasks;
namespace Serein.Proto.WebSocket.Handle
{
@@ -27,7 +28,7 @@ namespace Serein.Proto.WebSocket.Handle
/// </summary>
/// <param name="moduleConfig">模块配置</param>
/// <returns></returns>
private WebSocketHandleModule AddMyHandleModule(WebSocketHandleModuleConfig moduleConfig)
private WebSocketHandleModule AddMsgHandleModule(WebSocketHandleModuleConfig moduleConfig)
{
var key = (moduleConfig.ThemeJsonKey, moduleConfig.DataJsonKey);
if (!MyHandleModuleDict.TryGetValue(key, out var myHandleModule))
@@ -45,7 +46,7 @@ namespace Serein.Proto.WebSocket.Handle
public void RemoveModule(ISocketHandleModule socketControlBase)
{
var type = socketControlBase.GetType();
var moduleAttribute = type.GetCustomAttribute<AutoSocketModuleAttribute>();
var moduleAttribute = type.GetCustomAttribute<WebSocketModuleAttribute>();
if (moduleAttribute is null)
{
return;
@@ -71,7 +72,7 @@ namespace Serein.Proto.WebSocket.Handle
where T : ISocketHandleModule
{
var type = typeof(T);
var moduleAttribute = type.GetCustomAttribute<AutoSocketModuleAttribute>();
var moduleAttribute = type.GetCustomAttribute<WebSocketModuleAttribute>();
if (moduleAttribute is null)
{
return;
@@ -89,11 +90,11 @@ namespace Serein.Proto.WebSocket.Handle
IsResponseUseReturn = isResponseUseReturn,
};
var handleModule = AddMyHandleModule(moduleConfig);
var handleModule = AddMsgHandleModule(moduleConfig);
var configs = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Select<MethodInfo, WebSocketHandleConfiguration?>(methodInfo =>
.Select<MethodInfo, WebSocketMethodHandleConfig?>(methodInfo =>
{
var methodsAttribute = methodInfo.GetCustomAttribute<AutoSocketHandleAttribute>();
var methodsAttribute = methodInfo.GetCustomAttribute<WsMethodAttribute>();
if (methodsAttribute is null)
{
return default;
@@ -105,8 +106,7 @@ namespace Serein.Proto.WebSocket.Handle
methodsAttribute.ThemeValue = methodInfo.Name;
}
#region
var config = new WebSocketHandleConfiguration();
var config = new WebSocketMethodHandleConfig();
config.ThemeValue = methodsAttribute.ThemeValue;
config.ArgNotNull = methodsAttribute.ArgNotNull;
@@ -142,24 +142,7 @@ namespace Serein.Proto.WebSocket.Handle
config.IsCheckArgNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NotNullAttribute>() != null).ToArray(); // 是否检查null
#endif
if (config.IsCheckArgNotNull is null)
{
config.IsCheckArgNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NeedfulAttribute>() != null).ToArray(); // 是否检查null
}
else
{
// 兼容两种非空特性的写法
var argNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NeedfulAttribute>() != null).ToArray(); // 是否检查null
for (int i = 0; i < config.IsCheckArgNotNull.Length; i++)
{
if (!config.IsCheckArgNotNull[i] && argNotNull[i])
{
config.IsCheckArgNotNull[i] = true;
}
}
}
#endregion
var value = methodsAttribute.ThemeValue;
@@ -189,7 +172,7 @@ namespace Serein.Proto.WebSocket.Handle
/// </summary>
/// <param name="context">此次请求的上下文</param>
/// <returns></returns>
public void Handle(WebSocketMsgContext context)
public async Task HandleAsync(WebSocketMsgContext context)
{
foreach (var module in MyHandleModuleDict.Values)
{
@@ -197,7 +180,7 @@ namespace Serein.Proto.WebSocket.Handle
{
return;
}
_ = module.HandleAsync(context);
await module.HandleAsync(context);
}