This commit is contained in:
CortexCore
2024-06-12 16:01:56 +08:00
parent 01b19130a9
commit 126131b842
8 changed files with 208 additions and 112 deletions

View File

@@ -8,6 +8,7 @@ using Timer = System.Timers.Timer;
using System.Threading.Tasks;
using System.IO;
using System.Numerics;
using System.Reflection;
using BITKit.Net.Examples;
using Newtonsoft.Json;
@@ -43,6 +44,9 @@ namespace BITKit.Net
private int _index = int.MinValue;
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,object> _rpcHandles = new();
public KcpNetClient()
{
client = new KcpClient(
@@ -124,7 +128,18 @@ namespace BITKit.Net
}
}
private async void OnData(ArraySegment<byte> bytes, KcpChannel channel)
private void OnData(ArraySegment<byte> bytes, KcpChannel channel)
{
try
{
OnDataInternel(bytes, channel);
}
catch (Exception e)
{
BIT4Log.LogException(e);
}
}
private async void OnDataInternel(ArraySegment<byte> bytes, KcpChannel channel)
{
using var ms = new MemoryStream(bytes.ToArray());
using var reader = new BinaryReader(ms);
@@ -191,19 +206,28 @@ namespace BITKit.Net
try
{
var requestId = reader.ReadInt32();
var commandObj = BITBinary.Read(reader);
if (_rpc.TryGetValue(commandObj.GetType().FullName, out var func) is false)
{
throw new NotImplementedException($"未找到对应的方法:{commandObj.GetType().FullName}");
}
using var _ms = new MemoryStream();
using var _writer = new BinaryWriter(_ms);
_writer.Write((byte)NetCommandType.ReturnToServer);
_writer.Write(requestId);
try
{
var value = await func.As<Func<object, UniTask<object>>>().Invoke(commandObj);
object value = null;
if (reader.ReadBoolean())
{
var path = reader.ReadString();
var method = _rpcMethods[path];
var pars = BITBinary.Read(reader).As<object[]>();
value = method.Invoke(_rpcHandles[path],pars);
}
else
{
var commandObj = BITBinary.Read(reader);
var func = _rpc[commandObj.GetType()!.FullName!];
value = await func.As<Func<object, UniTask<object>>>().Invoke(commandObj);
}
_writer.Write(true);
BITBinary.Write(_writer, value);
@@ -278,13 +302,22 @@ namespace BITKit.Net
Send(NetCommandType.TargetCommand,id,command);
}
public async UniTask<T> GetFromServer<T>(T command = default)
public async UniTask<T> GetFromServer<T>(string path = default,T command = default)
{
var id = _index++;
using var ms = new MemoryStream();
await using var writer = new BinaryWriter(ms);
writer.Write((byte)NetCommandType.GetFromServer);
writer.Write(id);
if (string.IsNullOrEmpty(path))
{
writer.Write(false);
}
else
{
writer.Write(true);
writer.Write(path);
}
BITBinary.Write(writer,command);
var bytes = ms.ToArray();
@@ -307,7 +340,7 @@ namespace BITKit.Net
}
}
public UniTask<T> GetFromClient<T>(int id, T command = default)
public UniTask<T> GetFromClient<T>(int id,string path, T command = default)
{
throw new NotImplementedException();
}