>.<
This commit is contained in:
36
backends/meshnetBackend.lua
Normal file
36
backends/meshnetBackend.lua
Normal file
@@ -0,0 +1,36 @@
|
||||
local backend = {}
|
||||
local network = require("libs.entrypointlib")
|
||||
local loop = coroutine.create(network.loop or function () while true do sleep(100) end end)
|
||||
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
|
||||
Reference in New Issue
Block a user