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);
}
}