summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2023-10-24 12:18:45 +0800
committer最萌小汐 <sumneko@hotmail.com>2023-10-24 12:18:45 +0800
commitadf8d529e0110f5b40a97ce02afd08822aeefe25 (patch)
treec7e1581b9f204766bc7794b52cbff7cdf2e6bdb1
parent2d4143cc1c5dd6a117264e0b6f4119b6f97b3d0b (diff)
downloadlua-language-server-adf8d529e0110f5b40a97ce02afd08822aeefe25.zip
update socket mode
#2382
-rw-r--r--script/brave/work.lua44
-rw-r--r--script/proto/proto.lua45
-rw-r--r--script/service/service.lua1
3 files changed, 54 insertions, 36 deletions
diff --git a/script/brave/work.lua b/script/brave/work.lua
index 9eb756eb..47cc9459 100644
--- a/script/brave/work.lua
+++ b/script/brave/work.lua
@@ -15,10 +15,7 @@ end)
brave.on('loadProtoBySocket', function (param)
local jsonrpc = require 'jsonrpc'
- local socket = require 'bee.socket'
- local util = require 'utility'
- local rfd = socket.fd(param.rfd)
- local wfd = socket.fd(param.wfd)
+ local net = require 'service.net'
local buf = ''
---@async
@@ -44,29 +41,24 @@ brave.on('loadProtoBySocket', function (param)
end
end)
+ local lsclient = net.connect('tcp', '127.0.0.1', param.port)
+ local lsmaster = net.connect('unix', param.unixPath)
+
+ assert(lsclient)
+ assert(lsmaster)
+
+ function lsclient:on_data(data)
+ buf = buf .. data
+ coroutine.resume(parser)
+ end
+
+ function lsmaster:on_data(data)
+ lsclient:write(data)
+ lsclient:update()
+ end
+
while true do
- local rd = socket.select({rfd, wfd}, nil, 10)
- if not rd or #rd == 0 then
- goto continue
- end
- if util.arrayHas(rd, wfd) then
- local needSend = wfd:recv()
- if needSend then
- rfd:send(needSend)
- elseif needSend == nil then
- error('socket closed!')
- end
- end
- if util.arrayHas(rd, rfd) then
- local recved = rfd:recv()
- if recved then
- buf = buf .. recved
- elseif recved == nil then
- error('socket closed!')
- end
- coroutine.resume(parser)
- end
- ::continue::
+ net.update(10)
end
end)
diff --git a/script/proto/proto.lua b/script/proto/proto.lua
index 73544ffc..991a3c60 100644
--- a/script/proto/proto.lua
+++ b/script/proto/proto.lua
@@ -8,6 +8,9 @@ local define = require 'proto.define'
local json = require 'json'
local inspect = require 'inspect'
local thread = require 'bee.thread'
+local fs = require 'bee.filesystem'
+local net = require 'service.net'
+local timer = require 'timer'
local reqCounter = util.counter()
@@ -32,8 +35,7 @@ m.ability = {}
m.waiting = {}
m.holdon = {}
m.mode = 'stdio'
----@type bee.socket.fd
-m.fd = nil
+m.client = nil
function m.getMethodName(proto)
if proto.method:sub(1, 2) == '$/' then
@@ -54,7 +56,8 @@ function m.send(data)
if m.mode == 'stdio' then
io.write(buf)
elseif m.mode == 'socket' then
- m.fd:send(buf)
+ m.client:write(buf)
+ m.client:update()
end
end
@@ -237,13 +240,37 @@ function m.listen(mode, socketPort)
io.stdout:setvbuf 'no'
pub.task('loadProtoByStdio')
elseif mode == 'socket' then
- local rfd = assert(socket('tcp'))
- rfd:connect('127.0.0.1', socketPort)
- local wfd1, wfd2 = socket.pair()
- m.fd = wfd1
+ local unixFolder = LOGPATH .. '/unix'
+ fs.create_directories(fs.path(unixFolder))
+ local unixPath = unixFolder .. '/' .. tostring(socketPort)
+
+ local server = net.listen('unix', unixPath)
+
+ assert(server)
+
+ local dummyClient = {
+ buf = '',
+ write = function (self, data)
+ self.buf = self.buf.. data
+ end,
+ update = function () end,
+ }
+ m.client = dummyClient
+
+ local t = timer.loop(0.1, function ()
+ net.update()
+ end)
+
+ function server:on_accept(client)
+ t:remove()
+ m.client = client
+ client:write(dummyClient.buf)
+ client:update()
+ end
+
pub.task('loadProtoBySocket', {
- wfd = wfd2:detach(),
- rfd = rfd:detach(),
+ port = socketPort,
+ unixPath = unixPath,
})
end
end
diff --git a/script/service/service.lua b/script/service/service.lua
index 7011ec4f..b6056390 100644
--- a/script/service/service.lua
+++ b/script/service/service.lua
@@ -257,7 +257,6 @@ function m.lockCache()
if err then
log.error(err)
end
- pub.task('removeCaches', cacheDir)
end
function m.start()