OC-PsychOS/modules/lib/relib.lua

26 lines
829 B
Lua
Raw Normal View History

_G.relib = {}
function relib.encode(p)
p.port = p.port or 0
local p2,p1 = p.port%256, math.floor(p.port/256)
p.pid = p.pid or string.char(math.random(255),math.random(255),math.random(255))
p.at = p.at or 0
p.pt = p.pt or 1
-- local data = string.char(p1,p2)..string.char(math.random(255),math.random(255),math.random(255),at%256,pt%256)..msg:sub(1,1500)
local data = string.char(p1,p2)..p.pid..string.char(p.at%256,p.pt%256)..p.msg:sub(1,1500)
return data
end
function relib.decode(dat)
if type(dat) == "string" then
if dat:len() > 7 then
local p = {}
p.port = tonumber(string.format("%x%x",string.byte(dat:sub(1,1)),string.byte(dat:sub(2,2))),16)
p.pid = dat:sub(3,5)
p.at = string.byte(dat:sub(6,6))
p.pt = string.byte(dat:sub(7,7))
p.msg = dat:sub(8,1507)
return p
end
end
return false
end