Skip to content

Commit

Permalink
Merge pull request #270 from dlamkins/bug/262-fix-hook-handler-concur…
Browse files Browse the repository at this point in the history
…rency

Added proper locks when enumerating this.Handlers.
  • Loading branch information
dlamkins authored Jun 30, 2020
2 parents 9de18dc + edce787 commit 1cbb26a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
5 changes: 1 addition & 4 deletions Blish HUD/GameServices/Input/DebugHelperInputHookManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ internal abstract class DebugHelperInputHookManager<THandlerDelegate, TEventMess

private bool isEnabled = false;


public DebugHelperInputHookManager(IMessageService debugHelperMessageService) {
this.DebugHelperMessageService = debugHelperMessageService;
debugHelperMessageService.Register<TEventMessage>(DummyHookCallback);
}


protected IList<THandlerDelegate> Handlers { get; } = new List<THandlerDelegate>();
protected IList<THandlerDelegate> Handlers { get; } = new SynchronizedCollection<THandlerDelegate>();

protected IMessageService DebugHelperMessageService { get; }


public virtual bool EnableHook() {
if (isEnabled) return false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Blish_HUD.DebugHelperLib.Models;
using System.Collections;
using Blish_HUD.DebugHelperLib.Models;
using Blish_HUD.DebugHelperLib.Services;
using Microsoft.Xna.Framework.Input;

Expand All @@ -14,9 +15,11 @@ protected override void HookCallback(KeyboardEventMessage message) {
KeyboardEventArgs keyboardEventArgs = new KeyboardEventArgs((KeyboardEventType)message.EventType, (Keys)message.Key);
bool isHandled = false;

foreach (HandleKeyboardInputDelegate handler in this.Handlers) {
isHandled = handler(keyboardEventArgs);
if (isHandled) break;
lock (((IList) this.Handlers).SyncRoot) {
foreach (HandleKeyboardInputDelegate handler in this.Handlers) {
isHandled = handler(keyboardEventArgs);
if (isHandled) break;
}
}

KeyboardResponseMessage response = new KeyboardResponseMessage {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Blish_HUD.Input.WinApi;
using Microsoft.Xna.Framework.Input;
Expand All @@ -18,9 +19,11 @@ protected override int HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
KeyboardEventArgs KeyboardEventArgs = new KeyboardEventArgs(eventType, key);
bool isHandled = false;

foreach (HandleKeyboardInputDelegate handler in this.Handlers) {
isHandled = handler(KeyboardEventArgs);
if (isHandled) break;
lock (((IList) this.Handlers).SyncRoot) {
foreach (HandleKeyboardInputDelegate handler in this.Handlers) {
isHandled = handler(KeyboardEventArgs);
if (isHandled) break;
}
}

if (isHandled)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Blish_HUD.DebugHelperLib.Models;
using System.Collections;
using Blish_HUD.DebugHelperLib.Models;
using Blish_HUD.DebugHelperLib.Services;

namespace Blish_HUD.Input {
Expand All @@ -15,9 +16,11 @@ protected override void HookCallback(MouseEventMessage message) {

bool isHandled = false;

foreach (HandleMouseInputDelegate handler in this.Handlers) {
isHandled = handler(mouseEventArgs);
if (isHandled) break;
lock (((IList) this.Handlers).SyncRoot) {
foreach (HandleMouseInputDelegate handler in this.Handlers) {
isHandled = handler(mouseEventArgs);
if (isHandled) break;
}
}

MouseResponseMessage response = new MouseResponseMessage {
Expand Down
9 changes: 6 additions & 3 deletions Blish HUD/GameServices/Input/Mouse/WinApiMouseHookManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Blish_HUD.Input.WinApi;

Expand All @@ -14,9 +15,11 @@ protected override int HookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
MouseEventArgs mouseEventArgs = new MouseEventArgs((MouseEventType)wParam, Marshal.PtrToStructure<MouseLLHookStruct>(lParam));
bool isHandled = false;

foreach (HandleMouseInputDelegate handler in this.Handlers) {
isHandled = handler(mouseEventArgs);
if (isHandled) break;
lock (((IList) this.Handlers).SyncRoot) {
foreach (HandleMouseInputDelegate handler in this.Handlers) {
isHandled = handler(mouseEventArgs);
if (isHandled) break;
}
}

if (isHandled)
Expand Down
2 changes: 1 addition & 1 deletion Blish HUD/GameServices/Input/WinApiInputHookManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal abstract class WinApiInputHookManager<THandlerDelegate> {

protected abstract HookType HookType { get; }

protected IList<THandlerDelegate> Handlers { get; } = new List<THandlerDelegate>();
protected IList<THandlerDelegate> Handlers { get; } = new SynchronizedCollection<THandlerDelegate>();


public virtual bool EnableHook() {
Expand Down

0 comments on commit 1cbb26a

Please sign in to comment.