diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-18 14:30:52 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-18 14:30:52 +0800 |
commit | a064ab3a5c024b02235e041a2d0941fd094843cd (patch) | |
tree | 7d7b048c7ce7452c0423083acd513040988a6fac /server | |
parent | 8e7cea5ebe6e41b817bda532ad077013f12bb4e5 (diff) | |
download | lua-language-server-a064ab3a5c024b02235e041a2d0941fd094843cd.zip |
简单的rpc
Diffstat (limited to 'server')
-rw-r--r-- | server/src/rpc.lua | 60 | ||||
-rw-r--r-- | server/src/service.lua | 22 |
2 files changed, 70 insertions, 12 deletions
diff --git a/server/src/rpc.lua b/server/src/rpc.lua new file mode 100644 index 00000000..559853e0 --- /dev/null +++ b/server/src/rpc.lua @@ -0,0 +1,60 @@ +local json = require 'json' + +local TIMEOUT = 1.0 + +local ID = 0 +local BUF = {} + +local function notify(self, data) + data.jsonrpc = '2.0' + local content = json.encode(data) + local buf = ('Content-Length: %d\r\n\r\n%s'):format(#content, content) + io.write(buf) +end + +local function request(self, data, callback) + ID = ID + 1 + data.jsonrpc = '2.0' + data.id = ID + local content = json.encode(data) + local buf = ('Content-Length: %d\r\n\r\n%s'):format(#content, content) + BUF[ID] = { + callback = callback, + timeout = os.clock() + TIMEOUT, + } + io.write(buf) +end + +local function response(self, id, data) + data.jsonrpc = '2.0' + data.id = id + local content = json.encode(data) + local buf = ('Content-Length: %d\r\n\r\n%s'):format(#content, content) + io.write(buf) +end + +local function recieve(self, proto) + local id = proto.id + local data = BUF[id] + log.warn('Recieve id not found: ', table.dump(proto)) + if not data then + return + end + BUF[id] = nil + if os.clock() > data.timeout then + log.warn('Recieve timeout: ', table.dump(proto.error)) + return + end + if proto.result then + data.callback(proto.result) + else + log.warn('Recieve: ', table.dump(proto.error)) + end +end + +return { + notify = notify, + request = request, + response = response, + recieve = recieve, +} diff --git a/server/src/service.lua b/server/src/service.lua index 99eb2656..fd748a96 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -1,7 +1,7 @@ local subprocess = require 'bee.subprocess' local method = require 'method' local thread = require 'thread' -local json = require 'json' +local rpc = require 'rpc' local parser = require 'parser' local matcher = require 'matcher' @@ -60,13 +60,6 @@ function mt:_callMethod(name, params) end end -function mt:_send(data) - data.jsonrpc = '2.0' - local content = json.encode(data) - local buf = ('Content-Length: %d\r\n\r\n%s'):format(#content, content) - io.write(buf) -end - function mt:_doProto(proto) local id = proto.id local name = proto.method @@ -76,13 +69,12 @@ function mt:_doProto(proto) return end local container = table.container() - container.id = id if err then container.error = err else container.result = response end - self:_send(container) + rpc:response(id, container) end function mt:_doDiagnostic() @@ -101,7 +93,7 @@ function mt:_doDiagnostic() local name = 'textDocument/publishDiagnostics' local res = self:_callMethod(name, data) if res then - self:_send { + rpc:notify { method = name, params = { uri = uri, @@ -219,7 +211,13 @@ end function mt:on_tick() local proto = thread.proto() if proto then - self:_doProto(proto) + if proto.method then + self:_doProto(proto) + elseif proto.result or proto.error then + rpc:recieve(proto) + else + log.warn('Unknow proto type.') + end end self:_buildTextCache() self:_doDiagnostic() |