Changeset 455 for binary-improvements/MapRendering/WebCommandResult.cs
- Timestamp:
- Jul 31, 2023, 4:06:13 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/WebCommandResult.cs
r454 r455 5 5 using System.Text; 6 6 using System.Threading; 7 using AllocsFixes.JSON;8 7 using UnityEngine; 8 using Utf8Json; 9 9 using Webserver; 10 10 11 namespace AllocsFixes. NetConnections.Servers.Web {11 namespace AllocsFixes.Web { 12 12 public class WebCommandResult : IConsoleConnection { 13 13 public enum ResultType { 14 14 Full, 15 15 ResultOnly, 16 Raw 16 Raw, 17 17 } 18 18 … … 35 35 responseType = _resultType; 36 36 } 37 38 private static readonly byte[] jsonKeyCommand = JsonWriter.GetEncodedPropertyNameWithBeginObject ("command"); 39 private static readonly byte[] jsonKeyParameters = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("parameters"); 40 private static readonly byte[] jsonKeyResult = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("result"); 37 41 38 42 public void SendLines (List<string> _output) { … … 44 48 } 45 49 50 string result = sb.ToString (); 51 46 52 try { 47 53 context.Response.SendChunked = false; 48 54 49 55 if (responseType == ResultType.Raw) { 50 WebUtils.WriteText (context.Response, sb.ToString ());56 WebUtils.WriteText (context.Response, result); 51 57 } else { 52 JSONNode result; 58 JsonWriter writer = new JsonWriter (); 59 53 60 if (responseType == ResultType.ResultOnly) { 54 result = new JSONString (sb.ToString ());61 writer.WriteString (result); 55 62 } else { 56 JSONObject resultObj = new JSONObject (); 57 58 resultObj.Add ("command", new JSONString (command)); 59 resultObj.Add ("parameters", new JSONString (parameters)); 60 resultObj.Add ("result", new JSONString (sb.ToString ())); 61 62 result = resultObj; 63 writer.WriteRaw (jsonKeyCommand); 64 writer.WriteString (command); 65 66 writer.WriteRaw (jsonKeyParameters); 67 writer.WriteString (parameters); 68 69 writer.WriteRaw (jsonKeyResult); 70 writer.WriteString (result); 71 72 writer.WriteEndObject (); 63 73 } 64 74 65 LegacyApiHelper.WriteJSON (context.Response, result);75 WebUtils.WriteJsonData (context.Response, ref writer); 66 76 } 67 77 } catch (IOException e) { 68 78 if (e.InnerException is SocketException) { 69 Log.Out ( "Error in WebCommandResult.SendLines(): Remote host closed connection: " + e.InnerException.Message);79 Log.Out ($"Error in WebCommandResult.SendLines(): Remote host closed connection: {e.InnerException.Message}"); 70 80 } else { 71 Log.Out ( "Error (IO) in WebCommandResult.SendLines(): " + e);81 Log.Out ($"Error (IO) in WebCommandResult.SendLines(): {e}"); 72 82 } 73 83 } catch (Exception e) { 74 Log.Out ( "Error in WebCommandResult.SendLines(): " + e);84 Log.Out ($"Error in WebCommandResult.SendLines(): {e}"); 75 85 } finally { 76 86 context.Response?.Close (); … … 99 109 100 110 public string GetDescription () { 101 return "WebCommandResult_for_" + command;111 return $"WebCommandResult_for_{command}"; 102 112 } 103 113 }
Note:
See TracChangeset
for help on using the changeset viewer.