diff --git a/doc/en/scripting/builtins/libnetwork.md b/doc/en/scripting/builtins/libnetwork.md index 48caefbdc..a3546336a 100644 --- a/doc/en/scripting/builtins/libnetwork.md +++ b/doc/en/scripting/builtins/libnetwork.md @@ -54,6 +54,9 @@ socket:recv( -- Closes the connection socket:close() +-- Returns the number of data bytes available for reading +socket:available() --> int + -- Checks that the socket exists and is not closed. socket:is_alive() --> bool diff --git a/doc/ru/scripting/builtins/libnetwork.md b/doc/ru/scripting/builtins/libnetwork.md index 7fedf2bd8..0770ea674 100644 --- a/doc/ru/scripting/builtins/libnetwork.md +++ b/doc/ru/scripting/builtins/libnetwork.md @@ -54,6 +54,9 @@ socket:recv( -- Закрывает соединение socket:close() +-- Возвращает количество доступных для чтения байт данных +socket:available() --> int + -- Проверяет, что сокет существует и не закрыт. socket:is_alive() --> bool diff --git a/res/scripts/classes.lua b/res/scripts/classes.lua index 909717cf1..0741c89b1 100644 --- a/res/scripts/classes.lua +++ b/res/scripts/classes.lua @@ -40,6 +40,7 @@ local Socket = {__index={ send=function(self, ...) return network.__send(self.id, ...) end, recv=function(self, ...) return network.__recv(self.id, ...) end, close=function(self) return network.__close(self.id) end, + available=function(self) return network.__available(self.id) or 0 end, is_alive=function(self) return network.__is_alive(self.id) end, is_connected=function(self) return network.__is_connected(self.id) end, get_address=function(self) return network.__get_address(self.id) end, diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 1b19b330c..f376e2106 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -424,6 +424,7 @@ function __vc_resume_coroutine(id) if co then coroutine.resume(co) if __vc_coroutine_error then + debug.error(__vc_coroutine_error) error(__vc_coroutine_error) end return coroutine.status(co) ~= "dead" diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index 0cf7e538e..b3417a13f 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -123,6 +123,14 @@ static int l_recv(lua::State* L) { return 1; } +static int l_available(lua::State* L) { + u64id_t id = lua::tointeger(L, 1); + if (auto connection = engine->getNetwork().getConnection(id)) { + return lua::pushinteger(L, connection->available()); + } + return 0; +} + static int l_open(lua::State* L) { int port = lua::tointeger(L, 1); lua::pushvalue(L, 2); @@ -200,6 +208,7 @@ const luaL_Reg networklib[] = { {"__close", lua::wrap}, {"__send", lua::wrap}, {"__recv", lua::wrap}, + {"__available", lua::wrap}, {"__is_alive", lua::wrap}, {"__is_connected", lua::wrap}, {"__get_address", lua::wrap},