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

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