[279] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[325] | 3 | using System.IO;
|
---|
[279] | 4 | using System.Net;
|
---|
[325] | 5 | using System.Net.Sockets;
|
---|
[279] | 6 | using System.Text;
|
---|
[325] | 7 | using System.Threading;
|
---|
[279] | 8 | using AllocsFixes.JSON;
|
---|
[306] | 9 | using AllocsFixes.NetConnections.Servers.Web.API;
|
---|
[325] | 10 | using UnityEngine;
|
---|
[279] | 11 |
|
---|
[325] | 12 | namespace AllocsFixes.NetConnections.Servers.Web {
|
---|
| 13 | public class WebCommandResult : IConsoleConnection {
|
---|
[306] | 14 | public enum ResultType {
|
---|
| 15 | Full,
|
---|
| 16 | ResultOnly,
|
---|
| 17 | Raw
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[325] | 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;
|
---|
[279] | 25 |
|
---|
[325] | 26 | private readonly HttpListenerResponse response;
|
---|
| 27 | private readonly ResultType responseType;
|
---|
[279] | 28 |
|
---|
[325] | 29 | public WebCommandResult (string _command, string _parameters, ResultType _responseType,
|
---|
| 30 | HttpListenerResponse _response) {
|
---|
[279] | 31 | Interlocked.Increment (ref handlingCount);
|
---|
| 32 | Interlocked.Increment (ref currentHandlers);
|
---|
| 33 |
|
---|
| 34 | response = _response;
|
---|
| 35 | command = _command;
|
---|
| 36 | parameters = _parameters;
|
---|
[306] | 37 | responseType = _responseType;
|
---|
[279] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public void SendLines (List<string> _output) {
|
---|
[332] | 41 | // MicroStopwatch msw = new MicroStopwatch ();
|
---|
[279] | 42 |
|
---|
| 43 | StringBuilder sb = new StringBuilder ();
|
---|
| 44 | foreach (string line in _output) {
|
---|
| 45 | sb.AppendLine (line);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[309] | 48 | try {
|
---|
| 49 | response.SendChunked = false;
|
---|
[279] | 50 |
|
---|
[306] | 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 ();
|
---|
[279] | 59 |
|
---|
[306] | 60 | resultObj.Add ("command", new JSONString (command));
|
---|
| 61 | resultObj.Add ("parameters", new JSONString (parameters));
|
---|
| 62 | resultObj.Add ("result", new JSONString (sb.ToString ()));
|
---|
[279] | 63 |
|
---|
[306] | 64 | result = resultObj;
|
---|
| 65 | }
|
---|
[325] | 66 |
|
---|
[306] | 67 | WebAPI.WriteJSON (response, result);
|
---|
| 68 | }
|
---|
[279] | 69 | } catch (IOException e) {
|
---|
| 70 | if (e.InnerException is SocketException) {
|
---|
[325] | 71 | Log.Out ("Error in WebCommandResult.SendLines(): Remote host closed connection: " +
|
---|
| 72 | e.InnerException.Message);
|
---|
[279] | 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 |
|
---|
[332] | 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 | // }
|
---|
[325] | 88 |
|
---|
[279] | 89 | Interlocked.Decrement (ref currentHandlers);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public void SendLine (string _text) {
|
---|
| 94 | //throw new NotImplementedException ();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[369] | 97 | public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
|
---|
[279] | 98 | //throw new NotImplementedException ();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[325] | 101 | public void EnableLogLevel (LogType _type, bool _enable) {
|
---|
[279] | 102 | //throw new NotImplementedException ();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | public string GetDescription () {
|
---|
| 106 | return "WebCommandResult_for_" + command;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
[325] | 109 | }
|
---|