DarthAffe / RGB.NET

The one-stop SDK for RGB-peripherals
http://lib.arge.be
GNU Lesser General Public License v2.1
403 stars 71 forks source link

Feature Discussion - Device Addition and Removal #360

Open diogotr7 opened 9 months ago

diogotr7 commented 9 months ago

I'm making this issue mostly because of OpenRGB's hotplug support. It will listen for changes and detect devices as they are plugged in. This is useful for some wireless devices that will be turned on and off whislt OpenRGB is expected to stay running.

The OpenRGB SDK implements this by sending a device update packet through the socket:

    private async Task ReadLoop()
    {
        while (!_cancellationTokenSource.IsCancellationRequested && Connected)
        {
            try
            {
                //todo: handle zero
                await _socket.ReceiveAsync(_headerBuffer, SocketFlags.None, _cancellationTokenSource.Token);

                var dataLength = ParseHeader();
                if (dataLength.Command == CommandId.DeviceListUpdated)
                {
                    //TODO: is this the best way to do this?
                    DeviceListUpdated?.Invoke(this, EventArgs.Empty);
                }
                else
                {
                    var dataBuffer = new byte[dataLength.DataLength];
                    await _socket.ReceiveAsync(dataBuffer, SocketFlags.None, _cancellationTokenSource.Token);
                    _pendingRequests[dataLength.Command].Add(dataBuffer);
                }
            }
            catch (TaskCanceledException)
            {
                //ignore
            }
        }
    }

My question is: does any other device provider implement something like this? Do we even want to handle this on the RGB.NET level? I could have artemis start up a new OpenRGB SDK connection and restart the plugin when one of these events is received if that's better. Opinions?

diogotr7 commented 9 months ago

338 #339

DarthAffe commented 9 months ago

I like the idea of supporting hotplugging/reconnect scenarios in general (and discussed that a while back with @Aytackydln) but the way RGB.NET works causes some problems in that regard and adds a lot of complexity and potential issues, which make a feature like this quite hard to implement in the lower levels. (Even on device-provider level is a lot that can break stuff for user).

Aytackydln commented 9 months ago

I've been using my changes on a fork on Aurora since I made the PRs. Though it only works when manually refreshing the devices and on start of OpenRGB, I've seen no reports of any weird behaviour.

Though as you've mentioned Aurora just uses rgb.net as an interface to send color data and not for the surfaces it provides

Edit: also testing with the latest hotplug fork of OpenRGB. So far good.