diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2023-05-10 17:44:34 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2023-05-10 17:44:34 +0800 |
commit | 7318bbba9808b94c59202d301ac4faf4dc7f1f5a (patch) | |
tree | 9d82c000652aa31d9770677ba41eadb36874c938 /script/proto/proto.lua | |
parent | d7e59826b5dec2d6f4b9ba59f6d64af42c078edd (diff) | |
download | lua-language-server-7318bbba9808b94c59202d301ac4faf4dc7f1f5a.zip |
support connecting by socket with `--socket=PORT`
Diffstat (limited to 'script/proto/proto.lua')
-rw-r--r-- | script/proto/proto.lua | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/script/proto/proto.lua b/script/proto/proto.lua index 7875b919..fb623106 100644 --- a/script/proto/proto.lua +++ b/script/proto/proto.lua @@ -1,4 +1,5 @@ local subprocess = require 'bee.subprocess' +local socket = require 'bee.socket' local util = require 'utility' local await = require 'await' local pub = require 'pub' @@ -6,6 +7,7 @@ local jsonrpc = require 'jsonrpc' local define = require 'proto.define' local json = require 'json' local inspect = require 'inspect' +local thread = require 'bee.thread' local reqCounter = util.counter() @@ -29,6 +31,9 @@ local m = {} m.ability = {} m.waiting = {} m.holdon = {} +m.mode = 'stdio' +---@type bee.socket.fd +m.fd = nil function m.getMethodName(proto) if proto.method:sub(1, 2) == '$/' then @@ -46,7 +51,11 @@ end function m.send(data) local buf = jsonrpc.encode(data) logSend(buf) - io.write(buf) + if m.mode == 'stdio' then + io.write(buf) + elseif m.mode == 'socket' then + m.fd:send(buf) + end end function m.response(id, res) @@ -219,12 +228,20 @@ function m.doResponse(proto) waiting.resume(proto.result) end -function m.listen() - subprocess.filemode(io.stdin, 'b') - subprocess.filemode(io.stdout, 'b') - io.stdin:setvbuf 'no' - io.stdout:setvbuf 'no' - pub.task('loadProto') +function m.listen(mode, socketPort) + m.mode = mode + if mode == 'stdio' then + subprocess.filemode(io.stdin, 'b') + subprocess.filemode(io.stdout, 'b') + io.stdin:setvbuf 'no' + io.stdout:setvbuf 'no' + pub.task('loadProtoByStdio') + elseif mode == 'socket' then + local fd = assert(socket('tcp')) + fd:connect('127.0.0.1', socketPort) + m.fd = fd + pub.task('loadProtoBySocket', fd:handle()) + end end return m |