Advertisement
osmarks

Skynet

Sep 9th, 2018
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. -- download JSON library
  2. local a=http.get"https://pastebin.com/raw/4nRg9CHU"local b=fs.open("json","w")b.write(a.readAll())a.close()b.close()
  3.  
  4. os.loadAPI "json" -- whyyyyyy
  5.  
  6. local skynet = {
  7.     server = "wss://osmarks.tk/skynet/connect/",
  8.     socket = nil,
  9.     open_channels = {},
  10.     json = json
  11. }
  12.  
  13. function skynet.connect(force)
  14.     if not skynet.socket or force then
  15.         -- If we already have a socket and are throwing it away, close old one.
  16.         if skynet.socket then skynet.socket.close() end
  17.         sock = http.websocket(skynet.server)
  18.         if not sock then error "Skynet server unavailable, broken or running newer protocol version." end
  19.         skynet.socket = sock
  20.        
  21.         for _, c in pairs(skynet.open_channels) do
  22.             skynet.open(channel)   
  23.         end
  24.     end
  25. end
  26.  
  27. function skynet.disconnect()
  28.     if skynet.socket then skynet.socket.close() end
  29. end
  30.  
  31. local function value_in_table(t, v)
  32.     for k, tv in pairs(t) do if tv == v then return true, k end end
  33.     return false
  34. end
  35.  
  36. local function send_raw(data)
  37.     skynet.connect()
  38.     skynet.socket.send(json.encode(data))
  39. end
  40.  
  41. -- Opens the given channel
  42. function skynet.open(channel)
  43.     -- Don't send unnecessary channel-open messages
  44.     if not value_in_table(skynet.open_channels, channel) then
  45.         send_raw {
  46.             type = "open",
  47.             channel = channel
  48.         }
  49.         table.insert(skynet.open_channels, channel)
  50.     end
  51. end
  52.  
  53. local function recv_one(filter)
  54.     skynet.connect()
  55.     while true do
  56.         local contents = skynet.socket.receive()
  57.         local result = json.decode(contents)
  58.         if type(result) == "table" and filter(result) then
  59.             return result
  60.         end
  61.     end
  62. end
  63.  
  64. local function recv_message(channel)
  65.     local m = recv_one(function(msg)
  66.         return msg.type == "message" and (channel == nil or msg.channel == channel)
  67.     end)
  68.     return m.channel, m.message, m
  69. end
  70.  
  71. local function recv_result(for_cmd)
  72.     return recv_one(function(m)
  73.         if m.type == "error" then
  74.             error(m.error)
  75.         elseif m.type == "result" and m["for"] == for_cmd then
  76.             return true
  77.         end
  78.     end)
  79. end
  80.  
  81. function skynet.logs()
  82.     send_raw {
  83.         type = "log"
  84.     }
  85.     return recv_result "log".log
  86. end
  87.  
  88. local listener_running = false
  89. -- Converts "websocket_message"s into "skynet_message"s.
  90. function skynet.listen(force_run)
  91.     local function run()
  92.         while true do
  93.             os.queueEvent("skynet_message", recv_message())
  94.         end
  95.     end
  96.     if not listener_running or force_run then
  97.         local ok, err = pcall(run)
  98.         listener_running = false
  99.         if not ok then
  100.             error(err)
  101.         end
  102.     end
  103. end
  104.  
  105. -- Receives one message on given channel
  106. -- Will open channel if it is not already open
  107. -- Returns the channel, message, and full message object
  108. function skynet.receive(channel)
  109.     if channel then skynet.open(channel) end
  110.     return recv_message(channel)
  111. end
  112.  
  113. -- Send given data on given channel
  114. -- Can accept a third argument - an object of extra metadata to send
  115. function skynet.send(channel, data, full)
  116.     local obj = full or {}
  117.     obj.type = "message"
  118.     obj.message = data
  119.     obj.channel = channel
  120.     send_raw(obj)
  121. end
  122.  
  123. return skynet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement