use iterator for pending messages to process messages as they come in

This commit is contained in:
payonel 2015-09-11 21:20:37 -07:00
parent 3be687ef91
commit 62c6e78e91

View File

@ -228,10 +228,8 @@ function modem_host.processPendingMessages()
end end
modem_host.acceptPendingClients() modem_host.acceptPendingClients()
modem_host.recvPendingMessages()
for _,packet in ipairs(modem_host.messages) do
for _,packet in modem_host.allPendingMessages() do
if packet.type == 'modem_message' then if packet.type == 'modem_message' then
-- broadcast if no target -- broadcast if no target
if packet.target == 0 then if packet.target == 0 then
@ -243,8 +241,6 @@ function modem_host.processPendingMessages()
modem_host.host_shutdown = true modem_host.host_shutdown = true
end end
end end
modem_host.messages = {}
end end
function modem_host.acceptPendingClients() function modem_host.acceptPendingClients()
@ -286,34 +282,42 @@ function modem_host.acceptPendingClients()
end end
end end
function modem_host.recvPendingMessages() function modem_host.allPendingMessages()
if modem_host.hosting then local msgIt = function(...)
for source, client in pairs(modem_host.clients) do if #modem_host.messages > 0 then
local packet, err = modem_host.readPacket(client) return 0, table.remove(modem_host.messages, 1)
if packet then
modem_host.pushMessage(packet)
elseif err ~= "timeout" then
client:close()
modem_host.clients[source] = nil
end
end end
elseif modem_host.socket then
while true do if modem_host.hosting then
local packet, err = modem_host.readPacket(modem_host.socket) for source, client in pairs(modem_host.clients) do
if packet then local packet, err = modem_host.readPacket(client)
modem_host.pushMessage(packet) if packet then
else return 0, packet
if err ~= "timeout" then elseif err ~= "timeout" then
if not modem_host.host_shutdown then client:close()
error("modem host was unexpectedly lost") modem_host.clients[source] = nil
end end
modem_host.connected = false end
modem_host.connectMessageBoard() elseif modem_host.socket then
while true do
local packet, err = modem_host.readPacket(modem_host.socket)
if packet then
return 0, packet
else
if err ~= "timeout" then
if not modem_host.host_shutdown then
error("modem host was unexpectedly lost")
end
modem_host.connected = false
modem_host.connectMessageBoard()
end
break
end end
break
end end
end end
end end
return msgIt, nil, 0
end end
function modem_host.createNewMessageBoard() function modem_host.createNewMessageBoard()