Changeset 306 for binary-improvements/MapRendering/Web/WebCommandResult.cs
- Timestamp:
- Aug 2, 2017, 6:46:15 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/WebCommandResult.cs
r279 r306 8 8 using System.Net.Sockets; 9 9 using System.Threading; 10 using AllocsFixes.NetConnections.Servers.Web.API; 10 11 11 12 namespace AllocsFixes.NetConnections.Servers.Web … … 13 14 public class WebCommandResult : IConsoleConnection 14 15 { 16 public enum ResultType { 17 Full, 18 ResultOnly, 19 Raw 20 } 21 15 22 public static int handlingCount = 0; 16 23 public static int currentHandlers = 0; … … 20 27 private string command; 21 28 private string parameters; 29 private ResultType responseType; 22 30 23 public WebCommandResult (string _command, string _parameters, HttpListenerResponse _response) {31 public WebCommandResult (string _command, string _parameters, ResultType _responseType, HttpListenerResponse _response) { 24 32 Interlocked.Increment (ref handlingCount); 25 33 Interlocked.Increment (ref currentHandlers); … … 28 36 command = _command; 29 37 parameters = _parameters; 38 responseType = _responseType; 30 39 } 31 40 … … 38 47 } 39 48 40 JSONObject result = new JSONObject ();41 42 result.Add ("command", new JSONString (command));43 result.Add ("parameters", new JSONString (parameters));44 result.Add ("result", new JSONString (sb.ToString ()));45 46 49 response.SendChunked = false; 47 50 48 51 try { 49 WriteJSON (response, result); 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 (); 60 61 resultObj.Add ("command", new JSONString (command)); 62 resultObj.Add ("parameters", new JSONString (parameters)); 63 resultObj.Add ("result", new JSONString (sb.ToString ())); 64 65 result = resultObj; 66 } 67 WebAPI.WriteJSON (response, result); 68 } 50 69 } catch (IOException e) { 51 70 if (e.InnerException is SocketException) { … … 62 81 63 82 msw.Stop (); 64 totalHandlingTime += msw.ElapsedMicroseconds; 65 Log.Out ("WebCommandResult.SendLines(): Took {0} µs", msw.ElapsedMicroseconds); 83 if (GamePrefs.GetInt (EnumGamePrefs.HideCommandExecutionLog) < 1) { 84 totalHandlingTime += msw.ElapsedMicroseconds; 85 Log.Out ("WebCommandResult.SendLines(): Took {0} µs", msw.ElapsedMicroseconds); 86 } 66 87 Interlocked.Decrement (ref currentHandlers); 67 88 } 68 }69 70 public void WriteJSON (HttpListenerResponse resp, JSON.JSONNode root)71 {72 byte[] buf = Encoding.UTF8.GetBytes (root.ToString());73 resp.ContentLength64 = buf.Length;74 resp.ContentType = "application/json";75 resp.ContentEncoding = Encoding.UTF8;76 resp.OutputStream.Write (buf, 0, buf.Length);77 89 } 78 90
Note:
See TracChangeset
for help on using the changeset viewer.