Skip to content

Commit d8f2ce4

Browse files
committed
better socket
1 parent 86e2d58 commit d8f2ce4

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

script/brave/work.lua

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,47 @@ brave.on('loadProtoByStdio', function ()
1313
end
1414
end)
1515

16-
brave.on('loadProtoBySocket', function (fdHandle)
16+
brave.on('loadProtoBySocket', function (param)
1717
local jsonrpc = require 'jsonrpc'
1818
local socket = require 'bee.socket'
19-
local thread = require 'bee.thread'
20-
local fd = socket.fd(fdHandle)
19+
local rfd = socket.fd(param.rfd)
20+
local wfd = socket.fd(param.wfd)
2121
local buf = ''
22-
while true do
23-
local proto, err = jsonrpc.decode(function (len)
24-
while true do
25-
if #buf >= len then
26-
local res = buf:sub(1, len)
27-
buf = buf:sub(len + 1)
28-
return res
29-
end
30-
local data = fd:recv()
31-
if data then
32-
buf = buf .. data
33-
else
34-
thread.sleep(0.01)
22+
23+
---@async
24+
local parser = coroutine.create(function ()
25+
while true do
26+
---@async
27+
local proto, err = jsonrpc.decode(function (len)
28+
while true do
29+
if #buf >= len then
30+
local res = buf:sub(1, len)
31+
buf = buf:sub(len + 1)
32+
return res
33+
end
34+
coroutine.yield()
3535
end
36+
end)
37+
--log.debug('loaded proto', proto.method)
38+
if not proto then
39+
brave.push('protoerror', err)
40+
return
3641
end
37-
end)
38-
--log.debug('loaded proto', proto.method)
39-
if not proto then
40-
brave.push('protoerror', err)
41-
return
42+
brave.push('proto', proto)
4243
end
43-
brave.push('proto', proto)
44+
end)
45+
46+
while true do
47+
socket.select({rfd, wfd}, nil, 10)
48+
local needSend = wfd:recv()
49+
if needSend then
50+
rfd:send(needSend)
51+
end
52+
local recved = rfd:recv()
53+
if recved then
54+
buf = buf .. recved
55+
end
56+
coroutine.resume(parser)
4457
end
4558
end)
4659

script/meta/bee/socket.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function socket.select(readfds, writefds, timeout) end
2222
---@return bee.socket.fd
2323
function socket.fd(handle) end
2424

25+
---@return bee.socket.fd
26+
---@return bee.socket.fd
27+
function socket.pair() end
28+
2529
---@class bee.socket.fd
2630
local fd = {}
2731

@@ -53,6 +57,9 @@ function fd:send(content) end
5357
---@return lightuserdata
5458
function fd:handle() end
5559

60+
---@return lightuserdata
61+
function fd:detach() end
62+
5663
---@return boolean
5764
function fd:status() end
5865

script/proto/proto.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,15 @@ function m.listen(mode, socketPort)
237237
io.stdout:setvbuf 'no'
238238
pub.task('loadProtoByStdio')
239239
elseif mode == 'socket' then
240-
local fd = assert(socket('tcp'))
241-
fd:connect('127.0.0.1', socketPort)
242-
m.fd = fd
243-
pub.task('loadProtoBySocket', fd:handle())
240+
local rfd = assert(socket('tcp'))
241+
rfd:connect('127.0.0.1', socketPort)
242+
local wfd1, wfd2 = socket.pair()
243+
m.fd = wfd1
244+
m.fdRefs = { rfd, wfd1, wfd2 }
245+
pub.task('loadProtoBySocket', {
246+
wfd = wfd2:handle(),
247+
rfd = rfd:handle(),
248+
})
244249
end
245250
end
246251

0 commit comments

Comments
 (0)