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();

View File

@@ -37,6 +37,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 readonly ConcurrentDictionary<int,DateTime> _lastHeartbeat = new();
@@ -48,6 +49,8 @@ namespace BITKit.Net
private readonly byte[] _heartBeat = new byte[] { (byte)NetCommandType.Heartbeat };
[NetRpc]
public event Action<int, float, bool> OnNetRpcTest;
public KCPNetServer()
{
server = new KcpServer(
@@ -63,6 +66,12 @@ namespace BITKit.Net
AddCommandListener<SimplePing>(F);
AddRpcHandle(this);
OnNetRpcTest += (_int, _float, _bool) =>
{
BIT4Log.Log<KCPNetServer>($"已收到Rpc测试:{_int},{_float},{_bool}");
};
return;
UniTask<SimplePing> F(SimplePing p)
{
@@ -307,13 +316,13 @@ namespace BITKit.Net
{
var path = reader.ReadString();
var pars = BITBinary.Read(reader).As<object[]>();
object value = null;
if (_rpcMethods.TryGetValue(path, out var methodInfo))
{
var isAwaitable = methodInfo.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null;
var handle = _rpcHandles[path];
object value = null;
if (methodInfo.GetParameters().Length is 0)
{
pars = new object[] { };
@@ -365,27 +374,38 @@ namespace BITKit.Net
return;
}
returnWriter.Write(true);
if (value is not null)
}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())
{
try
{
BITBinary.Write(returnWriter, value);
}
catch (NullReferenceException e)
{
throw;
}
catch (Exception e)
{
throw;
}
del.Method.Invoke(del.Target, pars);
}
}
else
{
throw new Exception($"未找到对应的Rpc方法:{path}");
}
if (value is not null)
{
try
{
BITBinary.Write(returnWriter, value);
}
catch (NullReferenceException e)
{
throw;
}
catch (Exception e)
{
throw;
}
}
}
else
{
@@ -542,9 +562,9 @@ namespace BITKit.Net
{
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);
_rpcEvents.TryAdd(eventInfo.Name, eventInfo);
_rpcHandles.TryAdd(eventInfo.Name, rpcHandle);
}
}