Fixed Issue 368. Fixed Issue 369. Fixed Issue 370.
1.1 --- a/GUI/MainForm.Designer.cs Sun Oct 28 14:06:50 2012 +0000
1.2 +++ b/GUI/MainForm.Designer.cs Sun Oct 28 15:19:45 2012 +0000
1.3 @@ -84,7 +84,7 @@
1.4 this.plotWindowMenuItem = new System.Windows.Forms.MenuItem();
1.5 this.plotBottomMenuItem = new System.Windows.Forms.MenuItem();
1.6 this.plotRightMenuItem = new System.Windows.Forms.MenuItem();
1.7 - this.MenuItem4 = new System.Windows.Forms.MenuItem();
1.8 + this.webMenuItemSeparator = new System.Windows.Forms.MenuItem();
1.9 this.webMenuItem = new System.Windows.Forms.MenuItem();
1.10 this.runWebServerMenuItem = new System.Windows.Forms.MenuItem();
1.11 this.serverPortMenuItem = new System.Windows.Forms.MenuItem();
1.12 @@ -354,7 +354,7 @@
1.13 this.separatorMenuItem,
1.14 this.temperatureUnitsMenuItem,
1.15 this.plotLocationMenuItem,
1.16 - this.MenuItem4,
1.17 + this.webMenuItemSeparator,
1.18 this.webMenuItem});
1.19 this.optionsMenuItem.Text = "Options";
1.20 //
1.21 @@ -434,8 +434,8 @@
1.22 //
1.23 // MenuItem4
1.24 //
1.25 - this.MenuItem4.Index = 7;
1.26 - this.MenuItem4.Text = "-";
1.27 + this.webMenuItemSeparator.Index = 7;
1.28 + this.webMenuItemSeparator.Text = "-";
1.29 //
1.30 // webMenuItem
1.31 //
1.32 @@ -596,7 +596,7 @@
1.33 private System.Windows.Forms.MenuItem minMenuItem;
1.34 private System.Windows.Forms.MenuItem maxMenuItem;
1.35 private System.Windows.Forms.MenuItem temperatureUnitsMenuItem;
1.36 - private System.Windows.Forms.MenuItem MenuItem4;
1.37 + private System.Windows.Forms.MenuItem webMenuItemSeparator;
1.38 private System.Windows.Forms.MenuItem celsiusMenuItem;
1.39 private System.Windows.Forms.MenuItem fahrenheitMenuItem;
1.40 private System.Windows.Forms.MenuItem sumbitReportMenuItem;
2.1 --- a/GUI/MainForm.cs Sun Oct 28 14:06:50 2012 +0000
2.2 +++ b/GUI/MainForm.cs Sun Oct 28 15:19:45 2012 +0000
2.3 @@ -263,11 +263,16 @@
2.4 fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked;
2.5
2.6 server = new HttpServer(root, this.settings.GetValue("listenerPort", 8085));
2.7 + if (server.PlatformNotSupported) {
2.8 + webMenuItemSeparator.Visible = false;
2.9 + webMenuItem.Visible = false;
2.10 + }
2.11 +
2.12 runWebServer = new UserOption("runWebServerMenuItem", false,
2.13 runWebServerMenuItem, settings);
2.14 runWebServer.Changed += delegate(object sender, EventArgs e) {
2.15 if (runWebServer.Value)
2.16 - runWebServer.Value = server.StartHTTPListener();
2.17 + server.StartHTTPListener();
2.18 else
2.19 server.StopHTTPListener();
2.20 };
2.21 @@ -293,8 +298,7 @@
2.22 computer.Close();
2.23 SaveConfiguration();
2.24 if (runWebServer.Value)
2.25 - server.Quit();
2.26 -
2.27 + server.Quit();
2.28 };
2.29 }
2.30
3.1 --- a/Utilities/HttpServer.cs Sun Oct 28 14:06:50 2012 +0000
3.2 +++ b/Utilities/HttpServer.cs Sun Oct 28 15:19:45 2012 +0000
3.3 @@ -28,16 +28,30 @@
3.4 private Thread listenerThread;
3.5 private Node root;
3.6
3.7 - public HttpServer(Node r, int p) {
3.8 - root = r;
3.9 - listenerPort = p;
3.10 + public HttpServer(Node node, int port) {
3.11 + root = node;
3.12 + listenerPort = port;
3.13
3.14 //JSON node count.
3.15 nodeCount = 0;
3.16 - listener = new HttpListener();
3.17 +
3.18 + try {
3.19 + listener = new HttpListener();
3.20 + } catch (PlatformNotSupportedException) {
3.21 + listener = null;
3.22 + }
3.23 + }
3.24 +
3.25 + public bool PlatformNotSupported {
3.26 + get {
3.27 + return listener == null;
3.28 + }
3.29 }
3.30
3.31 public Boolean StartHTTPListener() {
3.32 + if (PlatformNotSupported)
3.33 + return false;
3.34 +
3.35 try {
3.36 if (listener.IsListening)
3.37 return true;
3.38 @@ -59,6 +73,9 @@
3.39 }
3.40
3.41 public Boolean StopHTTPListener() {
3.42 + if (PlatformNotSupported)
3.43 + return false;
3.44 +
3.45 try {
3.46 listenerThread.Abort();
3.47 listener.Stop();
3.48 @@ -71,7 +88,7 @@
3.49 return true;
3.50 }
3.51
3.52 - public void HandleRequests() {
3.53 + private void HandleRequests() {
3.54
3.55 while (listener.IsListening) {
3.56 var context = listener.BeginGetContext(
3.57 @@ -80,22 +97,30 @@
3.58 }
3.59 }
3.60
3.61 - public void ListenerCallback(IAsyncResult result) {
3.62 + private void ListenerCallback(IAsyncResult result) {
3.63 HttpListener listener = (HttpListener)result.AsyncState;
3.64 if (listener == null || !listener.IsListening)
3.65 return;
3.66 +
3.67 // Call EndGetContext to complete the asynchronous operation.
3.68 - HttpListenerContext context = listener.EndGetContext(result);
3.69 + HttpListenerContext context;
3.70 + try {
3.71 + context = listener.EndGetContext(result);
3.72 + } catch (Exception) {
3.73 + return;
3.74 + }
3.75 +
3.76 HttpListenerRequest request = context.Request;
3.77
3.78 var requestedFile = request.RawUrl.Substring(1);
3.79 if (requestedFile == "data.json") {
3.80 - SendJSON(context);
3.81 + SendJSON(context.Response);
3.82 return;
3.83 }
3.84
3.85 if (requestedFile.Contains("images_icon")) {
3.86 - ServeResourceImage(context, requestedFile.Replace("images_icon/", ""));
3.87 + ServeResourceImage(context.Response,
3.88 + requestedFile.Replace("images_icon/", ""));
3.89 return;
3.90 }
3.91
3.92 @@ -105,13 +130,13 @@
3.93
3.94 string[] splits = requestedFile.Split('.');
3.95 string ext = splits[splits.Length - 1];
3.96 - ServeResourceFile(context, "Web." + requestedFile.Replace('/', '.'), ext);
3.97 + ServeResourceFile(context.Response,
3.98 + "Web." + requestedFile.Replace('/', '.'), ext);
3.99 }
3.100
3.101 - private void ServeResourceFile(HttpListenerContext context, string name,
3.102 + private void ServeResourceFile(HttpListenerResponse response, string name,
3.103 string ext)
3.104 {
3.105 -
3.106 // resource names do not support the hyphen
3.107 name = "OpenHardwareMonitor.Resources." +
3.108 name.Replace("custom-theme", "custom_theme");
3.109 @@ -122,24 +147,29 @@
3.110 if (names[i].Replace('\\', '.') == name) {
3.111 using (Stream stream = Assembly.GetExecutingAssembly().
3.112 GetManifestResourceStream(names[i])) {
3.113 - context.Response.ContentType = GetcontentType("." + ext);
3.114 - context.Response.ContentLength64 = stream.Length;
3.115 + response.ContentType = GetcontentType("." + ext);
3.116 + response.ContentLength64 = stream.Length;
3.117 byte[] buffer = new byte[512 * 1024];
3.118 int len;
3.119 - while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) {
3.120 - context.Response.OutputStream.Write(buffer, 0, len);
3.121 + try {
3.122 + Stream output = response.OutputStream;
3.123 + while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) {
3.124 + output.Write(buffer, 0, len);
3.125 + }
3.126 + output.Close();
3.127 + } catch (HttpListenerException) {
3.128 }
3.129 - context.Response.OutputStream.Close();
3.130 - }
3.131 - return;
3.132 + response.Close();
3.133 + return;
3.134 + }
3.135 }
3.136 }
3.137 - context.Response.OutputStream.Close();
3.138 - context.Response.StatusCode = 404;
3.139 - context.Response.Close();
3.140 +
3.141 + response.StatusCode = 404;
3.142 + response.Close();
3.143 }
3.144
3.145 - private void ServeResourceImage(HttpListenerContext context, string name) {
3.146 + private void ServeResourceImage(HttpListenerResponse response, string name) {
3.147 name = "OpenHardwareMonitor.Resources." + name;
3.148
3.149 string[] names =
3.150 @@ -150,23 +180,28 @@
3.151 GetManifestResourceStream(names[i])) {
3.152
3.153 Image image = Image.FromStream(stream);
3.154 - context.Response.ContentType = "image/png";
3.155 - using (MemoryStream ms = new MemoryStream()) {
3.156 - image.Save(ms, ImageFormat.Png);
3.157 - ms.WriteTo(context.Response.OutputStream);
3.158 + response.ContentType = "image/png";
3.159 + try {
3.160 + Stream output = response.OutputStream;
3.161 + using (MemoryStream ms = new MemoryStream()) {
3.162 + image.Save(ms, ImageFormat.Png);
3.163 + ms.WriteTo(output);
3.164 + }
3.165 + output.Close();
3.166 + } catch (HttpListenerException) {
3.167 }
3.168 - context.Response.OutputStream.Close();
3.169 image.Dispose();
3.170 + response.Close();
3.171 return;
3.172 }
3.173 }
3.174 }
3.175 - context.Response.OutputStream.Close();
3.176 - context.Response.StatusCode = 404;
3.177 - context.Response.Close();
3.178 +
3.179 + response.StatusCode = 404;
3.180 + response.Close();
3.181 }
3.182
3.183 - private void SendJSON(HttpListenerContext context) {
3.184 + private void SendJSON(HttpListenerResponse response) {
3.185
3.186 string JSON = "{\"id\": 0, \"Text\": \"Sensor\", \"Children\": [";
3.187 nodeCount = 1;
3.188 @@ -181,13 +216,19 @@
3.189 var responseContent = JSON;
3.190 byte[] buffer = Encoding.UTF8.GetBytes(responseContent);
3.191
3.192 - context.Response.ContentLength64 = buffer.Length;
3.193 - context.Response.ContentType = "application/json";
3.194 + response.AddHeader("Cache-Control", "no-cache");
3.195
3.196 - Stream outputStream = context.Response.OutputStream;
3.197 - outputStream.Write(buffer, 0, buffer.Length);
3.198 - outputStream.Close();
3.199 + response.ContentLength64 = buffer.Length;
3.200 + response.ContentType = "application/json";
3.201
3.202 + try {
3.203 + Stream output = response.OutputStream;
3.204 + output.Write(buffer, 0, buffer.Length);
3.205 + output.Close();
3.206 + } catch (HttpListenerException) {
3.207 + }
3.208 +
3.209 + response.Close();
3.210 }
3.211
3.212 private string GenerateJSON(Node n) {
3.213 @@ -327,11 +368,17 @@
3.214 }
3.215
3.216 ~HttpServer() {
3.217 + if (PlatformNotSupported)
3.218 + return;
3.219 +
3.220 StopHTTPListener();
3.221 listener.Abort();
3.222 }
3.223
3.224 public void Quit() {
3.225 + if (PlatformNotSupported)
3.226 + return;
3.227 +
3.228 StopHTTPListener();
3.229 listener.Abort();
3.230 }