Browse Source

Probably fixed async.pullFiltered

master
BuildTools 1 year ago
parent
commit
9a36ccbc63
  1. 1
      .gitignore
  2. 1
      .vscode/settings.json
  3. 2
      get-all.lua
  4. 101
      lib/async.lua
  5. 40
      lib/liblua.lua
  6. 33
      lib/net.lua

1
.gitignore

@ -1,2 +1,3 @@
gp/
windowmanager/
test*

1
.vscode/settings.json

@ -0,0 +1 @@
123,125

2
get-all.lua

@ -70,4 +70,4 @@ sandbox_env = {
-- -- Run downloaded file
-- local c = fileName
-- for _, i in ipairs(newArgs) do c = c.." "..i end
-- shell.execute(c)
-- shell.execute(c)function split(s, sep)

101
lib/async.lua

@ -7,6 +7,101 @@ local table = require("table")
local async = {}
-- TODO FINISH THIS
-- An async wrapper for event.pullFiltered
function async.pullFiltered(...)
-- Check if we're in a coroutine or not
_, status = coroutine.running()
local start = computer.uptime()
local args = {...}
-- Break on interrupts
function f(name, ...)
-- if args[1] happens to be "interrupted" already the second won't trigger
if name == args[1] or name == "interrupted" then
return true
end
end
if type(args[1]) == "string" then
-- Check if not in a coroutine
if not status then return event.pullFiltered(f, ...) end
print("Testing")
repeat
coroutine.yield()
tmp = {event.pullFiltered(.01, f, ...)}
until #tmp > 0
else
local timeout = nil
if type(args[1]) == "number" then
timeout = args[1]
args = ll.slice({...}, 2)
-- For speed purposes check up here even though it's more repeated code
elseif type(args[1]) == "function" then
-- Wrap other function so this will handle interrupts
function g(name, ...)
local a = ll.slice(args, 2)
-- Handle interrupt
if name == "interrupted" then
return true
end
-- Otherwise run other function
return args[1](name, ...)
end
if timeout ~= nil then
-- Check if not in a coroutine
if not status then
return event.pullFiltered(timeout, g, table.unpack(args))
end
repeat
coroutine.yield()
tmp = {event.pullFiltered(.01, g, table.unpack(args))}
until #tmp > 0 or computer.uptime() - start >= timeout
else
-- Check if not in a coroutine
if not status then
return event.pullFiltered(g, ...)
end
repeat
coroutine.yield()
tmp = {event.pullFiltered(.01, g, table.unpack(args))}
until #tmp > 0
end
else
if timeout ~= nil then
-- Check if not in a coroutine
if not status then
return event.pullFiltered(timeout, f, table.unpack(args))
end
repeat
coroutine.yield()
tmp = {event.pullFiltered(.01, f, table.unpack(args))}
until #tmp > 0 or computer.uptime() - start >= timeout
else
-- Check if not in a coroutine
if not status then
return event.pullFiltered(f, table.unpack(args))
end
repeat
coroutine.yield()
tmp = {event.pullFiltered(.01, f, table.unpack(args))}
until #tmp > 0
end
end
end
return table.unpack(tmp)
end
-- An alias for async.pullFiltered which does all of the logic for both functions
async.pull = async.pullFiltered
-- Sleep in a coroutine friendly way
function async.sleep(s)
local _, status = coroutine.running()
@ -67,11 +162,11 @@ function async.roundRobin(loop, ignoreError)
if not status and not ignoreError then
io.stderr:write(results[1])
return
-- Don't exit but print the bad message
-- Don't exit but print the bad message
elseif not status then
print(results[1])
end
-- Stop running dead coroutines and add the results
if coroutine.status(b.coro) == "dead" then
b.results = results
@ -85,4 +180,4 @@ end
return async
-- wget -f http://mc.bashed.rocks:13699/lib/async.lua async.lua
-- wget -f http://mc.bashed.rocks:13699/lib/async.lua /usr/lib/async.lua

40
lib/liblua.lua

@ -97,43 +97,9 @@ function ll.tprint(...)
end
end
-- An async wrapper for event.pullFiltered
function ll.pullFiltered(...)
-- Check if we're in a coroutine or not
_, status = coroutine.running()
-- Check use regular if we don't need to block
if status then return event.pullFiltered(...) end
local start = computer.uptime()
local args = {...}
if type(args[1]) == "string" then
repeat
coroutine.yield()
tmp = {event.pullFiltered(.01, ...)}
until #tmp > 0
else
checkArg(1, args[1], "number")
local timeout = args[1]
local args = ll.slice({...}, 2)
-- For speed purposes check up here even though it's more repeated code
if type(args[1]) == "function" then
repeat
coroutine.yield()
tmp = {event.pullFiltered(.01, table.unpack(args))}
until #tmp > 0 or computer.uptime() - start >= timeout
else
repeat
coroutine.yield()
tmp = {event.pull(.01, table.unpack(args))}
until #tmp > 0 or computer.uptime() - start >= timeout
end
end
end
-- An alias for ll.pullFiltered which does all of the logic for both functions
function ll.pull(...)
ll.pullFiltered(...)
function ll.round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
return ll

33
lib/net.lua

@ -1,7 +1,7 @@
local async = require("async")
local computer = require("computer")
local component = require("component")
local coroutine = require("coroutine")
local event = require("event")
local dns = require("dns")
local serial = require("serialization")
local table = require("table")
@ -111,17 +111,15 @@ function net.recv(t)
addr = nil,
fuzzy = false,
timeout = nil,
nonblocking = false,
filter = nil,
open = true,
}})
local port, addr, timeout, nonblocking, filter, open =
local port, addr, timeout, filter, open =
t[1] or t.port,
t[2] or t.addr,
t[3] or t.timeout,
t[4] or t.nonblocking,
t[5] or t.filter,
t[6] or t.open
t[4] or t.filter,
t[5] or t.open
if addr ~= nil then
addr = checkAddr(addr)
@ -136,7 +134,6 @@ function net.recv(t)
-- Basic filter function used to call user defined filter functions
function f(name, ...)
-- Get rid of all non modem messages
if name == "interrupted" then return true end
if name ~= "modem_message" then return false end
local arg = {...}
local resAddress, resPort = arg[2], arg[3]
@ -159,27 +156,11 @@ function net.recv(t)
end
end
----------------
-- Supply timeout
if timeout ~= nil then
if nonblocking then
local start = computer.uptime()
repeat
coroutine.yield()
tmp = {event.pullFiltered(.05, f)}
until #tmp > 0 or computer.uptime() - start >= timeout
else
tmp = {event.pullFiltered(timeout, f)}
end
tmp = {async.pullFiltered(timeout, f)}
else
if nonblocking then
repeat
coroutine.yield()
tmp = {event.pullFiltered(.05, f)}
until #tmp > 0
else
tmp = {event.pullFiltered(f)}
end
tmp = {async.pullFiltered(f)}
end
end

Loading…
Cancel
Save