parent
d2a1a8ebe5
commit
4fabea6653
@ -0,0 +1,58 @@
|
|||||||
|
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
|
||||||
Loading…
Reference in new issue