mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
修改了WebSocket工具的抽象结构
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -64,4 +65,6 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,250 +0,0 @@
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class JsonMsgHandleConfig
|
||||
{
|
||||
public Guid HandleGuid { get; }
|
||||
|
||||
|
||||
internal JsonMsgHandleConfig(SocketHandleModule model,
|
||||
ISocketHandleModule instance,
|
||||
MethodInfo methodInfo,
|
||||
Action<Exception, Action<object>> onExceptionTracking,
|
||||
bool ArgNotNull)
|
||||
{
|
||||
DelegateDetails = new DelegateDetails(methodInfo);
|
||||
this.Module = model;
|
||||
Instance = instance;
|
||||
var parameterInfos = methodInfo.GetParameters();
|
||||
this.ParameterType = parameterInfos.Select(t => t.ParameterType).ToArray();
|
||||
this.ParameterName = parameterInfos.Select(t => t.Name).ToArray();
|
||||
this.HandleGuid = instance.HandleGuid;
|
||||
this.OnExceptionTracking = onExceptionTracking;
|
||||
this.ArgNotNull = ArgNotNull;
|
||||
|
||||
this.useData = parameterInfos.Select(p => p.GetCustomAttribute<UseDataAttribute>() != null).ToArray();
|
||||
this.useMsgId = parameterInfos.Select(p => p.GetCustomAttribute<UseMsgIdAttribute>() != null).ToArray();
|
||||
#if NET5_0_OR_GREATER
|
||||
this.IsCheckArgNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NotNullAttribute>() != null).ToArray();
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
if(IsCheckArgNotNull is null)
|
||||
{
|
||||
IsCheckArgNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NeedfulAttribute>() != null).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 兼容两种非空特性的写法
|
||||
var argNotNull = parameterInfos.Select(p => p.GetCustomAttribute<NeedfulAttribute>() != null).ToArray();
|
||||
for (int i = 0; i < IsCheckArgNotNull.Length; i++)
|
||||
{
|
||||
if (!IsCheckArgNotNull[i] && argNotNull[i])
|
||||
{
|
||||
IsCheckArgNotNull[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 参数不能为空
|
||||
/// </summary>
|
||||
private bool ArgNotNull;
|
||||
/// <summary>
|
||||
/// Emit委托
|
||||
/// </summary>
|
||||
private readonly DelegateDetails DelegateDetails;
|
||||
/// <summary>
|
||||
/// 未捕获的异常跟踪
|
||||
/// </summary>
|
||||
private readonly Action<Exception, Action<object>> OnExceptionTracking;
|
||||
/// <summary>
|
||||
/// 所在的模块
|
||||
/// </summary>
|
||||
private readonly SocketHandleModule Module;
|
||||
/// <summary>
|
||||
/// 所使用的实例
|
||||
/// </summary>
|
||||
private readonly ISocketHandleModule Instance;
|
||||
/// <summary>
|
||||
/// 参数名称
|
||||
/// </summary>
|
||||
private readonly string[] ParameterName;
|
||||
/// <summary>
|
||||
/// 参数类型
|
||||
/// </summary>
|
||||
private readonly Type[] ParameterType;
|
||||
/// <summary>
|
||||
/// 是否使Data整体内容作为入参参数
|
||||
/// </summary>
|
||||
private readonly bool[] useData;
|
||||
/// <summary>
|
||||
/// 是否使用消息ID作为入参参数
|
||||
/// </summary>
|
||||
private readonly bool[] useMsgId;
|
||||
/// <summary>
|
||||
/// 是否检查变量为空
|
||||
/// </summary>
|
||||
private readonly bool[] IsCheckArgNotNull;
|
||||
|
||||
public async void Handle(Func<object, Task> SendAsync,string msgId, JObject jsonObject)
|
||||
{
|
||||
object[] args = new object[ParameterType.Length];
|
||||
bool isCanInvoke = true;; // 表示是否可以调用方法
|
||||
for (int i = 0; i < ParameterType.Length; i++)
|
||||
{
|
||||
var type = ParameterType[i]; // 入参变量类型
|
||||
var argName = ParameterName[i]; // 入参参数名称
|
||||
#region 传递消息ID
|
||||
if (useMsgId[i])
|
||||
{
|
||||
args[i] = msgId;
|
||||
}
|
||||
#endregion
|
||||
#region DATA JSON数据
|
||||
else if (useData[i])
|
||||
{
|
||||
args[i] = jsonObject.ToObject(type);
|
||||
}
|
||||
#endregion
|
||||
#region 值类型参数
|
||||
else if (type.IsValueType)
|
||||
{
|
||||
var jsonValue = jsonObject.GetValue(argName);
|
||||
if (!(jsonValue is null))
|
||||
{
|
||||
args[i] = jsonValue.ToObject(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ArgNotNull && !IsCheckArgNotNull[i]) // 检查不能为空
|
||||
{
|
||||
|
||||
args[i] = Activator.CreateInstance(type); // 值类型返回默认值
|
||||
}
|
||||
else
|
||||
{
|
||||
isCanInvoke = false; // 参数不能为空,终止调用
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region 引用类型参数
|
||||
else if (type.IsClass)
|
||||
{
|
||||
var jsonValue = jsonObject.GetValue(argName);
|
||||
if (!(jsonValue is null))
|
||||
{
|
||||
args[i] = jsonValue.ToObject(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ArgNotNull && !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 = JsonConvert.SerializeObject(data);
|
||||
await SendAsync.Invoke(jsonText);
|
||||
});
|
||||
}
|
||||
else if (type.IsAssignableFrom(typeof(Func<string, Task>)))
|
||||
{
|
||||
args[i] = new Func<string, Task>(async data =>
|
||||
{
|
||||
await SendAsync.Invoke(data);
|
||||
});
|
||||
}
|
||||
else if (type.IsAssignableFrom(typeof(Action<object>)))
|
||||
{
|
||||
args[i] = new Action<object>(async data =>
|
||||
{
|
||||
var jsonText = JsonConvert.SerializeObject(data);
|
||||
await SendAsync.Invoke(jsonText);
|
||||
});
|
||||
}
|
||||
else if (type.IsAssignableFrom(typeof(Action<string>)))
|
||||
{
|
||||
args[i] = new Action<string>(async data =>
|
||||
{
|
||||
var jsonText = JsonConvert.SerializeObject(data);
|
||||
await SendAsync.Invoke(jsonText);
|
||||
});
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
if (!isCanInvoke)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
object result;
|
||||
try
|
||||
{
|
||||
result = await DelegateDetails.InvokeAsync(Instance, args);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = null;
|
||||
await Console.Out.WriteLineAsync(ex.Message);
|
||||
this.OnExceptionTracking.Invoke(ex, (async exData =>
|
||||
{
|
||||
await SendAsync.Invoke(exData);
|
||||
}));
|
||||
}
|
||||
|
||||
if (Module.IsReturnValue)
|
||||
{
|
||||
_ = SendAsync.Invoke(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -11,6 +11,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Json消息处理模块
|
||||
/// </summary>
|
||||
@@ -19,43 +20,35 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
/// <summary>
|
||||
/// Json消息处理模块
|
||||
/// </summary>
|
||||
public WebSocketHandleModule(string themeJsonKey, string dataJsonKey, string msgIdJsonKey)
|
||||
public WebSocketHandleModule(WebSocketHandleModuleConfig config)
|
||||
{
|
||||
this.ThemeJsonKey = themeJsonKey;
|
||||
this.DataJsonKey = dataJsonKey;
|
||||
this.MsgIdJsonKey = msgIdJsonKey;
|
||||
this.moduleConfig = config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 指示处理模块该使用 Json 中的哪个 Key 作为业务区别字段
|
||||
/// 模块的处理配置
|
||||
/// </summary>
|
||||
public string ThemeJsonKey { get; }
|
||||
private readonly WebSocketHandleModuleConfig moduleConfig;
|
||||
|
||||
/// <summary>
|
||||
/// 指示处理模块该使用 Json 中的哪个 Key 作为业务数据字段
|
||||
/// 用来判断消息是否重复
|
||||
/// </summary>
|
||||
public string DataJsonKey { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 指示处理模块该使用 Json 中的哪个 Key 作为业务消息ID字段
|
||||
/// </summary>
|
||||
public string MsgIdJsonKey { get; }
|
||||
|
||||
private HashSet<string> _myMsgIdHash = new HashSet<string>();
|
||||
/// <summary>
|
||||
/// 存储处理数据的配置
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<string, JsonMsgHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, JsonMsgHandleConfig>();
|
||||
public ConcurrentDictionary<string, HandleConfiguration> MyHandleConfigs = new ConcurrentDictionary<string, HandleConfiguration>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加处理配置
|
||||
/// </summary>
|
||||
/// <param name="module">处理模块</param>
|
||||
/// <param name="jsonMsgHandleConfig">处理配置</param>
|
||||
internal bool AddHandleConfigs(SocketHandleModule module,JsonMsgHandleConfig jsonMsgHandleConfig)
|
||||
/// <param name="config">处理模块</param>
|
||||
internal bool AddHandleConfigs(WebSocketHandleConfiguration config)
|
||||
{
|
||||
if (!MyHandleConfigs.ContainsKey(module.ThemeValue))
|
||||
if (!MyHandleConfigs.ContainsKey(config.ThemeValue))
|
||||
{
|
||||
MyHandleConfigs[module.ThemeValue] = jsonMsgHandleConfig;
|
||||
MyHandleConfigs[config.ThemeValue] = config;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
@@ -74,7 +67,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
foreach (var kv in MyHandleConfigs.ToArray())
|
||||
{
|
||||
var config = kv.Value;
|
||||
if (config.HandleGuid.Equals(socketControlBase.HandleGuid))
|
||||
if (config.Instance.HandleGuid.Equals(socketControlBase.HandleGuid))
|
||||
{
|
||||
MyHandleConfigs.TryRemove(kv.Key, out _);
|
||||
}
|
||||
@@ -91,38 +84,42 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
MyHandleConfigs.Clear();
|
||||
}
|
||||
|
||||
private HashSet<string> _myMsgIdHash = new HashSet<string>();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 处理JSON数据
|
||||
/// </summary>
|
||||
/// <param name="sendAsync"></param>
|
||||
/// <param name="jsonObject"></param>
|
||||
public void HandleSocketMsg(Func<string, Task> sendAsync, JObject jsonObject)
|
||||
public async void HandleSocketMsg(WebSocketMsgContext context) // Func<string, Task> sendAsync, JObject jsonObject
|
||||
{
|
||||
// 获取到消息
|
||||
string theme = jsonObject.GetValue(ThemeJsonKey)?.ToString();
|
||||
|
||||
var jsonObject = context.JsonObject; // 获取到消息
|
||||
string theme = jsonObject.GetValue(moduleConfig.ThemeJsonKey)?.ToString();
|
||||
if (!MyHandleConfigs.TryGetValue(theme, out var handldConfig))
|
||||
{
|
||||
// 没有主题
|
||||
return;
|
||||
return; // 没有主题
|
||||
}
|
||||
string msgId = jsonObject.GetValue(MsgIdJsonKey)?.ToString();
|
||||
context.MsgTheme = theme; // 添加主题
|
||||
string msgId = jsonObject.GetValue(moduleConfig.MsgIdJsonKey)?.ToString();
|
||||
if (_myMsgIdHash.Contains(msgId))
|
||||
{
|
||||
Console.WriteLine($"[{msgId}]{theme} 消息重复");
|
||||
return;
|
||||
}
|
||||
context.MsgId = msgId; // 添加 ID
|
||||
_myMsgIdHash.Add(msgId);
|
||||
|
||||
try
|
||||
{
|
||||
JObject dataObj = jsonObject.GetValue(DataJsonKey)?.ToObject<JObject>();
|
||||
handldConfig.Handle(async (data) =>
|
||||
var dataObj = jsonObject.GetValue(moduleConfig.DataJsonKey)?.ToObject<JObject>();
|
||||
context.MsgData = dataObj; // 添加消息
|
||||
if (TryGetParameters(handldConfig, context, out var args))
|
||||
{
|
||||
await this.SendAsync(sendAsync, msgId, theme, data);
|
||||
}, msgId, dataObj);
|
||||
|
||||
var result = await WebSocketHandleModule.HandleAsync(handldConfig, args);
|
||||
if (handldConfig.IsReturnValue)
|
||||
{
|
||||
await context.RepliedAsync(moduleConfig, context, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -133,52 +130,130 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送消息
|
||||
/// 调用
|
||||
/// </summary>
|
||||
/// <param name="sendAsync"></param>
|
||||
/// <param name="msgId"></param>
|
||||
/// <param name="theme"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="args"></param>
|
||||
/// <returns></returns>
|
||||
public async Task SendAsync(Func<string, Task> sendAsync,string msgId, string theme, object data)
|
||||
public static async Task<object> HandleAsync(HandleConfiguration config, object[] args)
|
||||
{
|
||||
JObject jsonData;
|
||||
var result = await config.DelegateDetails.InvokeAsync(config.Instance, args);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (data is null)
|
||||
|
||||
/// <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++)
|
||||
{
|
||||
jsonData = new JObject()
|
||||
var type = config.ParameterType[i]; // 入参变量类型
|
||||
var argName = config.ParameterName[i]; // 入参参数名称
|
||||
#region 传递消息ID
|
||||
if (config.UseMsgId[i])
|
||||
{
|
||||
[MsgIdJsonKey] = msgId,
|
||||
[ThemeJsonKey] = theme,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
JToken dataToken;
|
||||
if ((data is System.Collections.IEnumerable || data is Array))
|
||||
args[i] = context.MsgId;
|
||||
}
|
||||
#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))
|
||||
{
|
||||
dataToken = JArray.FromObject(data);
|
||||
args[i] = jsonValue.ToObject(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
dataToken = JObject.FromObject(data);
|
||||
if (config.ArgNotNull && !config.IsCheckArgNotNull[i]) // 检查不能为空
|
||||
{
|
||||
|
||||
args[i] = Activator.CreateInstance(type); // 值类型返回默认值
|
||||
}
|
||||
else
|
||||
{
|
||||
isCanInvoke = false; // 参数不能为空,终止调用
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
jsonData = new JObject()
|
||||
}
|
||||
#endregion
|
||||
#region 引用类型参数
|
||||
else if (type.IsClass)
|
||||
{
|
||||
var jsonValue = context.MsgData.GetValue(argName);
|
||||
if (!(jsonValue is null))
|
||||
{
|
||||
[MsgIdJsonKey] = msgId,
|
||||
[ThemeJsonKey] = theme,
|
||||
[DataJsonKey] = dataToken
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
args[i] = jsonValue.ToObject(type);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (config.ArgNotNull && !config.IsCheckArgNotNull[i])
|
||||
{
|
||||
|
||||
var msg = jsonData.ToString();
|
||||
|
||||
|
||||
await sendAsync.Invoke(msg);
|
||||
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 = JsonConvert.SerializeObject(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 = JsonConvert.SerializeObject(data);
|
||||
await context.SendAsync(jsonText);
|
||||
});
|
||||
}
|
||||
else if (type.IsAssignableFrom(typeof(Action<string>)))
|
||||
{
|
||||
args[i] = new Action<string>(async data =>
|
||||
{
|
||||
var jsonText = JsonConvert.SerializeObject(data);
|
||||
await context.SendAsync(jsonText);
|
||||
});
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
return isCanInvoke;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Network.WebSocketCommunication.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; }
|
||||
}
|
||||
|
||||
}
|
||||
112
Library/Network/WebSocket/Handle/WebSocketMsgContext.cs
Normal file
112
Library/Network/WebSocket/Handle/WebSocketMsgContext.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 消息处理上下文
|
||||
/// </summary>
|
||||
public class WebSocketMsgContext
|
||||
{
|
||||
public WebSocketMsgContext(Func<string, Task> sendAsync)
|
||||
{
|
||||
this._sendAsync = sendAsync;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标记是否已经处理,如果是,则提前退出
|
||||
/// </summary>
|
||||
public bool Handle { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息本体(文本)
|
||||
/// </summary>
|
||||
public string Msg { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息本体(JObject)
|
||||
/// </summary>
|
||||
public JObject JsonObject { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息请求的主题
|
||||
/// </summary>
|
||||
public string MsgTheme { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息附带的ID
|
||||
/// </summary>
|
||||
public string MsgId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 此次消息的数据
|
||||
/// </summary>
|
||||
public JObject 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)
|
||||
{
|
||||
JObject jsonData;
|
||||
|
||||
if (data is null)
|
||||
{
|
||||
jsonData = new JObject()
|
||||
{
|
||||
[moduleConfig.MsgIdJsonKey] = context.MsgId,
|
||||
[moduleConfig.ThemeJsonKey] = context.MsgTheme,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
JToken dataToken;
|
||||
if (data is System.Collections.IEnumerable || data is Array)
|
||||
{
|
||||
dataToken = JArray.FromObject(data);
|
||||
}
|
||||
else
|
||||
{
|
||||
dataToken = JObject.FromObject(data);
|
||||
}
|
||||
|
||||
jsonData = new JObject()
|
||||
{
|
||||
[moduleConfig.MsgIdJsonKey] = context.MsgId,
|
||||
[moduleConfig.ThemeJsonKey] = context.MsgTheme,
|
||||
[moduleConfig.DataJsonKey] = dataToken
|
||||
};
|
||||
}
|
||||
var msg = jsonData.ToString();
|
||||
//Console.WriteLine($"[{msgId}] => {theme}");
|
||||
await SendAsync(msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,6 +16,7 @@ using System.Linq.Expressions;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
{
|
||||
@@ -39,16 +40,14 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
/// <summary>
|
||||
/// 添加消息处理与异常处理
|
||||
/// </summary>
|
||||
/// <param name="themeKeyName"></param>
|
||||
/// <param name="dataKeyName"></param>
|
||||
/// <param name="msgIdKeyName"></param>
|
||||
/// <param name="moduleConfig">模块配置</param>
|
||||
/// <returns></returns>
|
||||
private WebSocketHandleModule AddMyHandleModule(string themeKeyName, string dataKeyName, string msgIdKeyName)
|
||||
private WebSocketHandleModule AddMyHandleModule(WebSocketHandleModuleConfig moduleConfig)
|
||||
{
|
||||
var key = (themeKeyName, dataKeyName);
|
||||
var key = (moduleConfig.ThemeJsonKey, moduleConfig.DataJsonKey);
|
||||
if (!MyHandleModuleDict.TryGetValue(key, out var myHandleModule))
|
||||
{
|
||||
myHandleModule = new WebSocketHandleModule(themeKeyName, dataKeyName, msgIdKeyName);
|
||||
myHandleModule = new WebSocketHandleModule(moduleConfig);
|
||||
MyHandleModuleDict[key] = myHandleModule;
|
||||
}
|
||||
return myHandleModule;
|
||||
@@ -95,34 +94,75 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
var themeKey = moduleAttribute.ThemeKey;
|
||||
var dataKey = moduleAttribute.DataKey;
|
||||
var msgIdKey = moduleAttribute.MsgIdKey;
|
||||
|
||||
var handleModule = AddMyHandleModule(themeKey, dataKey, msgIdKey);
|
||||
var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Select(method =>
|
||||
var moduleConfig = new WebSocketHandleModuleConfig()
|
||||
{
|
||||
ThemeJsonKey = themeKey,
|
||||
DataJsonKey = dataKey,
|
||||
MsgIdJsonKey = msgIdKey,
|
||||
};
|
||||
|
||||
var handleModule = AddMyHandleModule(moduleConfig);
|
||||
var configs = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
||||
.Select<MethodInfo, WebSocketHandleConfiguration>(methodInfo =>
|
||||
{
|
||||
var methodsAttribute = method.GetCustomAttribute<AutoSocketHandleAttribute>();
|
||||
var methodsAttribute = methodInfo.GetCustomAttribute<AutoSocketHandleAttribute>();
|
||||
if (methodsAttribute is null)
|
||||
{
|
||||
return (null, null,false);
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(methodsAttribute.ThemeValue))
|
||||
{
|
||||
methodsAttribute.ThemeValue = method.Name;
|
||||
methodsAttribute.ThemeValue = methodInfo.Name;
|
||||
}
|
||||
var model = new SocketHandleModule
|
||||
|
||||
#region 生成处理配置
|
||||
var config = new WebSocketHandleConfiguration
|
||||
{
|
||||
IsReturnValue = methodsAttribute.IsReturnValue,
|
||||
ThemeValue = methodsAttribute.ThemeValue,
|
||||
ArgNotNull = methodsAttribute.ArgNotNull,
|
||||
};
|
||||
var parameterInfos = methodInfo.GetParameters();
|
||||
|
||||
config.DelegateDetails = new DelegateDetails(methodInfo); // 对应theme的emit构造委托调用工具类
|
||||
config.Instance = socketControlBase; // 调用emit委托时的实例
|
||||
config.OnExceptionTracking = onExceptionTracking; // 异常追踪
|
||||
config.ParameterType = parameterInfos.Select(t => t.ParameterType).ToArray(); // 入参参数类型
|
||||
config.ParameterName = parameterInfos.Select(t => t.Name).ToArray(); // 入参参数名称
|
||||
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;
|
||||
var argNotNull = methodsAttribute.ArgNotNull;
|
||||
return (model, method, argNotNull);
|
||||
|
||||
return config;
|
||||
}
|
||||
})
|
||||
.Where(x => !(x.model is null)).ToList();
|
||||
if (methods.Count == 0)
|
||||
.Where(config => config != null).ToList();
|
||||
if (configs.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -131,23 +171,10 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
|
||||
Console.WriteLine($"add websocket handle model :");
|
||||
Console.WriteLine($"theme key, data key : {themeKey}, {dataKey}");
|
||||
foreach ((var module, var method,var argNotNull) in methods)
|
||||
foreach (var config in configs)
|
||||
{
|
||||
Console.WriteLine($"theme value : {module.ThemeValue}");
|
||||
try
|
||||
{
|
||||
var jsonMsgHandleConfig = new JsonMsgHandleConfig(module, socketControlBase, method, onExceptionTracking, argNotNull);
|
||||
var result = handleModule.AddHandleConfigs(module,jsonMsgHandleConfig);
|
||||
if (!result)
|
||||
{
|
||||
throw new Exception("添加失败,已经添加过相同的配置");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Console.WriteLine($"error in add method: {method.Name}{Environment.NewLine}{ex}");
|
||||
}
|
||||
Console.WriteLine($"theme value : {config.ThemeValue} ");
|
||||
var result = handleModule.AddHandleConfigs(config);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -155,20 +182,21 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
|
||||
/// <summary>
|
||||
/// 异步处理消息
|
||||
/// </summary>
|
||||
/// <param name="sendAsync"></param>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="context">此次请求的上下文</param>
|
||||
/// <returns></returns>
|
||||
public void HandleMsg(Func<string, Task> sendAsync, string message)
|
||||
public void HandleMsg(WebSocketMsgContext context)
|
||||
{
|
||||
JObject json = JObject.Parse(message);
|
||||
// OnExceptionTracking
|
||||
foreach (var module in MyHandleModuleDict.Values)
|
||||
{
|
||||
module.HandleSocketMsg(sendAsync, json);
|
||||
module.HandleSocketMsg(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user