Fallback to os.clock() if computer.uptime() is not available

To be compatible with systems that don't have computer.uptime(), use
os.clock() as a fallback.  The time is only used for timeout
calculations.
dev
rahix 5 years ago
parent 67cbb5badc
commit e2da98fbf4

@ -1,5 +1,4 @@
local uuid = require("uuid") local uuid = require("uuid")
local computer = require("computer")
local bit32 = require("bit32") local bit32 = require("bit32")
local Version = {0, 1} local Version = {0, 1}
@ -31,6 +30,18 @@ local Query = {
Below = setmetatable({ty=0x424c4f, invalid=true}, query_param), Below = setmetatable({ty=0x424c4f, invalid=true}, query_param),
} }
-- Helpers ---------------------------------------------------------------- {{{
local clock -- A monotonically increasing timer for timeout calculation
do
local status, computer = pcall(require, "computer")
if status and computer.uptime ~= nil then
clock = computer.uptime
else
clock = os.clock
end
end
-- }}}
-- Network ---------------------------------------------------------------- {{{ -- Network ---------------------------------------------------------------- {{{
local Network = { local Network = {
default_port = bit32.bor(0xBC00, bit32.lshift(Version[1], 4), Version[2]), default_port = bit32.bor(0xBC00, bit32.lshift(Version[1], 4), Version[2]),
@ -190,11 +201,11 @@ function BaseControl:finalize(waits, timeout)
end end
end end
local tstart = computer.uptime() local tstart = clock()
local timeout_remaining = timeout local timeout_remaining = timeout
while num_remaining > 0 do while num_remaining > 0 do
if timeout ~= nil then if timeout ~= nil then
timeout_remaining = timeout - (computer.uptime() - tstart) timeout_remaining = timeout - (clock() - tstart)
if timeout_remaining <= 0 then if timeout_remaining <= 0 then
error("timeout") error("timeout")
end end

Loading…
Cancel
Save