diff --git a/Embedded/microtel-3-mini.lua b/Embedded/microtel-3-mini.lua index 0926da1..e5aebbf 100644 --- a/Embedded/microtel-3-mini.lua +++ b/Embedded/microtel-3-mini.lua @@ -1,101 +1,76 @@ _G.net={} -net.port=4096 -net.hostname=computer.address():sub(1,8) -net.debug=false -net.rctime=30 -net.pctime=30 -net.retry=30 do -local rcpe,PC,RC,pQ,M=computer.pullSignal,{},{},{},{} -local cI,cU,cPS=component.invoke,computer.uptime,computer.pushSignal -for a,t in component.list("modem") do -M[#M+1]=component.proxy(a) -M[#M].open(net.port) +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) end local function gP() -local npID="" +local pID="" for i=1,16 do -npID=npID .. string.char(math.random(32,126)) +pID=pID .. string.char(math.random(32,126)) end -return npID +return pID end -local function sP(pID,pT,D,S,vP,dA) -if RC[D] then -cI(RC[D][1],"send",RC[D][2],net.port,pID,pT,D,S,vP,dA) +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) else for k,v in pairs(M) do -v.broadcast(net.port,pID,pT,D,S,vP,dA) +v.broadcast(net.port,pID,pT,T,net.hostname,vP,D) end end end -local function pC() -for k,v in pairs(RC) do -if v[3]v+30 then +pC[k]=nil end end +for k,v in pairs(rC) do +if C.uptime()>v[3]+30 then +rC[k]=nil end end -function computer.pullSignal(t) -pC() -pP() -local tev={rcpe(t)} -if tev[1]=="modem_message" and tev[4]==net.port and not cPC(tev[6]) then -if tev[8]==net.hostname then -if tev[7]==1 then -sP(gP(),2,tev[9],net.hostname,tev[10],tev[6]) -end -if tev[7]==2 then -pQ[tev[11]]=nil -cPS("net_ack",tev[11]) -end -if tev[7]~=2 then -cPS("net_msg",tev[9],tev[10],tev[11]) +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]) end else -sP(tev[6],tev[7],tev[8],tev[9],tev[10],tev[11]) +packetQueue[eT[11]]=nil end -if not RC[tev[9]] then -RC[tev[9]]={tev[2],tev[3],cU()+net.rctime} +elseif net.route and cC(eT[6]) then +sP(eT[6],eT[7],eT[8],eT[9],eT[10],eT[11]) end -if not PC[tev[6]] then -PC[tev[6]]=cU()+net.pctime +pC[eT[6]]=C.uptime() +end +for k,v in pairs(packetQueue) do +if C.uptime()>v[5] then +sP(k,table.unpack(v)) +v[5]=C.uptime()+30 end end -return table.unpack(tev) -end -function net.usend(to,vP,dA,npID) -npID=npID or gP() -pQ[npID]={0,to,vP,dA,0,0} -end -function net.rsend(to,vP,dA,npID) -npID=npID or gP() -pQ[npID]={1,to,vP,dA,0,0} -repeat -local te={computer.pullSignal()} -until te[1]=="net_ack" and te[2]==npID +return table.unpack(eT) end end diff --git a/Embedded/microtel-3.lua b/Embedded/microtel-3.lua index d40be6b..ccb8d9d 100644 --- a/Embedded/microtel-3.lua +++ b/Embedded/microtel-3.lua @@ -1,111 +1,84 @@ -_G.net = {} -net.port = 4096 -net.hostname = computer.address():sub(1,8) -net.debug = false -net.rctime = 30 -net.pctime = 30 -net.retry = 30 +_G.net={} do +local modems,packetQueue,packetCache,routeCache,C = {},{},{},{},NOTCOMPUTER +net.port,net.hostname,net.route=4096,computer.address():sub(1,8),true -local rcpe, pcache, rcache, pqueue, modems = computer.pullSignal, {}, {}, {}, {} ---subhere - -for a,t in component.list("modem") do - modems[#modems+1] = component.proxy(a) - modems[#modems].open(net.port) +for a in component.list("modem") do + modems[a] = component.proxy(a) + modems[a].open(net.port) end local function genPacketID() - local npID = "" + local packetID = "" for i = 1, 16 do - npID = npID .. string.char(math.random(32,126)) + packetID = packetID .. string.char(math.random(32,126)) end - return npID + return packetID end -local function sendPacket(packetID,packetType,dest,sender,vport,data) - if rcache[dest] then - component.invoke(rcache[dest][1],"send",rcache[dest][2],net.port,packetID,packetType,dest,sender,vport,data) +local function sendPacket(packetID,packetType,to,vport,data) + packetCache[packetID] = computer.uptime() + if routeCache[to] then + modems[routeCache[to][1]].send(routeCache[to][2],net.port,packetID,packetType,to,net.hostname,vport,data) else for k,v in pairs(modems) do - v.broadcast(net.port,packetID,packetType,dest,sender,vport,data) + v.broadcast(net.port,packetID,packetType,to,net.hostname,vport,data) end end end -local function pruneCache() - for k,v in pairs(rcache) do - if v[3] < computer.uptime() then - rcache[k] = nil - end - end - for k,v in pairs(pcache) do - if v < computer.uptime() then - pcache[k] = nil - end - end +function net.send(to,vport,data,packetType,packetID) + packetType,packetID = packetType or 1, packetID or genPacketID() + packetQueue[packetID] = {packetType,to,vport,data,0} + sendPacket(packetID,packetType,to,vport,data) end -local function checkPCache(packetID) - for k,v in pairs(pcache) do - if k == packetID then return true end - end - return false -end -local function packetPusher() - for k,v in pairs(pqueue) do - if v[5] < computer.uptime() then - sendPacket(k,v[1],v[2],net.hostname,v[3],v[4]) - if v[1] ~= 1 or v[6] == 255 then - pqueue[k] = nil - else - pqueue[k][5]=computer.uptime()+net.retry - pqueue[k][6]=pqueue[k][6]+1 - end +local function checkCache(packetID) + for k,v in pairs(packetCache) do + if k == packetID then + return false end end + return true end +local realComputerPullSignal = computer.pullSignal function computer.pullSignal(t) - pruneCache() - packetPusher() - local tev = {rcpe(t)} - if tev[1] == "modem_message" and tev[4] == net.port and not checkPCache(tev[6]) then - if tev[8] == net.hostname then - if tev[7] == 1 then - sendPacket(genPacketID(),2,tev[9],net.hostname,tev[10],tev[6]) - end - if tev[7] == 2 then - pqueue[tev[11]] = nil - computer.pushSignal("net_ack",tev[11]) - end - if tev[7] ~= 2 then - computer.pushSignal("net_msg",tev[9],tev[10],tev[11]) - end - else - sendPacket(tev[6],tev[7],tev[8],tev[9],tev[10],tev[11]) - end - if not rcache[tev[9]] then - rcache[tev[9]] = {tev[2],tev[3],computer.uptime()+net.rctime} - end - if not pcache[tev[6]] then - pcache[tev[6]] = computer.uptime()+net.pctime + local eventTab = {realComputerPullSignal(t)} + for k,v in pairs(packetCache) do + if computer.uptime() > v+30 then + packetCache[k] = nil end end - return table.unpack(tev) -end - -function net.usend(to,vport,data,npID) - npID = npID or genPacketID() - pqueue[npID] = {0,to,vport,data,0,0} -end -function net.rsend(to,vport,data,npID) - npID = npID or genPacketID() - pqueue[npID] = {1,to,vport,data,0,0} - repeat - local te={computer.pullSignal()} - until te[1] == "net_ack" and te[2] == npID + for k,v in pairs(routeCache) do + if computer.uptime() > v[3]+30 then + routeCache[k] = nil + end + end + if eventTab[1] == "modem_message" and (eventTab[4] == net.port or eventTab[4] == 0) and checkCache(eventTab[6]) then + routeCache[eventTab[9]] = {eventTab[2],eventTab[3],computer.uptime()} + if eventTab[8] == net.hostname then + if eventTab[7] ~= 2 then + computer.pushSignal("net_msg",eventTab[9],eventTab[10],eventTab[11]) + if eventTab[7] == 1 then + sendPacket(genPacketID(),2,eventTab[9],eventTab[10],eventTab[6]) + end + else + packetQueue[eventTab[11]] = nil + end + elseif net.route and checkCache(eventTab[6]) then + sendPacket(eventTab[6],eventTab[7],eventTab[8],eventTab[9],eventTab[10],eventTab[11]) + end + packetCache[eventTab[6]] = computer.uptime() + end + for k,v in pairs(packetQueue) do + if computer.uptime() > v[5] then + sendPacket(k,table.unpack(v)) + v[5]=computer.uptime()+30 + end + end + return table.unpack(eventTab) end end diff --git a/Embedded/minify.sh b/Embedded/minify.sh index 6f534ad..c2be619 100755 --- a/Embedded/minify.sh +++ b/Embedded/minify.sh @@ -1,25 +1,22 @@ #!/usr/bin/env bash #microtel-3.lua cp microtel-3.lua microtel-3-mini.lua -sed -i -e 's/pcache/PC/g' microtel-3-mini.lua -sed -i -e 's/rcache/RC/g' microtel-3-mini.lua -sed -i -e 's/pqueue/pQ/g' microtel-3-mini.lua +sed -i -e 's/eventTab/eT/g' microtel-3-mini.lua sed -i -e 's/modems/M/g' microtel-3-mini.lua +sed -i -e 's/qPacket/qP/g' microtel-3-mini.lua sed -i -e 's/packetID/pID/g' microtel-3-mini.lua sed -i -e 's/packetType/pT/g' microtel-3-mini.lua -sed -i -e 's/dest/D/g' microtel-3-mini.lua -sed -i -e 's/sender/S/g' microtel-3-mini.lua +sed -i -e 's/to/T/g' microtel-3-mini.lua sed -i -e 's/vport/vP/g' microtel-3-mini.lua -sed -i -e 's/data/dA/g' microtel-3-mini.lua -sed -i -e 's/genPacketID/gP/g' microtel-3-mini.lua +sed -i -e 's/data/D/g' microtel-3-mini.lua sed -i -e 's/sendPacket/sP/g' microtel-3-mini.lua -sed -i -e 's/pruneCache/pC/g' microtel-3-mini.lua -sed -i -e 's/checkPCache/cPC/g' microtel-3-mini.lua -sed -i -e 's/packetPusher/pP/g' microtel-3-mini.lua -sed -i -e 's/component.invoke/cI/g' microtel-3-mini.lua -sed -i -e 's/computer.uptime/cU/g' microtel-3-mini.lua -sed -i -e 's/computer.pushSignal/cPS/g' microtel-3-mini.lua -sed -i -e 's/--subhere/local cI,cU,cPS = component.invoke,computer.uptime,computer.pushSignal/g' microtel-3-mini.lua +sed -i -e 's/genPacketID/gP/g' microtel-3-mini.lua +sed -i -e 's/packetCache/pC/g' microtel-3-mini.lua +sed -i -e 's/checkCache/cC/g' microtel-3-mini.lua +sed -i -e 's/routeCache/rC/g' microtel-3-mini.lua +sed -i -e 's/realComputerPullSignal/rCPE/g' microtel-3-mini.lua +sed -i -e 's/computer/C/g' microtel-3-mini.lua +sed -i -e 's/NOTCOMPUTER/computer/g' microtel-3-mini.lua lua strip.lua microtel-3-mini.lua microtel-3-mini.lua #microtel-4.lua diff --git a/Embedded/old-microtel-3.lua b/Embedded/old-microtel-3.lua new file mode 100644 index 0000000..d40be6b --- /dev/null +++ b/Embedded/old-microtel-3.lua @@ -0,0 +1,111 @@ +_G.net = {} +net.port = 4096 +net.hostname = computer.address():sub(1,8) +net.debug = false +net.rctime = 30 +net.pctime = 30 +net.retry = 30 + +do + +local rcpe, pcache, rcache, pqueue, modems = computer.pullSignal, {}, {}, {}, {} +--subhere + +for a,t in component.list("modem") do + modems[#modems+1] = component.proxy(a) + modems[#modems].open(net.port) +end + +local function genPacketID() + local npID = "" + for i = 1, 16 do + npID = npID .. string.char(math.random(32,126)) + end + return npID +end + +local function sendPacket(packetID,packetType,dest,sender,vport,data) + if rcache[dest] then + component.invoke(rcache[dest][1],"send",rcache[dest][2],net.port,packetID,packetType,dest,sender,vport,data) + else + for k,v in pairs(modems) do + v.broadcast(net.port,packetID,packetType,dest,sender,vport,data) + end + end +end + +local function pruneCache() + for k,v in pairs(rcache) do + if v[3] < computer.uptime() then + rcache[k] = nil + end + end + for k,v in pairs(pcache) do + if v < computer.uptime() then + pcache[k] = nil + end + end +end + +local function checkPCache(packetID) + for k,v in pairs(pcache) do + if k == packetID then return true end + end + return false +end +local function packetPusher() + for k,v in pairs(pqueue) do + if v[5] < computer.uptime() then + sendPacket(k,v[1],v[2],net.hostname,v[3],v[4]) + if v[1] ~= 1 or v[6] == 255 then + pqueue[k] = nil + else + pqueue[k][5]=computer.uptime()+net.retry + pqueue[k][6]=pqueue[k][6]+1 + end + end + end +end + +function computer.pullSignal(t) + pruneCache() + packetPusher() + local tev = {rcpe(t)} + if tev[1] == "modem_message" and tev[4] == net.port and not checkPCache(tev[6]) then + if tev[8] == net.hostname then + if tev[7] == 1 then + sendPacket(genPacketID(),2,tev[9],net.hostname,tev[10],tev[6]) + end + if tev[7] == 2 then + pqueue[tev[11]] = nil + computer.pushSignal("net_ack",tev[11]) + end + if tev[7] ~= 2 then + computer.pushSignal("net_msg",tev[9],tev[10],tev[11]) + end + else + sendPacket(tev[6],tev[7],tev[8],tev[9],tev[10],tev[11]) + end + if not rcache[tev[9]] then + rcache[tev[9]] = {tev[2],tev[3],computer.uptime()+net.rctime} + end + if not pcache[tev[6]] then + pcache[tev[6]] = computer.uptime()+net.pctime + end + end + return table.unpack(tev) +end + +function net.usend(to,vport,data,npID) + npID = npID or genPacketID() + pqueue[npID] = {0,to,vport,data,0,0} +end +function net.rsend(to,vport,data,npID) + npID = npID or genPacketID() + pqueue[npID] = {1,to,vport,data,0,0} + repeat + local te={computer.pullSignal()} + until te[1] == "net_ack" and te[2] == npID +end + +end