diff --git a/Embedded/microtel-5-core-mini.lua b/Embedded/microtel-5-core-mini.lua new file mode 100644 index 0000000..d5fbacf --- /dev/null +++ b/Embedded/microtel-5-core-mini.lua @@ -0,0 +1,14 @@ +net.timeout=60 +function net.open(A,V) +local st=computer.uptime() +net.send(A,P,"openstream") +repeat +_,F,P,D=computer.pullSignal(0.5) +if computer.uptime()>st+net.timeout then return false end +until F==A and P==V and tonumber(D) +V=tonumber(D) +repeat +_,F,P,D=computer.pullSignal(0.5) +until F==A and P==V +return socket(A,V,D) +end diff --git a/Embedded/microtel-5-core.lua b/Embedded/microtel-5-core.lua new file mode 100644 index 0000000..2c32fa9 --- /dev/null +++ b/Embedded/microtel-5-core.lua @@ -0,0 +1,26 @@ +function net.socket(address, port, sclose) + local conn = {} + local conn.state, conn.buffer, conn.port, rb, conn.address = "o", "", tonumber(port), "", address + function conn.r(self,length) + rb=self.buffer:sub(1,l) + self.buffer=self.buffer:sub(l+1) + return rb + end + function conn.w(self,data) + net.lsend(self.address,self.port,data) + end + function conn.c(s) + net.send(conn.address,conn.port,sclose) + end + function net.hook[sclose](etype, from, port, data) + if from == conn.address and port == conn.port then + if data == sclose then + net.hook[sclose] = nil + conn.state = "c" + return + end + conn.buffer = conn.buffer..data + end + end + return conn +end diff --git a/Embedded/microtel-5-flisten-mini.lua b/Embedded/microtel-5-flisten-mini.lua new file mode 100644 index 0000000..1f89fec --- /dev/null +++ b/Embedded/microtel-5-flisten-mini.lua @@ -0,0 +1,10 @@ +function net.flisten(V,handler) + function net.hook[V](E,F,P,D) + if P == V and D == "openstream" then + local nP,S = math.random(2^15,2^16),tostring(math.random(-2^16,2^16)) + net.send(F,P,tostring(nP)) + net.send(F,nP,S) + handler(net.socket(F,nP,S)) + end + end +end diff --git a/Embedded/microtel-5-flisten.lua b/Embedded/microtel-5-flisten.lua new file mode 100644 index 0000000..9c801cf --- /dev/null +++ b/Embedded/microtel-5-flisten.lua @@ -0,0 +1,10 @@ +function net.flisten(vport,handler) + function net.hook[vport](etype,from,port,data) + if port == vport and data == "openstream" then + local nport,sclose = math.random(2^15,2^16),tostring(math.random(-2^16,2^16)) + net.send(from,port,tostring(nport)) + net.send(from,nport,sclose) + handler(net.socket(from,nport,sclose)) + end + end +end diff --git a/Embedded/microtel-5-listen-mini.lua b/Embedded/microtel-5-listen-mini.lua new file mode 100644 index 0000000..c44b633 --- /dev/null +++ b/Embedded/microtel-5-listen-mini.lua @@ -0,0 +1,9 @@ +function net.listen(V) + repeat + local E, F, P, D = computer.pullSignal(0.5) + until P == V and D == "openstream" + local nP,S = math.random(2^15,2^16),tostring(math.random(-2^16,2^16)) + net.send(F,P,tostring(nP)) + net.send(F,nP,S) + return net.socket(F,nP,S) +end diff --git a/Embedded/microtel-5-listen.lua b/Embedded/microtel-5-listen.lua new file mode 100644 index 0000000..59b1158 --- /dev/null +++ b/Embedded/microtel-5-listen.lua @@ -0,0 +1,9 @@ +function net.listen(vport) + repeat + local etype, from, port, data = computer.pullSignal(0.5) + until port == vport and data == "openstream" + local nport,sclose = math.random(2^15,2^16),tostring(math.random(-2^16,2^16)) + net.send(from,port,tostring(nport)) + net.send(from,nport,sclose) + return net.socket(from,nport,sclose) +end diff --git a/Embedded/microtel-5-mini.lua b/Embedded/microtel-5-mini.lua deleted file mode 100644 index f67ab10..0000000 --- a/Embedded/microtel-5-mini.lua +++ /dev/null @@ -1,81 +0,0 @@ -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 st+net.timeout then return false end + until F == A and P == V and tonumber(D) + V=tonumber(D) + repeat + _, F, P, D = computer.pullSignal(0.5) + until F == A and P == V + return socket(A,V,D) +end diff --git a/Embedded/microtel-5-open.lua b/Embedded/microtel-5-open.lua new file mode 100644 index 0000000..2a54388 --- /dev/null +++ b/Embedded/microtel-5-open.lua @@ -0,0 +1,14 @@ +net.timeout = 60 +function net.open(address,vport) + local st=computer.uptime() + net.send(address,port,"openstream") + repeat + _, from, port, data = computer.pullSignal(0.5) + if computer.uptime() > st+net.timeout then return false end + until from == address and port == vport and tonumber(data) + vport=tonumber(data) + repeat + _, from, port, data = computer.pullSignal(0.5) + until from == address and port == vport + return socket(address,vport,data) +end diff --git a/Embedded/microtel-5.lua b/Embedded/microtel-5.lua deleted file mode 100644 index 23924e7..0000000 --- a/Embedded/microtel-5.lua +++ /dev/null @@ -1,86 +0,0 @@ -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 diff --git a/Embedded/minify.sh b/Embedded/minify.sh index 9fbb805..efd17e8 100755 --- a/Embedded/minify.sh +++ b/Embedded/minify.sh @@ -32,23 +32,58 @@ 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 +#microtel-5-core.lua +cp microtel-5-core.lua microtel-5-core-mini.lua +sed -i -e 's/address/A/g' microtel-5-core-mini.lua +sed -i -e 's/vport/V/g' microtel-5-core-mini.lua +sed -i -e 's/sclose/S/g' microtel-5-core-mini.lua +sed -i -e 's/port/P/g' microtel-5-core-mini.lua +sed -i -e 's/conn.state/conn.s/g' microtel-5-core-mini.lua +sed -i -e 's/conn.buffer/conn.b/g' microtel-5-core-mini.lua +sed -i -e 's/conn/C/g' microtel-5-core-mini.lua +sed -i -e 's/etype/E/g' microtel-5-core-mini.lua +sed -i -e 's/from/F/g' microtel-5-core-mini.lua +sed -i -e 's/data/D/g' microtel-5-core-mini.lua +lua strip.lua microtel-5-core-mini.lua microtel-5-core-mini.lua + +#microtel-5-listen.lua +cp microtel-5-listen.lua microtel-5-listen-mini.lua +sed -i -e 's/address/A/g' microtel-5-listen-mini.lua +sed -i -e 's/vport/V/g' microtel-5-listen-mini.lua +sed -i -e 's/sclose/S/g' microtel-5-listen-mini.lua +sed -i -e 's/port/P/g' microtel-5-listen-mini.lua +sed -i -e 's/conn.state/conn.s/g' microtel-5-listen-mini.lua +sed -i -e 's/conn.buffer/conn.b/g' microtel-5-listen-mini.lua +sed -i -e 's/conn/C/g' microtel-5-listen-mini.lua +sed -i -e 's/etype/E/g' microtel-5-listen-mini.lua +sed -i -e 's/from/F/g' microtel-5-listen-mini.lua +sed -i -e 's/data/D/g' microtel-5-listen-mini.lua +lua strip.lua microtel-5-listen-mini.lua microtel-5-core-mini.lua + +#microtel-5-flisten.lua +cp microtel-5-flisten.lua microtel-5-flisten-mini.lua +sed -i -e 's/address/A/g' microtel-5-flisten-mini.lua +sed -i -e 's/vport/V/g' microtel-5-flisten-mini.lua +sed -i -e 's/sclose/S/g' microtel-5-flisten-mini.lua +sed -i -e 's/port/P/g' microtel-5-flisten-mini.lua +sed -i -e 's/conn.state/conn.s/g' microtel-5-flisten-mini.lua +sed -i -e 's/conn.buffer/conn.b/g' microtel-5-flisten-mini.lua +sed -i -e 's/conn/C/g' microtel-5-flisten-mini.lua +sed -i -e 's/etype/E/g' microtel-5-flisten-mini.lua +sed -i -e 's/from/F/g' microtel-5-flisten-mini.lua +sed -i -e 's/data/D/g' microtel-5-flisten-mini.lua +lua strip.lua microtel-5-flisten-mini.lua microtel-5-core-mini.lua + +#microtel-5-open.lua +cp microtel-5-open.lua microtel-5-open-mini.lua +sed -i -e 's/address/A/g' microtel-5-open-mini.lua +sed -i -e 's/vport/V/g' microtel-5-open-mini.lua +sed -i -e 's/sclose/S/g' microtel-5-open-mini.lua +sed -i -e 's/port/P/g' microtel-5-open-mini.lua +sed -i -e 's/conn.state/conn.s/g' microtel-5-open-mini.lua +sed -i -e 's/conn.buffer/conn.b/g' microtel-5-open-mini.lua +sed -i -e 's/conn/C/g' microtel-5-open-mini.lua +sed -i -e 's/etype/E/g' microtel-5-open-mini.lua +sed -i -e 's/from/F/g' microtel-5-open-mini.lua +sed -i -e 's/data/D/g' microtel-5-open-mini.lua +lua strip.lua microtel-5-open-mini.lua microtel-5-core-mini.lua