1
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Timers;
|
||||
using Cysharp.Threading.Tasks;
|
||||
@@ -38,6 +39,10 @@ namespace BITKit.Net
|
||||
private readonly GenericEvent _events = new();
|
||||
|
||||
private readonly ValidHandle _isConnected = new();
|
||||
|
||||
private int _index = 1001;
|
||||
private readonly ConcurrentDictionary<int, object> _p2p = new();
|
||||
private readonly ConcurrentDictionary<string,object> _rpc = new();
|
||||
public KcpNetClient()
|
||||
{
|
||||
client = new KcpClient(
|
||||
@@ -161,6 +166,11 @@ namespace BITKit.Net
|
||||
case NetCommandType.Ping:
|
||||
Ping = (int)(DateTime.Now - _lastPingTime).TotalMilliseconds;
|
||||
break;
|
||||
case NetCommandType.ReturnToClient:
|
||||
var id = reader.ReadInt32();
|
||||
var value = BITBinary.Read(reader);
|
||||
_p2p.TryAdd(id,value);
|
||||
break;
|
||||
default:
|
||||
BIT4Log.Log<KcpClient>($"未知消息类型:{type},字节:{(byte)type}");
|
||||
if (bytes.Array != null)
|
||||
@@ -217,15 +227,36 @@ namespace BITKit.Net
|
||||
Send(NetCommandType.TargetCommand,id,command);
|
||||
}
|
||||
|
||||
public UniTask<T> GetFromServer<T>(string addressablePath = Constant.System.Internal)
|
||||
public async UniTask<T> GetFromServer<T>(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);
|
||||
BITBinary.Write(writer,command);
|
||||
|
||||
var bytes = ms.ToArray();
|
||||
commandQueue.Enqueue(bytes);
|
||||
var startTime = DateTime.Now;
|
||||
while (true)
|
||||
{
|
||||
if(DateTime.Now-startTime>TimeSpan.FromSeconds(5) || IsConnected is false)
|
||||
throw new TimeoutException();
|
||||
|
||||
if (_p2p.TryRemove(id, out var value))
|
||||
{
|
||||
return (T)value;
|
||||
}
|
||||
await Task.Delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask<T> GetFromClient<T>(int id, T command = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public UniTask<T> GetFromClient<T>(int id, string addressablePath = Constant.System.Internal)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void AddRpcHandle(object rpcHandle)
|
||||
{
|
||||
@@ -237,9 +268,19 @@ namespace BITKit.Net
|
||||
_events.AddListener<T>(handle);
|
||||
}
|
||||
|
||||
public void AddCommandListener<T>(Func<T, T> func)
|
||||
{
|
||||
_rpc.GetOrAdd(typeof(T).FullName, func);
|
||||
}
|
||||
|
||||
public void RemoveCommandListener<T>(Func<T, T> func)
|
||||
{
|
||||
_rpc.TryRemove(typeof(T).FullName, out _);
|
||||
}
|
||||
|
||||
public void RemoveCommandListener<T>(Action<T> handle)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
_events.RemoveListener<T>(handle);
|
||||
}
|
||||
|
||||
public void SendRT(string rpcName, params object[] pars)
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Timers;
|
||||
using Cysharp.Threading.Tasks;
|
||||
@@ -26,6 +27,11 @@ namespace BITKit.Net
|
||||
AutoReset = true
|
||||
};
|
||||
|
||||
private int _index = 1001;
|
||||
private readonly ConcurrentDictionary<int, object> _p2p = new();
|
||||
private readonly ConcurrentDictionary<string,Func<object,object>> _rpc = new();
|
||||
|
||||
|
||||
public KCPNetServer()
|
||||
{
|
||||
server = new KcpServer(
|
||||
@@ -37,6 +43,12 @@ namespace BITKit.Net
|
||||
);
|
||||
_timer.Elapsed += Tick;
|
||||
BIT4Log.Log<KCPNetServer>("已创建KCP服务器");
|
||||
|
||||
AddCommandListener<SimplePing>(x =>
|
||||
{
|
||||
x.EndTime = DateTime.Now;
|
||||
return x;
|
||||
});
|
||||
}
|
||||
|
||||
private void Tick(object sender, ElapsedEventArgs e)
|
||||
@@ -161,6 +173,35 @@ namespace BITKit.Net
|
||||
case NetCommandType.Ping:
|
||||
server.Send(Id,new byte[]{(byte)NetCommandType.Ping},channel);
|
||||
break;
|
||||
case NetCommandType.GetFromServer:
|
||||
try
|
||||
{
|
||||
var requestId = reader.ReadInt32();
|
||||
var commandObj = BITBinary.Read(reader);
|
||||
|
||||
BIT4Log.Log<KCPNetServer>($"已收到请求:{commandObj},请求ID:{requestId}");
|
||||
|
||||
if (_rpc.TryGetValue(commandObj.GetType().FullName, out var func) is false)
|
||||
{
|
||||
throw new NotImplementedException($"未找到对应的方法:{commandObj.GetType().FullName}");
|
||||
}
|
||||
|
||||
var value = func.As<Func<object,object>>().Invoke(commandObj);
|
||||
|
||||
using var _ms = new MemoryStream();
|
||||
using var _writer = new BinaryWriter(_ms);
|
||||
_writer.Write((byte)NetCommandType.ReturnToClient);
|
||||
_writer.Write(requestId);
|
||||
BITBinary.Write(_writer,value);
|
||||
var _bytes = _ms.ToArray();
|
||||
server.Send(Id,_bytes,KcpChannel.Reliable);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
Send(Id,NetCommandType.ReturnToClient,-1,0);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
BIT4Log.Log<KCPNetServer>($"未知消息类型:{type},字节:{(byte)type}");
|
||||
BIT4Log.Log<KCPNetServer>($"已收到:({Id}, {BitConverter.ToString(bytes.Array, bytes.Offset, bytes.Count)} @ {channel})");
|
||||
@@ -190,12 +231,12 @@ namespace BITKit.Net
|
||||
Send(id,NetCommandType.Command,command);
|
||||
}
|
||||
|
||||
public UniTask<T> GetFromServer<T>(string addressablePath = Constant.System.Internal)
|
||||
public UniTask<T> GetFromServer<T>(T command = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public UniTask<T> GetFromClient<T>(int id, string addressablePath = Constant.System.Internal)
|
||||
public UniTask<T> GetFromClient<T>(int id, T command = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@@ -209,7 +250,21 @@ namespace BITKit.Net
|
||||
{
|
||||
_events.AddListener<T>(handle);
|
||||
}
|
||||
|
||||
|
||||
public void AddCommandListener<T>(Func<T, T> func)
|
||||
{
|
||||
_rpc.TryAdd(typeof(T).FullName, F);
|
||||
return;
|
||||
|
||||
object F(object o)
|
||||
{
|
||||
return func.Invoke((T)o);
|
||||
}
|
||||
}
|
||||
public void RemoveCommandListener<T>(Func<T, T> func)
|
||||
{
|
||||
_rpc.TryRemove(typeof(T).FullName, out _);
|
||||
}
|
||||
public void AddCommandListenerWithId<T>(Action<int, T> handle)
|
||||
{
|
||||
_events.AddListenerDirect(typeof(T).FullName, Callback);
|
||||
@@ -233,6 +288,11 @@ namespace BITKit.Net
|
||||
_events.RemoveListener<T>(handle);
|
||||
}
|
||||
|
||||
public void RemoveCommandListener<T>(Func<string, T> func)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SendRT(string rpcName, params object[] pars)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
Reference in New Issue
Block a user