summaryrefslogtreecommitdiff
path: root/script/brave/work.lua
diff options
context:
space:
mode:
Diffstat (limited to 'script/brave/work.lua')
-rw-r--r--script/brave/work.lua33
1 files changed, 32 insertions, 1 deletions
diff --git a/script/brave/work.lua b/script/brave/work.lua
index 3a565e08..3b82b452 100644
--- a/script/brave/work.lua
+++ b/script/brave/work.lua
@@ -1,6 +1,6 @@
local brave = require 'brave.brave'
-brave.on('loadProto', function ()
+brave.on('loadProtoByStdio', function ()
local jsonrpc = require 'jsonrpc'
while true do
local proto, err = jsonrpc.decode(io.read)
@@ -13,6 +13,37 @@ brave.on('loadProto', function ()
end
end)
+brave.on('loadProtoBySocket', function (fdHandle)
+ local jsonrpc = require 'jsonrpc'
+ local socket = require 'bee.socket'
+ local thread = require 'bee.thread'
+ local fd = socket.fd(fdHandle)
+ 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)
+ end
+ end
+ end)
+ --log.debug('loaded proto', proto.method)
+ if not proto then
+ brave.push('protoerror', err)
+ return
+ end
+ brave.push('proto', proto)
+ end
+end)
+
brave.on('timer', function (time)
local thread = require 'bee.thread'
while true do