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 AllocsFixes.NetConnections.Servers.Web.API;
|
---|
9 | using UnityEngine;
|
---|
10 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
|
---|
11 |
|
---|
12 | namespace 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, HttpListenerResponse _response) {
|
---|
30 | Interlocked.Increment (ref handlingCount);
|
---|
31 | Interlocked.Increment (ref currentHandlers);
|
---|
32 |
|
---|
33 | response = _response;
|
---|
34 | command = _command;
|
---|
35 | parameters = _parameters;
|
---|
36 | responseType = _responseType;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public void SendLines (List<string> _output) {
|
---|
40 | // MicroStopwatch msw = new MicroStopwatch ();
|
---|
41 |
|
---|
42 | StringBuilder sb = new StringBuilder ();
|
---|
43 | foreach (string line in _output) {
|
---|
44 | sb.AppendLine (line);
|
---|
45 | }
|
---|
46 |
|
---|
47 | try {
|
---|
48 | response.SendChunked = false;
|
---|
49 |
|
---|
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 ();
|
---|
58 |
|
---|
59 | resultObj.Add ("command", new JSONString (command));
|
---|
60 | resultObj.Add ("parameters", new JSONString (parameters));
|
---|
61 | resultObj.Add ("result", new JSONString (sb.ToString ()));
|
---|
62 |
|
---|
63 | result = resultObj;
|
---|
64 | }
|
---|
65 |
|
---|
66 | WebAPI.WriteJSON (response, result);
|
---|
67 | }
|
---|
68 | } catch (IOException e) {
|
---|
69 | if (e.InnerException is SocketException) {
|
---|
70 | Log.Out ("Error in WebCommandResult.SendLines(): Remote host closed connection: " +
|
---|
71 | e.InnerException.Message);
|
---|
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 |
|
---|
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 | // }
|
---|
87 |
|
---|
88 | Interlocked.Decrement (ref currentHandlers);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void SendLine (string _text) {
|
---|
93 | //throw new NotImplementedException ();
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void SendLog (string _formattedMessage, string _plainMessage, string _trace, LogType _type, DateTime _timestamp, long _uptime) {
|
---|
97 | //throw new NotImplementedException ();
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void EnableLogLevel (LogType _type, bool _enable) {
|
---|
101 | //throw new NotImplementedException ();
|
---|
102 | }
|
---|
103 |
|
---|
104 | public string GetDescription () {
|
---|
105 | return "WebCommandResult_for_" + command;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|