Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. --config
  2. local netComponentName = "tunnel"
  3. local address,port = "",""
  4.  
  5. --
  6.  
  7. local event=require("event")
  8. local component=require("component")
  9. local serialization=require("serialization")
  10.  
  11. --net
  12. local net=component[netComponentName]
  13.  
  14. local maxPacketSize=net.maxPacketSize()
  15.  
  16. local function send(...)
  17.     net.send(...)
  18. end
  19. --
  20.  
  21. --specials
  22. local specials={}
  23.  
  24. local function bindSpecial(value)
  25.     local index=#specials+1
  26.     specials[index]=value
  27.     return index
  28. end
  29.  
  30. local function unbindSpecial(index)
  31.     specials[index]=nil
  32. end
  33.  
  34. local function specialByIndex(index)
  35.     return specials[index]
  36. end
  37. --
  38.  
  39. local work=true
  40.  
  41. local reactions={
  42.     ["component.primary"] =
  43.         function(ctype)
  44.         local r=component.list(ctype)()
  45.         print("primaryResult", r)
  46.             send("primaryResult", r)
  47.         end,
  48.     ["component.invoke"] =
  49.         function(address, method, ...)
  50.             local args={...}
  51.             local specialIndex=0
  52.             for i=0,#args do
  53.                 if(type(args[i])=="string") and string.sub(args[i],1,9)=="{special=" then
  54.                     specialIndex=tonumber(string.sub(args[i],10,-2))
  55.                     args[i]=specialByIndex(specialIndex)
  56.                 end        
  57.             end
  58.             local r={component.proxy(address)[method](table.unpack(args))}
  59.             for i=0,#r do
  60.                 if type(r[i])=="table" then
  61.                     local nr=serialization.serialize(r[i])
  62.                     if nr=="{type=\"userdata\"}" then
  63.                         nr="{special="..bindSpecial(r[i]).."}"
  64.                     end
  65.                     r[i]=nr
  66.                 end        
  67.             end
  68.             os.sleep(0.1)
  69.             send("invokeResult",table.unpack(r))
  70.            
  71.             if(method=="close")then
  72.                 unbindSpecial(specialIndex)
  73.             end
  74.             print("test")
  75.         end,
  76.     ["specials.unbind"]=
  77.         function(index)
  78.             unbindSpecial(index)
  79.         end
  80. }
  81.  
  82. local function eventHandler(_, _, from, port, _, request, ...)
  83.     print("received message", request, ...)
  84.     reactions[request](...)
  85. end
  86.  
  87. while work do
  88.     eventHandler(event.pull("modem_message"))
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement