This commit is contained in:
parent
07b6fbc2f3
commit
cda86975a0
|
@ -1,12 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.Tracing;
|
||||||
using System.Timers;
|
using System.Timers;
|
||||||
using Cysharp.Threading.Tasks;
|
using Cysharp.Threading.Tasks;
|
||||||
using kcp2k;
|
using kcp2k;
|
||||||
using Timer = System.Timers.Timer;
|
using Timer = System.Timers.Timer;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
@ -272,6 +274,9 @@ namespace BITKit.Net
|
||||||
{
|
{
|
||||||
var isAwaitable = methodInfo.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null;
|
var isAwaitable = methodInfo.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null;
|
||||||
var handle = _rpcHandles[path];
|
var handle = _rpcHandles[path];
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
if (isAwaitable)
|
if (isAwaitable)
|
||||||
{
|
{
|
||||||
dynamic result = methodInfo.Invoke(handle, pars)!;
|
dynamic result = methodInfo.Invoke(handle, pars)!;
|
||||||
|
@ -282,6 +287,13 @@ namespace BITKit.Net
|
||||||
{
|
{
|
||||||
value = methodInfo.Invoke(handle, pars);
|
value = methodInfo.Invoke(handle, pars);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
BIT4Log.Warning<KcpNetClient>(path);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
returnWriter.Write(true);
|
returnWriter.Write(true);
|
||||||
BITBinary.Write(returnWriter, value);
|
BITBinary.Write(returnWriter, value);
|
||||||
|
@ -326,10 +338,51 @@ namespace BITKit.Net
|
||||||
if (_rpcMethods.TryGetValue(rpcName, out var methodInfo))
|
if (_rpcMethods.TryGetValue(rpcName, out var methodInfo))
|
||||||
{
|
{
|
||||||
var pars = BITBinary.Read(reader).As<object[]>();
|
var pars = BITBinary.Read(reader).As<object[]>();
|
||||||
|
try
|
||||||
|
{
|
||||||
methodInfo.Invoke(_rpcHandles[rpcName], pars);
|
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
|
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}");
|
BIT4Log.Warning<KcpClient>($"未找到对应的Rpc方法:{rpcName}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -474,11 +527,15 @@ namespace BITKit.Net
|
||||||
{
|
{
|
||||||
var att = eventInfo.GetCustomAttribute<NetRpcAttribute>();
|
var att = eventInfo.GetCustomAttribute<NetRpcAttribute>();
|
||||||
if(att is null)continue;
|
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]");
|
reportBuilder.AppendLine($"Add [{eventInfo.Name} as EventInfo]");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BIT4Log.Log<KcpNetClient>(reportBuilder);
|
BIT4Log.Log<KcpNetClient>(reportBuilder);
|
||||||
|
|
|
@ -500,6 +500,15 @@ namespace BITKit.Net
|
||||||
_rpcMethods.TryAdd(methodInfo.Name, methodInfo);
|
_rpcMethods.TryAdd(methodInfo.Name, methodInfo);
|
||||||
_rpcHandles.TryAdd(methodInfo.Name, rpcHandle);
|
_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]
|
[NetRpc]
|
||||||
public string MyRpcTest(string hello)
|
public string MyRpcTest(string hello)
|
||||||
|
|
|
@ -2,6 +2,7 @@ using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Threading;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Cysharp.Threading.Tasks;
|
using Cysharp.Threading.Tasks;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -135,6 +136,7 @@ namespace BITKit
|
||||||
BIT4Log.OnWarning += Debug.LogWarning;
|
BIT4Log.OnWarning += Debug.LogWarning;
|
||||||
BIT4Log.OnException += Debug.LogException;
|
BIT4Log.OnException += Debug.LogException;
|
||||||
//启动BITApp
|
//启动BITApp
|
||||||
|
BITApp.SynchronizationContext = SynchronizationContext.Current;
|
||||||
BITApp.Start(Application.productName, new BITApp.AppSettings()).Forget();
|
BITApp.Start(Application.productName, new BITApp.AppSettings()).Forget();
|
||||||
|
|
||||||
AllowCursor = new();
|
AllowCursor = new();
|
||||||
|
|
Loading…
Reference in New Issue