summaryrefslogtreecommitdiff
path: root/script/jsonrpc.lua
diff options
context:
space:
mode:
Diffstat (limited to 'script/jsonrpc.lua')
-rw-r--r--script/jsonrpc.lua13
1 files changed, 11 insertions, 2 deletions
diff --git a/script/jsonrpc.lua b/script/jsonrpc.lua
index 7411fee8..6de6da5b 100644
--- a/script/jsonrpc.lua
+++ b/script/jsonrpc.lua
@@ -14,17 +14,23 @@ function m.encode(pack)
return buf
end
+---@param reader fun(arg: integer):string
local function readProtoHead(reader)
local head = {}
+ local line = ''
while true do
- local line = reader 'L'
- if line == nil then
+ local char = reader(1)
+ if char == nil then
-- 说明管道已经关闭了
return nil, 'Disconnected!'
end
+ line = line .. char
if line == '\r\n' then
break
end
+ if line:sub(-2) ~= '\r\n' then
+ goto continue
+ end
local k, v = line:match '^([^:]+)%s*%:%s*(.+)\r\n$'
if not k then
return nil, 'Proto header error: ' .. line
@@ -33,10 +39,13 @@ local function readProtoHead(reader)
v = tonumber(v)
end
head[k] = v
+ line = ''
+ ::continue::
end
return head
end
+---@param reader fun(arg: integer):string
function m.decode(reader)
local head, err = readProtoHead(reader)
if not head then