Changeset 402 for binary-improvements2/WebServer/src/WebCommandResult.cs
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/WebCommandResult.cs
r399 r402 4 4 using System.Net.Sockets; 5 5 using System.Text; 6 using AllocsFixes.JSON;7 6 using UnityEngine; 8 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;7 using Utf8Json; 9 8 10 9 namespace Webserver { … … 19 18 private readonly string parameters; 20 19 21 private readonly HttpListenerResponse response;20 private readonly RequestContext context; 22 21 private readonly ResultType responseType; 23 22 24 public WebCommandResult (string _command, string _parameters, ResultType _responseType, HttpListenerResponse _response) {25 response = _response;23 public WebCommandResult (string _command, string _parameters, ResultType _responseType, RequestContext _context) { 24 context = _context; 26 25 command = _command; 27 26 parameters = _parameters; … … 29 28 } 30 29 30 private static readonly byte[] jsonRawKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("resultRaw"); 31 32 private static readonly byte[] jsonCommandKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("command"); 33 private static readonly byte[] jsonParametersKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("parameters"); 34 private static readonly byte[] jsonResultKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("result"); 35 31 36 public void SendLines (List<string> _output) { 32 37 StringBuilder sb = new StringBuilder (); … … 35 40 } 36 41 42 string commandOutput = sb.ToString (); 43 37 44 try { 38 response.SendChunked = false;39 40 45 if (responseType == ResultType.Raw) { 41 WebUtils.WriteText ( response, sb.ToString ());46 WebUtils.WriteText (context.Response, commandOutput); 42 47 } else { 43 JsonNode result; 48 WebUtils.PrepareEnvelopedResult (out JsonWriter writer); 49 44 50 if (responseType == ResultType.ResultOnly) { 45 result = new JsonString (sb.ToString ()); 51 writer.WriteRaw (jsonRawKey); 52 writer.WriteString (commandOutput); 53 writer.WriteEndObject (); 46 54 } else { 47 JsonObject resultObj = new JsonObject (); 48 49 resultObj.Add ("command", new JsonString (command)); 50 resultObj.Add ("parameters", new JsonString (parameters)); 51 resultObj.Add ("result", new JsonString (sb.ToString ())); 52 53 result = resultObj; 55 writer.WriteRaw (jsonCommandKey); 56 writer.WriteString (command); 57 58 writer.WriteRaw (jsonParametersKey); 59 writer.WriteString (parameters); 60 61 writer.WriteRaw (jsonResultKey); 62 writer.WriteString (commandOutput); 63 64 writer.WriteEndObject (); 54 65 } 55 66 56 WebUtils. WriteJson (response, result);67 WebUtils.SendEnvelopedResult (context, ref writer); 57 68 } 58 69 } catch (IOException e) { 59 70 if (e.InnerException is SocketException) { 60 Log.Warning ( "[Web] Error in WebCommandResult.SendLines(): Remote host closed connection: " + e.InnerException.Message);71 Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): Remote host closed connection: {e.InnerException.Message}"); 61 72 } else { 62 Log.Warning ( "[Web] Error (IO) in WebCommandResult.SendLines(): " + e);73 Log.Warning ($"[Web] Error (IO) in WebCommandResult.SendLines(): {e}"); 63 74 } 64 75 } catch (Exception e) { 65 Log.Warning ( "[Web] Error in WebCommandResult.SendLines(): " + e);76 Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): {e}"); 66 77 } finally { 67 response?.Close ();78 context?.Response?.Close (); 68 79 } 69 80 } … … 82 93 83 94 public string GetDescription () { 84 return "WebCommandResult_for_" + command;95 return $"WebCommandResult_for_{command}"; 85 96 } 86 97 }
Note:
See TracChangeset
for help on using the changeset viewer.