Skip to content

Commit

Permalink
use a named mutex to guard against starting a new server
Browse files Browse the repository at this point in the history
  • Loading branch information
inorton committed Mar 27, 2014
1 parent 7ee6a23 commit 7b38482
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
32 changes: 20 additions & 12 deletions CClash/CClashServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.IO.Pipes;
using System.Web.Script.Serialization;
using System.Threading;

namespace CClash
{
Expand Down Expand Up @@ -35,19 +36,26 @@ void YieldLocks()
public void Listen(string cachedir)
{

try
{
var mtx = new Mutex(false, "cclash_serv_" + cachedir.ToLower().GetHashCode());

try {
if (!mtx.WaitOne(500)) {
return; // some other process is holding it
}
} catch (AbandonedMutexException) {
// past server must have died!
}

try {
Logging.Emit("server listening..");

using (var nss = new NamedPipeServerStream(MakePipeName(cachedir), PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.WriteThrough | PipeOptions.Asynchronous))
{
using (var nss = new NamedPipeServerStream(MakePipeName(cachedir), PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.WriteThrough | PipeOptions.Asynchronous)) {
cache = new DirectCompilerCacheServer(cachedir);
var msgbuf = new List<byte>();
var rxbuf = new byte[256*1024];
var rxbuf = new byte[256 * 1024];
DateTime lastConnection = DateTime.Now;

do
{

do {
// don't hog folders
System.IO.Directory.SetCurrentDirectory(mydocs);
Logging.Emit("server waiting..");
Expand Down Expand Up @@ -103,19 +111,19 @@ public void Listen(string cachedir)
} catch (IOException) {
Logging.Warning("error on client pipe");
nss.Disconnect();

} catch (Exception e) {
Logging.Error("server exception {0}", e);
Stop();
}
} while (!quitnow);
Logging.Emit("server quitting");
}
}
catch (IOException ex)
{
} catch (IOException ex) {
Logging.Emit("{0}", ex);
return;
} finally {
mtx.ReleaseMutex();
}
}

Expand Down
30 changes: 11 additions & 19 deletions CClash/CClashServerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,17 @@ void Connect()

// start the server, but lets not try to use it here, the next instance can
try {
var pl = System.Diagnostics.Process.GetProcesses();
var serverrunning = false;
foreach (Process proc in pl) {
if (proc.StartInfo.Arguments.Contains("--cclash-server")) {
serverrunning = true;
break;
}
}
if (!serverrunning) {
var p = new Process();
p.StartInfo = new ProcessStartInfo(GetType().Assembly.Location, "--cclash-server");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = "--cclash-server";
p.StartInfo.ErrorDialog = false;
p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
}

var p = new Process();
p.StartInfo = new ProcessStartInfo(GetType().Assembly.Location, "--cclash-server");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = "--cclash-server";
p.StartInfo.ErrorDialog = false;
p.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();

} catch (Exception e) {
Logging.Emit("error starting cclash server process", e.Message);
}
Expand Down
Binary file modified cclash.v11.suo
Binary file not shown.

0 comments on commit 7b38482

Please sign in to comment.