Advertisement
Romanok2805

Untitled

Apr 11th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. local a=require("event")local b=require("computer")local c=require("component")local d=c.internet;local e={}local f="stem.fomalhaut.me"local g=5733;local h={MESSAGE=0,SUBSCRIBE=1,UNSUBSCRIBE=2,PING=3,PONG=4}local i=5;local j={__address=nil,__port=nil,__socket=nil,__channels={},__stream="",__build_package=function(k,l,m)local n=string.char(k)if k==h.PING or k==h.PONG then n=n..l else n=n..string.char(#l)..l;if m~=nil then n=n..m end end;local o=#n;n=string.char(math.floor(o/256),o%256)..n;return n end,isSubscribed=function(self,l)return self.__channels[l]end,send=function(self,l,m)if self:isConnected()then local p=self.__build_package(h.MESSAGE,l,m)local q=0;repeat local r,m=self.__socket.write(p:sub(q+1))if not r then return nil,m end;q=q+r until q==#p;return true else return nil,"not connected"end end,subscribe=function(self,l)if self:isConnected()then self.__socket.write(self.__build_package(h.SUBSCRIBE,l))self.__channels[l]=true;return true else return nil,"not connected"end end,unsubscribe=function(self,l)if self:isConnected()then self.__socket.write(self.__build_package(h.UNSUBSCRIBE,l))self.__channels[l]=false;return true else return nil,"not connected"end end,ping=function(self,s,t)self.__socket.write(self.__build_package(h.PING,s))local u=os.time()local v=t or i;while true do local w,p=a.pull(v,"stem_pong")if w=="stem_pong"then return p==s else local x=os.time()-u;if x>=v*20 then return false else v=(t or i)-x/20 end end end end,isConnected=function(self)if self.__socket==nil then return nil,"there were no connection attempts"else return self.__socket.finishConnect()end end,reconnect=function(self)if self:isConnected()then self:disconnect()end;self.__socket=d.connect(self.__address or f,self.__port or g)a.listen('internet_ready',self.__listener)while true do local r,y=self.__socket.finishConnect()if r then return self elseif r==nil then self:disconnect()return nil,y end end end,disconnect=function(self)self.__socket.close()self.__channels={}self.__stream=""a.ignore('internet_ready',self.__listener)end,__incoming=function(self,z)if self.__socket.id()==z then while true do local A=self.__socket.read()if A~=nil and#A>0 then self.__stream=self.__stream..A else break end end;while true do if self.len==nil and#self.__stream>=2 then local B,C=self.__stream:byte(1,2)self.len=B*256+C end;if self.len~=nil and#self.__stream>=self.len+2 then local k=self.__stream:byte(3)local n={type=k}if k==h.PING or k==h.PONG then n.content=self.__stream:sub(4,self.len+2)else local D=self.__stream:byte(4)local l=self.__stream:sub(5,D+4)n.id=l;if k==h.MESSAGE then n.message=self.__stream:sub(D+5,self.len+2)end end;self:__process(n)self.__stream=self.__stream:sub(self.len+3)self.len=nil else break end end end end,__process=function(self,n)if n.type==h.MESSAGE then b.pushSignal("stem_message",n.id,n.message)elseif n.type==h.PING then self.__socket.write(self.__build_package(h.PONG,n.content))elseif n.type==h.PONG then b.pushSignal("stem_pong",n.content)end end}j.__index=j;function e.connect(E,F)local G={__address=E,__port=F,__socket=socket}setmetatable(G,j)G.__listener=function(H,H,l)G:__incoming(l)end;return G:reconnect()end;return e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement