summaryrefslogtreecommitdiff
path: root/script-beta
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-11-12 11:29:46 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-11-12 11:29:46 +0800
commit812180e032c0149f0d96c00d418df3e4f9dc8c09 (patch)
treec71cf89ceba51a16f34baea6d2a45490a281ab56 /script-beta
parent9adba2607bfd7b10879492ceb7b8926723bf40d3 (diff)
downloadlua-language-server-812180e032c0149f0d96c00d418df3e4f9dc8c09.zip
#250 协议解析失败后通知主线程,由主线程来退出
Diffstat (limited to 'script-beta')
-rw-r--r--script-beta/brave/work.lua10
-rw-r--r--script-beta/jsonrpc.lua22
-rw-r--r--script-beta/pub/report.lua5
3 files changed, 22 insertions, 15 deletions
diff --git a/script-beta/brave/work.lua b/script-beta/brave/work.lua
index bee159d6..5ec8178f 100644
--- a/script-beta/brave/work.lua
+++ b/script-beta/brave/work.lua
@@ -3,15 +3,19 @@ local parser = require 'parser'
local fs = require 'bee.filesystem'
local furi = require 'file-uri'
local util = require 'utility'
+local thread = require 'bee.thread'
brave.on('loadProto', function ()
local jsonrpc = require 'jsonrpc'
while true do
- local proto = jsonrpc.decode(io.read, log.error)
+ local proto, err = jsonrpc.decode(io.read, log.error)
--log.debug('loaded proto', proto.method)
- if proto then
- brave.push('proto', proto)
+ if not proto then
+ brave.push('protoerror', err)
+ return
end
+ brave.push('proto', proto)
+ thread.sleep(0.001)
end
end)
diff --git a/script-beta/jsonrpc.lua b/script-beta/jsonrpc.lua
index d4b79c6e..17e6e73d 100644
--- a/script-beta/jsonrpc.lua
+++ b/script-beta/jsonrpc.lua
@@ -15,23 +15,20 @@ function m.encode(pack)
return buf
end
-local function readProtoHead(reader, errHandle)
+local function readProtoHead(reader)
local head = {}
while true do
local line = reader 'L'
if line == nil then
-- 说明管道已经关闭了
- log.warn('stdin closed!')
- os.exit(true)
- return nil
+ return nil, 'Disconnected!'
end
if line == '\r\n' then
break
end
local k, v = line:match '^([^:]+)%s*%:%s*(.+)\r\n$'
if not k then
- errHandle('Proto header error:', line)
- break
+ return nil, 'Proto header error: ' .. line
end
if k == 'Content-Length' then
v = tonumber(v)
@@ -42,23 +39,24 @@ local function readProtoHead(reader, errHandle)
end
function m.decode(reader, errHandle)
- local head = readProtoHead(reader, errHandle)
+ local head, err = readProtoHead(reader, errHandle)
+ if not head then
+ return nil, err
+ end
local len = head['Content-Length']
if not len then
- errHandle('Proto header error:', util.dump(head))
- return nil
+ return nil, 'Proto header error: ' .. util.dump(head)
end
local content = reader(len)
if not content then
- return nil
+ return nil, 'Proto read error'
end
local null = json.null
json.null = nil
local suc, res = pcall(json.decode, content)
json.null = null
if not suc then
- errHandle('Proto parse error: ' .. res)
- return nil
+ return nil, 'Proto parse error: ' .. res
end
return res
end
diff --git a/script-beta/pub/report.lua b/script-beta/pub/report.lua
index 66f3fca8..549277e1 100644
--- a/script-beta/pub/report.lua
+++ b/script-beta/pub/report.lua
@@ -19,3 +19,8 @@ pub.on('proto', function (params)
end
end)
end)
+
+pub.on('protoerror', function (err)
+ log.warn('Load proto error:', err)
+ os.exit(true)
+end)