diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-05-22 18:03:56 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-05-22 18:04:01 +0800 |
commit | d8f2ce4a7e7068f0d81cd48a4589226eec249415 (patch) | |
tree | 6ec1054ef804d658f67f8daa74f16c78c6b52478 /script/brave | |
parent | 86e2d589c30e73198226b11c4248a31fada4e620 (diff) | |
download | lua-language-server-d8f2ce4a7e7068f0d81cd48a4589226eec249415.zip |
better socket
Diffstat (limited to 'script/brave')
-rw-r--r-- | script/brave/work.lua | 57 |
1 files changed, 35 insertions, 22 deletions
diff --git a/script/brave/work.lua b/script/brave/work.lua index 3b82b452..b3e79272 100644 --- a/script/brave/work.lua +++ b/script/brave/work.lua @@ -13,34 +13,47 @@ brave.on('loadProtoByStdio', function () end end) -brave.on('loadProtoBySocket', function (fdHandle) +brave.on('loadProtoBySocket', function (param) local jsonrpc = require 'jsonrpc' local socket = require 'bee.socket' - local thread = require 'bee.thread' - local fd = socket.fd(fdHandle) + local rfd = socket.fd(param.rfd) + local wfd = socket.fd(param.wfd) local buf = '' - while true do - local proto, err = jsonrpc.decode(function (len) - while true do - if #buf >= len then - local res = buf:sub(1, len) - buf = buf:sub(len + 1) - return res - end - local data = fd:recv() - if data then - buf = buf .. data - else - thread.sleep(0.01) + + ---@async + local parser = coroutine.create(function () + while true do + ---@async + local proto, err = jsonrpc.decode(function (len) + while true do + if #buf >= len then + local res = buf:sub(1, len) + buf = buf:sub(len + 1) + return res + end + coroutine.yield() end + end) + --log.debug('loaded proto', proto.method) + if not proto then + brave.push('protoerror', err) + return end - end) - --log.debug('loaded proto', proto.method) - if not proto then - brave.push('protoerror', err) - return + brave.push('proto', proto) end - brave.push('proto', proto) + end) + + while true do + socket.select({rfd, wfd}, nil, 10) + local needSend = wfd:recv() + if needSend then + rfd:send(needSend) + end + local recved = rfd:recv() + if recved then + buf = buf .. recved + end + coroutine.resume(parser) end end) |