Added exception-handling to all UpdateQueues

This commit is contained in:
DarthAffe committed 2023-03-05 16:34:01 +01:00
1 parent 6e313ea6a1
commit 37e4954583
18 files changed
+222 -97

No files matched your search

@@ -50,6 +50,26 @@ public class CorsairDeviceProvider : AbstractRGBDeviceProvider
/// </summary>
public CorsairSessionDetails SessionDetails { get; private set; } = new();
private CorsairSessionState _sessionState = CorsairSessionState.Invalid;
public CorsairSessionState SessionState
{
get => _sessionState;
private set
{
_sessionState = value;
try { SessionStateChanged?.Invoke(this, SessionState); }
catch { /* catch faulty event-handlers*/ }
}
}
#endregion
#region Events
// ReSharper disable once UnassignedField.Global
public EventHandler<CorsairSessionState>? SessionStateChanged;
#endregion
#region Constructors
@@ -74,7 +94,7 @@ public class CorsairDeviceProvider : AbstractRGBDeviceProvider
using ManualResetEventSlim waitEvent = new(false);
void OnSessionStateChanged(object? sender, CorsairSessionState state)
void OnInitializeSessionStateChanged(object? sender, CorsairSessionState state)
{
if (state == CorsairSessionState.Connected)
// ReSharper disable once AccessToDisposedClosure
@@ -84,6 +104,7 @@ public class CorsairDeviceProvider : AbstractRGBDeviceProvider
try
{
_CUESDK.SessionStateChanged += OnSessionStateChanged;
_CUESDK.SessionStateChanged += OnInitializeSessionStateChanged;
CorsairError errorCode = _CUESDK.CorsairConnect();
if (errorCode != CorsairError.Success)
@@ -100,10 +121,12 @@ public class CorsairDeviceProvider : AbstractRGBDeviceProvider
}
finally
{
_CUESDK.SessionStateChanged -= OnSessionStateChanged;
_CUESDK.SessionStateChanged -= OnInitializeSessionStateChanged;
}
}
private void OnSessionStateChanged(object? sender, CorsairSessionState state) => SessionState = state;
/// <inheritdoc />
protected override IEnumerable<IRGBDevice> LoadDevices()
{
@@ -129,7 +152,7 @@ public class CorsairDeviceProvider : AbstractRGBDeviceProvider
Throw(new RGBDeviceException($"Failed to take control of device '{device.id}'. (ErrorCode: {error})"));
CorsairDeviceUpdateQueue updateQueue = new(GetUpdateTrigger(), device);
int channelLedCount = 0;
for (int i = 0; i < device.channelCount; i++)
{
@@ -40,17 +40,24 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override unsafe void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
Span<_CorsairLedColor> colors = new((void*)_colorPtr, dataSet.Length);
for (int i = 0; i < colors.Length; i++)
try
{
(object id, Color color) = dataSet[i];
(byte a, byte r, byte g, byte b) = color.GetRGBBytes();
colors[i] = new _CorsairLedColor((CorsairLedId)id, r, g, b, a);
}
Span<_CorsairLedColor> colors = new((void*)_colorPtr, dataSet.Length);
for (int i = 0; i < colors.Length; i++)
{
(object id, Color color) = dataSet[i];
(byte a, byte r, byte g, byte b) = color.GetRGBBytes();
colors[i] = new _CorsairLedColor((CorsairLedId)id, r, g, b, a);
}
CorsairError error = _CUESDK.CorsairSetLedColors(_device.id!, dataSet.Length, _colorPtr);
if (error != CorsairError.Success)
throw new RGBDeviceException($"Failed to update device '{_device.id}'. (ErrorCode: {error})");
CorsairError error = _CUESDK.CorsairSetLedColors(_device.id!, dataSet.Length, _colorPtr);
if (error != CorsairError.Success)
throw new RGBDeviceException($"Failed to update device '{_device.id}'. (ErrorCode: {error})");
}
catch (Exception ex)
{
CorsairDeviceProvider.Instance.Throw(ex, true);
}
}
/// <inheritdoc />