Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local VERSION = "1.0"
- local PACKET_HEARTBEAT = "heartbeat"
- local PACKET_CONNECT = "connect"
- local PACKET_DATA = "data"
- local DEBUG = nil
- function tablelength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- function serialize(id, data)
- local packet = {}
- packet["id"] = id
- packet["data"] = data
- return textutils.serialize(packet)..""
- end
- function unserialize(packet)
- return textutils.unserialize(packet)
- end
- function log(message)
- if DEBUG then
- print("[Debug] "..message)
- end
- end
- function create(socket_type, debug)
- DEBUG = debug
- rednet.open("back")
- prototype = {}
- if socket_type == "server" then
- local server_events = {}
- local clients = {}
- clients["count"] = 0
- local hostname = "Sockets Proxy Server"
- local maxclients = 30
- local _listen = false
- prototype["hostname"] = function(value)
- hostname = value
- return prototype
- end
- prototype["maxclients"] = function(value)
- maxclients = value
- return prototype
- end
- prototype["stop"] = function()
- _listen = false
- return prototype
- end
- prototype["listen"] = function()
- _listen = true
- while _listen do
- log("Wait for connection")
- local socketid, packet, distance = rednet.receive()
- local _socketid = socketid
- if packet ~= nil then
- log("["..socketid.."] accepting packet")
- packet = unserialize(packet)
- log("["..socketid.."] check packet")
- if packet.data ~= nil then
- log("["..socketid.."] packet id "..packet.id)
- if packet.data.destination ~= nil then
- log("["..socketid.."] client is proxy swapping to real client "..packet.data.destination)
- socketid = packet.data.destination
- end
- if packet.id == PACKET_CONNECT and clients[socketid] ~= nil then
- log("["..socketid.."] restored socket")
- local socket = clients[socketid]
- socket.emit('connect')
- prototype.trigger("connect", {clients[socketid]})
- elseif packet.id == PACKET_CONNECT and clients[socketid] == nil then
- log("["..socketid.."] creating socket")
- local socket = {}
- socket["id"] = socketid
- socket["events"] = {}
- socket["emit"] = function(event, args)
- if args == nil then
- args = {}
- end
- local data = {}
- data["name"] = event
- data["args"] = args
- if _socketid ~= socketid then
- data["destination"] = socketid
- end
- local packet = serialize(PACKET_DATA, data)
- rednet.send(_socketid, packet)
- end
- socket["on"] = function(event, func)
- if socket.events[event] == nil then
- socket.events[event] = {}
- end
- table.insert(socket.events[event], func)
- return socket
- end
- socket["trigger"] = function(event, args)
- log("["..socketid.."] trigger "..event)
- if args == nil then
- args = {}
- end
- for index, func in pairs(socket.events[event]) do
- func(unpack(args))
- end
- return socket
- end
- socket.emit('connect')
- clients[socket.id] = socket
- clients.count = clients.count + 1
- log("["..socketid.."] registered ")
- prototype.trigger("connect", {socket})
- elseif packet.id == PACKET_DATA and clients[socketid] then
- log("["..socketid.."] receiving data packet")
- local socket = nil
- if clients[socketid] ~= nil then
- socket = clients[socketid]
- elseif packet.data.destination ~= nil then
- socket = clients[packet.data.destination]
- end
- if socket ~= nil then
- if packet.data.name ~= nil then
- local args = {}
- if packet.data.args == nil then
- args = packet.data.args
- end
- socket.trigger(packet.data.name, args)
- end
- end
- end
- end
- log("["..socketid.."] free")
- end
- sleep(0.1)
- end
- end
- prototype["on"] = function(event, func)
- if server_events[event] == nil then
- server_events[event] = {}
- end
- table.insert(server_events[event], func)
- return prototype
- end
- prototype["trigger"] = function(event, args)
- if args == nil then
- args = {}
- end
- for index, func in pairs(server_events[event]) do
- func(unpack(args))
- end
- return prototype
- end
- elseif socket_type == "client" or socket_type == nil then
- local connected = false
- local client_events = {}
- local server_host = nil
- local _listen = false
- prototype["send"] = function(packet)
- if server_host ~= nil then
- log("Sending packet...")
- rednet.send(host, packet)
- else
- log("Broadcasting packet...")
- rednet.broadcast(packet)
- end
- return prototype
- end
- prototype["connect"] = function(host)
- server_host = host
- local packet = serialize(PACKET_CONNECT, {})
- prototype.send(packet)
- _listen = true
- while _listen do
- local socketid, packet, distance = rednet.receive()
- if packet ~= nil then
- log("["..socketid.."] accepted packet")
- packet = unserialize(packet)
- log("["..socketid.."] check packet")
- if packet.data ~= nil then
- log("["..socketid.."] packet id "..packet.id)
- if packet.id == PACKET_DATA then
- if packet.data.name ~= nil then
- local args = {}
- if packet.data.args == nil then
- args = packet.data.args
- end
- prototype.trigger(packet.data.name, args)
- end
- end
- else
- log("["..socketid.."] invalid packet")
- end
- end
- sleep(0.1)
- end
- return prototype
- end
- prototype["disconnect"] = function(event, args)
- return prototype
- end
- prototype["on"] = function(event, func)
- if client_events[event] == nil then
- client_events[event] = {}
- end
- table.insert(client_events[event], func)
- return prototype
- end
- prototype.on("connect", function()
- log("Connected")
- end)
- prototype["emit"] = function(event, args)
- if args == nil then
- args = {}
- end
- local data = {}
- data["name"] = event
- data["args"] = args
- local packet = serialize(PACKET_DATA, data)
- prototype.send(packet)
- return prototype
- end
- prototype["trigger"] = function(event, args)
- if args == nil then
- args = {}
- end
- for index, func in pairs(client_events[event]) do
- func(unpack(args))
- end
- return prototype
- end
- end
- return prototype
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement