--[[ lua 5.1.5 socket 2.0.2 ]]-- local socket = require("socket") localsub = string.sub localbyte = string.byte localconcat = table.concat localtonumber = tonumber localtostring = tostring local _M = { _VERSION = "0.1", } local mt = { __index = _M } function _M.new(self) local sock, err = socket.tcp() ifnot sock then returnnil, err end returnsetmetatable({_sock = sock, _subscribed = false }, mt) end function _M.connect(self, ...) local args = {...} local sock = rawget(self, "_sock") ifnot sock then returnnil, "not initialized" end self._subscribed = false return sock:connect(...) end function _M.close(self) local sock = rawget(self, "_sock") ifnot sock then returnnil, "not initialized" end return sock:close() end localfunction _gen_req(args) local nargs = #args local req = "" req = req .. "*" .. nargs .. "rn" for i = 1, nargs do localarg = args[i] iftype(arg) ~= "string"thenarg = tostring(arg) end req = req .. "$" req = req .. #arg req = req .. "rn" req = req .. arg req = req .. "rn" end -- print("req : ", req) return req end localfunction _read_reply(self, sock) local line, err = sock:receive() ifnot line then if err == "timeout"then sock:close() end returnnil, err end local prefix = byte(line) if prefix == 42then-- char "*" local n = tonumber(sub(line, 2)) if n < 0thenreturnnilend local vals = {} local ind = 1 for i = 1, n do local res, err = _read_reply(self, sock) if res then