Skip to content

Commit adf8d52

Browse files
committed
update socket mode
#2382
1 parent 2d4143c commit adf8d52

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

script/brave/work.lua

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ end)
1515

1616
brave.on('loadProtoBySocket', function (param)
1717
local jsonrpc = require 'jsonrpc'
18-
local socket = require 'bee.socket'
19-
local util = require 'utility'
20-
local rfd = socket.fd(param.rfd)
21-
local wfd = socket.fd(param.wfd)
18+
local net = require 'service.net'
2219
local buf = ''
2320

2421
---@async
@@ -44,29 +41,24 @@ brave.on('loadProtoBySocket', function (param)
4441
end
4542
end)
4643

44+
local lsclient = net.connect('tcp', '127.0.0.1', param.port)
45+
local lsmaster = net.connect('unix', param.unixPath)
46+
47+
assert(lsclient)
48+
assert(lsmaster)
49+
50+
function lsclient:on_data(data)
51+
buf = buf .. data
52+
coroutine.resume(parser)
53+
end
54+
55+
function lsmaster:on_data(data)
56+
lsclient:write(data)
57+
lsclient:update()
58+
end
59+
4760
while true do
48-
local rd = socket.select({rfd, wfd}, nil, 10)
49-
if not rd or #rd == 0 then
50-
goto continue
51-
end
52-
if util.arrayHas(rd, wfd) then
53-
local needSend = wfd:recv()
54-
if needSend then
55-
rfd:send(needSend)
56-
elseif needSend == nil then
57-
error('socket closed!')
58-
end
59-
end
60-
if util.arrayHas(rd, rfd) then
61-
local recved = rfd:recv()
62-
if recved then
63-
buf = buf .. recved
64-
elseif recved == nil then
65-
error('socket closed!')
66-
end
67-
coroutine.resume(parser)
68-
end
69-
::continue::
61+
net.update(10)
7062
end
7163
end)
7264

script/proto/proto.lua

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ local define = require 'proto.define'
88
local json = require 'json'
99
local inspect = require 'inspect'
1010
local thread = require 'bee.thread'
11+
local fs = require 'bee.filesystem'
12+
local net = require 'service.net'
13+
local timer = require 'timer'
1114

1215
local reqCounter = util.counter()
1316

@@ -32,8 +35,7 @@ m.ability = {}
3235
m.waiting = {}
3336
m.holdon = {}
3437
m.mode = 'stdio'
35-
---@type bee.socket.fd
36-
m.fd = nil
38+
m.client = nil
3739

3840
function m.getMethodName(proto)
3941
if proto.method:sub(1, 2) == '$/' then
@@ -54,7 +56,8 @@ function m.send(data)
5456
if m.mode == 'stdio' then
5557
io.write(buf)
5658
elseif m.mode == 'socket' then
57-
m.fd:send(buf)
59+
m.client:write(buf)
60+
m.client:update()
5861
end
5962
end
6063

@@ -237,13 +240,37 @@ function m.listen(mode, socketPort)
237240
io.stdout:setvbuf 'no'
238241
pub.task('loadProtoByStdio')
239242
elseif mode == 'socket' then
240-
local rfd = assert(socket('tcp'))
241-
rfd:connect('127.0.0.1', socketPort)
242-
local wfd1, wfd2 = socket.pair()
243-
m.fd = wfd1
243+
local unixFolder = LOGPATH .. '/unix'
244+
fs.create_directories(fs.path(unixFolder))
245+
local unixPath = unixFolder .. '/' .. tostring(socketPort)
246+
247+
local server = net.listen('unix', unixPath)
248+
249+
assert(server)
250+
251+
local dummyClient = {
252+
buf = '',
253+
write = function (self, data)
254+
self.buf = self.buf.. data
255+
end,
256+
update = function () end,
257+
}
258+
m.client = dummyClient
259+
260+
local t = timer.loop(0.1, function ()
261+
net.update()
262+
end)
263+
264+
function server:on_accept(client)
265+
t:remove()
266+
m.client = client
267+
client:write(dummyClient.buf)
268+
client:update()
269+
end
270+
244271
pub.task('loadProtoBySocket', {
245-
wfd = wfd2:detach(),
246-
rfd = rfd:detach(),
272+
port = socketPort,
273+
unixPath = unixPath,
247274
})
248275
end
249276
end

script/service/service.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ function m.lockCache()
257257
if err then
258258
log.error(err)
259259
end
260-
pub.task('removeCaches', cacheDir)
261260
end
262261

263262
function m.start()

0 commit comments

Comments
 (0)