[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;
|
---|
[306] | 8 | using AllocsFixes.NetConnections.Servers.Web.API;
|
---|
[325] | 9 | using UnityEngine;
|
---|
[382] | 10 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
|
---|
[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 |
|
---|
[382] | 29 | public WebCommandResult (string _command, string _parameters, ResultType _responseType, HttpListenerResponse _response) {
|
---|
[279] | 30 | Interlocked.Increment (ref handlingCount);
|
---|
| 31 | Interlocked.Increment (ref currentHandlers);
|
---|
| 32 |
|
---|
| 33 | response = _response;
|
---|
| 34 | command = _command;
|
---|
| 35 | parameters = _parameters;
|
---|
[306] | 36 | responseType = _responseType;
|
---|
[279] | 37 | }
|
---|
| 38 |
|
---|
| 39 | public void SendLines (List<string> _output) {
|
---|
[332] | 40 | // MicroStopwatch msw = new MicroStopwatch ();
|
---|
[279] | 41 |
|
---|
| 42 | StringBuilder sb = new StringBuilder ();
|
---|
| 43 | foreach (string line in _output) {
|
---|
| 44 | sb.AppendLine (line);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[309] | 47 | try {
|
---|
| 48 | response.SendChunked = false;
|
---|
[279] | 49 |
|
---|
[306] | 50 | if (responseType == ResultType.Raw) {
|
---|
| 51 | WebAPI.WriteText (response, sb.ToString ());
|
---|
| 52 | } else {
|
---|
| 53 | JSONNode result;
|
---|
| 54 | if (responseType == ResultType.ResultOnly) {
|
---|
| 55 | result = new JSONString (sb.ToString ());
|
---|
| 56 | } else {
|
---|
| 57 | JSONObject resultObj = new JSONObject ();
|
---|
[279] | 58 |
|
---|
[306] | 59 | resultObj.Add ("command", new JSONString (command));
|
---|
| 60 | resultObj.Add ("parameters", new JSONString (parameters));
|
---|
| 61 | resultObj.Add ("result", new JSONString (sb.ToString ()));
|
---|
[279] | 62 |
|
---|
[306] | 63 | result = resultObj;
|
---|
| 64 | }
|
---|
[325] | 65 |
|
---|
[306] | 66 | WebAPI.WriteJSON (response, result);
|
---|
| 67 | }
|
---|
[279] | 68 | } catch (IOException e) {
|
---|
| 69 | if (e.InnerException is SocketException) {
|
---|
[325] | 70 | Log.Out ("Error in WebCommandResult.SendLines(): Remote host closed connection: " +
|
---|
| 71 | e.InnerException.Message);
|
---|
[279] | 72 | } else {
|
---|
| 73 | Log.Out ("Error (IO) in WebCommandResult.SendLines(): " + e);
|
---|
| 74 | }
|
---|
| 75 | } catch (Exception e) {
|
---|
| 76 | Log.Out ("Error in WebCommandResult.SendLines(): " + e);
|
---|
| 77 | } finally {
|
---|
| 78 | if (response != null) {
|
---|
| 79 | response.Close ();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[332] | 82 | // msw.Stop ();
|
---|
| 83 | // if (GamePrefs.GetInt (EnumGamePrefs.HideCommandExecutionLog) < 1) {
|
---|
| 84 | // totalHandlingTime += msw.ElapsedMicroseconds;
|
---|
| 85 | // Log.Out ("WebCommandResult.SendLines(): Took {0} µs", msw.ElapsedMicroseconds);
|
---|
| 86 | // }
|
---|
[325] | 87 |
|
---|
[279] | 88 | Interlocked.Decrement (ref currentHandlers);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public void SendLine (string _text) {
|
---|
| 93 | //throw new NotImplementedException ();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[369] | 96 | public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
|
---|
[279] | 97 | //throw new NotImplementedException ();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[325] | 100 | public void EnableLogLevel (LogType _type, bool _enable) {
|
---|
[279] | 101 | //throw new NotImplementedException ();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | public string GetDescription () {
|
---|
| 105 | return "WebCommandResult_for_" + command;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
[325] | 108 | }
|
---|