1
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user