1
0
mirror of https://github.com/ShadowKatStudios/OC-Minitel.git synced 2024-11-23 02:28:05 +11:00

added more stuff to the embedded version, improved minification

This commit is contained in:
Izaya 2018-03-13 12:11:21 +11:00
parent bce7d5d30f
commit 95ad568b86
7 changed files with 353 additions and 22 deletions

View File

@ -0,0 +1,101 @@
_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=component.invoke,computer.uptime
for a,t in component.list("modem") do
M[#M+1]=component.proxy(a)
M[#M].open(net.port)
end
local function gP()
local npID=""
for i=1,16 do
npID=npID .. string.char(math.random(32,126))
end
return npID
end
local function sP(packetID,packetType,dest,sender,vport,data)
if RC[dest] then
cI(RC[dest][1],"send",RC[dest][2],net.port,packetID,packetType,dest,sender,vport,data)
else
for k,v in pairs(M) do
v.broadcast(net.port,packetID,packetType,dest,sender,vport,data)
end
end
end
local function pC()
for k,v in pairs(RC) do
if v[3]<cU() then
RC[k]=nil
end
end
for k,v in pairs(PC) do
if v<cU() then
PC[k]=nil
end
end
end
local function cPC(packetID)
for k,v in pairs(PC) do
if k==packetID then return true end
end
return false
end
local function pP()
for k,v in pairs(pQ) do
if v[5]<cU() then
sP(k,v[1],v[2],net.hostname,v[3],v[4])
if v[1]~=1 or v[6]==255 then
pQ[k]=nil
else
pQ[k][5]=cU()+net.retry
pQ[k][6]=pQ[k][6]+1
end
end
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
computer.pushSignal("net_ack",data)
end
if tev[7]~=2 then
computer.pushSignal("net_msg",tev[9],tev[10],tev[11])
end
else
sP(tev[6],tev[7],tev[8],tev[9],tev[10],tev[11])
end
if not RC[tev[9]] then
RC[tev[9]]={tev[2],tev[3],cU()+net.rctime}
end
if not PC[tev[6]] then
PC[tev[6]]=cU()+net.pctime
end
end
return table.unpack(tev)
end
function net.usend(to,vport,data,npID)
npID=npID or gP()
pQ[npID]={0,to,vport,data,0,0}
end
function net.rsend(to,vport,data,npID)
npID=npID or gP()
pQ[npID]={1,to,vport,data,0,0}
repeat
local te={computer.pullSignal()}
until te[1]=="net_ack" and te[2]==npID
end
end

View File

@ -1,7 +1,6 @@
_G.net = {}
net.port = 4096
net.hostname = computer.address():sub(1,8)
net.hostname = "micro"
net.debug = false
net.rctime = 30
net.pctime = 30
@ -81,7 +80,7 @@ function computer.pullSignal(t)
pqueue[tev[11]] = nil
computer.pushSignal("net_ack",data)
end
if packetType ~= 2 then
if tev[7] ~= 2 then
computer.pushSignal("net_msg",tev[9],tev[10],tev[11])
end
else
@ -94,15 +93,19 @@ function computer.pullSignal(t)
pcache[tev[6]] = computer.uptime()+net.pctime
end
end
return table.unpack(tev)
end
function net.send(ptype,to,vport,data,npID)
function net.usend(to,vport,data,npID)
npID = npID or genPacketID()
pqueue[npID] = {ptype,to,vport,data,0,0}
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
while true do
computer.pullSignal()
end

View File

@ -0,0 +1,15 @@
net.mtu=4096
function net.send(T,P,L)
local D={}
if L:len()>net.mtu then
for i=1,L:len(),net.mtu do
D[#D+1]=L:sub(1,net.mtu)
L=L:sub(net.mtu+1)
end
else
D={L}
end
for k,v in ipairs(D) do
net.rsend(T,P,v)
end
end

15
Embedded/microtel-4.lua Normal file
View File

@ -0,0 +1,15 @@
net.mtu = 4096
function net.send(to,vport,ldata)
local tdata = {}
if ldata:len() > net.mtu then
for i = 1, ldata:len(), net.mtu do
tdata[#tdata+1] = ldata:sub(1,net.mtu)
ldata = ldata:sub(net.mtu+1)
end
else
tdata = {ldata}
end
for k,v in ipairs(tdata) do
net.rsend(to,vport,v)
end
end

View File

@ -0,0 +1,81 @@
do
local H={}
function computer.pullSignal(T)
tE={computer.pullSignal(T)}
for k,v in pairs(H) do
v(table.unpack(tE))
end
return table.unpack(tE)
end
local function cW(S,D)
if S.state=="open" then
net.send(S.A,S.P,D)
end
end
local function cR(S,lN)
local rD=""
rD=S.rB:sub(1,lN)
S.rB=S.rB:sub(lN+1)
return rD
end
local function socket(A,P,sC) local C={}
C.A,C.P=A,tonumber(P)
C.rB=""
C.write=cW
C.read=cR
C.state="open"
local function lI(_,f,p,d)
if f==C.A and p==C.P then
if d==sC then
C:close()
else
C.rB=C.rB .. d
end
end
end
sL=tostring(lI)
H[sL]=lI
function C.close(S)
H[sL]=nil
C.state="closed"
net.rsend(A,P,sC)
end
return C
end
function net.open(to,P)
net.rsend(to,P,"openstream")
local st=computer.uptime()+net.streamdelay
local est=false
while true do
_,F,rP,D=event.pull("net_msg")
if to==F and rP==P then
if tonumber(D) then
est=true
end
break
end
if st<computer.uptime() then
return nil,"timed out"
end
end
if not est then
return nil,"refused"
end
D=tonumber(D)
sC=""
repeat
_,F,nP,sC=event.pull("net_msg")
until F==to and nP==D
return socket(to,D,sC)
end
function net.listen(P)
repeat
_,F,rP,D=computer.pullSignal()
until rP==P and D=="openstream"
local nP=math.random(net.minP,net.maxP)
local sC=net.genPacketID()
net.rsend(F,rP,tostring(nP))
net.rsend(F,nP,sC)
return socket(F,nP,sC)
end
end

86
Embedded/microtel-5.lua Normal file
View File

@ -0,0 +1,86 @@
do
local hooks = {}
function computer.pullSignal(T)
tE = {computer.pullSignal(T)}
for k,v in pairs(hooks) do
v(table.unpack(tE))
end
return table.unpack(tE)
end
local function cwrite(self,data)
if self.state == "open" then
net.send(self.addr,self.port,data)
end
end
local function cread(self,length)
local rdata = ""
rdata = self.rbuffer:sub(1,length)
self.rbuffer = self.rbuffer:sub(length+1)
return rdata
end
local function socket(addr,port,sclose) -- todo, add remote closing of sockets
local conn = {}
conn.addr,conn.port = addr,tonumber(port)
conn.rbuffer = ""
conn.write = cwrite
conn.read = cread
conn.state = "open"
local function listener(_,f,p,d)
if f == conn.addr and p == conn.port then
if d == sclose then
conn:close()
else
conn.rbuffer = conn.rbuffer .. d
end
end
end
slisten = tostring(listener)
hooks[slisten]=listener
function conn.close(self)
hooks[slisten]=nil
conn.state = "closed"
net.rsend(addr,port,sclose)
end
return conn
end
function net.open(to,port)
net.rsend(to,port,"openstream")
local st = computer.uptime()+net.streamdelay
local est = false
while true do
_,from,rport,data = event.pull("net_msg")
if to == from and rport == port then
if tonumber(data) then
est = true
end
break
end
if st < computer.uptime() then
return nil, "timed out"
end
end
if not est then
return nil, "refused"
end
data = tonumber(data)
sclose = ""
repeat
_,from,nport,sclose = event.pull("net_msg")
until from == to and nport == data
return socket(to,data,sclose)
end
function net.listen(port)
repeat
_, from, rport, data = computer.pullSignal()
until rport == port and data == "openstream"
local nport = math.random(net.minport,net.maxport)
local sclose = net.genPacketID()
net.rsend(from,rport,tostring(nport))
net.rsend(from,nport,sclose)
return socket(from,nport,sclose)
end
end

View File

@ -1,15 +1,45 @@
#!/usr/bin/env bash
cp $1 $2
sed -i -e 's/pcache/PC/g' $2
sed -i -e 's/rcache/RC/g' $2
sed -i -e 's/pqueue/pQ/g' $2
sed -i -e 's/modems/M/g' $2
sed -i -e 's/genPacketID/gP/g' $2
sed -i -e 's/sendPacket/sP/g' $2
sed -i -e 's/pruneCache/pC/g' $2
sed -i -e 's/checkPCache/cPC/g' $2
sed -i -e 's/packetPusher/pP/g' $2
sed -i -e 's/component.invoke/cI/g' $2
sed -i -e 's/computer.uptime/cU/g' $2
sed -i -e 's/--subhere/local cI,cU = component.invoke,computer.uptime/g' $2
lua strip.lua $2 $2
#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/modems/M/g' microtel-3-mini.lua
sed -i -e 's/genPacketID/gP/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/--subhere/local cI,cU = component.invoke,computer.uptime/g' microtel-3-mini.lua
lua strip.lua microtel-3-mini.lua microtel-3-mini.lua
#microtel-4.lua
cp microtel-4.lua microtel-4-mini.lua
sed -i -e 's/to/T/g' microtel-4-mini.lua
sed -i -e 's/vport/P/g' microtel-4-mini.lua
sed -i -e 's/ldata/L/g' microtel-4-mini.lua
sed -i -e 's/tdata/D/g' microtel-4-mini.lua
lua strip.lua microtel-4-mini.lua microtel-4-mini.lua
#microtel-5.lua
cp microtel-5.lua microtel-5-mini.lua
sed -i -e 's/hooks/H/g' microtel-5-mini.lua
sed -i -e 's/cwrite/cW/g' microtel-5-mini.lua
sed -i -e 's/cread/cR/g' microtel-5-mini.lua
sed -i -e 's/conn/C/g' microtel-5-mini.lua
sed -i -e 's/listener/lI/g' microtel-5-mini.lua
sed -i -e 's/slisten/sL/g' microtel-5-mini.lua
sed -i -e 's/self/S/g' microtel-5-mini.lua
sed -i -e 's/addr/A/g' microtel-5-mini.lua
sed -i -e 's/rbuffer/rB/g' microtel-5-mini.lua
sed -i -e 's/rport/rP/g' microtel-5-mini.lua
sed -i -e 's/nport/nP/g' microtel-5-mini.lua
sed -i -e 's/port/P/g' microtel-5-mini.lua
sed -i -e 's/sclose/sC/g' microtel-5-mini.lua
sed -i -e 's/rdata/rD/g' microtel-5-mini.lua
sed -i -e 's/length/lN/g' microtel-5-mini.lua
sed -i -e 's/data/D/g' microtel-5-mini.lua
sed -i -e 's/from/F/g' microtel-5-mini.lua
lua strip.lua microtel-5-mini.lua microtel-5-mini.lua