This commit is contained in:
CortexCore 2024-06-24 17:30:44 +08:00
parent 07b6fbc2f3
commit cda86975a0
3 changed files with 79 additions and 11 deletions

View File

@ -1,12 +1,14 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Timers;
using Cysharp.Threading.Tasks;
using kcp2k;
using Timer = System.Timers.Timer;
using System.Threading.Tasks;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
@ -272,16 +274,26 @@ namespace BITKit.Net
{
var isAwaitable = methodInfo.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null;
var handle = _rpcHandles[path];
if (isAwaitable)
{
dynamic result = methodInfo.Invoke(handle, pars)!;
value = await result;
}
else
try
{
value = methodInfo.Invoke(handle, pars);
if (isAwaitable)
{
dynamic result = methodInfo.Invoke(handle, pars)!;
value = await result;
}
else
{
value = methodInfo.Invoke(handle, pars);
}
}
catch (Exception e)
{
BIT4Log.Warning<KcpNetClient>(path);
throw;
}
returnWriter.Write(true);
BITBinary.Write(returnWriter, value);
@ -326,10 +338,51 @@ namespace BITKit.Net
if (_rpcMethods.TryGetValue(rpcName, out var methodInfo))
{
var pars = BITBinary.Read(reader).As<object[]>();
methodInfo.Invoke(_rpcHandles[rpcName], pars);
try
{
methodInfo.Invoke(_rpcHandles[rpcName], pars);
}
catch (TargetException targetException)
{
var reportBuilder = new StringBuilder();
reportBuilder.AppendLine("正在检查参数类型");
var parameterInfos = methodInfo.GetParameters();
if (parameterInfos.Length != pars.Length)
{
reportBuilder.AppendLine("参数数量不匹配,期望:" + parameterInfos.Length + ",实际:" + pars.Length);
}
for (var i = 0; i < pars.Length; i++)
{
var parameterInfo = parameterInfos[i];
var parameter = pars[i];
if (parameterInfo.ParameterType != parameter.GetType())
{
reportBuilder.AppendLine($"参数{i}类型不匹配,期望:{parameterInfo.ParameterType},实际:{parameter.GetType()}");
}
else
{
reportBuilder.AppendLine($"参数{parameter.GetType()}类型匹配:{parameterInfo.ParameterType}");
}
}
BIT4Log.Warning<KcpNetClient>(reportBuilder);
throw;
}
catch (Exception e)
{
BIT4Log.Warning<KcpNetClient>(rpcName);
throw;
}
}
else
{
BIT4Log.Warning<KcpClient>($"未找到对应的Rpc方法:{rpcName}");
}
}
@ -474,11 +527,15 @@ namespace BITKit.Net
{
var att = eventInfo.GetCustomAttribute<NetRpcAttribute>();
if(att is null)continue;
var handle = eventInfo.EventHandlerType.GetMethod("Invoke");
_rpcMethods.AddOrUpdate(handle.Name, handle, (s, info) => handle);
_rpcHandles.AddOrUpdate(handle.Name, rpcHandle, (s, info) => rpcHandle);
// var handle = eventInfo.EventHandlerType.GetMethod("Invoke");
var handle = eventInfo.GetAddMethod();
_rpcMethods.AddOrUpdate(eventInfo.Name, handle, (s, info) => handle);
_rpcHandles.AddOrUpdate(eventInfo.Name, rpcHandle, (s, info) => rpcHandle);
reportBuilder.AppendLine($"Add [{eventInfo.Name} as EventInfo]");
}
BIT4Log.Log<KcpNetClient>(reportBuilder);

View File

@ -500,6 +500,15 @@ namespace BITKit.Net
_rpcMethods.TryAdd(methodInfo.Name, methodInfo);
_rpcHandles.TryAdd(methodInfo.Name, rpcHandle);
}
foreach (var eventInfo in rpcHandle.GetType().GetEvents())
{
var att = eventInfo.GetCustomAttribute<NetRpcAttribute>();
if(att is null)continue;
var handle = eventInfo.EventHandlerType.GetMethod("Invoke");
_rpcMethods.AddOrUpdate(eventInfo.Name, handle, (s, info) => handle);
_rpcHandles.AddOrUpdate(eventInfo.Name, rpcHandle, (s, info) => rpcHandle);
}
}
[NetRpc]
public string MyRpcTest(string hello)

View File

@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
@ -135,6 +136,7 @@ namespace BITKit
BIT4Log.OnWarning += Debug.LogWarning;
BIT4Log.OnException += Debug.LogException;
//启动BITApp
BITApp.SynchronizationContext = SynchronizationContext.Current;
BITApp.Start(Application.productName, new BITApp.AppSettings()).Forget();
AllowCursor = new();