This commit is contained in:
parent
fe0c98608b
commit
554af9ca4e
|
@ -7,7 +7,7 @@ namespace BITKit.Net
|
|||
public static readonly KcpConfig Config = new KcpConfig(
|
||||
NoDelay: true,
|
||||
DualMode: false,
|
||||
Interval: 1, // 1ms so at interval code at least runs.
|
||||
Interval: 32, // 1ms so at interval code at least runs.
|
||||
Timeout: 8000,
|
||||
CongestionWindow: false
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace BITKit.Net
|
|||
{
|
||||
public class KCPNetServer:INetServer,INetProvider
|
||||
{
|
||||
public int TickRate { get; set; }
|
||||
public int TickRate { get; set; } = 16;
|
||||
public bool ManualTick { get; set; }
|
||||
public event Action<int> OnClientConnected;
|
||||
public event Action<int> OnClientDisconnected;
|
||||
|
@ -38,6 +38,9 @@ namespace BITKit.Net
|
|||
private readonly ConcurrentDictionary<string,object> _rpcHandles = new();
|
||||
|
||||
private readonly ConcurrentQueue<(int id,byte[] bytes)> _sendQueue = new();
|
||||
|
||||
private DateTime _now=DateTime.Now;
|
||||
private TimeSpan _interval=TimeSpan.FromSeconds(0.32);
|
||||
|
||||
public KCPNetServer()
|
||||
{
|
||||
|
@ -64,6 +67,7 @@ namespace BITKit.Net
|
|||
|
||||
private void Tick(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
_now = DateTime.UtcNow;
|
||||
try
|
||||
{
|
||||
while (_sendQueue.TryDequeue(out var value))
|
||||
|
@ -100,6 +104,7 @@ namespace BITKit.Net
|
|||
{
|
||||
_timer.Interval = 1000f / TickRate;
|
||||
}
|
||||
_interval = TimeSpan.FromSeconds(1.0 / TickRate);
|
||||
OnStartServer?.Invoke();
|
||||
server.Start(port);
|
||||
_timer.Start();
|
||||
|
@ -379,6 +384,7 @@ namespace BITKit.Net
|
|||
|
||||
public async UniTask<T> GetFromClient<T>(int id,string path=default, params object[] pars)
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
var index = _index++;
|
||||
using var ms = new MemoryStream();
|
||||
await using var writer = new BinaryWriter(ms);
|
||||
|
@ -398,14 +404,15 @@ namespace BITKit.Net
|
|||
var bytes = ms.ToArray();
|
||||
//server.Send(id,bytes,KcpChannel.Reliable);
|
||||
_sendQueue.Enqueue((id,bytes));
|
||||
var startTime = DateTime.Now;
|
||||
var startTime = _now;
|
||||
while (true)
|
||||
{
|
||||
if(DateTime.Now-startTime>TimeSpan.FromSeconds(5))
|
||||
if((_now-startTime).TotalSeconds>5)
|
||||
throw new TimeoutException($"等待超时,Id:{id},时间{DateTime.Now-startTime}");
|
||||
|
||||
if (_p2p.TryRemove(index, out var value))
|
||||
{
|
||||
await BITApp.SwitchToMainThread();
|
||||
if (value is Exception e)
|
||||
{
|
||||
throw e;
|
||||
|
@ -420,8 +427,7 @@ namespace BITKit.Net
|
|||
}
|
||||
return (T)value;
|
||||
}
|
||||
|
||||
await UniTask.Yield();
|
||||
await Task.Delay(_interval);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue