You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.5 KiB
69 lines
1.5 KiB
local coroutine = require("coroutine")
|
|
local net = require("net")
|
|
local table = require("table")
|
|
|
|
function eventLoop(...)
|
|
-- Put in here in for later usage
|
|
local done = {}
|
|
|
|
-- Get running coroutines
|
|
local running = {...}
|
|
local canRun = true
|
|
local i = 1
|
|
|
|
while canRun do
|
|
while coroutine.status(running[i]) == "dead" do
|
|
table.insert(done, #done+1, table.remove(running, i))
|
|
if #running == 0 then
|
|
goto done
|
|
end
|
|
end
|
|
|
|
if #running == 0 then
|
|
canRun = false
|
|
else
|
|
_, pkt = coroutine.resume(running[i])
|
|
if pkt ~= nil then print("Received: "..pkt.msg) end
|
|
i = (i % #running) + 1
|
|
end
|
|
|
|
end
|
|
::done::
|
|
end
|
|
|
|
-- TODO finish this
|
|
function betterLoop(...)
|
|
-- Get running coroutines
|
|
local done = {}
|
|
local results = {}
|
|
local running = {...}
|
|
local canRun = true
|
|
local i = 1
|
|
|
|
while true do
|
|
local coro = running[i][1]
|
|
status, t = coroutine.resume(coro, slice(running[i], 2, #(running[i])))
|
|
if t ~= nil then
|
|
print(#t)
|
|
end
|
|
i = (i % #running) + 1
|
|
while coroutine.status(coro) == "dead" do
|
|
table.insert(results, #results+1, table.remove(running, i)) -- can do this better
|
|
if #running == 0 then
|
|
goto done
|
|
end
|
|
end
|
|
end
|
|
::done::
|
|
return results
|
|
end
|
|
|
|
function simple()
|
|
print("Foo")
|
|
coroutine.yield()
|
|
print("Done")
|
|
return "a"
|
|
end
|
|
|
|
local coro = coroutine.create(simple)
|
|
betterLoop({coro})
|