Skip to content

Address stability issues #89

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

Open
wants to merge 1 commit into
base: 4.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Visual Studio Projects/ZWaveJS.NET/ZWaveJS.NET/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Driver
internal static List<int> UsedPorts = new List<int>();

internal Websocket.Client.WebsocketClient ClientWebSocket;
internal Dictionary<Guid, Action<JObject>> Callbacks;
internal TSafeDictionary<Guid, Action<JObject>> Callbacks;
internal bool Inited = false;
internal ZWaveOptions Options;
internal const string FWUSAPIKey = "921f8000486fcc2744721cfc747aab2db8fc025b5d487cbf2eba76e88ff6f79a064644bf";
Expand Down Expand Up @@ -574,7 +574,7 @@ public Driver(Uri Server, int SchemaVersion = 0, int ServerErrorThrottleTime = 1
_schemaVersion = SchemaVersion;
}

Callbacks = new Dictionary<Guid, Action<JObject>>();
Callbacks = new TSafeDictionary<Guid, Action<JObject>>();
MapEvents();

this.WSAddress = Server;
Expand Down Expand Up @@ -607,7 +607,7 @@ public Driver(string SerialPort, ZWaveOptions Options, int ServerCommunicationPo
settings.Converters.Add(new ZWJSSJsonConverter(this));
_jsonSerializer = JsonSerializer.Create(settings);

Callbacks = new Dictionary<Guid, Action<JObject>>();
Callbacks = new TSafeDictionary<Guid, Action<JObject>>();
MapEvents();

this.SerialPort = SerialPort;
Expand Down
42 changes: 42 additions & 0 deletions Visual Studio Projects/ZWaveJS.NET/ZWaveJS.NET/Structures.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
Expand Down Expand Up @@ -579,4 +580,45 @@ internal FirmwareUpdate() { }
public string filename { get; internal set; }
public int? firmwareTarget { get; internal set; }
}

/// <summary>
/// Thread-safe Dictionary
/// </summary>
/// <remarks>
/// ConcurrentDictionary wrapper class exposing a partial
/// "Dictionary-like" interface for use in this application.
/// </remarks>
public class TSafeDictionary<K, V> {

private ConcurrentDictionary<K, V> _dictionary
= new ConcurrentDictionary<K, V>();

public V this[K key] {
get {
if (!_dictionary.TryGetValue(key, out V value)) {
throw new KeyNotFoundException();
}
return value;
}
private set { }
}

public ICollection<K> Keys {
get { return (_dictionary.Keys); }
}

public void Add(K key, V callback) {
if (!_dictionary.TryAdd(key, callback)) {
throw new ArgumentException();
}
}

public Boolean Remove(K key) {
return _dictionary.TryRemove(key, out _);
}

public Boolean ContainsKey(K key) {
return _dictionary.ContainsKey(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public void Send(ArraySegment<byte> message)
_messagesBinaryToSendQueue.Writer.TryWrite(message);
}

/// <summary>
/// Synchronize the SendInstant calls to avoid
/// the documented issue with simultaneous usage.
/// </summary>
private Object _sendInstantLock = new Object();

/// <summary>
/// Send text message to the websocket channel.
/// It doesn't use a sending queue,
Expand All @@ -69,7 +75,9 @@ public Task SendInstant(string message)
{
Validations.Validations.ValidateInput(message, nameof(message));

return SendInternalSynchronized(message);
lock (_sendInstantLock) {
return SendInternalSynchronized(message);
}
}

/// <summary>
Expand All @@ -81,7 +89,9 @@ public Task SendInstant(string message)
/// <param name="message">Message to be sent</param>
public Task SendInstant(byte[] message)
{
return SendInternalSynchronized(new ArraySegment<byte>(message));
lock (_sendInstantLock) {
return SendInternalSynchronized(new ArraySegment<byte>(message));
}
}

/// <summary>
Expand Down