From 6b222a0a14135e6f39eb9e80f3c2bf879811ca2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 8 Nov 2019 14:59:03 +0800 Subject: =?UTF-8?q?=E5=8D=8F=E4=BD=9C=E6=A8=A1=E5=BC=8F=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-beta/src/await.lua | 38 ++++++++++++++++++++++++++++++--- server-beta/src/provider/diagnostic.lua | 2 +- server-beta/src/pub/pub.lua | 8 +++---- server-beta/src/service/service.lua | 8 ++++--- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/server-beta/src/await.lua b/server-beta/src/await.lua index ee64e896..37cade79 100644 --- a/server-beta/src/await.lua +++ b/server-beta/src/await.lua @@ -5,6 +5,8 @@ local m = {} m.type = 'await' m.coTracker = setmetatable({}, { __mode = 'k' }) +m.delayQueue = {} +m.delayQueueIndex = 1 --- 设置错误处理器 ---@param errHandle function {comment = '当有错误发生时,会以错误堆栈为参数调用该函数'} @@ -44,8 +46,8 @@ function m.sleep(time, ...) end --- 等待直到唤醒 ----@param waker function -function m.wait(waker, ...) +---@param callback function +function m.wait(callback, ...) local co, main = coroutine.running() if main then if m.errorHandle then @@ -53,10 +55,40 @@ function m.wait(waker, ...) end return end - waker(function (...) + callback(function (...) return m.checkResult(co, coroutine.resume(co, ...)) end) return coroutine.yield(...) end +--- 延迟 +function m.delay(...) + local co, main = coroutine.running() + if main then + if m.errorHandle then + m.errorHandle(debug.traceback('Cant wait in main thread')) + end + return + end + m.delayQueue[#m.delayQueue+1] = function (...) + return m.checkResult(co, coroutine.resume(co, ...)) + end + return coroutine.yield(...) +end + +--- 步进 +function m.step() + local waker = m.delayQueue[m.delayQueueIndex] + if waker then + m.delayQueueIndex = m.delayQueueIndex + 1 + waker() + return true + else + for i = 1, #m.delayQueue do + m.delayQueue[i] = nil + end + return false + end +end + return m diff --git a/server-beta/src/provider/diagnostic.lua b/server-beta/src/provider/diagnostic.lua index 2b4ae9b3..d7d4be1e 100644 --- a/server-beta/src/provider/diagnostic.lua +++ b/server-beta/src/provider/diagnostic.lua @@ -173,7 +173,7 @@ function m.refresh(uri) for destUri in files.eachFile() do if destUri ~= uri then m.doDiagnostic(files.getOriginUri(destUri)) - await.sleep(0.001) + await.delay() if myVersion ~= m.version then return end diff --git a/server-beta/src/pub/pub.lua b/server-beta/src/pub/pub.lua index 3e97fe9f..19800e95 100644 --- a/server-beta/src/pub/pub.lua +++ b/server-beta/src/pub/pub.lua @@ -226,11 +226,9 @@ function m.checkDead() end end -function m.listen() - timer.loop(0.001, function () - m.checkDead() - m.recieve() - end) +function m.step() + m.checkDead() + m.recieve() end return m diff --git a/server-beta/src/service/service.lua b/server-beta/src/service/service.lua index 6ff82897..1d65a1ea 100644 --- a/server-beta/src/service/service.lua +++ b/server-beta/src/service/service.lua @@ -73,8 +73,11 @@ end function m.startTimer() while true do - thread.sleep(0.001) - timer.update() + pub.step() + if not await.step() then + thread.sleep(0.001) + timer.update() + end end end @@ -82,7 +85,6 @@ function m.start() await.setErrorHandle(log.error) pub.recruitBraves(4) proto.listen() - pub.listen() m.report() require 'provider' -- cgit v1.2.3