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

Last change on this file since 399 was 399, checked in by alloc, 2 years ago

Updated logging strings

File size: 2.4 KB
RevLine 
[391]1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net.Sockets;
5using System.Text;
6using AllocsFixes.JSON;
7using UnityEngine;
8using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
9
10namespace Webserver {
11 public class WebCommandResult : IConsoleConnection {
12 public enum ResultType {
13 Full,
14 ResultOnly,
15 Raw
16 }
17
18 private readonly string command;
19 private readonly string parameters;
20
21 private readonly HttpListenerResponse response;
22 private readonly ResultType responseType;
23
24 public WebCommandResult (string _command, string _parameters, ResultType _responseType, HttpListenerResponse _response) {
25 response = _response;
26 command = _command;
27 parameters = _parameters;
28 responseType = _responseType;
29 }
30
31 public void SendLines (List<string> _output) {
32 StringBuilder sb = new StringBuilder ();
33 foreach (string line in _output) {
34 sb.AppendLine (line);
35 }
36
37 try {
38 response.SendChunked = false;
39
40 if (responseType == ResultType.Raw) {
41 WebUtils.WriteText (response, sb.ToString ());
42 } else {
43 JsonNode result;
44 if (responseType == ResultType.ResultOnly) {
45 result = new JsonString (sb.ToString ());
46 } 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;
54 }
55
56 WebUtils.WriteJson (response, result);
57 }
58 } catch (IOException e) {
59 if (e.InnerException is SocketException) {
[399]60 Log.Warning ("[Web] Error in WebCommandResult.SendLines(): Remote host closed connection: " + e.InnerException.Message);
[391]61 } else {
[399]62 Log.Warning ("[Web] Error (IO) in WebCommandResult.SendLines(): " + e);
[391]63 }
64 } catch (Exception e) {
[399]65 Log.Warning ("[Web] Error in WebCommandResult.SendLines(): " + e);
[391]66 } finally {
67 response?.Close ();
68 }
69 }
70
71 public void SendLine (string _text) {
72 //throw new NotImplementedException ();
73 }
74
75 public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
76 //throw new NotImplementedException ();
77 }
78
79 public void EnableLogLevel (LogType _type, bool _enable) {
80 //throw new NotImplementedException ();
81 }
82
83 public string GetDescription () {
84 return "WebCommandResult_for_" + command;
85 }
86 }
87}
Note: See TracBrowser for help on using the repository browser.