refactor vc coroutines error-handling & update socket:is_alive(), socket:send(...)

This commit is contained in:
MihailRis 2025-01-15 05:27:10 +03:00
parent 00ce65ef66
commit b8944f86c6
2 changed files with 10 additions and 13 deletions

View File

@ -413,15 +413,9 @@ end
local __vc_coroutines = {} local __vc_coroutines = {}
local __vc_named_coroutines = {} local __vc_named_coroutines = {}
local __vc_next_coroutine = 1 local __vc_next_coroutine = 1
local __vc_coroutine_error = nil
function __vc_start_coroutine(chunk) function __vc_start_coroutine(chunk)
local co = coroutine.create(function() local co = coroutine.create(chunk)
local status, err = pcall(chunk)
if not status then
__vc_coroutine_error = err
end
end)
local id = __vc_next_coroutine local id = __vc_next_coroutine
__vc_next_coroutine = __vc_next_coroutine + 1 __vc_next_coroutine = __vc_next_coroutine + 1
__vc_coroutines[id] = co __vc_coroutines[id] = co
@ -431,10 +425,10 @@ end
function __vc_resume_coroutine(id) function __vc_resume_coroutine(id)
local co = __vc_coroutines[id] local co = __vc_coroutines[id]
if co then if co then
coroutine.resume(co) local success, err = coroutine.resume(co)
if __vc_coroutine_error then if not success then
debug.error(__vc_coroutine_error) debug.error(err)
error(__vc_coroutine_error) error(err)
end end
return coroutine.status(co) ~= "dead" return coroutine.status(co) ~= "dead"
end end

View File

@ -89,7 +89,8 @@ static int l_closeserver(lua::State* L) {
static int l_send(lua::State* L) { static int l_send(lua::State* L) {
u64id_t id = lua::tointeger(L, 1); u64id_t id = lua::tointeger(L, 1);
auto connection = engine->getNetwork().getConnection(id); auto connection = engine->getNetwork().getConnection(id);
if (connection == nullptr) { if (connection == nullptr ||
connection->getState() == network::ConnectionState::CLOSED) {
return 0; return 0;
} }
if (lua::istable(L, 2)) { if (lua::istable(L, 2)) {
@ -167,7 +168,9 @@ static int l_is_alive(lua::State* L) {
u64id_t id = lua::tointeger(L, 1); u64id_t id = lua::tointeger(L, 1);
if (auto connection = engine->getNetwork().getConnection(id)) { if (auto connection = engine->getNetwork().getConnection(id)) {
return lua::pushboolean( return lua::pushboolean(
L, connection->getState() != network::ConnectionState::CLOSED L,
connection->getState() != network::ConnectionState::CLOSED ||
connection->available() > 0
); );
} }
return lua::pushboolean(L, false); return lua::pushboolean(L, false);