summaryrefslogtreecommitdiff
path: root/server/src/async/proto.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-01-22 15:18:27 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-01-22 15:18:27 +0800
commit4ffc826d7ce5ca57c2929e040ed5eefb53f9d115 (patch)
tree9087cb647ac573848cd44f6660cb7a460ea38e60 /server/src/async/proto.lua
parentce28b76cfc597e0cef3a62d8f7e181cf7ff4c904 (diff)
downloadlua-language-server-4ffc826d7ce5ca57c2929e040ed5eefb53f9d115.zip
整理代码
Diffstat (limited to 'server/src/async/proto.lua')
-rw-r--r--server/src/async/proto.lua59
1 files changed, 59 insertions, 0 deletions
diff --git a/server/src/async/proto.lua b/server/src/async/proto.lua
new file mode 100644
index 00000000..4570de2e
--- /dev/null
+++ b/server/src/async/proto.lua
@@ -0,0 +1,59 @@
+local thread = require 'bee.thread'
+local json = require 'json'
+local proto = thread.channel 'proto'
+local errlog = thread.channel 'errlog'
+
+local function pushError(...)
+ local t = table.pack(...)
+ for i = 1, t.n do
+ t[i] = tostring(t[i])
+ end
+ local buf = table.concat(t, '\t')
+ errlog:push(buf)
+end
+
+local function readProtoHeader()
+ local header = io.read 'l'
+ if header:sub(1, #'Content-Length') == 'Content-Length' then
+ return header
+ elseif header:sub(1, #'Content-Type') == 'Content-Type' then
+ return nil
+ else
+ pushError('Proto header error:', header)
+ return nil
+ end
+end
+
+local function readProtoContent(header)
+ local len = tonumber(header:match('%d+'))
+ if not len then
+ pushError('Proto header error:', header)
+ return nil
+ end
+ local buf = io.read(len+2)
+ if not buf then
+ return nil
+ end
+ local suc, res = pcall(json.decode, buf)
+ if not suc then
+ pushError('Proto error:', buf)
+ return nil
+ end
+ return res
+end
+
+local function readProto()
+ local header = readProtoHeader()
+ if not header then
+ return
+ end
+ local data = readProtoContent(header)
+ if not data then
+ return
+ end
+ proto:push(data)
+end
+
+while true do
+ readProto()
+end