From add38911ee54e689647f4750bffd233a60cbd24a Mon Sep 17 00:00:00 2001 From: Marcel Mueller Date: Tue, 2 Oct 2018 16:59:28 +0200 Subject: [PATCH 1/2] Check if connection is established before performing a locked operation. Without this check a call to PerformLockedOperation waits indefinitely to aquire a lock if the connection isn't available. --- src/KNXLib/KnxLockManager.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/KNXLib/KnxLockManager.cs b/src/KNXLib/KnxLockManager.cs index dfc9257..dd07981 100644 --- a/src/KNXLib/KnxLockManager.cs +++ b/src/KNXLib/KnxLockManager.cs @@ -42,7 +42,8 @@ public void UnlockConnection() public void PerformLockedOperation(Action action) { - // TODO: Shouldn't this check if we are connected? + if (!_isConnected) + throw new InvalidOperationException("Unable to perform action: KNX is not connected."); try { From c2249f81558c35616b63a9c233d1589405835128 Mon Sep 17 00:00:00 2001 From: Marcel Mueller Date: Thu, 4 Oct 2018 10:17:12 +0200 Subject: [PATCH 2/2] Fix line-endings --- src/KNXLib/KnxLockManager.cs | 166 +++++++++++++++++------------------ 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/src/KNXLib/KnxLockManager.cs b/src/KNXLib/KnxLockManager.cs index dd07981..ce5ed05 100644 --- a/src/KNXLib/KnxLockManager.cs +++ b/src/KNXLib/KnxLockManager.cs @@ -1,87 +1,87 @@ -using System; -using System.Threading; - -namespace KNXLib -{ - internal class KnxLockManager - { - private readonly SemaphoreSlim _sendLock = new SemaphoreSlim(0); - private readonly object _connectedLock = new object(); - private bool _isConnected; - - internal int IntervalMs { get; set; } = 200; - - public int LockCount - { - get { return _sendLock.CurrentCount; } - } - - public void LockConnection() - { - lock (_connectedLock) - { - if (!_isConnected) - return; - - SendLock(); - _isConnected = false; - } - } - - public void UnlockConnection() - { - lock (_connectedLock) - { - if (_isConnected) - return; - - _isConnected = true; - SendUnlock(); - } - } - - public void PerformLockedOperation(Action action) - { - if (!_isConnected) - throw new InvalidOperationException("Unable to perform action: KNX is not connected."); - - try - { - SendLock(); - action(); - } - finally - { - SendUnlockPause(); - } - } - - private void SendLock() - { - _sendLock.Wait(); - } - - private void SendUnlock() - { - _sendLock.Release(); - } - - private void SendUnlockPause() - { +using System; +using System.Threading; + +namespace KNXLib +{ + internal class KnxLockManager + { + private readonly SemaphoreSlim _sendLock = new SemaphoreSlim(0); + private readonly object _connectedLock = new object(); + private bool _isConnected; + + internal int IntervalMs { get; set; } = 200; + + public int LockCount + { + get { return _sendLock.CurrentCount; } + } + + public void LockConnection() + { + lock (_connectedLock) + { + if (!_isConnected) + return; + + SendLock(); + _isConnected = false; + } + } + + public void UnlockConnection() + { + lock (_connectedLock) + { + if (_isConnected) + return; + + _isConnected = true; + SendUnlock(); + } + } + + public void PerformLockedOperation(Action action) + { + if (!_isConnected) + throw new InvalidOperationException("Unable to perform action: KNX is not connected."); + + try + { + SendLock(); + action(); + } + finally + { + SendUnlockPause(); + } + } + + private void SendLock() + { + _sendLock.Wait(); + } + + private void SendUnlock() + { + _sendLock.Release(); + } + + private void SendUnlockPause() + { if (IntervalMs == 0) { - _sendLock.Release(); + _sendLock.Release(); return; - } - - var t = new Thread(SendUnlockPauseThread) { IsBackground = true }; - t.Start(); - } - - private void SendUnlockPauseThread() - { - Thread.Sleep(IntervalMs); - _sendLock.Release(); - } - } + } + + var t = new Thread(SendUnlockPauseThread) { IsBackground = true }; + t.Start(); + } + + private void SendUnlockPauseThread() + { + Thread.Sleep(IntervalMs); + _sendLock.Release(); + } + } } \ No newline at end of file