重写了Emit构造委托的执行

This commit is contained in:
fengjiayi
2024-10-10 20:52:19 +08:00
parent ef96b353ac
commit 2d0f354895
17 changed files with 233 additions and 177 deletions

View File

@@ -15,14 +15,14 @@ using System.Threading.Tasks;
namespace Serein.Library.Network.WebSocketCommunication.Handle
{
public class MyHandleConfig
public class WebSocketHandleConfig
{
private readonly Delegate EmitDelegate;
private readonly EmitHelper.EmitMethodType EmitMethodType;
private Action<Exception, Action<object>> OnExceptionTracking;
public MyHandleConfig(SocketHandleModel model,ISocketControlBase instance, MethodInfo methodInfo, Action<Exception, Action<object>> onExceptionTracking)
internal WebSocketHandleConfig(SocketHandleModel model,ISocketHandleModule instance, MethodInfo methodInfo, Action<Exception, Action<object>> onExceptionTracking)
{
EmitMethodType = EmitHelper.CreateDynamicMethod(methodInfo,out EmitDelegate);
this.Model = model;
@@ -36,7 +36,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
}
private SocketHandleModel Model;
private ISocketControlBase Instance;
private ISocketHandleModule Instance;
public Guid HandleGuid { get; }
private string[] ParameterName;
private Type[] ParameterType;
@@ -51,7 +51,7 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
var argName = ParameterName[i];
if (type.IsGenericType)
{
if (type.IsAssignableFrom(typeof(Func<object, Task>)))
if (type.IsAssignableFrom(typeof(Func<object, Task>)))
{
args[i] = new Func<object, Task>(async data =>
{

View File

@@ -8,9 +8,9 @@ using System.Threading.Tasks;
namespace Serein.Library.Network.WebSocketCommunication.Handle
{
public class MyHandleModule
public class WebSocketHandleModule
{
public MyHandleModule(string ThemeJsonKey, string DataJsonKey)
public WebSocketHandleModule(string ThemeJsonKey, string DataJsonKey)
{
this.ThemeJsonKey = ThemeJsonKey;
this.DataJsonKey = DataJsonKey;
@@ -21,17 +21,17 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
public ConcurrentDictionary<string, MyHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, MyHandleConfig>();
public void AddHandleConfigs(SocketHandleModel model, ISocketControlBase instance, MethodInfo methodInfo
public ConcurrentDictionary<string, WebSocketHandleConfig> MyHandleConfigs = new ConcurrentDictionary<string, WebSocketHandleConfig>();
internal void AddHandleConfigs(SocketHandleModel model, ISocketHandleModule instance, MethodInfo methodInfo
, Action<Exception, Action<object>> onExceptionTracking)
{
if (!MyHandleConfigs.ContainsKey(model.ThemeValue))
{
var myHandleConfig = new MyHandleConfig(model,instance, methodInfo, onExceptionTracking);
var myHandleConfig = new WebSocketHandleConfig(model,instance, methodInfo, onExceptionTracking);
MyHandleConfigs[model.ThemeValue] = myHandleConfig;
}
}
public bool ResetConfig(ISocketControlBase socketControlBase)
public bool ResetConfig(ISocketHandleModule socketControlBase)
{
foreach (var kv in MyHandleConfigs.ToArray())
{

View File

@@ -20,13 +20,13 @@ using System.Security.Cryptography;
namespace Serein.Library.Network.WebSocketCommunication.Handle
{
public class SocketMsgHandleHelper
public class WebSocketMsgHandleHelper
{
/// <summary>
/// (Theme Name ,Data Name) - HandleModule
/// </summary>
public ConcurrentDictionary<(string, string), MyHandleModule> MyHandleModuleDict
= new ConcurrentDictionary<(string, string), MyHandleModule>();
public ConcurrentDictionary<(string, string), WebSocketHandleModule> MyHandleModuleDict
= new ConcurrentDictionary<(string, string), WebSocketHandleModule>();
private Action<Exception, Action<object>> _onExceptionTracking;
/// <summary>
@@ -34,18 +34,18 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
/// </summary>
public event Action<Exception, Action<object>> OnExceptionTracking;
private MyHandleModule AddMyHandleModule(string themeKeyName, string dataKeyName)
private WebSocketHandleModule AddMyHandleModule(string themeKeyName, string dataKeyName)
{
var key = (themeKeyName, dataKeyName);
if (!MyHandleModuleDict.TryGetValue(key, out var myHandleModule))
{
myHandleModule = new MyHandleModule(themeKeyName, dataKeyName);
myHandleModule = new WebSocketHandleModule(themeKeyName, dataKeyName);
MyHandleModuleDict[key] = myHandleModule;
}
return myHandleModule;
}
public void RemoteModule(ISocketControlBase socketControlBase)
public void RemoteModule(ISocketHandleModule socketControlBase)
{
var type = socketControlBase.GetType();
var moduleAttribute = type.GetCustomAttribute<AutoSocketModuleAttribute>();
@@ -53,8 +53,8 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
{
return;
}
var themeKeyName = moduleAttribute.JsonThemeField;
var dataKeyName = moduleAttribute.JsonDataField;
var themeKeyName = moduleAttribute.ThemeKey;
var dataKeyName = moduleAttribute.DataKey;
var key = (themeKeyName, dataKeyName);
if (MyHandleModuleDict.TryGetValue(key, out var myHandleModules))
{
@@ -63,7 +63,14 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
}
}
public void AddModule(ISocketControlBase socketControlBase, Action<Exception, Action<object>> onExceptionTracking)
/// <summary>
/// 添加
/// </summary>
/// <param name="socketControlBase"></param>
/// <param name="onExceptionTracking"></param>
public void AddModule(ISocketHandleModule socketControlBase, Action<Exception, Action<object>> onExceptionTracking)
{
var type = socketControlBase.GetType();
var moduleAttribute = type.GetCustomAttribute<AutoSocketModuleAttribute>();
@@ -72,8 +79,8 @@ namespace Serein.Library.Network.WebSocketCommunication.Handle
return;
}
var themeKey = moduleAttribute.JsonThemeField;
var dataKey = moduleAttribute.JsonDataField;
var themeKey = moduleAttribute.ThemeKey;
var dataKey = moduleAttribute.DataKey;
var handlemodule = AddMyHandleModule(themeKey, dataKey);
var methods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)