This commit is contained in:
CortexCore
2024-06-13 16:02:49 +08:00
parent ceea55b67a
commit 4a2ab82e20
4 changed files with 57 additions and 35 deletions

View File

@@ -359,7 +359,7 @@ namespace BITKit.Net
{
throw e;
}
return (T)value;
return value.As<T>();
}
await Task.Delay(100);
}

View File

@@ -234,6 +234,10 @@ namespace BITKit.Net
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[]{};
}
if (isAwaitable)
{
dynamic result = methodInfo.Invoke(handle, pars)!;
@@ -250,8 +254,7 @@ namespace BITKit.Net
}
else
{
returnWriter.Write(false);
returnWriter.Write("未找到对应的Rpc方法");
throw new Exception("未找到对应的Rpc方法");
}
}
@@ -261,23 +264,13 @@ namespace BITKit.Net
.As<object[]>()[0];
if (_rpc.TryGetValue(commandObj.GetType()!.FullName!, out var func))
{
try
{
var value = await func.As<Func<object, UniTask<object>>>().Invoke(commandObj);
returnWriter.Write(true);
BITBinary.Write(returnWriter, value);
}
catch (Exception e)
{
//BIT4Log.LogException(e);
returnWriter.Write(false);
returnWriter.Write(e.Message);
}
var value = await func.As<Func<object, UniTask<object>>>().Invoke(commandObj);
returnWriter.Write(true);
BITBinary.Write(returnWriter, value);
}
else
{
returnWriter.Write(false);
returnWriter.Write("未找到对应的command方法");
throw new Exception("未找到对应的Rpc方法");
}
}
{
@@ -287,6 +280,11 @@ namespace BITKit.Net
}
catch (Exception e)
{
returnWriter.Write(false);
returnWriter.Write(e.Message);
var _bytes = returnMS.ToArray();
server.Send(Id, _bytes, KcpChannel.Reliable);
BIT4Log.LogException(e);
}
}

View File

@@ -1,10 +1,12 @@
using System;
using System.Collections;
using System.Linq;
namespace BITKit
{
public static class MathO
{
public static T As<T>(this object self) where T : class
public static T As<T>(this object self)
{
// {
// if (typeof(T) == typeof(object[]))
@@ -20,7 +22,28 @@ namespace BITKit
// return o[0] as T;
// }
// }
return self as T;
try
{
return (T)self;
}
catch (InvalidCastException)
{
if(self.GetType().IsArray && typeof(T).IsArray && self is IEnumerable enumerable)
{
var newArray = Array.CreateInstance(typeof(T).GetElementType()!, enumerable.Cast<object>().Count());
var i = 0;
foreach (var x in enumerable)
{
newArray.SetValue(x, i++);
}
if (newArray is T newValue)
{
return newValue;
}
}
throw;
}
}
}
}