Skip to content

Commit

Permalink
add socket:available() --> int
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Jan 7, 2025
1 parent e94b70f commit 13de581
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/en/scripting/builtins/libnetwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions doc/ru/scripting/builtins/libnetwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ socket:recv(
-- Закрывает соединение
socket:close()

-- Возвращает количество доступных для чтения байт данных
socket:available() --> int

-- Проверяет, что сокет существует и не закрыт.
socket:is_alive() --> bool

Expand Down
1 change: 1 addition & 0 deletions res/scripts/classes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions res/scripts/stdlib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions src/logic/scripting/lua/libs/libnetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -200,6 +208,7 @@ const luaL_Reg networklib[] = {
{"__close", lua::wrap<l_close>},
{"__send", lua::wrap<l_send>},
{"__recv", lua::wrap<l_recv>},
{"__available", lua::wrap<l_available>},
{"__is_alive", lua::wrap<l_is_alive>},
{"__is_connected", lua::wrap<l_is_connected>},
{"__get_address", lua::wrap<l_get_address>},
Expand Down

0 comments on commit 13de581

Please sign in to comment.