88 lines
2.1 KiB
Lua
88 lines
2.1 KiB
Lua
net = {}
|
|
net.id = computer.address():sub(1,8)
|
|
net.np = 4957
|
|
net.tm = {}
|
|
function net.send(id,po,msg) -- id, port, message
|
|
if id and po and msg then
|
|
event.push("sendmsg",id,po,msg)
|
|
else return false
|
|
end
|
|
end
|
|
if cdlib and relib then
|
|
spawn("copperd",function() print(pcall(function ()
|
|
local pt = {}
|
|
local ps = {}
|
|
for a,t in component.list("modem") do
|
|
table.insert(net.tm,component.proxy(a))
|
|
component.proxy(a).open(net.np)
|
|
end
|
|
while true do
|
|
local ev = {event.pull()}
|
|
if ev[1] == "rsendmsg" then
|
|
dst,data = ev[2],ev[3]
|
|
for k,v in ipairs(net.tm) do
|
|
v.broadcast(net.np,"copper",cdlib.encode(0,net.id,dst,data))
|
|
end
|
|
elseif ev[1] == "sendmsg" then
|
|
local p = {}
|
|
p.pid = string.char(math.random(255),math.random(255),math.random(255))
|
|
p.nid = ev[2]
|
|
p.port = ev[3]
|
|
p.msg = ev[4]
|
|
p.at = 0
|
|
p.lt = 0
|
|
pt[cdlib.encode(0,net.id,p.nid,relib.encode(p)):sub(2)] = os.time()
|
|
ps[p.pid] = p
|
|
elseif ev[1] == "modem_message" and ev[4] == net.np and ev[6] == "copper" then
|
|
local hops,src,dst,data = cdlib.decode(ev[7])
|
|
if not pt[ev[7]:sub(2)] then
|
|
pt[ev[7]:sub(2)] = os.time()
|
|
if dst == net.id then
|
|
event.push("net_rmsg",src,data)
|
|
local pd = relib.decode(data)
|
|
if pd then
|
|
if pd.port and pd.pid and pd.at and pd.pt and pd.msg then
|
|
if pd.pt == 1 then
|
|
event.push("net_msg",src,pd.port,pd.msg)
|
|
pd.pt = 2
|
|
pd.nid = src
|
|
pd.lt = 0
|
|
pd.msg = ""
|
|
ps[pd.pid] = pd
|
|
elseif pd.pt == 2 then
|
|
ps[pd.pid] = nil
|
|
end
|
|
end
|
|
end
|
|
else
|
|
if hops < 255 then
|
|
for k,v in ipairs(net.tm) do
|
|
v.broadcast(net.np,"copper",cdlib.encode(hops+1,src,dst,data))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
for k,v in pairs(ps) do
|
|
if v.lt < os.time()-1 then
|
|
for l,m in ipairs(net.tm) do
|
|
m.broadcast(net.np,"copper",cdlib.encode(0,net.id,v.nid,relib.encode(v)))
|
|
end
|
|
v.at = v.at + 1
|
|
v.lt = os.time()
|
|
if v.pt == 2 then ps[k] = nil end
|
|
end
|
|
if v.at > 255 then
|
|
ps[k] = nil
|
|
end
|
|
end
|
|
local cot = os.time()
|
|
for k,v in pairs(pt) do
|
|
if v < cot-16 then
|
|
pt[k] = nil
|
|
end
|
|
end
|
|
end
|
|
end)) end)
|
|
end
|