1
0
mirror of https://github.com/kepler155c/opus synced 2025-07-04 11:02:52 +00:00

http fixes

This commit is contained in:
kepler155c@gmail.com 2017-10-08 20:03:01 -04:00
parent a625b52bad
commit 955f11042b
4 changed files with 66 additions and 48 deletions

View File

@ -1,5 +1,4 @@
local synchronized = require('sync') local Util = require('util')
local Util = require('util')
local fs = _G.fs local fs = _G.fs
@ -51,9 +50,7 @@ function urlfs.open(node, fn, fl)
local c = node.cache local c = node.cache
if not c then if not c then
synchronized(node.url, function() c = Util.httpGet(node.url)
c = Util.download(node.url)
end)
if c then if c then
node.cache = c node.cache = c
node.size = #c node.size = #c

View File

@ -6,41 +6,53 @@ local fs = _G.fs
local http = _G.http local http = _G.http
local os = _G.os local os = _G.os
-- fix broken http get if not http._patched then
local syncLocks = { } -- fix broken http get
local syncLocks = { }
local function sync(obj, fn) local function sync(obj, fn)
local key = tostring(obj) local key = tostring(obj)
if syncLocks[key] then if syncLocks[key] then
local cos = tostring(coroutine.running()) local cos = tostring(coroutine.running())
table.insert(syncLocks[key], cos) table.insert(syncLocks[key], cos)
repeat repeat
local _, co = os.pullEvent('sync_lock') local _, co = os.pullEvent('sync_lock')
until co == cos until co == cos
else else
syncLocks[key] = { } syncLocks[key] = { }
end
local s, m = pcall(fn)
local co = table.remove(syncLocks[key], 1)
if co then
os.queueEvent('sync_lock', co)
else
syncLocks[key] = nil
end
if not s then
error(m)
end
end end
local s, m = pcall(fn)
local co = table.remove(syncLocks[key], 1) -- todo -- completely replace http.get with function that
if co then -- checks for success on permanent redirects (minecraft 1.75 bug)
os.queueEvent('sync_lock', co)
else http._patched = http.get
syncLocks[key] = nil function http.get(url, headers)
end local s, m
if not s then sync(url, function()
error(m) s, m = http._patched(url, headers)
end)
return s, m
end end
end end
local function loadUrl(url) local function loadUrl(url)
local c local c
sync(url, function() local h = http.get(url)
local h = http.get(url) if h then
if h then c = h.readAll()
c = h.readAll() h.close()
h.close() end
end
end)
if c and #c > 0 then if c and #c > 0 then
return c return c
end end

View File

@ -1,5 +1,11 @@
local Util = { } local Util = { }
local fs = _G.fs
local http = _G.http
local os = _G.os
local term = _G.term
local textutils = _G.textutils
function Util.tryTimed(timeout, f, ...) function Util.tryTimed(timeout, f, ...)
local c = os.clock() local c = os.clock()
repeat repeat
@ -12,7 +18,7 @@ end
function Util.tryTimes(attempts, f, ...) function Util.tryTimes(attempts, f, ...)
local result local result
for i = 1, attempts do for _ = 1, attempts do
result = { f(...) } result = { f(...) }
if result[1] then if result[1] then
return unpack(result) return unpack(result)
@ -72,11 +78,11 @@ end
function Util.getVersion() function Util.getVersion()
local version local version
if _CC_VERSION then if _G._CC_VERSION then
version = tonumber(_CC_VERSION:gmatch('[%d]+%.?[%d][%d]', '%1')()) version = tonumber(_G._CC_VERSION:gmatch('[%d]+%.?[%d][%d]', '%1')())
end end
if not version and _HOST then if not version and _G._HOST then
version = tonumber(_HOST:gmatch('[%d]+%.?[%d][%d]', '%1')()) version = tonumber(_G._HOST:gmatch('[%d]+%.?[%d][%d]', '%1')())
end end
return version or 1.7 return version or 1.7
@ -351,13 +357,18 @@ function Util.loadTable(fname)
end end
--[[ loading and running functions ]] -- --[[ loading and running functions ]] --
function Util.download(url, filename) function Util.httpGet(url, headers)
local h = http.get(url) local h, msg = http.get(url, headers)
if not h then if h then
error('Failed to download ' .. url) local contents = h.readAll()
h.close()
return contents
end end
local contents = h.readAll() return h, msg
h.close() end
function Util.download(url, filename)
local contents = Util.httpGet(url)
if not contents then if not contents then
error('Failed to download ' .. url) error('Failed to download ' .. url)
end end
@ -418,8 +429,8 @@ function Util.toBytes(n)
return tostring(n) return tostring(n)
end end
function Util.insertString(os, is, pos) function Util.insertString(str, istr, pos)
return os:sub(1, pos - 1) .. is .. os:sub(pos) return str:sub(1, pos - 1) .. istr .. str:sub(pos)
end end
function Util.split(str, pattern) function Util.split(str, pattern)

View File

@ -111,7 +111,6 @@ device.wireless_modem.open(999)
print('discovery: listening on port 999') print('discovery: listening on port 999')
Event.on('modem_message', function(e, s, sport, id, info, distance) Event.on('modem_message', function(e, s, sport, id, info, distance)
debug(info)
if sport == 999 and tonumber(id) and type(info) == 'table' then if sport == 999 and tonumber(id) and type(info) == 'table' then
if not network[id] then if not network[id] then
network[id] = { } network[id] = { }
@ -151,7 +150,6 @@ end
-- every 10 seconds, send out this computer's info -- every 10 seconds, send out this computer's info
Event.onInterval(10, function() Event.onInterval(10, function()
debug('timer')
sendInfo() sendInfo()
for _,c in pairs(_G.network) do for _,c in pairs(_G.network) do
local elapsed = os.clock()-c.timestamp local elapsed = os.clock()-c.timestamp