Skip to content

Commit

Permalink
update socket mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Oct 24, 2023
1 parent 2d4143c commit adf8d52
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
44 changes: 18 additions & 26 deletions script/brave/work.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
45 changes: 36 additions & 9 deletions script/proto/proto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion script/service/service.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ function m.lockCache()
if err then
log.error(err)
end
pub.task('removeCaches', cacheDir)
end

function m.start()
Expand Down

0 comments on commit adf8d52

Please sign in to comment.