Skip to content
This repository has been archived by the owner on Jan 11, 2018. It is now read-only.

Commit

Permalink
Fixed a concurrency error in ServerSocket.cs
Browse files Browse the repository at this point in the history
An issue with concurrent handling of events, and adding to the events
list was fixed.
  • Loading branch information
Skeen committed Mar 18, 2014
1 parent 1f2d7ab commit 3217d66
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions projects/Bot/src/ServerSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ServerSocket
private const Int32 SERVER_PORT = 8111;

// Work variables
private Mutex mutex;
private Thread thread;
private List<string> events;

Expand All @@ -29,6 +30,8 @@ public ServerSocket()

events = new List<string>();
thread = new Thread(new ThreadStart(run));

mutex = new Mutex();
}

public void stop()
Expand Down Expand Up @@ -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
Expand All @@ -108,13 +115,17 @@ 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)
{
handle_event(evnt);
}
// Remove all events for list
events.Clear();
// Release the Mutex.
mutex.ReleaseMutex();
}

private void handle_event(string data)
Expand Down

0 comments on commit 3217d66

Please sign in to comment.