This commit is contained in:
CortexCore
2024-06-08 10:07:40 +08:00
parent ccc9957809
commit b9731c20a1
11 changed files with 231 additions and 44 deletions

View File

@@ -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();