Posted 01 August 2012 - 05:18 PM
Hi,
I'd like to write a small server function and actually have all of the saving, loading, sending functions etc. ready. The only problem there is, is that I do not understand how I can ensure that I do not miss any incoming message (aka any event at all).
My last idea was to use two functions called "collectMessages()" and "handleMessages()".
collectMessages() was supposed to loop indefinitely adding all of the occurring events to a global event queue which would then be emptied by handleMessages(). Since collectMessages() would've been running at any point of the program, functions running inside handleMessages() would not make me miss any events.
Sadly waitForAny does not break the loop I use in collectMessages() and thus this setup will never reach the point where it will handle the messages.
Any suggestions on how I can achieve my goal other than this?
Cyclonit
I'd like to write a small server function and actually have all of the saving, loading, sending functions etc. ready. The only problem there is, is that I do not understand how I can ensure that I do not miss any incoming message (aka any event at all).
My last idea was to use two functions called "collectMessages()" and "handleMessages()".
collectMessages() was supposed to loop indefinitely adding all of the occurring events to a global event queue which would then be emptied by handleMessages(). Since collectMessages() would've been running at any point of the program, functions running inside handleMessages() would not make me miss any events.
function collectMessages()
local event, p1, p2, p3 = os.pullEvent()
while (true) do
messageQueue = cf.list.pushBack(messageQueue, event)
-- next event
event, p1, p2, p3 = os.pullEvent()
end
end
function handleMessages(currentQueue)
if (currentQueue == nil) then
return
end
local message = cf.list.popFront(currentQueue)
while (message ~= nil) do
-- do something here
end
end
function serve()
while (true) do
currentQueue = messageQueue
messageQueue = cf.list.new()
parallel.waitForAny(collectMessages(), handleMessages(currentQueue))
print("*")
end
end
Sadly waitForAny does not break the loop I use in collectMessages() and thus this setup will never reach the point where it will handle the messages.
Any suggestions on how I can achieve my goal other than this?
Cyclonit