1
This commit is contained in:
@@ -40,9 +40,9 @@ namespace BITKit.Net
|
||||
|
||||
private readonly ValidHandle _isConnected = new();
|
||||
|
||||
private int _index = 1001;
|
||||
private int _index = int.MinValue;
|
||||
private readonly ConcurrentDictionary<int, object> _p2p = new();
|
||||
private readonly ConcurrentDictionary<string,object> _rpc = new();
|
||||
private readonly ConcurrentDictionary<string,Func<object,UniTask<object>>> _rpc = new();
|
||||
public KcpNetClient()
|
||||
{
|
||||
client = new KcpClient(
|
||||
@@ -168,8 +168,42 @@ namespace BITKit.Net
|
||||
break;
|
||||
case NetCommandType.ReturnToClient:
|
||||
var id = reader.ReadInt32();
|
||||
var value = BITBinary.Read(reader);
|
||||
_p2p.TryAdd(id,value);
|
||||
try
|
||||
{
|
||||
var value = BITBinary.Read(reader);
|
||||
_p2p.TryAdd(id,value);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.Warning<INetClient>($"请求返回失败:{id}");
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
break;
|
||||
case NetCommandType.GetFromClient:
|
||||
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}");
|
||||
}
|
||||
|
||||
var value = await func.As<Func<object, UniTask<object>>>().Invoke(commandObj);
|
||||
using var _ms = new MemoryStream();
|
||||
using var _writer = new BinaryWriter(_ms);
|
||||
_writer.Write((byte)NetCommandType.ReturnToServer);
|
||||
_writer.Write(requestId);
|
||||
BITBinary.Write(_writer, value);
|
||||
var _bytes = _ms.ToArray();
|
||||
commandQueue.Enqueue(_bytes);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
BIT4Log.Log<KcpClient>($"未知消息类型:{type},字节:{(byte)type}");
|
||||
@@ -268,12 +302,18 @@ namespace BITKit.Net
|
||||
_events.AddListener<T>(handle);
|
||||
}
|
||||
|
||||
public void AddCommandListener<T>(Func<T, T> func)
|
||||
public void AddCommandListener<T>(Func<T,UniTask<T>> func)
|
||||
{
|
||||
_rpc.GetOrAdd(typeof(T).FullName, func);
|
||||
_rpc.TryAdd(typeof(T).FullName, F);
|
||||
return;
|
||||
|
||||
async UniTask<object> F(object o)
|
||||
{
|
||||
return await func.Invoke((T)o);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveCommandListener<T>(Func<T, T> func)
|
||||
public void RemoveCommandListener<T>(Func<T,UniTask<T>> func)
|
||||
{
|
||||
_rpc.TryRemove(typeof(T).FullName, out _);
|
||||
}
|
||||
|
Reference in New Issue
Block a user