fix 'resume dead coroutine' for client

This commit is contained in:
MihailRis
2025-07-26 18:08:10 +03:00
parent aa3d1bb2e3
commit 47c97ccd46
3 changed files with 63 additions and 44 deletions
+29 -25
View File
@@ -89,20 +89,6 @@ static int l_post(lua::State* L, network::Network& network) {
return 0;
}
static int l_connect(lua::State* L, network::Network& network) {
std::string address = lua::require_string(L, 1);
int port = lua::tointeger(L, 2);
lua::pushvalue(L, 3);
auto callback = lua::create_lambda_nothrow(L);
u64id_t id = network.connect(address, port, [callback](u64id_t id) {
engine->postRunnable([=]() {
callback({id});
});
});
return lua::pushinteger(L, id);
}
static int l_close(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto connection = network.getConnection(id)) {
@@ -182,17 +168,32 @@ static int l_available(lua::State* L, network::Network& network) {
return 0;
}
struct ConnectionEvent {
enum NetworkEventType {
CLIENT_CONNECTED = 1,
CONNECTED_TO_SERVER
};
struct NetworkEvent {
NetworkEventType type;
u64id_t server;
u64id_t client;
};
static std::vector<ConnectionEvent> clients_queue {};
static std::vector<NetworkEvent> events_queue {};
static int l_connect(lua::State* L, network::Network& network) {
std::string address = lua::require_string(L, 1);
int port = lua::tointeger(L, 2);
u64id_t id = network.connect(address, port, [](u64id_t cid) {
events_queue.push_back({CONNECTED_TO_SERVER, 0, cid});
});
return lua::pushinteger(L, id);
}
static int l_open(lua::State* L, network::Network& network) {
int port = lua::tointeger(L, 1);
u64id_t id = network.openServer(port, [](u64id_t sid, u64id_t id) {
clients_queue.push_back({sid, id});
events_queue.push_back({CLIENT_CONNECTED, sid, id});
});
return lua::pushinteger(L, id);
}
@@ -253,20 +254,23 @@ static int l_get_total_download(lua::State* L, network::Network& network) {
return lua::pushinteger(L, network.getTotalDownload());
}
static int l_pull_connections(lua::State* L, network::Network& network) {
lua::createtable(L, clients_queue.size(), 0);
for (size_t i = 0; i < clients_queue.size(); i++) {
lua::createtable(L, 2, 0);
static int l_pull_events(lua::State* L, network::Network& network) {
lua::createtable(L, events_queue.size(), 0);
for (size_t i = 0; i < events_queue.size(); i++) {
lua::createtable(L, 3, 0);
lua::pushinteger(L, clients_queue[i].server);
lua::pushinteger(L, events_queue[i].type);
lua::rawseti(L, 1);
lua::pushinteger(L, clients_queue[i].client);
lua::pushinteger(L, events_queue[i].server);
lua::rawseti(L, 2);
lua::pushinteger(L, events_queue[i].client);
lua::rawseti(L, 3);
lua::rawseti(L, i + 1);
}
clients_queue.clear();
events_queue.clear();
return 1;
}
@@ -293,7 +297,7 @@ const luaL_Reg networklib[] = {
{"post", wrap<l_post>},
{"get_total_upload", wrap<l_get_total_upload>},
{"get_total_download", wrap<l_get_total_download>},
{"__pull_connections", wrap<l_pull_connections>},
{"__pull_events", wrap<l_pull_events>},
{"__open", wrap<l_open>},
{"__closeserver", wrap<l_closeserver>},
{"__connect", wrap<l_connect>},