fix 'resume dead coroutine' for client
This commit is contained in:
+33
-18
@@ -46,14 +46,6 @@ local Socket = {__index={
|
||||
get_address=function(self) return network.__get_address(self.id) end,
|
||||
}}
|
||||
|
||||
network.tcp_connect = function(address, port, callback)
|
||||
local socket = setmetatable({id=0}, Socket)
|
||||
socket.id = network.__connect(address, port, function(id)
|
||||
callback(socket)
|
||||
end)
|
||||
return socket
|
||||
end
|
||||
|
||||
local ServerSocket = {__index={
|
||||
close=function(self) return network.__closeserver(self.id) end,
|
||||
is_open=function(self) return network.__is_serveropen(self.id) end,
|
||||
@@ -62,34 +54,57 @@ local ServerSocket = {__index={
|
||||
|
||||
|
||||
local _tcp_server_callbacks = {}
|
||||
local _tcp_client_callbacks = {}
|
||||
|
||||
network.tcp_open = function (port, handler)
|
||||
local socket = setmetatable({id=network.__open(port)}, ServerSocket)
|
||||
|
||||
_tcp_server_callbacks[socket.id] = function(id)
|
||||
_tcp_server_callbacks[socket.id] = function(id)
|
||||
handler(setmetatable({id=id}, Socket))
|
||||
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]
|
||||
network.tcp_connect = function(address, port, callback)
|
||||
local socket = setmetatable({id=0}, Socket)
|
||||
socket.id = network.__connect(address, port)
|
||||
_tcp_client_callbacks[socket.id] = function() callback(socket) end
|
||||
return socket
|
||||
end
|
||||
|
||||
if callback then
|
||||
callback(cid)
|
||||
network.__process_events = function()
|
||||
local CLIENT_CONNECTED = 1
|
||||
local CONNECTED_TO_SERVER = 2
|
||||
|
||||
local cleaned = false
|
||||
local events = network.__pull_events()
|
||||
for i, event in ipairs(events) do
|
||||
local etype, sid, cid = unpack(event)
|
||||
|
||||
if etype == CLIENT_CONNECTED then
|
||||
local callback = _tcp_server_callbacks[sid]
|
||||
if callback then
|
||||
callback(cid)
|
||||
end
|
||||
elseif etype == CONNECTED_TO_SERVER then
|
||||
local callback = _tcp_client_callbacks[cid]
|
||||
if callback then
|
||||
callback()
|
||||
end
|
||||
end
|
||||
|
||||
-- remove dead servers
|
||||
if not cleaned then
|
||||
for sid, callback in pairs(_tcp_server_callbacks) do
|
||||
for sid, _ in pairs(_tcp_server_callbacks) do
|
||||
if not network.__is_serveropen(sid) then
|
||||
_tcp_server_callbacks[sid] = nil
|
||||
end
|
||||
end
|
||||
for cid, _ in pairs(_tcp_client_callbacks) do
|
||||
if not network.__is_alive(cid) then
|
||||
_tcp_client_callbacks[cid] = nil
|
||||
end
|
||||
end
|
||||
cleaned = true
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user