summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server-beta/src/jsonrpc.lua40
-rw-r--r--server-beta/src/log.lua12
-rw-r--r--server-beta/src/pub/brave.lua33
-rw-r--r--server-beta/src/service.lua2
-rw-r--r--server-beta/src/work/init.lua11
-rw-r--r--server-beta/src/work/log.lua50
6 files changed, 131 insertions, 17 deletions
diff --git a/server-beta/src/jsonrpc.lua b/server-beta/src/jsonrpc.lua
index f92b12a1..b2bf1524 100644
--- a/server-beta/src/jsonrpc.lua
+++ b/server-beta/src/jsonrpc.lua
@@ -1,15 +1,41 @@
-local json = require 'json'
-local ioWrite = io.write
-local osClock = os.clock
+local json = require 'json'
+local pcall = pcall
_ENV = nil
-local TIMEOUT = 600.0
-local ID = 0
-local Cache = {}
-
---@class jsonrpc
local m = {}
m.type = 'jsonrpc'
+function m.encode(pack)
+ pack.jsonrpc = '2.0'
+ local content = json.encode(pack)
+ local buf = ('Content-Length: %d\r\n\r\n%s'):format(#content, content)
+ return buf
+end
+
+function m.decode(reader, errHandle)
+ -- 读取协议头
+ local line = reader 'l'
+ -- 不支持修改文本编码
+ if line:find('Content-Type', 1, true) then
+ return nil
+ end
+ local len = line:match('Content-Length: (%d+)')
+ if not len then
+ errHandle('Error header: ' .. line)
+ return nil
+ end
+ local content = reader(len + 2)
+ if not content then
+ return nil
+ end
+ local suc, res = pcall(json.decode, content)
+ if not suc then
+ errHandle('Proto parse error: ' .. res)
+ return nil
+ end
+ return res
+end
+
return m
diff --git a/server-beta/src/log.lua b/server-beta/src/log.lua
index 2f249a50..cd62df97 100644
--- a/server-beta/src/log.lua
+++ b/server-beta/src/log.lua
@@ -30,7 +30,7 @@ local function init_log_file()
end
end
-local function push_log(level, ...)
+local function pushLog(level, ...)
if not log.path then
return
end
@@ -68,23 +68,23 @@ local function push_log(level, ...)
end
function log.info(...)
- push_log('info', ...)
+ pushLog('info', ...)
end
function log.debug(...)
- push_log('debug', ...)
+ pushLog('debug', ...)
end
function log.trace(...)
- push_log('trace', ...)
+ pushLog('trace', ...)
end
function log.warn(...)
- push_log('warn', ...)
+ pushLog('warn', ...)
end
function log.error(...)
- push_log('error', ...)
+ pushLog('error', ...)
end
function log.init(root, path)
diff --git a/server-beta/src/pub/brave.lua b/server-beta/src/pub/brave.lua
index 7662a39e..1102e8a3 100644
--- a/server-beta/src/pub/brave.lua
+++ b/server-beta/src/pub/brave.lua
@@ -1,26 +1,53 @@
local thread = require 'bee.thread'
+local work = require 'work'
---@class pub_brave
local m = {}
m.type = 'pub.brave'
+m.ability = {}
--- 注册成为勇者
function m.register(id)
m.taskpad = thread.channel('taskpad' .. id)
m.waiter = thread.channel('waiter' .. id)
+ m.id = id
+ work.init()
m.start()
end
+--- 注册能力
+function m.on(name, callback)
+ m.ability[name] = callback
+end
+
+--- 报告
+function m.push(name, params)
+ m.waiter:push(name, params)
+end
+
--- 开始找工作
function m.start()
while true do
local suc, name, id, params = m.taskpad:pop()
if not suc then
-- 找不到工作的勇者,只好睡觉
- thread.sleep(0.01)
+ thread.sleep(0.001)
+ goto CONTINUE
+ end
+ local ability = m.ability[name]
+ -- TODO
+ if not ability then
+ m.waiter:push(id)
+ log.error('Brave can not handle this work: ' .. name)
+ goto CONTINUE
+ end
+ local suc, res = xpcall(ability, log.error, params)
+ if not suc then
+ m.waiter:push(id)
+ goto CONTINUE
end
- local result = require(name)(params)
- m.waiter:push(id, result)
+ m.waiter:push(id, res)
+ ::CONTINUE::
end
end
diff --git a/server-beta/src/service.lua b/server-beta/src/service.lua
index 0699043b..650fe558 100644
--- a/server-beta/src/service.lua
+++ b/server-beta/src/service.lua
@@ -39,7 +39,7 @@ function m.startTimer()
local current = os.clock()
local delta = current - last
last = current
- m.update(delta)
+ timer.update(delta)
end
end
diff --git a/server-beta/src/work/init.lua b/server-beta/src/work/init.lua
new file mode 100644
index 00000000..b77b970c
--- /dev/null
+++ b/server-beta/src/work/init.lua
@@ -0,0 +1,11 @@
+local brave = require 'pub.brave'
+local jsonrpc = require 'jsonrpc'
+
+brave.on('loadProto', function ()
+ while true do
+ local proto = jsonrpc.decode(io.read, log.error)
+ if proto then
+ return proto
+ end
+ end
+end)
diff --git a/server-beta/src/work/log.lua b/server-beta/src/work/log.lua
new file mode 100644
index 00000000..4059b461
--- /dev/null
+++ b/server-beta/src/work/log.lua
@@ -0,0 +1,50 @@
+local brave = require 'pub.brave'
+
+local tablePack = table.pack
+local tostring = tostring
+local tableConcat = table.concat
+local debugTraceBack = debug.traceback
+local debugGetInfo = debug.getinfo
+
+_ENV = nil
+
+local function pushLog(level, ...)
+ local t = tablePack(...)
+ for i = 1, t.n do
+ t[i] = tostring(t[i])
+ end
+ local str = tableConcat(t, '\t', 1, t.n)
+ if level == 'error' then
+ str = str .. '\n' .. debugTraceBack(nil, 3)
+ end
+ local info = debugGetInfo(3, 'Sl')
+ brave.push('log', {
+ msg = str,
+ info = info,
+ })
+ return str
+end
+
+local m = {}
+
+function m.info(...)
+ pushLog('info', ...)
+end
+
+function m.debug(...)
+ pushLog('debug', ...)
+end
+
+function m.trace(...)
+ pushLog('trace', ...)
+end
+
+function m.warn(...)
+ pushLog('warn', ...)
+end
+
+function m.error(...)
+ pushLog('error', ...)
+end
+
+return m