Advertisement
Guest User

Untitled

a guest
Dec 1st, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.43 KB | None | 0 0
  1. local VERSION = "1.0"
  2. local PACKET_HEARTBEAT = "heartbeat"
  3. local PACKET_CONNECT = "connect"
  4. local PACKET_DATA = "data"
  5.  
  6. local DEBUG = nil
  7.  
  8. function tablelength(T)
  9.   local count = 0
  10.   for _ in pairs(T) do count = count + 1 end
  11.   return count
  12. end
  13.  
  14. function serialize(id, data)
  15.     local packet = {}
  16.     packet["id"] = id
  17.     packet["data"] = data
  18.     return textutils.serialize(packet)..""
  19. end
  20.  
  21. function unserialize(packet)
  22.     return textutils.unserialize(packet)
  23. end
  24.  
  25. function log(message)
  26.     if DEBUG then
  27.         print("[Debug] "..message)
  28.     end
  29. end
  30.  
  31. function create(socket_type, debug)
  32.     DEBUG = debug
  33.     rednet.open("back")
  34.     prototype = {}
  35.     if socket_type == "server" then
  36.         local server_events = {}
  37.         local clients = {}
  38.         clients["count"] = 0
  39.  
  40.         local hostname = "Sockets Proxy Server"
  41.         local maxclients = 30
  42.  
  43.         local _listen = false
  44.        
  45.         prototype["hostname"] = function(value)
  46.             hostname = value
  47.             return prototype
  48.         end
  49.  
  50.         prototype["maxclients"] = function(value)
  51.             maxclients = value
  52.             return prototype
  53.         end
  54.  
  55.         prototype["stop"] = function()
  56.             _listen = false
  57.             return prototype
  58.         end
  59.  
  60.         prototype["listen"] = function()
  61.             _listen = true
  62.             while _listen do
  63.                 log("Wait for connection")
  64.                 local socketid, packet, distance = rednet.receive()
  65.                 local _socketid = socketid
  66.                 if packet ~= nil then
  67.                     log("["..socketid.."] accepting packet")
  68.                     packet = unserialize(packet)
  69.                     log("["..socketid.."] check packet")
  70.                     if packet.data ~= nil then
  71.                         log("["..socketid.."] packet id "..packet.id)
  72.                         if packet.data.destination ~= nil then
  73.                             log("["..socketid.."] client is proxy swapping to real client "..packet.data.destination)
  74.                             socketid = packet.data.destination
  75.                         end
  76.                         if packet.id == PACKET_CONNECT and clients[socketid] ~= nil then
  77.                             log("["..socketid.."] restored socket")
  78.                             local socket = clients[socketid]
  79.                             socket.emit('connect')
  80.                             prototype.trigger("connect", {clients[socketid]})
  81.                         elseif packet.id == PACKET_CONNECT and clients[socketid] == nil then
  82.                             log("["..socketid.."] creating socket")
  83.                             local socket = {}
  84.                             socket["id"] = socketid
  85.                             socket["events"] = {}
  86.                             socket["emit"] = function(event, args)
  87.                                 if args == nil then
  88.                                     args = {}
  89.                                 end
  90.                                 local data = {}
  91.                                 data["name"] = event
  92.                                 data["args"] = args
  93.                                 if _socketid ~= socketid then
  94.                                     data["destination"] = socketid
  95.                                 end
  96.                                 local packet = serialize(PACKET_DATA, data)
  97.                                 rednet.send(_socketid, packet)
  98.                             end
  99.                             socket["on"] = function(event, func)
  100.                                 if socket.events[event] == nil then
  101.                                     socket.events[event] = {}
  102.                                 end
  103.                                 table.insert(socket.events[event], func)
  104.                                 return socket
  105.                             end
  106.                             socket["trigger"] = function(event, args)
  107.                                 log("["..socketid.."] trigger "..event)
  108.                                 if args == nil then
  109.                                     args = {}
  110.                                 end
  111.                                 for index, func in pairs(socket.events[event]) do
  112.                                     func(unpack(args))
  113.                                 end
  114.                                 return socket
  115.                             end
  116.                             socket.emit('connect')
  117.                             clients[socket.id] = socket
  118.                             clients.count = clients.count + 1
  119.                             log("["..socketid.."] registered ")
  120.                             prototype.trigger("connect", {socket})
  121.                         elseif packet.id == PACKET_DATA and clients[socketid] then
  122.                             log("["..socketid.."] receiving data packet")
  123.                             local socket = nil
  124.                             if clients[socketid] ~= nil then
  125.                                 socket = clients[socketid]
  126.                             elseif packet.data.destination ~= nil then
  127.                                 socket = clients[packet.data.destination]
  128.                             end
  129.                             if socket ~= nil then
  130.                                 if packet.data.name ~= nil then
  131.                                     local args = {}
  132.                                     if packet.data.args == nil then
  133.                                         args = packet.data.args
  134.                                     end
  135.                                     socket.trigger(packet.data.name, args)
  136.                                 end
  137.                             end
  138.                         end
  139.                     end
  140.                     log("["..socketid.."] free")
  141.                 end
  142.                 sleep(0.1)
  143.             end
  144.         end
  145.        
  146.         prototype["on"] = function(event, func)
  147.             if server_events[event] == nil then
  148.                 server_events[event] = {}
  149.             end
  150.             table.insert(server_events[event], func)
  151.             return prototype
  152.         end
  153.  
  154.         prototype["trigger"] = function(event, args)
  155.             if args == nil then
  156.                 args = {}
  157.             end
  158.             for index, func in pairs(server_events[event]) do
  159.                 func(unpack(args))
  160.             end
  161.             return prototype
  162.         end
  163.     elseif socket_type == "client" or socket_type == nil then
  164.         local connected = false
  165.         local client_events = {}
  166.         local server_host = nil
  167.         local _listen = false
  168.  
  169.         prototype["send"] = function(packet)
  170.             if server_host ~= nil then
  171.                 log("Sending packet...")
  172.                 rednet.send(host, packet)
  173.             else
  174.                 log("Broadcasting packet...")
  175.                 rednet.broadcast(packet)
  176.             end
  177.             return prototype
  178.         end
  179.  
  180.         prototype["connect"] = function(host)
  181.             server_host = host
  182.             local packet = serialize(PACKET_CONNECT, {})
  183.             prototype.send(packet)
  184.             _listen = true
  185.             while _listen do
  186.                 local socketid, packet, distance = rednet.receive()
  187.                 if packet ~= nil then
  188.                     log("["..socketid.."] accepted packet")
  189.                     packet = unserialize(packet)
  190.                     log("["..socketid.."] check packet")
  191.                     if packet.data ~= nil then
  192.                         log("["..socketid.."] packet id "..packet.id)
  193.                         if packet.id == PACKET_DATA then
  194.                             if packet.data.name ~= nil then
  195.                                 local args = {}
  196.                                 if packet.data.args == nil then
  197.                                     args = packet.data.args
  198.                                 end
  199.                                 prototype.trigger(packet.data.name, args)
  200.                             end
  201.                         end
  202.                     else
  203.                         log("["..socketid.."] invalid packet")
  204.                     end
  205.                 end
  206.                 sleep(0.1)
  207.             end
  208.             return prototype
  209.         end
  210.        
  211.         prototype["disconnect"] = function(event, args)
  212.             return prototype
  213.         end
  214.        
  215.         prototype["on"] = function(event, func)
  216.             if client_events[event] == nil then
  217.                 client_events[event] = {}
  218.             end
  219.             table.insert(client_events[event], func)
  220.             return prototype
  221.         end
  222.  
  223.         prototype.on("connect", function()
  224.             log("Connected")
  225.         end)
  226.  
  227.         prototype["emit"] = function(event, args)
  228.             if args == nil then
  229.                 args = {}
  230.             end
  231.             local data = {}
  232.             data["name"] = event
  233.             data["args"] = args
  234.             local packet = serialize(PACKET_DATA, data)
  235.             prototype.send(packet)
  236.             return prototype
  237.         end
  238.  
  239.         prototype["trigger"] = function(event, args)
  240.             if args == nil then
  241.                 args = {}
  242.             end
  243.             for index, func in pairs(client_events[event]) do
  244.                 func(unpack(args))
  245.             end
  246.             return prototype
  247.         end
  248.     end
  249.     return prototype
  250. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement