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

Last change on this file since 419 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
Line 
1using System.Net;
2using System.Text;
3using AllocsFixes.JSON;
4
5namespace AllocsFixes.NetConnections.Servers.Web.API {
6 public abstract class WebAPI {
7 public readonly string Name;
8
9 protected WebAPI (string _name = null) {
10 Name = _name ?? GetType ().Name;
11 }
12
13#if ENABLE_PROFILER
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");
16#endif
17
18 public static void WriteJSON (HttpListenerResponse _resp, JSONNode _root) {
19#if ENABLE_PROFILER
20 jsonSerializeSampler.Begin ();
21#endif
22 StringBuilder sb = new StringBuilder ();
23 _root.ToString (sb);
24#if ENABLE_PROFILER
25 jsonSerializeSampler.End ();
26 netWriteSampler.Begin ();
27#endif
28 byte[] buf = Encoding.UTF8.GetBytes (sb.ToString ());
29 _resp.ContentLength64 = buf.Length;
30 _resp.ContentType = "application/json";
31 _resp.ContentEncoding = Encoding.UTF8;
32 _resp.OutputStream.Write (buf, 0, buf.Length);
33#if ENABLE_PROFILER
34 netWriteSampler.End ();
35#endif
36 }
37
38 public static void WriteText (HttpListenerResponse _resp, string _text) {
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
46 public abstract void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
47 int _permissionLevel);
48
49 public virtual int DefaultPermissionLevel () {
50 return 0;
51 }
52 }
53}
Note: See TracBrowser for help on using the repository browser.