1
This commit is contained in:
@@ -37,6 +37,7 @@ namespace BITKit.Net
|
||||
|
||||
private readonly GenericEvent _events = new();
|
||||
|
||||
private readonly ValidHandle _isConnected = new();
|
||||
public KcpNetClient()
|
||||
{
|
||||
client = new KcpClient(
|
||||
@@ -52,6 +53,14 @@ namespace BITKit.Net
|
||||
{
|
||||
Id = x.Id;
|
||||
});
|
||||
_isConnected.AddListener(ConnectionCallback);
|
||||
}
|
||||
|
||||
private async void ConnectionCallback(bool x)
|
||||
{
|
||||
if (x is false) return;
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
OnConnected?.Invoke();
|
||||
}
|
||||
|
||||
private void Tick(object sender, ElapsedEventArgs e)
|
||||
@@ -89,11 +98,15 @@ namespace BITKit.Net
|
||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
|
||||
OnConnected?.Invoke();
|
||||
if (client.connected)
|
||||
{
|
||||
SendServerMessage(Environment.MachineName);
|
||||
}
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
client.Tick();
|
||||
await Task.Delay(100);
|
||||
}
|
||||
return client.connected;
|
||||
}
|
||||
catch (Exception e)
|
||||
@@ -143,6 +156,7 @@ namespace BITKit.Net
|
||||
break;
|
||||
case NetCommandType.Heartbeat:
|
||||
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
|
||||
_isConnected.AddElement(this);
|
||||
break;
|
||||
case NetCommandType.Ping:
|
||||
Ping = (int)(DateTime.Now - _lastPingTime).TotalMilliseconds;
|
||||
@@ -173,6 +187,7 @@ namespace BITKit.Net
|
||||
if (BITApp.SynchronizationContext is not null)
|
||||
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext,
|
||||
BITApp.CancellationToken);
|
||||
_isConnected.RemoveElement(this);
|
||||
OnDisconnected?.Invoke();
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
|
Reference in New Issue
Block a user