Skip to content

Commit

Permalink
Feedback on final handshake timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 17, 2025
1 parent 69483dc commit ca3e4d4
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions SlimeVrOta.Gui/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ public async Task ReceiveHandshake(int delay = 0, CancellationToken cancelToken
}
catch
{
if (cancelToken.IsCancellationRequested)
return;
cancelToken.ThrowIfCancellationRequested();
}
}

Expand All @@ -258,13 +257,15 @@ public async Task ReceiveHandshake(int delay = 0, CancellationToken cancelToken
}
}

while (!cancelToken.IsCancellationRequested && _socket.IsBound)
while (_socket.IsBound)
{
cancelToken.ThrowIfCancellationRequested();
try
{
// Clear socket buffer
while (!cancelToken.IsCancellationRequested && _socket.Available > 0)
while (_socket.Available > 0)
{
cancelToken.ThrowIfCancellationRequested();
await _socket.ReceiveFromAsync(_udpBuffer, _endPoint, cancelToken);
}

Expand Down Expand Up @@ -340,7 +341,6 @@ private void UpdateFlashStatus()
else
{
FlashButton.IsEnabled = false;

switch (CurrentState)
{
case FlashState.Connecting:
Expand Down Expand Up @@ -421,31 +421,57 @@ public async void FlashFirmware(object? sender, RoutedEventArgs e)
Debug.WriteLine("Waiting for tracker response");
CurrentState = FlashState.Waiting;

using var cancelSrc = new CancellationTokenSource();
var handshake = ReceiveHandshake(cancelToken: cancelSrc.Token);
using var timeoutSrc = new CancellationTokenSource();
var handshake = ReceiveHandshake(cancelToken: timeoutSrc.Token);
// 45 second timeout, it should not take that long
cancelSrc.CancelAfter(45000);
timeoutSrc.CancelAfter(45000);

using var timerCancel = new CancellationTokenSource();
// Visually display a reasonable waiting time on the progress bar
try
{
handshake.GetAwaiter().OnCompleted(timerCancel.Cancel);
using var onHandshakeSrc = CancellationTokenSource.CreateLinkedTokenSource(
timeoutSrc.Token
);
handshake
.GetAwaiter()
.OnCompleted(() =>
{
try
{
onHandshakeSrc.Cancel();
}
catch
{
// Ignore
}
});

while (
!timerCancel.IsCancellationRequested
!onHandshakeSrc.IsCancellationRequested
&& !handshake.IsCompleted
&& FlashProgress.Value < 95.0
)
{
await Task.Delay(300, timerCancel.Token);
await Task.Delay(300, onHandshakeSrc.Token);
FlashProgress.Value += 1.0;
}
}
catch
{
// Ignore, this is just to wait for the handshake to finish
// Ignore errors, this is purely visual
}

await handshake;
try
{
await handshake;
}
catch (Exception ex)
{
throw new Exception(
"Waiting for handshake timed out. Tracker may be bricked, proceed with caution.",
ex
);
}

Debug.WriteLine("Firmware flashed successfully");
FlashProgress.Value = 100.0;
Expand Down

0 comments on commit ca3e4d4

Please sign in to comment.