From a2aecde49c36fc92f767f585792534687417f226 Mon Sep 17 00:00:00 2001 From: simadude Date: Wed, 21 Feb 2024 13:21:42 +1000 Subject: [PATCH 1/2] Fix for "Port already in use" error when trying to reopen WebSocket server on the same port. --- src/apis/http.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/apis/http.cpp b/src/apis/http.cpp index 15eb747f..9ef6c6a9 100644 --- a/src/apis/http.cpp +++ b/src/apis/http.cpp @@ -1364,7 +1364,9 @@ static int websocket_server_listen(lua_State *L) { */ static int websocket_server_close(lua_State *L) { lastCFunction = __func__; + Computer * comp = get_comp(L); websocket_server::Factory * f = *(websocket_server::Factory**)lua_touserdata(L, lua_upvalueindex(1)); + comp->openWebsocketServers.erase(f->srv->port()); if (f == NULL) return 0; f->srv->stop(); delete f->srv; @@ -1374,7 +1376,9 @@ static int websocket_server_close(lua_State *L) { static int websocket_server_free(lua_State *L) { lastCFunction = __func__; + Computer * comp = get_comp(L); websocket_server::Factory * f = *(websocket_server::Factory**)lua_touserdata(L, 1); + comp->openWebsocketServers.erase(f->srv->port()); if (f == NULL) return 0; f->srv->stop(); delete f->srv; From 1ce3dce40ba0a53895b096313233f2b38df7c82a Mon Sep 17 00:00:00 2001 From: simadude Date: Sat, 24 Feb 2024 13:00:52 +1000 Subject: [PATCH 2/2] New term.forceUpdate() function --- api/Terminal.hpp | 1 + src/apis/term.cpp | 10 ++++++++++ src/peripheral/monitor.cpp | 8 ++++++++ src/peripheral/monitor.hpp | 1 + src/termsupport.cpp | 3 ++- 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/api/Terminal.hpp b/api/Terminal.hpp index e97060f3..d310563a 100644 --- a/api/Terminal.hpp +++ b/api/Terminal.hpp @@ -152,6 +152,7 @@ class Terminal { // The following fields are available in API version 10.1 and later. bool frozen = false; // Whether the terminal should stop rendering + bool forcedUpdate = false; // Ignores `frozen` to render the terminal once // The following fields are available in API version 10.2 and later. std::list mouseButtonOrder; // An ordered list of mouse buttons that have been pressed diff --git a/src/apis/term.cpp b/src/apis/term.cpp index c8d13bcd..34b7e4ba 100644 --- a/src/apis/term.cpp +++ b/src/apis/term.cpp @@ -649,6 +649,15 @@ static int term_getFrozen(lua_State *L) { return 1; } +static int term_forceUpdate(lua_State *L) { + lastCFunction = __func__; + Terminal * term = get_comp(L)->term; + if (term == NULL) return 0; + std::lock_guard lock(term->locked); + term->forcedUpdate = true; + return 0; +} + /* export */ int term_benchmark(lua_State *L) { lastCFunction = __func__; if (get_comp(L)->term == NULL) return 0; @@ -695,6 +704,7 @@ static luaL_Reg term_reg[] = { {"relativeMouse", term_relativeMouse}, {"setFrozen", term_setFrozen}, {"getFrozen", term_getFrozen}, + {"forceUpdate", term_forceUpdate}, {NULL, NULL} }; diff --git a/src/peripheral/monitor.cpp b/src/peripheral/monitor.cpp index 5ad0f507..e5fba4cf 100644 --- a/src/peripheral/monitor.cpp +++ b/src/peripheral/monitor.cpp @@ -569,6 +569,14 @@ int monitor::setBlockSize(lua_State *L) { return 0; } +int monitor::forceUpdate(lua_State *L) { + lastCFunction = __func__; + if (term == NULL) return 0; + std::lock_guard lock(term->locked); + term->forcedUpdate = true; + return 0; +} + int monitor::call(lua_State *L, const char * method) { std::string m(method); if (m == "write") return write(L); diff --git a/src/peripheral/monitor.hpp b/src/peripheral/monitor.hpp index 553cc1f0..269d92e3 100644 --- a/src/peripheral/monitor.hpp +++ b/src/peripheral/monitor.hpp @@ -51,6 +51,7 @@ class monitor : public peripheral { int getFrozen(lua_State *L); int setSize(lua_State *L); int setBlockSize(lua_State *L); + int forceUpdate(lua_State *L); public: Terminal * term; static library_t methods; diff --git a/src/termsupport.cpp b/src/termsupport.cpp index 04e5187a..0aabb9a5 100644 --- a/src/termsupport.cpp +++ b/src/termsupport.cpp @@ -582,8 +582,9 @@ static bool renderTerminal(Terminal * term, bool& pushEvent) { term->last_blink = std::chrono::high_resolution_clock::now(); term->changed = true; } - if (term->frozen) return false; + if (term->frozen && !term->forcedUpdate) return false; changed = term->changed; + term->forcedUpdate = false; } try { term->render();