This commit is contained in:
CortexCore
2024-06-20 10:55:18 +08:00
parent fe0c98608b
commit 554af9ca4e
3 changed files with 22 additions and 13 deletions

View File

@@ -48,7 +48,9 @@ namespace BITKit.Net
private readonly ConcurrentDictionary<string,MethodInfo> _rpcMethods = new();
private readonly ConcurrentDictionary<string,object> _rpcHandles = new();
private DateTime _lastHeartbeat = DateTime.Now;
private DateTime _now = DateTime.Now;
private TimeSpan _interval = TimeSpan.FromMilliseconds(100);
public KcpNetClient()
{
client = new KcpClient(
@@ -112,6 +114,7 @@ namespace BITKit.Net
_lastHeartbeat = DateTime.Now;
client.Connect(address, port);
_timer.Start();
_interval = TimeSpan.FromMilliseconds(_timer.Interval);
// for (var i = 0; i < 5; i++)
// {
// client.Tick();
@@ -354,6 +357,7 @@ namespace BITKit.Net
public async UniTask<T> GetFromServer<T>(string path = default,params object[] pars)
{
await UniTask.SwitchToThreadPool();
var id = _index++;
using var ms = new MemoryStream();
await using var writer = new BinaryWriter(ms);
@@ -372,18 +376,17 @@ namespace BITKit.Net
var bytes = ms.ToArray();
_commandQueue.Enqueue(bytes);
var startTime = DateTime.Now;
var startTime = _now;
while (true)
{
if(DateTime.Now-startTime>TimeSpan.FromSeconds(5) || IsConnected is false)
if((_now-startTime).TotalSeconds>5 || IsConnected is false)
throw new TimeoutException("请求超时或已断开连接");
if (_p2p.TryRemove(id, out var value))
{
await BITApp.SwitchToMainThread();
if (value is Exception e)
{
throw e;
@@ -394,8 +397,7 @@ namespace BITKit.Net
}
return value.As<T>();
}
await UniTask.Yield();
await Task.Delay(_interval);
}
}
@@ -490,6 +492,7 @@ namespace BITKit.Net
{
try
{
_now = DateTime.UtcNow;
if (IsConnected)
{
if (DateTime.Now - _lastHeartbeat > TimeSpan.FromSeconds(5))