summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--script/brave/work.lua57
-rw-r--r--script/meta/bee/socket.lua7
-rw-r--r--script/proto/proto.lua13
3 files changed, 51 insertions, 26 deletions
diff --git a/script/brave/work.lua b/script/brave/work.lua
index 3b82b452..b3e79272 100644
--- a/script/brave/work.lua
+++ b/script/brave/work.lua
@@ -13,34 +13,47 @@ brave.on('loadProtoByStdio', function ()
end
end)
-brave.on('loadProtoBySocket', function (fdHandle)
+brave.on('loadProtoBySocket', function (param)
local jsonrpc = require 'jsonrpc'
local socket = require 'bee.socket'
- local thread = require 'bee.thread'
- local fd = socket.fd(fdHandle)
+ local rfd = socket.fd(param.rfd)
+ local wfd = socket.fd(param.wfd)
local buf = ''
- while true do
- local proto, err = jsonrpc.decode(function (len)
- while true do
- if #buf >= len then
- local res = buf:sub(1, len)
- buf = buf:sub(len + 1)
- return res
- end
- local data = fd:recv()
- if data then
- buf = buf .. data
- else
- thread.sleep(0.01)
+
+ ---@async
+ local parser = coroutine.create(function ()
+ while true do
+ ---@async
+ local proto, err = jsonrpc.decode(function (len)
+ while true do
+ if #buf >= len then
+ local res = buf:sub(1, len)
+ buf = buf:sub(len + 1)
+ return res
+ end
+ coroutine.yield()
end
+ end)
+ --log.debug('loaded proto', proto.method)
+ if not proto then
+ brave.push('protoerror', err)
+ return
end
- end)
- --log.debug('loaded proto', proto.method)
- if not proto then
- brave.push('protoerror', err)
- return
+ brave.push('proto', proto)
end
- brave.push('proto', proto)
+ end)
+
+ while true do
+ socket.select({rfd, wfd}, nil, 10)
+ local needSend = wfd:recv()
+ if needSend then
+ rfd:send(needSend)
+ end
+ local recved = rfd:recv()
+ if recved then
+ buf = buf .. recved
+ end
+ coroutine.resume(parser)
end
end)
diff --git a/script/meta/bee/socket.lua b/script/meta/bee/socket.lua
index b77c498b..1724cbb3 100644
--- a/script/meta/bee/socket.lua
+++ b/script/meta/bee/socket.lua
@@ -22,6 +22,10 @@ function socket.select(readfds, writefds, timeout) end
---@return bee.socket.fd
function socket.fd(handle) end
+---@return bee.socket.fd
+---@return bee.socket.fd
+function socket.pair() end
+
---@class bee.socket.fd
local fd = {}
@@ -53,6 +57,9 @@ function fd:send(content) end
---@return lightuserdata
function fd:handle() end
+---@return lightuserdata
+function fd:detach() end
+
---@return boolean
function fd:status() end
diff --git a/script/proto/proto.lua b/script/proto/proto.lua
index fb623106..7a196c35 100644
--- a/script/proto/proto.lua
+++ b/script/proto/proto.lua
@@ -237,10 +237,15 @@ function m.listen(mode, socketPort)
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())
+ local rfd = assert(socket('tcp'))
+ rfd:connect('127.0.0.1', socketPort)
+ local wfd1, wfd2 = socket.pair()
+ m.fd = wfd1
+ m.fdRefs = { rfd, wfd1, wfd2 }
+ pub.task('loadProtoBySocket', {
+ wfd = wfd2:handle(),
+ rfd = rfd:handle(),
+ })
end
end