Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Making ButtplugClient use ConcurrentDictionary #665

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions Buttplug/Client/ButtplugClient.cs
Expand Up @@ -5,6 +5,7 @@
// </copyright>

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -110,8 +111,8 @@ public class ButtplugClient
/// Stores information about devices currently connected to the server.
/// </summary>
[NotNull]
private readonly Dictionary<uint, ButtplugClientDevice> _devices =
new Dictionary<uint, ButtplugClientDevice>();
private readonly ConcurrentDictionary<uint, ButtplugClientDevice> _devices =
new ConcurrentDictionary<uint, ButtplugClientDevice>();

/// <summary>
/// Connector to use for the client. Can be local (server embedded), IPC, Websocket, etc...
Expand Down Expand Up @@ -328,7 +329,7 @@ private async void MessageReceivedHandler(object aSender, MessageReceivedEventAr

case DeviceAdded d:
var dev = new ButtplugClientDevice(_bpLogManager, this, SendDeviceMessageAsync, d);
_devices.Add(d.DeviceIndex, dev);
_devices.AddOrUpdate(d.DeviceIndex, dev, (u, device) => dev);
DeviceAdded?.Invoke(this, new DeviceAddedEventArgs(dev));
break;

Expand All @@ -344,8 +345,11 @@ private async void MessageReceivedHandler(object aSender, MessageReceivedEventAr
}

var oldDev = _devices[d.DeviceIndex];
_devices.Remove(d.DeviceIndex);
DeviceRemoved?.Invoke(this, new DeviceRemovedEventArgs(oldDev));
if (_devices.TryRemove(d.DeviceIndex, out _))
{
DeviceRemoved?.Invoke(this, new DeviceRemovedEventArgs(oldDev));
}

break;

case ScanningFinished _:
Expand Down