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.
This commit is contained in:
parent
67cbb5badc
commit
e2da98fbf4
1 changed files with 14 additions and 3 deletions
17
bc.lua
17
bc.lua
|
|
@ -1,5 +1,4 @@
|
|||
local uuid = require("uuid")
|
||||
local computer = require("computer")
|
||||
local bit32 = require("bit32")
|
||||
|
||||
local Version = {0, 1}
|
||||
|
|
@ -31,6 +30,18 @@ local Query = {
|
|||
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 ---------------------------------------------------------------- {{{
|
||||
local Network = {
|
||||
default_port = bit32.bor(0xBC00, bit32.lshift(Version[1], 4), Version[2]),
|
||||
|
|
@ -190,11 +201,11 @@ function BaseControl:finalize(waits, timeout)
|
|||
end
|
||||
end
|
||||
|
||||
local tstart = computer.uptime()
|
||||
local tstart = clock()
|
||||
local timeout_remaining = timeout
|
||||
while num_remaining > 0 do
|
||||
if timeout ~= nil then
|
||||
timeout_remaining = timeout - (computer.uptime() - tstart)
|
||||
timeout_remaining = timeout - (clock() - tstart)
|
||||
if timeout_remaining <= 0 then
|
||||
error("timeout")
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue