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,12 +282,17 @@ function modem_host.acceptPendingClients()
end end
end end
function modem_host.recvPendingMessages() function modem_host.allPendingMessages()
local msgIt = function(...)
if #modem_host.messages > 0 then
return 0, table.remove(modem_host.messages, 1)
end
if modem_host.hosting then if modem_host.hosting then
for source, client in pairs(modem_host.clients) do for source, client in pairs(modem_host.clients) do
local packet, err = modem_host.readPacket(client) local packet, err = modem_host.readPacket(client)
if packet then if packet then
modem_host.pushMessage(packet) return 0, packet
elseif err ~= "timeout" then elseif err ~= "timeout" then
client:close() client:close()
modem_host.clients[source] = nil modem_host.clients[source] = nil
@ -301,7 +302,7 @@ function modem_host.recvPendingMessages()
while true do while true do
local packet, err = modem_host.readPacket(modem_host.socket) local packet, err = modem_host.readPacket(modem_host.socket)
if packet then if packet then
modem_host.pushMessage(packet) return 0, packet
else else
if err ~= "timeout" then if err ~= "timeout" then
if not modem_host.host_shutdown then if not modem_host.host_shutdown then
@ -316,6 +317,9 @@ function modem_host.recvPendingMessages()
end end
end end
return msgIt, nil, 0
end
function modem_host.createNewMessageBoard() function modem_host.createNewMessageBoard()
local why local why
modem_host.socket, why = socket.bind(modem_host.comms_ip, modem_host.comms_port) modem_host.socket, why = socket.bind(modem_host.comms_ip, modem_host.comms_port)