从Serein.Library分离了WebSocket/Modbus;新增了Json门户类,用于未来的Http、WebSocket、Mqtt、gRPC、QUIC扩展。

This commit is contained in:
fengjiayi
2025-07-27 23:34:01 +08:00
parent ab2adfde80
commit d3c3210ccc
70 changed files with 2306 additions and 554 deletions

View File

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

View File

@@ -0,0 +1,73 @@
using Serein.Library;
namespace Serein.Proto.WebSocket.Handle
{
/// <summary>
/// socket模块处理数据配置
/// </summary>
public class HandleConfiguration
{
/// <summary>
/// Emit委托
/// </summary>
public DelegateDetails DelegateDetails { get; set; }
/// <summary>
/// 未捕获的异常跟踪
/// </summary>
public Action<Exception, Action<object>> OnExceptionTracking { get; set; }
/// <summary>
/// 所使用的实例
/// </summary>
public Func<ISocketHandleModule> InstanceFactory { get; set; }
/// <summary>
/// 是否需要返回
/// </summary>
public bool IsReturnValue { get; set; } = true;
/// <summary>
/// 是否要求必须不为null
/// </summary>
public bool ArgNotNull { get; set; } = true;
/// <summary>
/// 是否使用Data整体内容作为入参参数
/// </summary>
public bool[] UseData { get; set; }
/// <summary>
/// 是否使用Request整体内容作为入参参数
/// </summary>
public bool[] UseRequest { get; set; }
/// <summary>
/// 是否使用消息ID作为入参参数
/// </summary>
public bool[] UseMsgId { get; set; }
/// <summary>
/// 参数名称
/// </summary>
public string[] ParameterName { get; set; }
/// <summary>
/// 参数类型
/// </summary>
public Type[] ParameterType { get; set; }
/// <summary>
/// 是否检查变量为空
/// </summary>
public bool[] IsCheckArgNotNull { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
namespace Serein.Proto.WebSocket.Handle
{
internal class WebSocketHandleConfiguration : HandleConfiguration
{
/// <summary>
/// 主题
/// </summary>
public string ThemeValue { get; set; } = string.Empty;
}
}

View File

@@ -0,0 +1,270 @@
using Serein.Library;
using Serein.Library.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Serein.Proto.WebSocket.Handle
{
/// <summary>
/// Json消息处理模块
/// </summary>
public class WebSocketHandleModule
{
/// <summary>
/// Json消息处理模块
/// </summary>
public WebSocketHandleModule(WebSocketHandleModuleConfig config)
{
moduleConfig = config;
}
/// <summary>
/// 模块的处理配置
/// </summary>
private readonly WebSocketHandleModuleConfig moduleConfig;
/// <summary>
/// 用来判断消息是否重复
/// </summary>
private HashSet<string> _myMsgIdHash = new HashSet<string>();
/// <summary>
/// 存储处理数据的配置
/// </summary>
public ConcurrentDictionary<string, HandleConfiguration> MyHandleConfigs = new ConcurrentDictionary<string, HandleConfiguration>();
/// <summary>
/// 添加处理配置
/// </summary>
/// <param name="config">处理模块</param>
internal bool AddHandleConfigs(WebSocketHandleConfiguration config)
{
if (!MyHandleConfigs.ContainsKey(config.ThemeValue))
{
MyHandleConfigs[config.ThemeValue] = config;
return true;
}
else
{
return false;
}
}
/// <summary>
/// 移除某个处理模块
/// </summary>
/// <param name="socketControlBase"></param>
/// <returns></returns>
public bool RemoveConfig(ISocketHandleModule socketControlBase)
{
foreach (var kv in MyHandleConfigs.ToArray())
{
var config = kv.Value;
MyHandleConfigs.TryRemove(kv.Key, out _);
}
return MyHandleConfigs.Count == 0;
}
/// <summary>
/// 卸载当前模块的所有配置
/// </summary>
public void UnloadConfig()
{
var temp = MyHandleConfigs.Values;
MyHandleConfigs.Clear();
}
/// <summary>
/// 处理JSON数据
/// </summary>
public async Task HandleAsync(WebSocketMsgContext context)
{
var jsonObject = context.MsgRequest; // 获取到消息
string theme = jsonObject.GetValue(moduleConfig.ThemeJsonKey)?.ToString();
if (!MyHandleConfigs.TryGetValue(theme, out var handldConfig))
{
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);
try
{
var dataObj = jsonObject.GetValue(moduleConfig.DataJsonKey);
context.MsgData = dataObj; // 添加消息
if (TryGetParameters(handldConfig, context, out var args))
{
var result = await HandleAsync(handldConfig, args);
if (handldConfig.IsReturnValue)
{
await context.RepliedAsync(moduleConfig, context, result);
}
}
}
catch (Exception ex)
{
SereinEnv.WriteLine(InfoType.ERROR, $"error in ws : {ex.Message}{Environment.NewLine}json value:{jsonObject}");
}
finally
{
context.Handle = true;
}
}
/// <summary>
/// 调用
/// </summary>
/// <param name="config"></param>
/// <param name="args"></param>
/// <returns></returns>
public static async Task<object> HandleAsync(HandleConfiguration config, object[] args)
{
var instance = config.InstanceFactory.Invoke();
var result = await config.DelegateDetails.InvokeAsync(instance, args);
return result;
}
/// <summary>
/// 获取入参参数
/// </summary>
/// <param name="config">处理配置</param>
/// <param name="context">处理上下文</param>
/// <param name="args">返回的入参参数</param>
/// <returns></returns>
internal static bool TryGetParameters(HandleConfiguration config, WebSocketMsgContext context, out object[] args)
{
args = new object[config.ParameterType.Length];
bool isCanInvoke = true; ; // 表示是否可以调用方法
for (int i = 0; i < config.ParameterType.Length; i++)
{
var type = config.ParameterType[i]; // 入参变量类型
var argName = config.ParameterName[i]; // 入参参数名称
#region ID
if (config.UseMsgId[i])
{
args[i] = context.MsgId;
}
#endregion
#region DATA JSON数据
else if (config.UseRequest[i])
{
args[i] = context.MsgRequest.ToObject(type);
}
#endregion
#region DATA JSON数据
else if (config.UseData[i])
{
args[i] = context.MsgData.ToObject(type);
}
#endregion
#region
else if (type.IsValueType)
{
var jsonValue = context.MsgData.GetValue(argName);
if (!(jsonValue is null))
{
args[i] = jsonValue.ToObject(type);
}
else
{
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;
}
}
}
#endregion
#region
else if (type.IsGenericType) // 传递SendAsync委托
{
if (type.IsAssignableFrom(typeof(Func<object, Task>)))
{
args[i] = new Func<object, Task>(async data =>
{
var jsonText = JsonHelper.Serialize(data);
await context.SendAsync(jsonText);
});
}
else if (type.IsAssignableFrom(typeof(Func<string, Task>)))
{
args[i] = new Func<string, Task>(async data =>
{
await context.SendAsync(data);
});
}
else if (type.IsAssignableFrom(typeof(Action<object>)))
{
args[i] = new Action<object>(async data =>
{
var jsonText = JsonHelper.Serialize(data);
await context.SendAsync(jsonText);
});
}
else if (type.IsAssignableFrom(typeof(Action<string>)))
{
args[i] = new Action<string>(async data =>
{
var jsonText = JsonHelper.Serialize(data);
await context.SendAsync(jsonText);
});
}
}
#endregion
}
return isCanInvoke;
}
}
}

View File

@@ -0,0 +1,26 @@
namespace Serein.Proto.WebSocket.Handle
{
/// <summary>
/// 远程环境配置
/// </summary>
public class WebSocketHandleModuleConfig
{
/// <summary>
/// 有关消息ID的 Json Key
/// </summary>
public string MsgIdJsonKey { get; set; }
/// <summary>
/// 有关消息主题的 Json Key
/// </summary>
public string ThemeJsonKey { get; set; }
/// <summary>
/// 有关数据的 Json Key
/// </summary>
public string DataJsonKey { get; set; }
/// <summary>
/// 使用怎么样的数据
/// </summary>
public bool IsResponseUseReturn { get; set; }
}
}

View File

@@ -0,0 +1,115 @@
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 = null;
MsgId = null;
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; }
/// <summary>
/// 此次消息附带的ID
/// </summary>
public string MsgId { get; set; }
/// <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)
{
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

@@ -0,0 +1,212 @@
using Serein.Library;
using Serein.Proto.WebSocket.Attributes;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace Serein.Proto.WebSocket.Handle
{
/// <summary>
/// 适用于Json数据格式的WebSocket消息处理类
/// </summary>
public class WebSocketMsgHandleHelper
{
/// <summary>
/// (Theme Name ,Data Name) - HandleModule
/// </summary>
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;
/// <summary>
/// 添加消息处理与异常处理
/// </summary>
/// <param name="moduleConfig">模块配置</param>
/// <returns></returns>
private WebSocketHandleModule AddMyHandleModule(WebSocketHandleModuleConfig moduleConfig)
{
var key = (moduleConfig.ThemeJsonKey, moduleConfig.DataJsonKey);
if (!MyHandleModuleDict.TryGetValue(key, out var myHandleModule))
{
myHandleModule = new WebSocketHandleModule(moduleConfig);
MyHandleModuleDict[key] = myHandleModule;
}
return myHandleModule;
}
/// <summary>
/// 移除某个模块的WebSocket消息处理
/// </summary>
/// <param name="socketControlBase"></param>
public void RemoveModule(ISocketHandleModule socketControlBase)
{
var type = socketControlBase.GetType();
var moduleAttribute = type.GetCustomAttribute<AutoSocketModuleAttribute>();
if (moduleAttribute is null)
{
return;
}
var themeKeyName = moduleAttribute.ThemeKey;
var dataKeyName = moduleAttribute.DataKey;
var key = (themeKeyName, dataKeyName);
if (MyHandleModuleDict.TryGetValue(key, out var myHandleModules))
{
var isRemote = myHandleModules.RemoveConfig(socketControlBase);
if (isRemote) MyHandleModuleDict.TryGetValue(key, out _);
}
}
/// <summary>
/// 添加消息处理以及异常处理
/// </summary>
/// <param name="socketControlBase"></param>
/// <param name="onExceptionTracking"></param>
public void AddModule<T>(Func<ISocketHandleModule> instanceFactory, Action<Exception, Action<object>> onExceptionTracking)
where T : ISocketHandleModule
{
var type = typeof(T);
var moduleAttribute = type.GetCustomAttribute<AutoSocketModuleAttribute>();
if (moduleAttribute is null)
{
return;
}
var themeKey = moduleAttribute.ThemeKey;
var dataKey = moduleAttribute.DataKey;
var msgIdKey = moduleAttribute.MsgIdKey;
var isResponseUseReturn = moduleAttribute.IsResponseUseReturn;
var moduleConfig = new WebSocketHandleModuleConfig()
{
ThemeJsonKey = themeKey,
DataJsonKey = dataKey,
MsgIdJsonKey = msgIdKey,
IsResponseUseReturn = isResponseUseReturn,
};
var handleModule = AddMyHandleModule(moduleConfig);
var configs = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Select<MethodInfo, WebSocketHandleConfiguration>(methodInfo =>
{
var methodsAttribute = methodInfo.GetCustomAttribute<AutoSocketHandleAttribute>();
if (methodsAttribute is null)
{
return null;
}
else
{
if (string.IsNullOrEmpty(methodsAttribute.ThemeValue))
{
methodsAttribute.ThemeValue = methodInfo.Name;
}
#region
var config = new WebSocketHandleConfiguration();
config.ThemeValue = methodsAttribute.ThemeValue;
config.ArgNotNull = methodsAttribute.ArgNotNull;
config.IsReturnValue = methodsAttribute.IsReturnValue;
//if (config.IsReturnValue)
//{
// // 重新检查是否能够返回
// if (methodInfo.ReturnType == typeof(void))
// {
// config.IsReturnValue = false; // void 不返回
// }
// else if (methodInfo.ReturnType == typeof(Unit))
// {
// config.IsReturnValue = false; // Unit 不返回
// }
// else if (methodInfo.ReturnType == typeof(Task))
// {
// config.IsReturnValue = false; // void 不返回
// }
//}
var parameterInfos = methodInfo.GetParameters();
config.DelegateDetails = new DelegateDetails(methodInfo); // 对应theme的emit构造委托调用工具类
config.InstanceFactory = instanceFactory; // 调用emit委托时的实例
config.OnExceptionTracking = onExceptionTracking; // 异常追踪
config.ParameterType = parameterInfos.Select(t => t.ParameterType).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
#if NET5_0_OR_GREATER
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;
return config;
}
})
.Where(config => config != null).ToList();
if (configs.Count == 0)
{
return;
}
SereinEnv.WriteLine(InfoType.INFO, $"add websocket handle model :");
SereinEnv.WriteLine(InfoType.INFO, $"theme key, data key : {themeKey}, {dataKey}");
foreach (var config in configs)
{
SereinEnv.WriteLine(InfoType.INFO, $"theme value : {config.ThemeValue} ");
var result = handleModule.AddHandleConfigs(config);
}
}
/// <summary>
/// 异步处理消息
/// </summary>
/// <param name="context">此次请求的上下文</param>
/// <returns></returns>
public void Handle(WebSocketMsgContext context)
{
foreach (var module in MyHandleModuleDict.Values)
{
if (context.Handle)
{
return;
}
_ = module.HandleAsync(context);
}
}
}
}