You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.2 KiB

local BaseControl = require("bc")
-- ComputerCraft RedNet Compatibility
local Network = {
default_protocol = string.format("bc-%d.%d", BaseControl.version[1], BaseControl.version[2]),
}
function Network:new(modem, protocol)
local self = {
rednet = rednet,
modem = modem or peripheral.getName(peripheral.find("modem")),
protocol = protocol or Network.default_protocol,
}
setmetatable(self, {__index=Network})
return self
end
function Network:start(callback)
self.rednet.open(self.modem)
self.callback = callback
end
function Network:send(addr, msg)
self.rednet.send(addr, msg, self.protocol)
end
function Network:broadcast(msg)
self.rednet.broadcast(msg, self.protocol)
end
function Network:pull(filter, timeout)
while true do
local remote, msg = self.rednet.receive(self.protocol, timeout)
if remote ~= nil and filter(remote, msg) then
return msg
end
end
end
function Network:stop()
self.rednet.close(self.modem)
end
function Network:serve()
while true do
local remote, msg = self.rednet.receive(self.protocol)
self.callback(remote, msg)
end
end
BaseControl.Network = Network
function BaseControl:serve()
self.network:serve()
end
return BaseControl