Skip to content

Instantly share code, notes, and snippets.

@comboy
Created June 7, 2016 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comboy/f6d7084fa3740cda3f3461d2c5c89a88 to your computer and use it in GitHub Desktop.
Save comboy/f6d7084fa3740cda3f3461d2c5c89a88 to your computer and use it in GitHub Desktop.
local band = bit.band
local bor = bit.bor
local rshift = bit.rshift
local char = string.char
local payload = _G.do_input.payload
local opcode = _G.do_input.opcode
opcode = opcode or 2
assert(type(opcode) == "number", "opcode must be number")
assert(type(payload) == "string", "payload must be string")
local len = #payload
local head = char(
bor(0x80, opcode),
bor(len < 126 and bor(0x80, len) or len < 0x10000 and 254 or 255)
)
if len >= 0x10000 then
head = head .. char(
0,0,0,0, -- 32 bit length is plenty, assume zero for rest
band(rshift(len, 24), 0xff),
band(rshift(len, 16), 0xff),
band(rshift(len, 8), 0xff),
band(len, 0xff)
)
elseif len >= 126 then
head = head .. char(band(rshift(len, 8), 0xff), band(len, 0xff))
end
head = head .. char(0,0,0,0)
return head .. payload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment