From 3217d66a66be7cc2f542a38a80730dd38685a319 Mon Sep 17 00:00:00 2001 From: HearthstoneBot Date: Tue, 18 Mar 2014 13:07:44 +0100 Subject: [PATCH] Fixed a concurrency error in ServerSocket.cs An issue with concurrent handling of events, and adding to the events list was fixed. --- projects/Bot/src/ServerSocket.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/projects/Bot/src/ServerSocket.cs b/projects/Bot/src/ServerSocket.cs index 7501235..d0487bc 100644 --- a/projects/Bot/src/ServerSocket.cs +++ b/projects/Bot/src/ServerSocket.cs @@ -20,6 +20,7 @@ public class ServerSocket private const Int32 SERVER_PORT = 8111; // Work variables + private Mutex mutex; private Thread thread; private List events; @@ -29,6 +30,8 @@ public ServerSocket() events = new List(); thread = new Thread(new ThreadStart(run)); + + mutex = new Mutex(); } public void stop() @@ -86,8 +89,12 @@ private void run() string command = sr.ReadToEnd(); // Report that we've got a command Log.log("Got network command"); + // Wait until it is safe to enter. + mutex.WaitOne(); // Append command to lazy list events.Add(command); + // Release the Mutex. + mutex.ReleaseMutex(); // At this point, we're done; // Close the network stream @@ -108,6 +115,8 @@ private void run() public void handle_events() { + // Wait until it is safe to enter. + mutex.WaitOne(); // Handle all events passed, since last visit foreach(string evnt in events) { @@ -115,6 +124,8 @@ public void handle_events() } // Remove all events for list events.Clear(); + // Release the Mutex. + mutex.ReleaseMutex(); } private void handle_event(string data)