source: binary-improvements/MapRendering/Web/API/WebAPI.cs@ 445

Last change on this file since 445 was 367, checked in by alloc, 3 years ago

Web:

  • Added SSE (ServerSentEvents) subsystem
  • Added log endpoint to SSE. Less heavy weight and more responsive way of watching the server log
  • Bunch of refactoring
File size: 1.6 KB
RevLine 
[230]1using System.Net;
2using System.Text;
[325]3using AllocsFixes.JSON;
[230]4
[325]5namespace AllocsFixes.NetConnections.Servers.Web.API {
6 public abstract class WebAPI {
[326]7 public readonly string Name;
8
[367]9 protected WebAPI (string _name = null) {
10 Name = _name ?? GetType ().Name;
[326]11 }
12
[332]13#if ENABLE_PROFILER
[367]14 private static readonly UnityEngine.Profiling.CustomSampler jsonSerializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Serialize");
15 private static readonly UnityEngine.Profiling.CustomSampler netWriteSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Write");
[332]16#endif
17
[351]18 public static void WriteJSON (HttpListenerResponse _resp, JSONNode _root) {
[332]19#if ENABLE_PROFILER
20 jsonSerializeSampler.Begin ();
21#endif
[309]22 StringBuilder sb = new StringBuilder ();
[351]23 _root.ToString (sb);
[332]24#if ENABLE_PROFILER
25 jsonSerializeSampler.End ();
26 netWriteSampler.Begin ();
27#endif
[309]28 byte[] buf = Encoding.UTF8.GetBytes (sb.ToString ());
[351]29 _resp.ContentLength64 = buf.Length;
30 _resp.ContentType = "application/json";
31 _resp.ContentEncoding = Encoding.UTF8;
32 _resp.OutputStream.Write (buf, 0, buf.Length);
[332]33#if ENABLE_PROFILER
34 netWriteSampler.End ();
35#endif
[230]36 }
37
[325]38 public static void WriteText (HttpListenerResponse _resp, string _text) {
[306]39 byte[] buf = Encoding.UTF8.GetBytes (_text);
40 _resp.ContentLength64 = buf.Length;
41 _resp.ContentType = "text/plain";
42 _resp.ContentEncoding = Encoding.UTF8;
43 _resp.OutputStream.Write (buf, 0, buf.Length);
44 }
45
[351]46 public abstract void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
47 int _permissionLevel);
[279]48
49 public virtual int DefaultPermissionLevel () {
50 return 0;
51 }
[230]52 }
[325]53}
Note: See TracBrowser for help on using the repository browser.