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

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Timers;
using Cysharp.Threading.Tasks;
using kcp2k;
@@ -21,6 +22,9 @@ namespace BITKit.Net
public int Ping => -1;
public int Id { get; private set; } = -1;
private readonly KcpClient client;
private readonly Queue<byte[]> commandQueue = new();
private readonly Timer _timer = new(100)
{
AutoReset = true
@@ -44,8 +48,13 @@ namespace BITKit.Net
});
}
private void Tick(object sender, ElapsedEventArgs e)
private async void Tick(object sender, ElapsedEventArgs e)
{
await UniTask.SwitchToThreadPool();
while (commandQueue.TryDequeue(out var bytes))
{
client.Send(bytes, KcpChannel.Reliable);
}
client.Tick();
}
@@ -108,8 +117,7 @@ namespace BITKit.Net
case NetCommandType.Command:
var command = BITBinary.Read(reader);
if (command is object[] { Length: 1 } objs) command = objs[0];
// BIT4Log.Log<KcpClient>(
// $"已收到指令:{command},值:\n{JsonConvert.SerializeObject(command, Formatting.Indented)}");
//BIT4Log.Log<KcpClient>($"已收到指令:{command},值:\n{JsonConvert.SerializeObject(command, Formatting.Indented)}");
try
{
if (BITApp.SynchronizationContext is not null)
@@ -128,7 +136,7 @@ namespace BITKit.Net
break;
case NetCommandType.Heartbeat:
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
//client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
break;
default:
BIT4Log.Log<KcpClient>($"未知消息类型:{type},字节:{(byte)type}");
@@ -249,7 +257,8 @@ namespace BITKit.Net
.Write((byte)commandType)
.WriteObject(values)
.Build();
client.Send(bytes, KcpChannel.Reliable);
commandQueue.Enqueue(bytes);
//client.Send(bytes, KcpChannel.Reliable);
}
}

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)
{