complete simple connection implementation

This commit is contained in:
MihailRis
2024-11-27 12:10:59 +03:00
parent 3933baccd2
commit fb0f4bff52
4 changed files with 164 additions and 59 deletions
+55 -1
View File
@@ -39,13 +39,67 @@ static int l_get_binary(lua::State* L) {
static int l_connect(lua::State* L) {
std::string address = lua::require_string(L, 1);
int port = lua::tointeger(L, 2);
u64id_t id = engine->getNetwork().connect(address, port);
lua::pushvalue(L, 3);
auto callback = lua::create_lambda(L);
u64id_t id = engine->getNetwork().connect(address, port, [callback](u64id_t id) {
callback({id});
});
return lua::pushinteger(L, id);
}
static int l_send(lua::State* L) {
u64id_t id = lua::tointeger(L, 1);
auto connection = engine->getNetwork().getConnection(id);
if (lua::istable(L, 2)) {
lua::pushvalue(L, 2);
size_t size = lua::objlen(L, 2);
util::Buffer<char> buffer(size);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1);
buffer[i] = lua::tointeger(L, -1);
lua::pop(L);
}
lua::pop(L);
connection->send(buffer.data(), size);
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 2)) {
connection->send(
reinterpret_cast<char*>(bytes->data().data()), bytes->data().size()
);
}
return 0;
}
static int l_recv(lua::State* L) {
u64id_t id = lua::tointeger(L, 1);
int length = lua::tointeger(L, 2);
auto connection = engine->getNetwork().getConnection(id);
util::Buffer<char> buffer(glm::min(length, connection->available()));
int size = connection->recv(buffer.data(), length);
if (size == -1) {
return 0;
}
if (lua::toboolean(L, 3)) {
lua::createtable(L, size, 0);
for (size_t i = 0; i < size; i++) {
lua::pushinteger(L, buffer[i] & 0xFF);
lua::rawseti(L, i+1);
}
} else {
lua::newuserdata<lua::LuaBytearray>(L, size);
auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1);
bytearray->data().reserve(size);
std::memcpy(bytearray->data().data(), buffer.data(), size);
}
return 1;
}
const luaL_Reg networklib[] = {
{"get", lua::wrap<l_get>},
{"get_binary", lua::wrap<l_get_binary>},
{"__connect", lua::wrap<l_connect>},
{"__send", lua::wrap<l_send>},
{"__recv", lua::wrap<l_recv>},
{NULL, NULL}
};