# HG changeset patch # User moel.mich # Date 1351437585 0 # Node ID 87093432c8439107a31048c015beb7d97ab4164e # Parent 7094d5dd924bc243c31f3fe9f25953bd8a94f842 Fixed Issue 368. Fixed Issue 369. Fixed Issue 370. diff -r 7094d5dd924b -r 87093432c843 GUI/MainForm.Designer.cs --- a/GUI/MainForm.Designer.cs Sun Oct 28 14:06:50 2012 +0000 +++ b/GUI/MainForm.Designer.cs Sun Oct 28 15:19:45 2012 +0000 @@ -84,7 +84,7 @@ this.plotWindowMenuItem = new System.Windows.Forms.MenuItem(); this.plotBottomMenuItem = new System.Windows.Forms.MenuItem(); this.plotRightMenuItem = new System.Windows.Forms.MenuItem(); - this.MenuItem4 = new System.Windows.Forms.MenuItem(); + this.webMenuItemSeparator = new System.Windows.Forms.MenuItem(); this.webMenuItem = new System.Windows.Forms.MenuItem(); this.runWebServerMenuItem = new System.Windows.Forms.MenuItem(); this.serverPortMenuItem = new System.Windows.Forms.MenuItem(); @@ -354,7 +354,7 @@ this.separatorMenuItem, this.temperatureUnitsMenuItem, this.plotLocationMenuItem, - this.MenuItem4, + this.webMenuItemSeparator, this.webMenuItem}); this.optionsMenuItem.Text = "Options"; // @@ -434,8 +434,8 @@ // // MenuItem4 // - this.MenuItem4.Index = 7; - this.MenuItem4.Text = "-"; + this.webMenuItemSeparator.Index = 7; + this.webMenuItemSeparator.Text = "-"; // // webMenuItem // @@ -596,7 +596,7 @@ private System.Windows.Forms.MenuItem minMenuItem; private System.Windows.Forms.MenuItem maxMenuItem; private System.Windows.Forms.MenuItem temperatureUnitsMenuItem; - private System.Windows.Forms.MenuItem MenuItem4; + private System.Windows.Forms.MenuItem webMenuItemSeparator; private System.Windows.Forms.MenuItem celsiusMenuItem; private System.Windows.Forms.MenuItem fahrenheitMenuItem; private System.Windows.Forms.MenuItem sumbitReportMenuItem; diff -r 7094d5dd924b -r 87093432c843 GUI/MainForm.cs --- a/GUI/MainForm.cs Sun Oct 28 14:06:50 2012 +0000 +++ b/GUI/MainForm.cs Sun Oct 28 15:19:45 2012 +0000 @@ -263,11 +263,16 @@ fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked; server = new HttpServer(root, this.settings.GetValue("listenerPort", 8085)); + if (server.PlatformNotSupported) { + webMenuItemSeparator.Visible = false; + webMenuItem.Visible = false; + } + runWebServer = new UserOption("runWebServerMenuItem", false, runWebServerMenuItem, settings); runWebServer.Changed += delegate(object sender, EventArgs e) { if (runWebServer.Value) - runWebServer.Value = server.StartHTTPListener(); + server.StartHTTPListener(); else server.StopHTTPListener(); }; @@ -293,8 +298,7 @@ computer.Close(); SaveConfiguration(); if (runWebServer.Value) - server.Quit(); - + server.Quit(); }; } diff -r 7094d5dd924b -r 87093432c843 Utilities/HttpServer.cs --- a/Utilities/HttpServer.cs Sun Oct 28 14:06:50 2012 +0000 +++ b/Utilities/HttpServer.cs Sun Oct 28 15:19:45 2012 +0000 @@ -28,16 +28,30 @@ private Thread listenerThread; private Node root; - public HttpServer(Node r, int p) { - root = r; - listenerPort = p; + public HttpServer(Node node, int port) { + root = node; + listenerPort = port; //JSON node count. nodeCount = 0; - listener = new HttpListener(); + + try { + listener = new HttpListener(); + } catch (PlatformNotSupportedException) { + listener = null; + } + } + + public bool PlatformNotSupported { + get { + return listener == null; + } } public Boolean StartHTTPListener() { + if (PlatformNotSupported) + return false; + try { if (listener.IsListening) return true; @@ -59,6 +73,9 @@ } public Boolean StopHTTPListener() { + if (PlatformNotSupported) + return false; + try { listenerThread.Abort(); listener.Stop(); @@ -71,7 +88,7 @@ return true; } - public void HandleRequests() { + private void HandleRequests() { while (listener.IsListening) { var context = listener.BeginGetContext( @@ -80,22 +97,30 @@ } } - public void ListenerCallback(IAsyncResult result) { + private void ListenerCallback(IAsyncResult result) { HttpListener listener = (HttpListener)result.AsyncState; if (listener == null || !listener.IsListening) return; + // Call EndGetContext to complete the asynchronous operation. - HttpListenerContext context = listener.EndGetContext(result); + HttpListenerContext context; + try { + context = listener.EndGetContext(result); + } catch (Exception) { + return; + } + HttpListenerRequest request = context.Request; var requestedFile = request.RawUrl.Substring(1); if (requestedFile == "data.json") { - SendJSON(context); + SendJSON(context.Response); return; } if (requestedFile.Contains("images_icon")) { - ServeResourceImage(context, requestedFile.Replace("images_icon/", "")); + ServeResourceImage(context.Response, + requestedFile.Replace("images_icon/", "")); return; } @@ -105,13 +130,13 @@ string[] splits = requestedFile.Split('.'); string ext = splits[splits.Length - 1]; - ServeResourceFile(context, "Web." + requestedFile.Replace('/', '.'), ext); + ServeResourceFile(context.Response, + "Web." + requestedFile.Replace('/', '.'), ext); } - private void ServeResourceFile(HttpListenerContext context, string name, + private void ServeResourceFile(HttpListenerResponse response, string name, string ext) { - // resource names do not support the hyphen name = "OpenHardwareMonitor.Resources." + name.Replace("custom-theme", "custom_theme"); @@ -122,24 +147,29 @@ if (names[i].Replace('\\', '.') == name) { using (Stream stream = Assembly.GetExecutingAssembly(). GetManifestResourceStream(names[i])) { - context.Response.ContentType = GetcontentType("." + ext); - context.Response.ContentLength64 = stream.Length; + response.ContentType = GetcontentType("." + ext); + response.ContentLength64 = stream.Length; byte[] buffer = new byte[512 * 1024]; int len; - while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) { - context.Response.OutputStream.Write(buffer, 0, len); + try { + Stream output = response.OutputStream; + while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) { + output.Write(buffer, 0, len); + } + output.Close(); + } catch (HttpListenerException) { } - context.Response.OutputStream.Close(); - } - return; + response.Close(); + return; + } } } - context.Response.OutputStream.Close(); - context.Response.StatusCode = 404; - context.Response.Close(); + + response.StatusCode = 404; + response.Close(); } - private void ServeResourceImage(HttpListenerContext context, string name) { + private void ServeResourceImage(HttpListenerResponse response, string name) { name = "OpenHardwareMonitor.Resources." + name; string[] names = @@ -150,23 +180,28 @@ GetManifestResourceStream(names[i])) { Image image = Image.FromStream(stream); - context.Response.ContentType = "image/png"; - using (MemoryStream ms = new MemoryStream()) { - image.Save(ms, ImageFormat.Png); - ms.WriteTo(context.Response.OutputStream); + response.ContentType = "image/png"; + try { + Stream output = response.OutputStream; + using (MemoryStream ms = new MemoryStream()) { + image.Save(ms, ImageFormat.Png); + ms.WriteTo(output); + } + output.Close(); + } catch (HttpListenerException) { } - context.Response.OutputStream.Close(); image.Dispose(); + response.Close(); return; } } } - context.Response.OutputStream.Close(); - context.Response.StatusCode = 404; - context.Response.Close(); + + response.StatusCode = 404; + response.Close(); } - private void SendJSON(HttpListenerContext context) { + private void SendJSON(HttpListenerResponse response) { string JSON = "{\"id\": 0, \"Text\": \"Sensor\", \"Children\": ["; nodeCount = 1; @@ -181,13 +216,19 @@ var responseContent = JSON; byte[] buffer = Encoding.UTF8.GetBytes(responseContent); - context.Response.ContentLength64 = buffer.Length; - context.Response.ContentType = "application/json"; + response.AddHeader("Cache-Control", "no-cache"); - Stream outputStream = context.Response.OutputStream; - outputStream.Write(buffer, 0, buffer.Length); - outputStream.Close(); + response.ContentLength64 = buffer.Length; + response.ContentType = "application/json"; + try { + Stream output = response.OutputStream; + output.Write(buffer, 0, buffer.Length); + output.Close(); + } catch (HttpListenerException) { + } + + response.Close(); } private string GenerateJSON(Node n) { @@ -327,11 +368,17 @@ } ~HttpServer() { + if (PlatformNotSupported) + return; + StopHTTPListener(); listener.Abort(); } public void Quit() { + if (PlatformNotSupported) + return; + StopHTTPListener(); listener.Abort(); }