/// <summary> /// Gets called when websocket connection throws an error. /// </summary> /// <param name="e">Error event data.</param> protected override void OnError(ErrorEventArgs e) { base.OnError(e); if (Config.DebugLog != (int)DebugState.None) { VDebug.LogErrorFormat("[Volplane (Websocket Service)] {0:G}", e.Message); VDebug.LogException(e.Exception); } }
/// <summary> /// Gets called when a new HTTP context is received. /// </summary> /// <param name="result">Context result.</param> private void ContextReady(IAsyncResult result) { try { // Enqueue context and flag as ready lock (contextQueue) { contextQueue.Enqueue(listener.EndGetContext(result)); ready.Set(); } } catch (Exception e) { if ((Config.DebugLog != (int)DebugState.None) && listener.IsListening) { VDebug.LogError("[Volplane (Web Listener)] WebListener failed resolving context."); VDebug.LogException(e); } } }
/// <summary> /// Volplane server procedure for processing the incoming request. /// Request for files: 'screen.html', and 'controller.html' are sent from the AirConsole simulator. /// </summary> /// <param name="context">HttpListener context.</param> protected override void ProcessRequest(HttpListenerContext context) { HttpListenerRequest request = context.Request; byte[] buffer; // If POST data is sent -> request coming from controller editor. if (!request.HasEntityBody) { string filePath; string serverPath = request.Url.LocalPath; if (serverPath.StartsWith("/build/")) { // Local build path serverPath = serverPath.Replace("/build/", "/"); filePath = String.Format( "{0:G}{1:G}", Config.BuildPath, serverPath ); } else if (serverPath.StartsWith("/volplane/")) { // Local web server path serverPath = serverPath.Replace("/volplane/", "/"); filePath = localPath + Config.WebServerPath + serverPath; } else { // WebGL template path filePath = localPath + Config.WebTemplatePath + serverPath; if (String.Equals(Path.GetFileName(filePath), "screen.html")) { filePath = filePath.Replace("screen.html", "index.html"); } } filePath = pathSeparatorReg.Replace(filePath, Path.DirectorySeparatorChar.ToString()); filePath = Uri.UnescapeDataString(filePath); if (File.Exists(filePath)) { buffer = File.ReadAllBytes(filePath); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = ReturnMIMEType(Path.GetExtension(filePath)); context.Response.ContentLength64 = buffer.Length; } else { // File not found buffer = System.Text.Encoding.UTF8.GetBytes("<html><head><title>404 Not Found.</title></head><body><h1>Error 404 - Not Found.</h1></body></html>"); context.Response.StatusCode = (int)HttpStatusCode.NotFound; context.Response.ContentType = ReturnMIMEType("text/html"); context.Response.ContentLength64 = buffer.Length; } using (Stream s = context.Response.OutputStream) { s.Write(buffer, 0, buffer.Length); } } else { // Saving controller data buffer = System.Text.Encoding.UTF8.GetBytes("saved"); string controllerName = null; try { controllerName = FileManager.WriteJSON(request.InputStream, String.Format("{0:G}{1:G}/data/controller", localPath, Config.WebServerPath)); // If the controller is currently used if (Config.SelectedController == controllerName) { // Copy selected controller data into WebGL template File.Copy(String.Format("{0:G}{1:G}/data/controller/{2:G}.json", localPath, Config.WebServerPath, controllerName), String.Format("{0:G}{1:G}/controller.json", localPath, Config.WebTemplatePath), true); } if (Config.DebugLog == (int)DebugState.All) { VDebug.Log("[Volplane (File Manager)] Controller saved!"); } } catch (Exception e) { buffer = System.Text.Encoding.UTF8.GetBytes( String.Format("Could not write data to path: {0:G}{1:G}/data/controller", localPath, Config.WebServerPath) ); if (Config.DebugLog == (int)DebugState.All) { VDebug.LogErrorFormat("[Volplane (File Manager)] Unable to write file to: '{0:G}{1:G}/data/controller'.", localPath, Config.WebServerPath); VDebug.LogException(e); } } finally { // Response context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = "text/plain"; context.Response.ContentLength64 = buffer.Length; using (Stream s = context.Response.OutputStream) { s.Write(buffer, 0, buffer.Length); } } } }