add socket:is_alive, :is_connected, serversocket:is_open

This commit is contained in:
MihailRis 2024-11-27 17:46:09 +03:00
parent 34974c4bb3
commit 951d2fd1f8
2 changed files with 36 additions and 2 deletions

View File

@ -37,9 +37,11 @@ end
local Socket = {__index={
send=function(self, bytes) return network.__send(self.id, bytes) end,
recv=function(self, len, usetable) return network.__recv(self.id, len, usetable) end,
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,
is_alive=function(self) return network.__is_alive(self.id) end,
is_connected=function(self) return network.__is_connected(self.id) end,
}}
network.tcp_connect = function(address, port, callback)
@ -52,6 +54,7 @@ end
local ServerSocket = {__index={
close=function(self) return network.__closeserver(self.id) end,
is_open=function(self) return network.__is_serveropen(self.id) end,
}}
network.tcp_open = function(port, handler)

View File

@ -131,6 +131,34 @@ static int l_open(lua::State* L) {
return lua::pushinteger(L, id);
}
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
);
}
return lua::pushboolean(L, false);
}
static int l_is_connected(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::CONNECTED
);
}
return lua::pushboolean(L, false);
}
static int l_is_serveropen(lua::State* L) {
u64id_t id = lua::tointeger(L, 1);
if (auto server = engine->getNetwork().getServer(id)) {
return lua::pushboolean(L, server->isOpen());
}
return lua::pushboolean(L, false);
}
const luaL_Reg networklib[] = {
{"get", lua::wrap<l_get>},
{"get_binary", lua::wrap<l_get_binary>},
@ -140,5 +168,8 @@ const luaL_Reg networklib[] = {
{"__close", lua::wrap<l_close>},
{"__send", lua::wrap<l_send>},
{"__recv", lua::wrap<l_recv>},
{"__is_alive", lua::wrap<l_is_alive>},
{"__is_connected", lua::wrap<l_is_connected>},
{"__is_serveropen", lua::wrap<l_is_serveropen>},
{NULL, NULL}
};