1
This commit is contained in:
@@ -21,7 +21,7 @@ namespace BITKit.Net
|
|||||||
public event Action OnConnected;
|
public event Action OnConnected;
|
||||||
public event Action OnDisconnected;
|
public event Action OnDisconnected;
|
||||||
public event Action OnConnectedFailed;
|
public event Action OnConnectedFailed;
|
||||||
public bool IsConnected => client.connected;
|
public bool IsConnected => _isConnected;
|
||||||
public float2 Traffic { get; set; }
|
public float2 Traffic { get; set; }
|
||||||
public bool ManualTick { 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,Func<object,UniTask<object>>> _rpc = new();
|
||||||
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;
|
||||||
|
|
||||||
public KcpNetClient()
|
public KcpNetClient()
|
||||||
{
|
{
|
||||||
@@ -72,12 +73,13 @@ namespace BITKit.Net
|
|||||||
if (x)
|
if (x)
|
||||||
{
|
{
|
||||||
OnConnected?.Invoke();
|
OnConnected?.Invoke();
|
||||||
|
BIT4Log.Log<KcpNetClient>("连接成功");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
OnDisconnected?.Invoke();
|
OnDisconnected?.Invoke();
|
||||||
|
BIT4Log.Warning<KcpNetClient>("连接已断开");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Tick(object sender, ElapsedEventArgs e)
|
private void Tick(object sender, ElapsedEventArgs e)
|
||||||
@@ -89,6 +91,7 @@ namespace BITKit.Net
|
|||||||
public async void Disconnect()
|
public async void Disconnect()
|
||||||
{
|
{
|
||||||
client.Disconnect();
|
client.Disconnect();
|
||||||
|
_isConnected.RemoveElement(this);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,BITApp.CancellationToken);
|
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,BITApp.CancellationToken);
|
||||||
@@ -163,8 +166,6 @@ namespace BITKit.Net
|
|||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
case NetCommandType.Message:
|
case NetCommandType.Message:
|
||||||
reader.ReadBoolean();
|
|
||||||
reader.ReadString();
|
|
||||||
BIT4Log.Log<KcpClient>($"已收到消息:{reader.ReadString()}");
|
BIT4Log.Log<KcpClient>($"已收到消息:{reader.ReadString()}");
|
||||||
break;
|
break;
|
||||||
case NetCommandType.AllClientCommand:
|
case NetCommandType.AllClientCommand:
|
||||||
@@ -193,6 +194,7 @@ namespace BITKit.Net
|
|||||||
Traffic+=new float2(1,0);
|
Traffic+=new float2(1,0);
|
||||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||||
_isConnected.AddElement(this);
|
_isConnected.AddElement(this);
|
||||||
|
_lastHeartbeat = DateTime.Now;
|
||||||
break;
|
break;
|
||||||
case NetCommandType.Ping:
|
case NetCommandType.Ping:
|
||||||
Ping = (int)(DateTime.Now - _lastPingTime).TotalMilliseconds;
|
Ping = (int)(DateTime.Now - _lastPingTime).TotalMilliseconds;
|
||||||
@@ -366,7 +368,7 @@ namespace BITKit.Net
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if(DateTime.Now-startTime>TimeSpan.FromSeconds(5) || IsConnected is false)
|
if(DateTime.Now-startTime>TimeSpan.FromSeconds(5) || IsConnected is false)
|
||||||
throw new TimeoutException();
|
throw new TimeoutException("请求超时或已断开连接");
|
||||||
|
|
||||||
if (_p2p.TryRemove(id, out var value))
|
if (_p2p.TryRemove(id, out var value))
|
||||||
{
|
{
|
||||||
@@ -466,6 +468,15 @@ namespace BITKit.Net
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (IsConnected)
|
||||||
|
{
|
||||||
|
if (DateTime.Now - _lastHeartbeat > TimeSpan.FromSeconds(5))
|
||||||
|
{
|
||||||
|
BIT4Log.Warning<KcpNetClient>("心跳超时,自动断开");
|
||||||
|
Disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (commandQueue.TryDequeue(out var bytes))
|
while (commandQueue.TryDequeue(out var bytes))
|
||||||
{
|
{
|
||||||
Traffic+=new float2(bytes.Length,0);
|
Traffic+=new float2(bytes.Length,0);
|
||||||
|
@@ -16,12 +16,14 @@ namespace BITKit.Net.Kcp
|
|||||||
[SerializeField] private ushort m_port;
|
[SerializeField] private ushort m_port;
|
||||||
[SerializeField] private bool connectOnStart;
|
[SerializeField] private bool connectOnStart;
|
||||||
[SerializeField] private bool autoReconnect;
|
[SerializeField] private bool autoReconnect;
|
||||||
|
[Header(Constant.Header.Debug)]
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
[ReadOnly]private Vector2 traffic;
|
[ReadOnly]private Vector2 traffic;
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
[ReadOnly]private string upTraffic;
|
[ReadOnly]private string upTraffic;
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
[ReadOnly]private string downTraffic;
|
[ReadOnly]private string downTraffic;
|
||||||
|
[SerializeField, ReadOnly] private bool isConnected;
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
[SerializeField] private Optional<string> allowDebugHost;
|
[SerializeField] private Optional<string> allowDebugHost;
|
||||||
@@ -34,6 +36,8 @@ namespace BITKit.Net.Kcp
|
|||||||
private INetClient _netClientImplementation=>client;
|
private INetClient _netClientImplementation=>client;
|
||||||
private INetProvider _netProviderImplementation=>client;
|
private INetProvider _netProviderImplementation=>client;
|
||||||
|
|
||||||
|
private readonly IntervalUpdate _reconnectInterval = new(1);
|
||||||
|
|
||||||
public event Action OnStartConnect
|
public event Action OnStartConnect
|
||||||
{
|
{
|
||||||
add => _netClientImplementation.OnStartConnect += value;
|
add => _netClientImplementation.OnStartConnect += value;
|
||||||
@@ -171,30 +175,10 @@ namespace BITKit.Net.Kcp
|
|||||||
{
|
{
|
||||||
_netProviderImplementation.HandShake();
|
_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()
|
private void Awake()
|
||||||
{
|
{
|
||||||
Singleton = this;
|
Singleton = this;
|
||||||
client = new KcpNetClient();
|
client = new KcpNetClient();
|
||||||
client.OnConnectedFailed += Reconnect;
|
|
||||||
}
|
}
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
@@ -224,12 +208,19 @@ namespace BITKit.Net.Kcp
|
|||||||
|
|
||||||
private void OnTick(float obj)
|
private void OnTick(float obj)
|
||||||
{
|
{
|
||||||
|
if (client.IsConnected is false && autoReconnect)
|
||||||
|
{
|
||||||
|
if (_reconnectInterval.AllowUpdate)
|
||||||
|
client.Connect(m_host, m_port).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
timer.Update(obj);
|
timer.Update(obj);
|
||||||
rate = timer;
|
rate = timer;
|
||||||
Tick();
|
Tick();
|
||||||
traffic = client.Traffic;
|
traffic = client.Traffic;
|
||||||
upTraffic = NetUtils.GetFileSize((long)traffic.x);
|
upTraffic = NetUtils.GetFileSize((long)traffic.x);
|
||||||
downTraffic = NetUtils.GetFileSize((long)traffic.y);
|
downTraffic = NetUtils.GetFileSize((long)traffic.y);
|
||||||
|
isConnected = IsConnected;
|
||||||
}
|
}
|
||||||
[BIT]
|
[BIT]
|
||||||
private void EditorConnect()
|
private void EditorConnect()
|
||||||
|
Reference in New Issue
Block a user