This commit is contained in:
CortexCore
2024-08-06 10:27:59 +08:00
parent cc3d6f0ef1
commit d5c21759b5
6 changed files with 412 additions and 84 deletions

View File

@@ -55,6 +55,7 @@ namespace BITKit.Net
private readonly ConcurrentDictionary<int, object> _p2p = new();
private readonly ConcurrentDictionary<string,Func<object,UniTask<object>>> _rpc = new();
private readonly ConcurrentDictionary<string,MethodInfo> _rpcMethods = new();
private readonly ConcurrentDictionary<string,EventInfo> _rpcEvents = new();
private readonly ConcurrentDictionary<string,object> _rpcHandles = new();
private DateTime _lastHeartbeat = DateTime.Now;
private DateTime _now = DateTime.Now;
@@ -291,7 +292,7 @@ namespace BITKit.Net
{
var path = reader.ReadString();
var pars = BITBinary.Read(reader).As<object[]>();
if (_rpcMethods.TryGetValue(path, out var methodInfo))
{
@@ -321,6 +322,18 @@ namespace BITKit.Net
returnWriter.Write(true);
BITBinary.Write(returnWriter, value);
}
else if (_rpcEvents.TryGetValue(path, out var eventInfo))
{
var handle = _rpcHandles[path];
var fieldInfo = handle.GetType().GetField(eventInfo.Name,ReflectionHelper.Flags)!;
var eventDelegate = fieldInfo.GetValue(handle) as MulticastDelegate;
foreach (var del in eventDelegate!.GetInvocationList())
{
del.Method.Invoke(del.Target, pars);
}
}
else
{
returnWriter.Write(false);
@@ -358,9 +371,9 @@ namespace BITKit.Net
case NetCommandType.TargetRpc:
{
var rpcName = reader.ReadString();
var pars = BITBinary.Read(reader).As<object[]>();
if (_rpcMethods.TryGetValue(rpcName, out var methodInfo))
{
var pars = BITBinary.Read(reader).As<object[]>();
try
{
methodInfo.Invoke(_rpcHandles[rpcName], pars);
@@ -402,6 +415,18 @@ namespace BITKit.Net
}
}
else if (_rpcEvents.TryGetValue(rpcName, out var eventInfo))
{
var handle = _rpcHandles[rpcName];
var fieldInfo = handle.GetType().GetField(eventInfo.Name,ReflectionHelper.Flags)!;
var eventDelegate = fieldInfo.GetValue(handle) as MulticastDelegate;
foreach (var del in eventDelegate!.GetInvocationList())
{
del.Method.Invoke(del.Target, pars);
}
}
else
{
@@ -436,7 +461,7 @@ namespace BITKit.Net
{
BIT4Log.Log<KcpNetClient>($"{client.remoteEndPoint}异常:{errorCode},{message}");
}
public void ServerCommand<T>(T command = default)
{
using var ms = new MemoryStream();
@@ -547,20 +572,15 @@ namespace BITKit.Net
reportBuilder.AppendLine($"Add [{methodInfo.Name}] as MethodInfo");
}
foreach (var eventInfo in rpcHandle.GetType().GetEvents())
{
var att = eventInfo.GetCustomAttribute<NetRpcAttribute>();
if(att is null)continue;
_rpcEvents.TryAdd(eventInfo.Name, eventInfo);
_rpcHandles.TryAdd(eventInfo.Name, 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);
@@ -596,7 +616,9 @@ namespace BITKit.Net
{
using var ms = new MemoryStream();
using var writer = new BinaryWriter(ms);
writer.Write((byte)NetCommandType.GetFromClient);
writer.Write((byte)NetCommandType.GetFromServer);
writer.Write(++_index);
writer.Write(true);
writer.Write(rpcName);
BITBinary.Write(writer,pars);
var bytes = ms.ToArray();