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