Advertisement
osmarks

BundleNet

Aug 5th, 2019 (edited)
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. -- Minified CRC32 blob - MIT license - from here: https://raw.githubusercontent.com/davidm/lua-digest-crc32lua/master/lmod/digest/crc32lua.lua
  2. do
  3.     local type=type;local require=require;local setmetatable=setmetatable;local a=bit.bxor;local b=bit.bnot;local c=bit.band;local d=bit.brshift;local e=0xEDB88320;local function f(g)local h={}local i=setmetatable({},h)function h:__index(j)local k=g(j)i[j]=k;return k end;return i end;local l=f(function(m)local n=m;for o=1,8 do local p=c(n,1)n=d(n,1)if p==1 then n=a(n,e)end end;return n end)local function q(r,n)n=b(n or 0)local s=d(n,8)local t=l[a(n%256,r)]return b(a(s,t))end;local function u(v,n)n=n or 0;for m=1,#v do n=q(v:byte(m),n)end;return n end;function crc32(v,n)if type(v)=='string'then return u(v,n)else return q(v,n)end end
  4. end
  5.  
  6. local function get_byte(num, byte)
  7.     return bit.band(bit.brshift(num, byte * 8), 0xFF)
  8. end
  9.  
  10. local function from_bytes(b)
  11.     local n = 0
  12.     for ix, byte in pairs(b) do
  13.         n = bit.bor(n, bit.blshift(byte, (ix - 1) * 8))
  14.     end
  15.     return n
  16. end
  17.  
  18. local side = settings.get "bundlenet.side" or "back"
  19.  
  20. local function send_raw(str)
  21.     local i = 1
  22.     for i = 1, math.ceil(#str / 2) do
  23.         local first = str:byte(i * 2 - 1)
  24.         local second = str:byte(i * 2) or 0
  25.         local u16 = first * 256 + second
  26.         rs.setBundledOutput(side, u16)
  27.         sleep(0.1)
  28.     end
  29.     rs.setBundledOutput(side, 0)
  30. end
  31.  
  32. local function receive_raw(length)
  33.     local str = ""
  34.     local count = 0
  35.     os.pullEvent "redstone"
  36.     while true do
  37.         local u16 = rs.getBundledInput(side)
  38.         local first = string.char(math.floor(u16 / 256))
  39.         if not length and first == "\0" then break
  40.         else
  41.             count = count + 1
  42.             str = str .. first
  43.             if count == length then break end
  44.             local second = string.char(u16 % 256)
  45.             if not length and second == "\0" then break
  46.             else
  47.                 count = count + 1
  48.                 str = str .. second
  49.                 if count == length then break end
  50.             end
  51.         end
  52.         sleep(0.1)
  53.     end
  54.     return str
  55. end
  56.  
  57. local function u32_to_string(u32)
  58.     return string.char(get_byte(u32, 0), get_byte(u32, 1), get_byte(u32, 2), get_byte(u32, 3))
  59. end
  60.  
  61. local function string_to_u32(str)
  62.     return from_bytes{str:byte(1), str:byte(2), str:byte(3), str:byte(4)}
  63. end
  64.  
  65. local function send(data)
  66.     local length = u32_to_string(#data)
  67.     local checksum = u32_to_string(crc32(data))
  68.     print("len", length, "checksum", checksum)
  69.     send_raw(length)
  70.     send_raw(checksum)
  71.     send_raw(data)
  72. end
  73.  
  74. local function receive()
  75.     local length = receive_raw(4)
  76.     --sleep(0.1)
  77.     local checksum = receive_raw(4)
  78.     print("len", length, "checksum", checksum, "l", string_to_u32(length), "c", string_to_u32(checksum))
  79.     --sleep(0.1)
  80.     local data = receive_raw(string_to_u32(length))
  81.     if crc32(data) ~= string_to_u32(checksum) then return false, "checksum mismatch", data end
  82.     return true, data
  83. end
  84.  
  85. local option = ...
  86.  
  87. if option == "send" then
  88.     write "Send: "
  89.     local text = read()
  90.     send(text)
  91. elseif option == "raw_receive" then
  92.     print(receive_raw())
  93. elseif option == "receive" then
  94.     print(receive())
  95. end
  96.  
  97. return { send = send, receive = receive }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement