2018-03-13 12:11:21 +11:00
|
|
|
_G.net={}
|
|
|
|
do
|
2018-03-18 12:13:47 +11:00
|
|
|
local M,packetQueue,pC,rC,C={},{},{},{},computer
|
|
|
|
net.port,net.hostname,net.route=4096,C.address():sub(1,8),true
|
|
|
|
for a in component.list("modem") do
|
|
|
|
M[a]=component.proxy(a)
|
|
|
|
M[a].open(net.port)
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
|
|
|
local function gP()
|
2018-03-18 12:13:47 +11:00
|
|
|
local pID=""
|
2018-03-13 12:11:21 +11:00
|
|
|
for i=1,16 do
|
2018-03-18 12:13:47 +11:00
|
|
|
pID=pID .. string.char(math.random(32,126))
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
return pID
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
local function sP(pID,pT,T,vP,D)
|
|
|
|
pC[pID]=C.uptime()
|
|
|
|
if rC[T] then
|
|
|
|
M[rC[T][1]].send(rC[T][2],net.port,pID,pT,T,net.hostname,vP,D)
|
2018-03-13 12:11:21 +11:00
|
|
|
else
|
|
|
|
for k,v in pairs(M) do
|
2018-03-18 12:13:47 +11:00
|
|
|
v.broadcast(net.port,pID,pT,T,net.hostname,vP,D)
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
function net.send(T,vP,D,pT,pID)
|
|
|
|
pT,pID=pT or 1,pID or gP()
|
|
|
|
packetQueue[pID]={pT,T,vP,D,0}
|
|
|
|
sP(pID,pT,T,vP,D)
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
local function cC(pID)
|
|
|
|
for k,v in pairs(pC) do
|
|
|
|
if k==pID then
|
2018-03-13 12:11:21 +11:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
return true
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
local rCPE=C.pullSignal
|
|
|
|
function C.pullSignal(t)
|
|
|
|
local eT={rCPE(t)}
|
|
|
|
for k,v in pairs(pC) do
|
|
|
|
if C.uptime()>v+30 then
|
|
|
|
pC[k]=nil
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
for k,v in pairs(rC) do
|
|
|
|
if C.uptime()>v[3]+30 then
|
|
|
|
rC[k]=nil
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
if eT[1]=="modem_message" and (eT[4]==net.port or eT[4]==0) and cC(eT[6]) then
|
|
|
|
rC[eT[9]]={eT[2],eT[3],C.uptime()}
|
|
|
|
if eT[8]==net.hostname then
|
|
|
|
if eT[7]~=2 then
|
|
|
|
C.pushSignal("net_msg",eT[9],eT[10],eT[11])
|
|
|
|
if eT[7]==1 then
|
|
|
|
sP(gP(),2,eT[9],eT[10],eT[6])
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
|
|
|
else
|
2018-03-18 12:13:47 +11:00
|
|
|
packetQueue[eT[11]]=nil
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
elseif net.route and cC(eT[6]) then
|
|
|
|
sP(eT[6],eT[7],eT[8],eT[9],eT[10],eT[11])
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
pC[eT[6]]=C.uptime()
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
for k,v in pairs(packetQueue) do
|
|
|
|
if C.uptime()>v[5] then
|
|
|
|
sP(k,table.unpack(v))
|
|
|
|
v[5]=C.uptime()+30
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
|
|
|
end
|
2018-03-18 12:13:47 +11:00
|
|
|
return table.unpack(eT)
|
2018-03-13 12:11:21 +11:00
|
|
|
end
|
|
|
|
end
|