type check RPC calls to address #40, plus do proper error propogation

This commit is contained in:
Izaya 2023-09-19 23:59:17 +10:00
parent f8903ad587
commit 32b7287445
1 changed files with 11 additions and 12 deletions

View File

@ -19,10 +19,13 @@ function rpc.call(hostname,fn,...)
local _, from, port, data = event.pull(30, "net_msg", hostname, rpc.port) local _, from, port, data = event.pull(30, "net_msg", hostname, rpc.port)
rt = serial.unserialize(data) or {} rt = serial.unserialize(data) or {}
until rt[1] == rv or computer.uptime() > st + 30 until rt[1] == rv or computer.uptime() > st + 30
if table.remove(rt,1) == rv then if rt[1] == rv then
return table.unpack(rt) if rt[2] then
return table.unpack(rt,3)
end
error(rt[3])
end end
return false error("timed out")
end end
function rpc.proxy(hostname,filter) function rpc.proxy(hostname,filter)
filter=(filter or "").."(.+)" filter=(filter or "").."(.+)"
@ -62,15 +65,11 @@ function rpc.register(name,fn)
event.listen("net_msg",function(_, from, port, data) event.listen("net_msg",function(_, from, port, data)
if port == rpc.port then if port == rpc.port then
local rpcrq = serial.unserialize(data) local rpcrq = serial.unserialize(data)
local rpcn, rpcid = table.remove(rpcrq,1), table.remove(rpcrq,1) if rpcf[rpcrq[1]] and isPermitted(from,rpcrq[1]) then
if rpcf[rpcn] and isPermitted(from,rpcn) then local rt = {pcall(rpcf[rpcrq[1]],table.unpack(rpcrq))}
local rt = {pcall(rpcf[rpcn],table.unpack(rpcrq))} minitel.send(from,port,serial.serialize({rpcrq[2],pcall(rpcf[rpcrq[1]],table.unpack(rpcrq,3))}))
if rt[1] == true then elseif type(rpcrq[2]) == "string" then
table.remove(rt,1) minitel.send(from,port,serial.serialize({rpcrq[2],false,"function unavailable"}))
end
minitel.send(from,port,serial.serialize({rpcid,table.unpack(rt)}))
else
minitel.send(from,port,serial.serialize({rpcid,false,"function unavailable"}))
end end
end end
end) end)