1:f765e0e5649b
Anton Shestakov <av6@dwimlabs.net>, Wed, 07 Sep 2016 21:15:06 +0800
.luacheckrc: set globals to min+busted for tests, enable error codes

next change 2:2038aaa338fb
previous change 0:616025111516

hglib.lua

Permissions: -rw-r--r--

Other formats: Feeds:
local sizes = { i4 = 4, u4 = 4, c = 1 }
local function read_u4(rh)
local value = 0
for i = 1, sizes.u4 do
local byte = rh:read(1):byte()
value = value + byte * (2 ^ ((sizes.u4 - i) * 8))
end
return value
end
local function read_c(rh)
return rh:read(sizes.c)
end
local function read_channel(rh)
local channel = read_c(rh)
local length = read_u4(rh)
if channel == 'I' or channel == 'L' then
return channel, length
else
return channel, rh:read(length)
end
end
local Client = {}
Client.__index = Client
function Client.connect(rh, wh)
local client = {}
setmetatable(client, Client)
client.rh = rh
client.wh = wh
return client
end
function Client:read_hello()
local channel, message = read_channel(self.rh)
assert(channel == 'o', 'channel for the hello message must be "o"')
local index = 1
while index <= #message do
local nexteol = message:find('\n', index + 1, true)
if nexteol == nil then nexteol = #message + 1 end
local line = message:sub(index, nexteol - 1)
local ci = line:find(': ', 1, true)
if ci ~= nil then
local key, value = line:sub(1, ci - 1), line:sub(ci + 2)
if key == 'capabilities' then
self.capabilities = {}
for cap in value:gmatch('%S+') do
self.capabilities[cap] = true
end
elseif key == 'encoding' then
self.encoding = value
elseif key == 'pid' then
self.pid = tonumber(value)
elseif key == 'pgid' then
self.pgid = tonumber(value)
end
end
index = nexteol + 1
end
end
return {
read_u4 = read_u4,
read_c = read_c,
read_channel = read_channel,
Client = Client
}