From aa4c8b521fc5a8417725a23dfc3d68bc8aae01d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 19 Sep 2019 20:19:54 +0800 Subject: =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-beta/src/log.lua | 2 +- server-beta/src/pub/client.lua | 53 ++++++++++++++++++++++++++---------------- server-beta/src/service.lua | 15 +++++++++++- server-beta/src/task.lua | 10 ++++---- 4 files changed, 54 insertions(+), 26 deletions(-) (limited to 'server-beta') diff --git a/server-beta/src/log.lua b/server-beta/src/log.lua index d8e782c0..2f249a50 100644 --- a/server-beta/src/log.lua +++ b/server-beta/src/log.lua @@ -84,7 +84,7 @@ function log.warn(...) end function log.error(...) - return push_log('error', ...) + push_log('error', ...) end function log.init(root, path) diff --git a/server-beta/src/pub/client.lua b/server-beta/src/pub/client.lua index 005bf9ce..8840b56a 100644 --- a/server-beta/src/pub/client.lua +++ b/server-beta/src/pub/client.lua @@ -48,30 +48,30 @@ end --- 给勇者推送任务 function m.pushTask(brave, name, params) local taskID = brave.counter() - local co = coroutine.running() brave.taskpad:push(name, taskID, params) - brave.taskList[taskID] = co - return coroutine.yield(co) + return task.wait(function (waker) + brave.taskList[taskID] = waker + end) end --- 从勇者处接收任务反馈 -function m.popTask(brave, id, params) - local co = brave.taskList[id] - if not co then +function m.popTask(brave, id, result) + local waker = brave.taskList[id] + if not waker then log.warn(('Brave pushed unknown task result: [%d] => [%d]'):format(brave.id, id)) return end brave.taskList[id] = nil - coroutine.resume(co, params) + waker(result) +end + +--- 从勇者处接收报告 +function m.popReport(brave, name, params) end --- 发布任务 ---@parma name string function m.task(name, params) - local _, main = coroutine.running() - if main then - error('不能在主线程中发布任务') - end for _, brave in ipairs(m.braves) do if m.isIdle(brave) then return m.pushTask(brave, name, params) @@ -80,18 +80,31 @@ function m.task(name, params) end --- 接收反馈 +---|返回接收到的反馈数量 +---@return integer function m.recieve() - local _, main = coroutine.running() - if main then - error('不能在主线程中接收反馈') - end - for _, brave in ipairs(m.braves) do - local suc, id, result - if not suc then - goto CONTINUE + local count = 0 + while true do + local hasRecived = false + for _, brave in ipairs(m.braves) do + local suc, id, result = brave.waiter:pop() + if not suc then + goto CONTINUE + end + count = count + 1 + hasRecived = true + if type(id) == 'string' then + m.popTask(brave, id, result) + else + m.popReport(brave, id, result) + end + ::CONTINUE:: + end + if not hasRecived then + break end - ::CONTINUE:: end + return count end return m diff --git a/server-beta/src/service.lua b/server-beta/src/service.lua index 639e2ae2..0699043b 100644 --- a/server-beta/src/service.lua +++ b/server-beta/src/service.lua @@ -21,10 +21,21 @@ function m.listenProto() end) end +function m.listenPub() + task.create(function () + while true do + local count = client.recieve() + if count == 0 then + task.sleep(0.001) + end + end + end) +end + function m.startTimer() local last = os.clock() while true do - thread.sleep(0.01) + thread.sleep(0.001) local current = os.clock() local delta = current - last last = current @@ -34,7 +45,9 @@ end function m.start() client.recruitBraves(4) + task.setErrorHandle(log.error) m.listenProto() + m.listenPub() m.startTimer() end diff --git a/server-beta/src/task.lua b/server-beta/src/task.lua index 23b6ae2f..264e2ac5 100644 --- a/server-beta/src/task.lua +++ b/server-beta/src/task.lua @@ -31,7 +31,7 @@ function m.sleep(time) m.errHandle(debug.traceback(co, err)) end end) - coroutine.yield() + return coroutine.yield() end --- 等待直到唤醒 @@ -44,11 +44,13 @@ function m.wait(waker) end return end - waker(function () - local suc, err = coroutine.resume(co) + waker(function (...) + local suc, err = coroutine.resume(co, ...) if not suc and m.errHandle then m.errHandle(debug.traceback(co, err)) end end) - coroutine.yield() + return coroutine.yield() end + +return m -- cgit v1.2.3