public bool process(HttpListenerRequest request, HttpListenerResponse response) { if (!request.RawUrl.StartsWith(PAGE_PREFIX)) { return(false); } // Work out how big this request was long byteCount = request.RawUrl.Length + request.ContentLength64; // Don't count headers + request.Headers.AllKeys.Sum(x => x.Length + request.Headers[x].Length + 1); dataRates.RecieveDataFromClient(Convert.ToInt32(byteCount)); IDictionary <string, object> apiRequests; if (request.HttpMethod.ToUpper() == "POST" && request.HasEntityBody) { System.IO.StreamReader streamReader = new System.IO.StreamReader(request.InputStream); apiRequests = parseJSONBody(streamReader.ReadToEnd()); } else { apiRequests = splitArguments(request.Url.Query); } var results = new Dictionary <string, object>(); var unknowns = new List <string>(); var errors = new Dictionary <string, string>(); foreach (var name in apiRequests.Keys) { try { results[name] = kspAPI.ProcessAPIString(apiRequests[name].ToString()); } catch (IKSPAPI.UnknownAPIException) { unknowns.Add(apiRequests[name].ToString()); } catch (Exception ex) { errors[apiRequests[name].ToString()] = ex.ToString(); } } // If we had any unrecognised API keys, let the user know if (unknowns.Count > 0) { results["unknown"] = unknowns; } if (errors.Count > 0) { results["errors"] = errors; } // Now, serialize the dictionary and write to the response var returnData = Encoding.UTF8.GetBytes(SimpleJson.SimpleJson.SerializeObject(results)); response.ContentEncoding = Encoding.UTF8; response.ContentType = "application/json"; response.WriteContent(returnData); dataRates.SendDataToClient(returnData.Length); return(true); }
public bool process(HttpListenerRequest request, HttpListenerResponse response) { //PluginLogger.debug(request.Url.AbsolutePath.TrimEnd('/')); //PluginLogger.debug(String.Join(",", CameraCaptureManager.classedInstance.cameras.Keys.ToArray())); //PluginLogger.debug("FLIGHT CAMERA: " + this.cameraCaptureTest); if (request.Url.AbsolutePath.TrimEnd('/').ToLower() == CAMERA_LIST_ENDPOINT) { // Work out how big this request was long byteCount = request.RawUrl.Length + request.ContentLength64; // Don't count headers + request.Headers.AllKeys.Sum(x => x.Length + request.Headers[x].Length + 1); dataRates.RecieveDataFromClient(Convert.ToInt32(byteCount)); return(processCameraManagerIndex(request, response)); } else if (cameraNameEndpointRegex.IsMatch(request.Url.AbsolutePath)) { Match match = cameraNameEndpointRegex.Match(request.Url.AbsolutePath); string cameraName = UnityEngine.WWW.UnEscapeURL(match.Groups[1].Value); //PluginLogger.debug("GET CAMERA: " + cameraName); return(processCameraImageRequest(cameraName, request, response)); } return(false); }
/// Process a message recieved from a client protected override void OnMessage(MessageEventArgs e) { // We only care about text messages, for now. if (e.Type != Opcode.Text) { return; } dataRates.RecieveDataFromClient(e.RawData.Length); // deserialize the message as JSON var json = SimpleJson.SimpleJson.DeserializeObject(e.Data) as SimpleJson.JsonObject; lock (dataLock) { // Do any tasks requested here foreach (var entry in json) { // Try converting the item to a list - this is the most common expected. // If we got a string, then add it to the list to allow "one-shot" submission string[] listContents = new string[] { }; if (entry.Value is SimpleJson.JsonArray) { listContents = (entry.Value as SimpleJson.JsonArray).OfType <string>().Select(x => x.Trim()).ToArray(); } else if (entry.Value is string) { listContents = new[] { entry.Value as string }; } // Process the possible API entries if (entry.Key == "+") { PluginLogger.print(string.Format("Client {0} added {1}", ID, string.Join(",", listContents))); subscriptions.UnionWith(listContents); } else if (entry.Key == "-") { PluginLogger.print(string.Format("Client {0} removed {1}", ID, string.Join(",", listContents))); subscriptions.ExceptWith(listContents); } else if (entry.Key == "run") { PluginLogger.print(string.Format("Client {0} running {1}", ID, string.Join(",", listContents))); oneShotRuns.UnionWith(listContents); } else if (entry.Key == "rate") { streamRate = Convert.ToInt32(entry.Value); PluginLogger.print(string.Format("Client {0} setting rate {1}", ID, streamRate)); } else if (entry.Key == "binary") { binarySubscriptions = listContents; PluginLogger.print(string.Format("Client {0} requests binary packets {1}", ID, string.Join(", ", listContents))); } else { PluginLogger.print(String.Format("Client {0} send unrecognised key {1}", ID, entry.Key)); } } } // Lock } // OnMessage