source: binary-improvements/MapRendering/Web/WebCommandResult.cs@ 445

Last change on this file since 445 was 369, checked in by alloc, 3 years ago

Preparations for A20 release
Changes usage of "SteamID" to "UserID" in console commands
Also changes a bunch of the WebAPI stuff to show / use UserIDs

File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
5using System.Net.Sockets;
6using System.Text;
7using System.Threading;
8using AllocsFixes.JSON;
9using AllocsFixes.NetConnections.Servers.Web.API;
10using UnityEngine;
11
12namespace AllocsFixes.NetConnections.Servers.Web {
13 public class WebCommandResult : IConsoleConnection {
14 public enum ResultType {
15 Full,
16 ResultOnly,
17 Raw
18 }
19
20 public static int handlingCount;
21 public static int currentHandlers;
22 public static long totalHandlingTime;
23 private readonly string command;
24 private readonly string parameters;
25
26 private readonly HttpListenerResponse response;
27 private readonly ResultType responseType;
28
29 public WebCommandResult (string _command, string _parameters, ResultType _responseType,
30 HttpListenerResponse _response) {
31 Interlocked.Increment (ref handlingCount);
32 Interlocked.Increment (ref currentHandlers);
33
34 response = _response;
35 command = _command;
36 parameters = _parameters;
37 responseType = _responseType;
38 }
39
40 public void SendLines (List<string> _output) {
41// MicroStopwatch msw = new MicroStopwatch ();
42
43 StringBuilder sb = new StringBuilder ();
44 foreach (string line in _output) {
45 sb.AppendLine (line);
46 }
47
48 try {
49 response.SendChunked = false;
50
51 if (responseType == ResultType.Raw) {
52 WebAPI.WriteText (response, sb.ToString ());
53 } else {
54 JSONNode result;
55 if (responseType == ResultType.ResultOnly) {
56 result = new JSONString (sb.ToString ());
57 } else {
58 JSONObject resultObj = new JSONObject ();
59
60 resultObj.Add ("command", new JSONString (command));
61 resultObj.Add ("parameters", new JSONString (parameters));
62 resultObj.Add ("result", new JSONString (sb.ToString ()));
63
64 result = resultObj;
65 }
66
67 WebAPI.WriteJSON (response, result);
68 }
69 } catch (IOException e) {
70 if (e.InnerException is SocketException) {
71 Log.Out ("Error in WebCommandResult.SendLines(): Remote host closed connection: " +
72 e.InnerException.Message);
73 } else {
74 Log.Out ("Error (IO) in WebCommandResult.SendLines(): " + e);
75 }
76 } catch (Exception e) {
77 Log.Out ("Error in WebCommandResult.SendLines(): " + e);
78 } finally {
79 if (response != null) {
80 response.Close ();
81 }
82
83// msw.Stop ();
84// if (GamePrefs.GetInt (EnumGamePrefs.HideCommandExecutionLog) < 1) {
85// totalHandlingTime += msw.ElapsedMicroseconds;
86// Log.Out ("WebCommandResult.SendLines(): Took {0} µs", msw.ElapsedMicroseconds);
87// }
88
89 Interlocked.Decrement (ref currentHandlers);
90 }
91 }
92
93 public void SendLine (string _text) {
94 //throw new NotImplementedException ();
95 }
96
97 public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
98 //throw new NotImplementedException ();
99 }
100
101 public void EnableLogLevel (LogType _type, bool _enable) {
102 //throw new NotImplementedException ();
103 }
104
105 public string GetDescription () {
106 return "WebCommandResult_for_" + command;
107 }
108 }
109}
Note: See TracBrowser for help on using the repository browser.