From b8944f86c6be48795dcf4b10cc3c59973d037356 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 15 Jan 2025 05:27:10 +0300 Subject: [PATCH] refactor vc coroutines error-handling & update socket:is_alive(), socket:send(...) --- res/scripts/stdlib.lua | 16 +++++----------- src/logic/scripting/lua/libs/libnetwork.cpp | 7 +++++-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index c9f38835..71ba2f4f 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -413,15 +413,9 @@ end local __vc_coroutines = {} local __vc_named_coroutines = {} local __vc_next_coroutine = 1 -local __vc_coroutine_error = nil function __vc_start_coroutine(chunk) - local co = coroutine.create(function() - local status, err = pcall(chunk) - if not status then - __vc_coroutine_error = err - end - end) + local co = coroutine.create(chunk) local id = __vc_next_coroutine __vc_next_coroutine = __vc_next_coroutine + 1 __vc_coroutines[id] = co @@ -431,10 +425,10 @@ end function __vc_resume_coroutine(id) local co = __vc_coroutines[id] if co then - coroutine.resume(co) - if __vc_coroutine_error then - debug.error(__vc_coroutine_error) - error(__vc_coroutine_error) + local success, err = coroutine.resume(co) + if not success then + debug.error(err) + error(err) end return coroutine.status(co) ~= "dead" end diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index d1edf185..304cf5c8 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -89,7 +89,8 @@ static int l_closeserver(lua::State* L) { static int l_send(lua::State* L) { u64id_t id = lua::tointeger(L, 1); auto connection = engine->getNetwork().getConnection(id); - if (connection == nullptr) { + if (connection == nullptr || + connection->getState() == network::ConnectionState::CLOSED) { return 0; } if (lua::istable(L, 2)) { @@ -167,7 +168,9 @@ static int l_is_alive(lua::State* L) { u64id_t id = lua::tointeger(L, 1); if (auto connection = engine->getNetwork().getConnection(id)) { return lua::pushboolean( - L, connection->getState() != network::ConnectionState::CLOSED + L, + connection->getState() != network::ConnectionState::CLOSED || + connection->available() > 0 ); } return lua::pushboolean(L, false);