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
{
///
///
///
public class JsonMsgHandleConfig
{
public Guid HandleGuid { get; }
internal JsonMsgHandleConfig(SocketHandleModule model,
ISocketHandleModule instance,
MethodInfo methodInfo,
Action> onExceptionTracking,
bool ArgNotNull)
{
EmitMethodType = EmitHelper.CreateDynamicMethod(methodInfo,out EmitDelegate);
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() != null).ToArray();
this.useMsgId = parameterInfos.Select(p => p.GetCustomAttribute() != null).ToArray();
#if NET5_0_OR_GREATER
this.IsCheckArgNotNull = parameterInfos.Select(p => p.GetCustomAttribute() != null).ToArray();
#endif
if(IsCheckArgNotNull is null)
{
IsCheckArgNotNull = parameterInfos.Select(p => p.GetCustomAttribute() != null).ToArray();
}
else
{
// 兼容两种非空特性的写法
var argNotNull = parameterInfos.Select(p => p.GetCustomAttribute() != null).ToArray();
for (int i = 0; i < IsCheckArgNotNull.Length; i++)
{
if (!IsCheckArgNotNull[i] && argNotNull[i])
{
IsCheckArgNotNull[i] = true;
}
}
}
}
///
/// 参数不能为空
///
private bool ArgNotNull;
///
/// Emit委托
///
private readonly Delegate EmitDelegate;
///
/// Emit委托类型
///
private readonly EmitHelper.EmitMethodType EmitMethodType;
///
/// 未捕获的异常跟踪
///
private readonly Action> OnExceptionTracking;
///
/// 所在的模块
///
private readonly SocketHandleModule Module;
///
/// 所使用的实例
///
private readonly ISocketHandleModule Instance;
///
/// 参数名称
///
private readonly string[] ParameterName;
///
/// 参数类型
///
private readonly Type[] ParameterType;
///
/// 是否使Data整体内容作为入参参数
///
private readonly bool[] useData;
///
/// 是否使用消息ID作为入参参数
///
private readonly bool[] useMsgId;
///
/// 是否检查变量为空
///
private readonly bool[] IsCheckArgNotNull;
//private object ConvertArg(Type type, string argName )
//{
//}
public async void Handle(Func