[279] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[325] | 3 | using System.IO;
|
---|
| 4 | using System.Net.Sockets;
|
---|
[279] | 5 | using System.Text;
|
---|
[325] | 6 | using System.Threading;
|
---|
[279] | 7 | using AllocsFixes.JSON;
|
---|
[325] | 8 | using UnityEngine;
|
---|
[454] | 9 | using Webserver;
|
---|
[279] | 10 |
|
---|
[325] | 11 | namespace AllocsFixes.NetConnections.Servers.Web {
|
---|
| 12 | public class WebCommandResult : IConsoleConnection {
|
---|
[306] | 13 | public enum ResultType {
|
---|
| 14 | Full,
|
---|
| 15 | ResultOnly,
|
---|
| 16 | Raw
|
---|
| 17 | }
|
---|
| 18 |
|
---|
[325] | 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;
|
---|
[279] | 24 |
|
---|
[454] | 25 | private readonly RequestContext context;
|
---|
[325] | 26 | private readonly ResultType responseType;
|
---|
[279] | 27 |
|
---|
[454] | 28 | public WebCommandResult (string _command, string _parameters, ResultType _resultType, RequestContext _context) {
|
---|
[279] | 29 | Interlocked.Increment (ref handlingCount);
|
---|
| 30 | Interlocked.Increment (ref currentHandlers);
|
---|
| 31 |
|
---|
[454] | 32 | context = _context;
|
---|
[279] | 33 | command = _command;
|
---|
| 34 | parameters = _parameters;
|
---|
[454] | 35 | responseType = _resultType;
|
---|
[279] | 36 | }
|
---|
| 37 |
|
---|
| 38 | public void SendLines (List<string> _output) {
|
---|
[332] | 39 | // MicroStopwatch msw = new MicroStopwatch ();
|
---|
[279] | 40 |
|
---|
| 41 | StringBuilder sb = new StringBuilder ();
|
---|
| 42 | foreach (string line in _output) {
|
---|
| 43 | sb.AppendLine (line);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[309] | 46 | try {
|
---|
[454] | 47 | context.Response.SendChunked = false;
|
---|
[279] | 48 |
|
---|
[306] | 49 | if (responseType == ResultType.Raw) {
|
---|
[454] | 50 | WebUtils.WriteText (context.Response, sb.ToString ());
|
---|
[306] | 51 | } else {
|
---|
| 52 | JSONNode result;
|
---|
| 53 | if (responseType == ResultType.ResultOnly) {
|
---|
| 54 | result = new JSONString (sb.ToString ());
|
---|
| 55 | } else {
|
---|
| 56 | JSONObject resultObj = new JSONObject ();
|
---|
[279] | 57 |
|
---|
[306] | 58 | resultObj.Add ("command", new JSONString (command));
|
---|
| 59 | resultObj.Add ("parameters", new JSONString (parameters));
|
---|
| 60 | resultObj.Add ("result", new JSONString (sb.ToString ()));
|
---|
[279] | 61 |
|
---|
[306] | 62 | result = resultObj;
|
---|
| 63 | }
|
---|
[325] | 64 |
|
---|
[454] | 65 | LegacyApiHelper.WriteJSON (context.Response, result);
|
---|
[306] | 66 | }
|
---|
[279] | 67 | } catch (IOException e) {
|
---|
| 68 | if (e.InnerException is SocketException) {
|
---|
[454] | 69 | Log.Out ("Error in WebCommandResult.SendLines(): Remote host closed connection: " + e.InnerException.Message);
|
---|
[279] | 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 {
|
---|
[454] | 76 | context.Response?.Close ();
|
---|
[279] | 77 |
|
---|
[454] | 78 | // msw.Stop ();
|
---|
[332] | 79 | // if (GamePrefs.GetInt (EnumGamePrefs.HideCommandExecutionLog) < 1) {
|
---|
| 80 | // totalHandlingTime += msw.ElapsedMicroseconds;
|
---|
| 81 | // Log.Out ("WebCommandResult.SendLines(): Took {0} µs", msw.ElapsedMicroseconds);
|
---|
| 82 | // }
|
---|
[325] | 83 |
|
---|
[279] | 84 | Interlocked.Decrement (ref currentHandlers);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public void SendLine (string _text) {
|
---|
| 89 | //throw new NotImplementedException ();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[369] | 92 | public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
|
---|
[279] | 93 | //throw new NotImplementedException ();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[325] | 96 | public void EnableLogLevel (LogType _type, bool _enable) {
|
---|
[279] | 97 | //throw new NotImplementedException ();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public string GetDescription () {
|
---|
| 101 | return "WebCommandResult_for_" + command;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
[325] | 104 | }
|
---|