Improve exception handling for SteamController

This commit is contained in:
Kamil Trzciński 2022-12-16 11:05:45 +01:00
parent 9f1288cfb0
commit 12b92ab151
2 changed files with 19 additions and 2 deletions

View file

@ -46,14 +46,14 @@ namespace SteamController.Devices
neptuneDevice.OpenDevice();
}
internal void Fail()
internal void Fail(bool immediately = false)
{
foreach (var action in AllActions)
action.Reset();
// Try to re-open every MaxFailures
failures++;
if (failures % MaxFailures == 0)
if (failures % MaxFailures == 0 || immediately)
{
OpenDevice();
failures = 0;
@ -89,6 +89,12 @@ namespace SteamController.Devices
BeforeUpdate(data);
Updated = true;
}
catch (hidapi.HidDeviceInvalidException)
{
// Steam might disconnect device
Fail();
Updated = false;
}
catch (Exception e)
{
TraceException("STEAM", "BeforeUpdate", e);
@ -107,6 +113,11 @@ namespace SteamController.Devices
UpdateLizardButtons();
UpdateLizardMouse();
}
catch (hidapi.HidDeviceInvalidException)
{
// Steam might disconnect device
Fail();
}
catch (Exception e)
{
TraceException("STEAM", "Update", e);

View file

@ -108,6 +108,12 @@ namespace SteamController.Devices
hapticTasks[position] = neptuneDevice.RequestFeatureReportAsync(bytes);
return true;
}
catch (hidapi.HidDeviceInvalidException)
{
// Steam might disconnect device
Fail();
return false;
}
catch (Exception e)
{
TraceException("STEAM", "Haptic", e);