source: binary-improvements/MapRendering/WebCommandResult.cs@ 461

Last change on this file since 461 was 455, checked in by alloc, 16 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 3.3 KB
RevLine 
[279]1using System;
2using System.Collections.Generic;
[325]3using System.IO;
4using System.Net.Sockets;
[279]5using System.Text;
[325]6using System.Threading;
7using UnityEngine;
[455]8using Utf8Json;
[454]9using Webserver;
[279]10
[455]11namespace AllocsFixes.Web {
[325]12 public class WebCommandResult : IConsoleConnection {
[306]13 public enum ResultType {
14 Full,
15 ResultOnly,
[455]16 Raw,
[306]17 }
18
[325]19 public static int handlingCount;
20 public static int currentHandlers;
21 public static long totalHandlingTime;
22 private readonly string command;
23 private readonly string parameters;
[279]24
[454]25 private readonly RequestContext context;
[325]26 private readonly ResultType responseType;
[279]27
[454]28 public WebCommandResult (string _command, string _parameters, ResultType _resultType, RequestContext _context) {
[279]29 Interlocked.Increment (ref handlingCount);
30 Interlocked.Increment (ref currentHandlers);
31
[454]32 context = _context;
[279]33 command = _command;
34 parameters = _parameters;
[454]35 responseType = _resultType;
[279]36 }
[455]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");
[279]41
42 public void SendLines (List<string> _output) {
[332]43// MicroStopwatch msw = new MicroStopwatch ();
[279]44
45 StringBuilder sb = new StringBuilder ();
46 foreach (string line in _output) {
47 sb.AppendLine (line);
48 }
49
[455]50 string result = sb.ToString ();
51
[309]52 try {
[454]53 context.Response.SendChunked = false;
[279]54
[306]55 if (responseType == ResultType.Raw) {
[455]56 WebUtils.WriteText (context.Response, result);
[306]57 } else {
[455]58 JsonWriter writer = new JsonWriter ();
59
[306]60 if (responseType == ResultType.ResultOnly) {
[455]61 writer.WriteString (result);
[306]62 } else {
[455]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 ();
[306]73 }
[325]74
[455]75 WebUtils.WriteJsonData (context.Response, ref writer);
[306]76 }
[279]77 } catch (IOException e) {
78 if (e.InnerException is SocketException) {
[455]79 Log.Out ($"Error in WebCommandResult.SendLines(): Remote host closed connection: {e.InnerException.Message}");
[279]80 } else {
[455]81 Log.Out ($"Error (IO) in WebCommandResult.SendLines(): {e}");
[279]82 }
83 } catch (Exception e) {
[455]84 Log.Out ($"Error in WebCommandResult.SendLines(): {e}");
[279]85 } finally {
[454]86 context.Response?.Close ();
[279]87
[454]88 // msw.Stop ();
[332]89// if (GamePrefs.GetInt (EnumGamePrefs.HideCommandExecutionLog) < 1) {
90// totalHandlingTime += msw.ElapsedMicroseconds;
91// Log.Out ("WebCommandResult.SendLines(): Took {0} µs", msw.ElapsedMicroseconds);
92// }
[325]93
[279]94 Interlocked.Decrement (ref currentHandlers);
95 }
96 }
97
98 public void SendLine (string _text) {
99 //throw new NotImplementedException ();
100 }
101
[369]102 public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
[279]103 //throw new NotImplementedException ();
104 }
105
[325]106 public void EnableLogLevel (LogType _type, bool _enable) {
[279]107 //throw new NotImplementedException ();
108 }
109
110 public string GetDescription () {
[455]111 return $"WebCommandResult_for_{command}";
[279]112 }
113 }
[325]114}
Note: See TracBrowser for help on using the repository browser.