This commit is contained in:
CortexCore
2023-10-06 23:43:19 +08:00
parent ebf9c1f526
commit 2c4710bc5d
186 changed files with 111802 additions and 764 deletions

View File

@@ -14,7 +14,7 @@ namespace BITKit.Net
{
public class KCPNetServer:INetServer,INetProvider
{
public event Action<int> OnClientConnect;
public event Action<int> OnClientConnected;
public event Action<int> OnClientDisconnected;
public event Action OnStartServer;
public event Action OnStopServer;
@@ -74,6 +74,9 @@ namespace BITKit.Net
new Dictionary<int, EndPoint>(
server.connections.Select(x => new KeyValuePair<int, EndPoint>(x.Key, x.Value.remoteEndPoint)
));
public void Tick() => server.Tick();
public void HandShake()
{
@@ -85,7 +88,7 @@ namespace BITKit.Net
private void OnConnected(int Id)
{
OnClientConnect?.Invoke(Id);
OnClientConnected?.Invoke(Id);
ClientCommand(Id,new NetClientAllocateIdCommand
{
Id = Id,
@@ -105,6 +108,7 @@ namespace BITKit.Net
using var ms = new MemoryStream(bytes.ToArray());
using var reader = new BinaryReader(ms);
BIT4Log.Log<INetServer>(Id);
var type = (NetCommandType)ms.ReadByte();
switch (type)
@@ -114,9 +118,12 @@ namespace BITKit.Net
break;
case NetCommandType.Command:
var command = BITBinary.Read(reader);
if (command is object[] objs && objs.Length is 1) command = objs[0];
//BIT4Log.Log<KCPNetServer>($"已收到指令:{command},值:\n{JsonConvert.SerializeObject(command, Formatting.Indented)}");
if (command is object[] { Length: 1 } objs) command = objs[0];
BIT4Log.Log<KCPNetServer>($"已收到指令:{command},值:\n{JsonConvert.SerializeObject(command, Formatting.Indented)}");
_events.Invoke(command.GetType().FullName, command);
(int Id,object Command) tuple = (Id,command);
_events.InvokeDirect(command.GetType().FullName,tuple);
break;
case NetCommandType.Heartbeat:
server.Send(Id,new byte[]{(byte)NetCommandType.Heartbeat}, KcpChannel.Reliable);
@@ -179,6 +186,17 @@ namespace BITKit.Net
{
_events.AddListener<T>(handle);
}
public void AddCommandListenerWithId<T>(Action<int, T> handle)
{
_events.AddListenerDirect(typeof(T).FullName, x =>
{
if (x is ValueTuple<int, T> tuple)
{
handle.Invoke(tuple.Item1,tuple.Item2);
}
});
}
public void RemoveCommandListener<T>(Action<T> handle)
{