1
This commit is contained in:
@@ -21,7 +21,7 @@ namespace BITKit.Net
|
||||
public event Action OnConnected;
|
||||
public event Action OnDisconnected;
|
||||
public event Action OnConnectedFailed;
|
||||
public bool IsConnected => client.connected;
|
||||
public bool IsConnected => _isConnected;
|
||||
public float2 Traffic { get; set; }
|
||||
public bool ManualTick { get; set; }
|
||||
|
||||
@@ -47,6 +47,7 @@ namespace BITKit.Net
|
||||
private readonly ConcurrentDictionary<string,Func<object,UniTask<object>>> _rpc = new();
|
||||
private readonly ConcurrentDictionary<string,MethodInfo> _rpcMethods = new();
|
||||
private readonly ConcurrentDictionary<string,object> _rpcHandles = new();
|
||||
private DateTime _lastHeartbeat = DateTime.Now;
|
||||
|
||||
public KcpNetClient()
|
||||
{
|
||||
@@ -72,12 +73,13 @@ namespace BITKit.Net
|
||||
if (x)
|
||||
{
|
||||
OnConnected?.Invoke();
|
||||
BIT4Log.Log<KcpNetClient>("连接成功");
|
||||
}
|
||||
else
|
||||
{
|
||||
OnDisconnected?.Invoke();
|
||||
BIT4Log.Warning<KcpNetClient>("连接已断开");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void Tick(object sender, ElapsedEventArgs e)
|
||||
@@ -89,6 +91,7 @@ namespace BITKit.Net
|
||||
public async void Disconnect()
|
||||
{
|
||||
client.Disconnect();
|
||||
_isConnected.RemoveElement(this);
|
||||
try
|
||||
{
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,BITApp.CancellationToken);
|
||||
@@ -163,8 +166,6 @@ namespace BITKit.Net
|
||||
switch (type)
|
||||
{
|
||||
case NetCommandType.Message:
|
||||
reader.ReadBoolean();
|
||||
reader.ReadString();
|
||||
BIT4Log.Log<KcpClient>($"已收到消息:{reader.ReadString()}");
|
||||
break;
|
||||
case NetCommandType.AllClientCommand:
|
||||
@@ -193,6 +194,7 @@ namespace BITKit.Net
|
||||
Traffic+=new float2(1,0);
|
||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
_isConnected.AddElement(this);
|
||||
_lastHeartbeat = DateTime.Now;
|
||||
break;
|
||||
case NetCommandType.Ping:
|
||||
Ping = (int)(DateTime.Now - _lastPingTime).TotalMilliseconds;
|
||||
@@ -366,7 +368,7 @@ namespace BITKit.Net
|
||||
while (true)
|
||||
{
|
||||
if(DateTime.Now-startTime>TimeSpan.FromSeconds(5) || IsConnected is false)
|
||||
throw new TimeoutException();
|
||||
throw new TimeoutException("请求超时或已断开连接");
|
||||
|
||||
if (_p2p.TryRemove(id, out var value))
|
||||
{
|
||||
@@ -466,6 +468,15 @@ namespace BITKit.Net
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsConnected)
|
||||
{
|
||||
if (DateTime.Now - _lastHeartbeat > TimeSpan.FromSeconds(5))
|
||||
{
|
||||
BIT4Log.Warning<KcpNetClient>("心跳超时,自动断开");
|
||||
Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
while (commandQueue.TryDequeue(out var bytes))
|
||||
{
|
||||
Traffic+=new float2(bytes.Length,0);
|
||||
|
Reference in New Issue
Block a user