source: binary-improvements/MapRendering/Web/WebCommandResult.cs@ 318

Last change on this file since 318 was 309, checked in by alloc, 7 years ago

Fixes 14_16_21

File size: 2.9 KB
RevLine 
[279]1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Text;
5
6using AllocsFixes.JSON;
7using System.IO;
8using System.Net.Sockets;
9using System.Threading;
[306]10using AllocsFixes.NetConnections.Servers.Web.API;
[279]11
12namespace AllocsFixes.NetConnections.Servers.Web
13{
14 public class WebCommandResult : IConsoleConnection
15 {
[306]16 public enum ResultType {
17 Full,
18 ResultOnly,
19 Raw
20 }
21
[279]22 public static int handlingCount = 0;
23 public static int currentHandlers = 0;
24 public static long totalHandlingTime = 0;
25
26 private HttpListenerResponse response;
27 private string command;
28 private string parameters;
[306]29 private ResultType responseType;
[279]30
[306]31 public WebCommandResult (string _command, string _parameters, ResultType _responseType, HttpListenerResponse _response) {
[279]32 Interlocked.Increment (ref handlingCount);
33 Interlocked.Increment (ref currentHandlers);
34
35 response = _response;
36 command = _command;
37 parameters = _parameters;
[306]38 responseType = _responseType;
[279]39 }
40
41 public void SendLines (List<string> _output) {
42 MicroStopwatch msw = new MicroStopwatch ();
43
44 StringBuilder sb = new StringBuilder ();
45 foreach (string line in _output) {
46 sb.AppendLine (line);
47 }
48
[309]49 try {
50 response.SendChunked = false;
[279]51
[306]52 if (responseType == ResultType.Raw) {
53 WebAPI.WriteText (response, sb.ToString ());
54 } else {
55 JSONNode result;
56 if (responseType == ResultType.ResultOnly) {
57 result = new JSONString (sb.ToString ());
58 } else {
59 JSONObject resultObj = new JSONObject ();
[279]60
[306]61 resultObj.Add ("command", new JSONString (command));
62 resultObj.Add ("parameters", new JSONString (parameters));
63 resultObj.Add ("result", new JSONString (sb.ToString ()));
[279]64
[306]65 result = resultObj;
66 }
67 WebAPI.WriteJSON (response, result);
68 }
[279]69 } catch (IOException e) {
70 if (e.InnerException is SocketException) {
71 Log.Out ("Error in WebCommandResult.SendLines(): Remote host closed connection: " + 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 ();
[306]83 if (GamePrefs.GetInt (EnumGamePrefs.HideCommandExecutionLog) < 1) {
84 totalHandlingTime += msw.ElapsedMicroseconds;
85 Log.Out ("WebCommandResult.SendLines(): Took {0} µs", msw.ElapsedMicroseconds);
86 }
[279]87 Interlocked.Decrement (ref currentHandlers);
88 }
89 }
90
91 public void SendLine (string _text) {
92 //throw new NotImplementedException ();
93 }
94
95 public void SendLog (string _msg, string _trace, UnityEngine.LogType _type) {
96 //throw new NotImplementedException ();
97 }
98
99 public void EnableLogLevel (UnityEngine.LogType _type, bool _enable) {
100 //throw new NotImplementedException ();
101 }
102
103 public string GetDescription () {
104 return "WebCommandResult_for_" + command;
105 }
106 }
107}
108
Note: See TracBrowser for help on using the repository browser.