1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00

Merge pull request #303 from DarthAffe/DeviceExceptionHandling

Added exception-handling to all UpdateQueues
This commit is contained in:
DarthAffe 2023-03-05 16:38:32 +01:00 committed by GitHub
commit 1b4b92b44c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 222 additions and 97 deletions

View File

@ -178,7 +178,7 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
/// </summary>
/// <param name="ex">The exception to throw.</param>
/// <param name="isCritical">Indicates if the exception is critical for device provider to work correctly.</param>
protected virtual void Throw(Exception ex, bool isCritical = false)
public virtual void Throw(Exception ex, bool isCritical = false)
{
ExceptionEventArgs args = new(ex, isCritical, ThrowsExceptions);
try { OnException(args); } catch { /* we don't want to throw due to bad event handlers */ }

View File

@ -20,6 +20,10 @@ public interface IRGBDeviceProvider : IDisposable
/// <summary>
/// Indicates if exceptions in the device provider are thrown or silently ignored.
/// </summary>
/// <remarks>
/// This should only be set to <c>true</c> for debugging/development purposes.
/// Production code should use the <see cref="Exception"/>-Event to handle exceptions.
/// </remarks>
bool ThrowsExceptions { get; }
/// <summary>

View File

@ -89,8 +89,9 @@ public class AsusUpdateQueue : UpdateQueue
Device.Apply();
}
catch
{ /* "The server threw an exception." seems to be a thing here ... */
catch (Exception ex)
{
AsusDeviceProvider.Instance.Throw(ex, true);
}
}

View File

@ -38,6 +38,8 @@ public class CoolerMasterUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
foreach ((object key, Color color) in dataSet)
{
@ -47,6 +49,11 @@ public class CoolerMasterUpdateQueue : UpdateQueue
_CoolerMasterSDK.SetAllLedColor(_deviceMatrix, _deviceIndex);
}
catch (Exception ex)
{
CoolerMasterDeviceProvider.Instance.Throw(ex, true);
}
}
#endregion
}

View File

@ -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()
{

View File

@ -39,6 +39,8 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override unsafe void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
Span<_CorsairLedColor> colors = new((void*)_colorPtr, dataSet.Length);
for (int i = 0; i < colors.Length; i++)
@ -52,6 +54,11 @@ public class CorsairDeviceUpdateQueue : UpdateQueue
if (error != CorsairError.Success)
throw new RGBDeviceException($"Failed to update device '{_device.id}'. (ErrorCode: {error})");
}
catch (Exception ex)
{
CorsairDeviceProvider.Instance.Throw(ex, true);
}
}
/// <inheritdoc />
public override void Dispose()

View File

@ -60,6 +60,8 @@ public class E131UpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
DataPacket.SetSequenceNumber(GetNextSequenceNumber());
@ -72,6 +74,11 @@ public class E131UpdateQueue : UpdateQueue
_socket.Send(DataPacket, DataPacket.Length);
}
catch (Exception ex)
{
DMXDeviceProvider.Instance.Throw(ex, true);
}
}
/// <summary>
/// Increments the sequence number and wraps it if neded.

View File

@ -26,6 +26,8 @@ public class LogitechPerDeviceUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
Color color = dataSet[0].color;
@ -34,6 +36,11 @@ public class LogitechPerDeviceUpdateQueue : UpdateQueue
(int)Math.Round(color.G * 100),
(int)Math.Round(color.B * 100));
}
catch (Exception ex)
{
LogitechDeviceProvider.Instance.Throw(ex, true);
}
}
#endregion
}

View File

@ -26,6 +26,8 @@ public class LogitechPerKeyUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
_LogitechGSDK.LogiLedSetTargetDevice(LogitechDeviceCaps.PerKeyRGB);
@ -39,6 +41,11 @@ public class LogitechPerKeyUpdateQueue : UpdateQueue
(int)MathF.Round(color.B * 100));
}
}
catch (Exception ex)
{
LogitechDeviceProvider.Instance.Throw(ex, true);
}
}
#endregion
}

View File

@ -35,10 +35,17 @@ public class MsiDeviceUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
foreach ((object key, Color color) in dataSet)
_MsiSDK.SetLedColor(_deviceType, (int)key, color.GetR(), color.GetG(), color.GetB());
}
catch (Exception ex)
{
MsiDeviceProvider.Instance.Throw(ex, true);
}
}
#endregion
}

View File

@ -36,10 +36,17 @@ public abstract class MidiUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
foreach ((object key, Color color) in dataSet)
SendMessage(CreateMessage(key, color));
}
catch (Exception ex)
{
NovationDeviceProvider.Instance.Throw(ex, true);
}
}
/// <summary>
/// Sends the specified message to the device this queue is performing updates for.

View File

@ -50,12 +50,19 @@ public class OpenRGBUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
foreach ((object key, Color color) in dataSet)
_colors[(int)key] = new OpenRGBColor(color.GetR(), color.GetG(), color.GetB());
_openRGB.UpdateLeds(_deviceid, _colors);
}
catch (Exception ex)
{
OpenRGBDeviceProvider.Instance.Throw(ex, true);
}
}
#endregion
}

View File

@ -45,6 +45,8 @@ public class PicoPiBulkUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
Span<byte> buffer = _dataBuffer;
foreach ((object key, Color color) in dataSet)
@ -61,6 +63,11 @@ public class PicoPiBulkUpdateQueue : UpdateQueue
_sdk.SendBulkUpdate(buffer, _channel);
}
catch (Exception ex)
{
PicoPiDeviceProvider.Instance.Throw(ex, true);
}
}
#endregion
}

View File

@ -42,6 +42,8 @@ public class PicoPiHIDUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
Span<byte> buffer = _dataBuffer;
foreach ((object key, Color color) in dataSet)
@ -58,6 +60,11 @@ public class PicoPiHIDUpdateQueue : UpdateQueue
_sdk.SendHidUpdate(buffer, _channel);
}
catch (Exception ex)
{
PicoPiDeviceProvider.Instance.Throw(ex, true);
}
}
#endregion
}

View File

@ -23,8 +23,7 @@ public abstract class RazerUpdateQueue : UpdateQueue
/// <param name="updateTrigger">The update trigger used to update this queue.</param>
protected RazerUpdateQueue(IDeviceUpdateTrigger updateTrigger)
: base(updateTrigger)
{
}
{ }
#endregion
@ -32,6 +31,8 @@ public abstract class RazerUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
IntPtr effectParams = CreateEffectParams(dataSet);
Guid effectId = Guid.NewGuid();
@ -44,6 +45,11 @@ public abstract class RazerUpdateQueue : UpdateQueue
_lastEffect = effectId;
}
catch (Exception ex)
{
RazerDeviceProvider.Instance.Throw(ex, true);
}
}
/// <summary>
/// Creates the effect used to update this device.

View File

@ -36,12 +36,19 @@ internal class SteelSeriesDeviceUpdateQueue : UpdateQueue
#region Methods
protected override void OnUpdate(object? sender, CustomUpdateData customData)
{
try
{
if (customData[CustomUpdateDataIndex.HEARTBEAT] as bool? ?? false)
SteelSeriesSDK.SendHeartbeat();
else
base.OnUpdate(sender, customData);
}
catch (Exception ex)
{
SteelSeriesDeviceProvider.Instance.Throw(ex, true);
}
}
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)

View File

@ -57,6 +57,8 @@ public abstract class SerialConnectionUpdateQueue<TData> : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
foreach (TData command in GetCommands(dataSet.ToArray()))
{
@ -64,6 +66,11 @@ public abstract class SerialConnectionUpdateQueue<TData> : UpdateQueue
SendCommand(command);
}
}
catch (Exception ex)
{
WS281XDeviceProvider.Instance.Throw(ex, true);
}
}
/// <summary>
/// Gets the commands that need to be sent to the device to update the requested colors.

View File

@ -32,6 +32,8 @@ public class WootingUpdateQueue : UpdateQueue
/// <inheritdoc />
protected override void Update(in ReadOnlySpan<(object key, Color color)> dataSet)
{
try
{
lock (_WootingSDK.SdkLock)
{
@ -46,6 +48,11 @@ public class WootingUpdateQueue : UpdateQueue
_WootingSDK.ArrayUpdateKeyboard();
}
}
catch (Exception ex)
{
WootingDeviceProvider.Instance.Throw(ex, true);
}
}
#endregion
}