source: binary-improvements2/WebServer/src/WebCommandResult.cs@ 402

Last change on this file since 402 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net.Sockets;
5using System.Text;
6using UnityEngine;
7using Utf8Json;
8
9namespace Webserver {
10 public class WebCommandResult : IConsoleConnection {
11 public enum ResultType {
12 Full,
13 ResultOnly,
14 Raw
15 }
16
17 private readonly string command;
18 private readonly string parameters;
19
20 private readonly RequestContext context;
21 private readonly ResultType responseType;
22
23 public WebCommandResult (string _command, string _parameters, ResultType _responseType, RequestContext _context) {
24 context = _context;
25 command = _command;
26 parameters = _parameters;
27 responseType = _responseType;
28 }
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
36 public void SendLines (List<string> _output) {
37 StringBuilder sb = new StringBuilder ();
38 foreach (string line in _output) {
39 sb.AppendLine (line);
40 }
41
42 string commandOutput = sb.ToString ();
43
44 try {
45 if (responseType == ResultType.Raw) {
46 WebUtils.WriteText (context.Response, commandOutput);
47 } else {
48 WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
49
50 if (responseType == ResultType.ResultOnly) {
51 writer.WriteRaw (jsonRawKey);
52 writer.WriteString (commandOutput);
53 writer.WriteEndObject ();
54 } else {
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 ();
65 }
66
67 WebUtils.SendEnvelopedResult (context, ref writer);
68 }
69 } catch (IOException e) {
70 if (e.InnerException is SocketException) {
71 Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): Remote host closed connection: {e.InnerException.Message}");
72 } else {
73 Log.Warning ($"[Web] Error (IO) in WebCommandResult.SendLines(): {e}");
74 }
75 } catch (Exception e) {
76 Log.Warning ($"[Web] Error in WebCommandResult.SendLines(): {e}");
77 } finally {
78 context?.Response?.Close ();
79 }
80 }
81
82 public void SendLine (string _text) {
83 //throw new NotImplementedException ();
84 }
85
86 public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
87 //throw new NotImplementedException ();
88 }
89
90 public void EnableLogLevel (LogType _type, bool _enable) {
91 //throw new NotImplementedException ();
92 }
93
94 public string GetDescription () {
95 return $"WebCommandResult_for_{command}";
96 }
97 }
98}
Note: See TracBrowser for help on using the repository browser.