fix "cannot resume dead coroutine" for servers

This commit is contained in:
MihailRis
2025-07-26 17:08:45 +03:00
parent 8489c36df7
commit 1bd3463488
7 changed files with 78 additions and 24 deletions
+32 -3
View File
@@ -60,8 +60,37 @@ local ServerSocket = {__index={
get_port=function(self) return network.__get_serverport(self.id) end,
}}
network.tcp_open = function(port, handler)
return setmetatable({id=network.__open(port, function(id)
local _tcp_server_callbacks = {}
network.tcp_open = function (port, handler)
local socket = setmetatable({id=network.__open(port)}, ServerSocket)
_tcp_server_callbacks[socket.id] = function(id)
handler(setmetatable({id=id}, Socket))
end)}, ServerSocket)
end
return socket
end
network.__pull_events = function()
local cleaned = false
local connections = network.__pull_connections()
for i, pair in ipairs(connections) do
local sid, cid = unpack(pair)
local callback = _tcp_server_callbacks[sid]
if callback then
callback(cid)
end
-- remove dead servers
if not cleaned then
for sid, callback in pairs(_tcp_server_callbacks) do
if not network.__is_serveropen(sid) then
_tcp_server_callbacks[sid] = nil
end
end
cleaned = true
end
end
end