1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Net.Sockets;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading;
|
---|
7 | using AllocsFixes.JSON;
|
---|
8 | using UnityEngine;
|
---|
9 | using Webserver;
|
---|
10 |
|
---|
11 | namespace AllocsFixes.NetConnections.Servers.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 | public void SendLines (List<string> _output) {
|
---|
39 | // MicroStopwatch msw = new MicroStopwatch ();
|
---|
40 |
|
---|
41 | StringBuilder sb = new StringBuilder ();
|
---|
42 | foreach (string line in _output) {
|
---|
43 | sb.AppendLine (line);
|
---|
44 | }
|
---|
45 |
|
---|
46 | try {
|
---|
47 | context.Response.SendChunked = false;
|
---|
48 |
|
---|
49 | if (responseType == ResultType.Raw) {
|
---|
50 | WebUtils.WriteText (context.Response, sb.ToString ());
|
---|
51 | } else {
|
---|
52 | JSONNode result;
|
---|
53 | if (responseType == ResultType.ResultOnly) {
|
---|
54 | result = new JSONString (sb.ToString ());
|
---|
55 | } else {
|
---|
56 | JSONObject resultObj = new JSONObject ();
|
---|
57 |
|
---|
58 | resultObj.Add ("command", new JSONString (command));
|
---|
59 | resultObj.Add ("parameters", new JSONString (parameters));
|
---|
60 | resultObj.Add ("result", new JSONString (sb.ToString ()));
|
---|
61 |
|
---|
62 | result = resultObj;
|
---|
63 | }
|
---|
64 |
|
---|
65 | LegacyApiHelper.WriteJSON (context.Response, result);
|
---|
66 | }
|
---|
67 | } catch (IOException e) {
|
---|
68 | if (e.InnerException is SocketException) {
|
---|
69 | Log.Out ("Error in WebCommandResult.SendLines(): Remote host closed connection: " + e.InnerException.Message);
|
---|
70 | } else {
|
---|
71 | Log.Out ("Error (IO) in WebCommandResult.SendLines(): " + e);
|
---|
72 | }
|
---|
73 | } catch (Exception e) {
|
---|
74 | Log.Out ("Error in WebCommandResult.SendLines(): " + e);
|
---|
75 | } finally {
|
---|
76 | context.Response?.Close ();
|
---|
77 |
|
---|
78 | // msw.Stop ();
|
---|
79 | // if (GamePrefs.GetInt (EnumGamePrefs.HideCommandExecutionLog) < 1) {
|
---|
80 | // totalHandlingTime += msw.ElapsedMicroseconds;
|
---|
81 | // Log.Out ("WebCommandResult.SendLines(): Took {0} µs", msw.ElapsedMicroseconds);
|
---|
82 | // }
|
---|
83 |
|
---|
84 | Interlocked.Decrement (ref currentHandlers);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public void SendLine (string _text) {
|
---|
89 | //throw new NotImplementedException ();
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
|
---|
93 | //throw new NotImplementedException ();
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void EnableLogLevel (LogType _type, bool _enable) {
|
---|
97 | //throw new NotImplementedException ();
|
---|
98 | }
|
---|
99 |
|
---|
100 | public string GetDescription () {
|
---|
101 | return "WebCommandResult_for_" + command;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|