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

Last change on this file since 348 was 332, checked in by alloc, 6 years ago

*Latest optimizations

File size: 2.9 KB
RevLine 
[279]1using System;
2using System.Collections.Generic;
[325]3using System.IO;
[279]4using System.Net;
[325]5using System.Net.Sockets;
[279]6using System.Text;
[325]7using System.Threading;
[279]8using AllocsFixes.JSON;
[306]9using AllocsFixes.NetConnections.Servers.Web.API;
[325]10using UnityEngine;
[279]11
[325]12namespace 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
[325]97 public void SendLog (string _msg, string _trace, LogType _type) {
[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}
Note: See TracBrowser for help on using the repository browser.