Changeset 382 for binary-improvements2/MapRendering/Web/SSE
- Timestamp:
- Aug 1, 2022, 12:54:31 PM (2 years ago)
- Location:
- binary-improvements2/MapRendering/Web/SSE
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/Web/SSE/EventBase.cs
r372 r382 2 2 using System.Collections.Generic; 3 3 using System.IO; 4 using System.Net;5 4 using System.Net.Sockets; 6 5 using System.Text; 7 6 using AllocsFixes.JSON; 7 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 8 8 9 9 namespace AllocsFixes.NetConnections.Servers.Web.SSE { … … 116 116 Log.Exception (e); 117 117 } 118 119 resp.Close ();120 118 } catch (Exception e) { 121 119 currentlyOpen--; -
binary-improvements2/MapRendering/Web/SSE/EventLog.cs
r369 r382 12 12 string date = $"{_timestamp.Year:0000}-{_timestamp.Month:00}-{_timestamp.Day:00}"; 13 13 string time = $"{_timestamp.Hour:00}:{_timestamp.Minute:00}:{_timestamp.Second:00}"; 14 string isotime = _timestamp.ToString ("o"); 14 15 string uptime = _uptime.ToString (); 15 16 string message = _plainMsg; … … 21 22 data.Add ("date", new JSONString (date)); 22 23 data.Add ("time", new JSONString (time)); 24 data.Add ("isotime", new JSONString (isotime)); 23 25 data.Add ("uptime", new JSONString (uptime)); 24 26 -
binary-improvements2/MapRendering/Web/SSE/SseHandler.cs
r367 r382 5 5 using System.Threading; 6 6 using AllocsFixes.NetConnections.Servers.Web.Handlers; 7 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 8 using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse; 7 9 8 10 // Implemented following HTML spec … … 10 12 11 13 namespace AllocsFixes.NetConnections.Servers.Web.SSE { 12 public class SseHandler : PathHandler {14 public class SseHandler : AbsHandler { 13 15 private readonly Dictionary<string, EventBase> events = new CaseInsensitiveStringDictionary<EventBase> (); 14 16 15 17 private ThreadManager.ThreadInfo queueThead; 16 18 private readonly AutoResetEvent evSendRequest = new AutoResetEvent (false); … … 18 20 19 21 public SseHandler (string _moduleName = null) : base (_moduleName) { 20 Type[] ctorTypes = { typeof (SseHandler)};21 object[] ctorParams = { this};22 Type[] ctorTypes = { typeof (SseHandler) }; 23 object[] ctorParams = { this }; 22 24 23 25 foreach (Type t in Assembly.GetExecutingAssembly ().GetTypes ()) { … … 25 27 ConstructorInfo ctor = t.GetConstructor (ctorTypes); 26 28 if (ctor != null) { 27 EventBase apiInstance = (EventBase) 28 addEvent (apiInstance.Name, apiInstance);29 EventBase apiInstance = (EventBase)ctor.Invoke (ctorParams); 30 AddEvent (apiInstance.Name, apiInstance); 29 31 } 30 32 } … … 34 36 public override void SetBasePathAndParent (Web _parent, string _relativePath) { 35 37 base.SetBasePathAndParent (_parent, _relativePath); 36 38 37 39 queueThead = ThreadManager.StartThread ("SSE-Processing_" + urlBasePath, QueueProcessThread, ThreadPriority.BelowNormal, 38 40 _useRealThread: true); … … 45 47 } 46 48 47 p rivate void addEvent (string _eventName, EventBase _eventInstance) {49 public void AddEvent (string _eventName, EventBase _eventInstance) { 48 50 events.Add (_eventName, _eventInstance); 49 51 WebPermissions.Instance.AddKnownModule ("webevent." + _eventName, _eventInstance.DefaultPermissionLevel ()); 50 52 } 51 53 52 public override void HandleRequest ( HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,54 public override void HandleRequest (string _requestPath, HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _con, 53 55 int _permissionLevel) { 54 string eventName = _req .Url.AbsolutePath.Remove (0, urlBasePath.Length);56 string eventName = _requestPath.Remove (0, urlBasePath.Length); 55 57 56 58 if (!events.TryGetValue (eventName, out EventBase eventInstance)) { 57 59 Log.Out ($"Error in {nameof (SseHandler)}.HandleRequest(): No handler found for event \"{eventName}\""); 58 _resp.StatusCode = (int) 60 _resp.StatusCode = (int)HttpStatusCode.NotFound; 59 61 return; 60 62 } 61 63 62 if (! AuthorizeForEvent (eventName, _permissionLevel)) {63 _resp.StatusCode = (int) 64 if (_ user!= null) {64 if (!IsAuthorizedForEvent (eventName, _permissionLevel)) { 65 _resp.StatusCode = (int)HttpStatusCode.Forbidden; 66 if (_con != null) { 65 67 //Log.Out ($"{nameof(SseHandler)}: user '{user.SteamID}' not allowed to access '{eventName}'"); 66 68 } … … 80 82 Log.Error ($"Error in {nameof (SseHandler)}.HandleRequest(): Handler {eventInstance.Name} threw an exception:"); 81 83 Log.Exception (e); 82 _resp.StatusCode = (int) 84 _resp.StatusCode = (int)HttpStatusCode.InternalServerError; 83 85 } 84 86 } 85 87 86 private bool AuthorizeForEvent (string _eventName, int _permissionLevel) {88 private bool IsAuthorizedForEvent (string _eventName, int _permissionLevel) { 87 89 return WebPermissions.Instance.ModuleAllowedWithLevel ("webevent." + _eventName, _permissionLevel); 88 90 } 89 91 90 92 private void QueueProcessThread (ThreadManager.ThreadInfo _threadInfo) { 91 try { 92 while (!shutdown && !_threadInfo.TerminationRequested ()) { 93 evSendRequest.WaitOne (500); 93 while (!shutdown && !_threadInfo.TerminationRequested ()) { 94 evSendRequest.WaitOne (500); 94 95 95 foreach (KeyValuePair<string, EventBase> kvp in events) { 96 foreach (KeyValuePair<string, EventBase> kvp in events) { 97 try { 96 98 kvp.Value.ProcessSendQueue (); 99 } catch (Exception e) { 100 Log.Error ($"SSE ({kvp.Key}): Error processing send queue"); 101 Log.Exception (e); 97 102 } 98 103 } 99 } catch (Exception e) {100 Log.Error ("SSE: Error processing send queue");101 Log.Exception (e);102 104 } 103 105 }
Note:
See TracChangeset
for help on using the changeset viewer.