Files
cash-register/stratumBackend/meshnetBackend.lua
2026-04-06 14:35:45 -07:00

36 lines
1001 B
Lua

local backend = {}
local network = require("libs.entrypointlib")
local loop = coroutine.create(network.loop)
local filter = nil
-- some black magic bs that i pulled from https://gist.github.com/MCJack123/473475f07b980d57dd2bd818026c97e8
local env = getfenv(rednet.run)
env.os = setmetatable({
pullEventRaw = function ()
local ev = table.pack(coroutine.yield())
if filter == nil or filter == ev[1] then
_,filter = coroutine.resume(loop,table.unpack(ev, 1, ev.n))
end
return table.unpack(ev, 1, ev.n)
end
},{__index=env.os})
local target = nil
function backend.setTarget(id)
target = id
end
function backend.sendMessage(msg, id)
if not (id or target) then error("failed to imply or specify target id",2) end
network.send(msg,id or target)
end
function backend.receiveMessage()
local _,msg,id,hops
repeat
_,msg,id,hops = os.pullEvent("network_packet")
until id == (target or id)
return msg,id
end
return backend