From 233bc3174c8751baf641571ccc7161c411c73638 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 27 Nov 2024 12:51:01 +0300 Subject: [PATCH] add network.__close --- src/logic/scripting/lua/libs/libnetwork.cpp | 17 ++++++++++++++++- src/network/Network.cpp | 5 ++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index 2245dc10..df60b741 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -47,10 +47,21 @@ static int l_connect(lua::State* L) { return lua::pushinteger(L, id); } + +static int l_close(lua::State* L) { + u64id_t id = lua::tointeger(L, 1); + if (auto connection = engine->getNetwork().getConnection(id)) { + connection->close(); + } + return 0; +} + static int l_send(lua::State* L) { u64id_t id = lua::tointeger(L, 1); auto connection = engine->getNetwork().getConnection(id); - + if (connection == nullptr) { + return 0; + } if (lua::istable(L, 2)) { lua::pushvalue(L, 2); size_t size = lua::objlen(L, 2); @@ -74,6 +85,9 @@ 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); + if (connection == nullptr) { + return 0; + } util::Buffer buffer(glm::min(length, connection->available())); int size = connection->recv(buffer.data(), length); @@ -99,6 +113,7 @@ const luaL_Reg networklib[] = { {"get", lua::wrap}, {"get_binary", lua::wrap}, {"__connect", lua::wrap}, + {"__close", lua::wrap}, {"__send", lua::wrap}, {"__recv", lua::wrap}, {NULL, NULL} diff --git a/src/network/Network.cpp b/src/network/Network.cpp index 23d0ec87..fa46b682 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -296,7 +296,9 @@ public: shutdown(descriptor, 2); closesocket(descriptor); } - thread->join(); + if (thread) { + thread->join(); + } freeaddrinfo(addr); } @@ -369,6 +371,7 @@ public: closesocket(descriptor); } thread->join(); + thread = nullptr; } size_t getTotalUpload() const override {