This commit is contained in:
parent
65f9e40105
commit
d22cef8990
|
@ -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);
|
||||
|
|
|
@ -16,12 +16,14 @@ namespace BITKit.Net.Kcp
|
|||
[SerializeField] private ushort m_port;
|
||||
[SerializeField] private bool connectOnStart;
|
||||
[SerializeField] private bool autoReconnect;
|
||||
[Header(Constant.Header.Debug)]
|
||||
[SerializeField]
|
||||
[ReadOnly]private Vector2 traffic;
|
||||
[SerializeField]
|
||||
[ReadOnly]private string upTraffic;
|
||||
[SerializeField]
|
||||
[ReadOnly]private string downTraffic;
|
||||
[SerializeField, ReadOnly] private bool isConnected;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[SerializeField] private Optional<string> allowDebugHost;
|
||||
|
@ -33,6 +35,8 @@ namespace BITKit.Net.Kcp
|
|||
private KcpNetClient client;
|
||||
private INetClient _netClientImplementation=>client;
|
||||
private INetProvider _netProviderImplementation=>client;
|
||||
|
||||
private readonly IntervalUpdate _reconnectInterval = new(1);
|
||||
|
||||
public event Action OnStartConnect
|
||||
{
|
||||
|
@ -171,30 +175,10 @@ namespace BITKit.Net.Kcp
|
|||
{
|
||||
_netProviderImplementation.HandShake();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private async void Reconnect()
|
||||
{
|
||||
if (autoReconnect is false) return;
|
||||
try
|
||||
{
|
||||
await Task.Delay(1000, destroyCancellationToken);
|
||||
if (client.IsConnected is false)
|
||||
{
|
||||
await client.Connect(m_host, m_port);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
private void Awake()
|
||||
{
|
||||
Singleton = this;
|
||||
client = new KcpNetClient();
|
||||
client.OnConnectedFailed += Reconnect;
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
|
@ -224,12 +208,19 @@ namespace BITKit.Net.Kcp
|
|||
|
||||
private void OnTick(float obj)
|
||||
{
|
||||
if (client.IsConnected is false && autoReconnect)
|
||||
{
|
||||
if (_reconnectInterval.AllowUpdate)
|
||||
client.Connect(m_host, m_port).Forget();
|
||||
}
|
||||
|
||||
timer.Update(obj);
|
||||
rate = timer;
|
||||
Tick();
|
||||
traffic = client.Traffic;
|
||||
upTraffic = NetUtils.GetFileSize((long)traffic.x);
|
||||
downTraffic = NetUtils.GetFileSize((long)traffic.y);
|
||||
isConnected = IsConnected;
|
||||
}
|
||||
[BIT]
|
||||
private void EditorConnect()
|
||||
|
|
Loading…
Reference in New Issue