From 7a6cc79e617184ebf42172be04c4584d6166d0e6 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 25 May 2021 00:49:50 +0200 Subject: [PATCH] Not waiting for the response, command list endpoint Returning the response right after the command is executed, most of the time that's enough, long polling should be used in addition --- TBConsole/TBConsoleMod.cs | 16 +++++++++++----- TBConsole/UnityLogHandler.cs | 6 +++--- TBConsole/WebServer.cs | 27 +++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/TBConsole/TBConsoleMod.cs b/TBConsole/TBConsoleMod.cs index f5ba658..a301490 100644 --- a/TBConsole/TBConsoleMod.cs +++ b/TBConsole/TBConsoleMod.cs @@ -19,15 +19,15 @@ namespace TBConsole public override void OnApplicationStart() { TechbloxModdingAPI.Main.Init(); - _server = new WebServer(CommandReceived); + _server = new WebServer(CommandReceived, GetCommandList); _server.Start(); } - private async Task CommandReceived(string command) + private string CommandReceived(string command) { if (_logHandler == null) Debug.unityLogger.logHandler = _logHandler = new UnityLogHandler(Debug.unityLogger.logHandler); - var logTask = _logHandler.CollectLogMessages(); + _logHandler.StartCollectingLogMessages(); bool inString = false; var cmdparts = new List(); command = command.Trim(); @@ -62,10 +62,16 @@ namespace TBConsole return "Too many arguments! Maximum for default commands is 3"; } - string result = await logTask; + string result = _logHandler.FinishCollectingLogMessages(); return $"Got it: {command}\n{result}"; } + public string GetCommandList() + { + return ExistingCommands.GetCommandNamesAndDescriptions() + .Select(command => command.Name + " - " + command.Description).Aggregate((a, b) => a + "\n" + b); + } + public override void OnApplicationQuit() { _server.Stop(); @@ -75,7 +81,7 @@ namespace TBConsole public static void Main(string[] args) { var mod = new TBConsoleMod(); - mod._server = new WebServer(mod.CommandReceived); + mod._server = new WebServer(mod.CommandReceived, mod.GetCommandList); mod._server.Start(); Console.ReadLine(); } diff --git a/TBConsole/UnityLogHandler.cs b/TBConsole/UnityLogHandler.cs index 21555c3..18d0a73 100644 --- a/TBConsole/UnityLogHandler.cs +++ b/TBConsole/UnityLogHandler.cs @@ -23,10 +23,10 @@ namespace TBConsole _original.LogException(exception, context); } - public async Task CollectLogMessages() + public void StartCollectingLogMessages() => _collectedLog = ""; + + public string FinishCollectingLogMessages() { - _collectedLog = ""; - await Task.Delay(1000); string ret = _collectedLog; _collectedLog = null; return ret; diff --git a/TBConsole/WebServer.cs b/TBConsole/WebServer.cs index b813b7d..991eb02 100644 --- a/TBConsole/WebServer.cs +++ b/TBConsole/WebServer.cs @@ -9,9 +9,15 @@ namespace TBConsole { private bool _running; private readonly HttpListener _listener = new HttpListener(); - private Func> _receiver; - public WebServer(Func> receiver) => _receiver = receiver; - + private readonly Func _commandReceiver; + private readonly Func _commandsListSender; + + public WebServer(Func commandReceiver, Func commandsListSender) + { + _commandReceiver = commandReceiver; + _commandsListSender = commandsListSender; + } + public void Start() { _running = true; @@ -33,7 +39,20 @@ namespace TBConsole try { var context = await _listener.GetContextAsync(); - string resp = await _receiver(await new StreamReader(context.Request.InputStream).ReadToEndAsync()); + string request = await new StreamReader(context.Request.InputStream).ReadToEndAsync(); + string resp; + switch (context.Request.Url.AbsolutePath) + { + case "/command": + resp = _commandReceiver(request); + break; + case "/commands": + resp = _commandsListSender(); + break; + default: + resp = ""; + break; + } string origin = context.Request.Headers["Origin"]; if (origin == "http://localhost:4200" || origin == "https://tbconsole.web.app") context.Response.AddHeader("Access-Control-Allow-Origin", origin);