You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.1 KiB
Lua

-- 服务处理模版
local skynet = require "skynet"
local fsm = require "zeus.fsm"
local shiftimer = require "skynet.shiftimer"
---------------------------------------------------------------
---------------------------------------------------------------
local Template = class("Template"):include(singleton)
function Template:initialize()
self._cmds = {} -- 已注册的命令
self._shiftimer = shiftimer.new() -- 定时器
self._fsm = fsm.create({
initial = "none",
events = {{
name = "initial",
from = "none",
to = "initial",
}, {
name = "running",
from = "initial",
to = "running",
}, {
name = "stop",
from = "running",
to = "stop",
}, {
name = "exit",
from = "stop",
to = "exit",
}},
callbacks = {
on_initial = handler("_on_initial", self),
on_running = handler("_on_running", self),
on_stop = handler("_on_stop", self),
on_exit = handler("_on_exit", self),
},
})
end
function Template:get_fsm()
return self._fsm
end
function Template:_on_initial()
end
function Template:_on_running()
self._shiftimer:star()
end
function Template:_on_stop()
end
function Template:_on_exit()
self._shiftimer:stop()
end
function Template:reg_cmd(cmd)
if self.m_cmds[cmd] then
skynet.error(cmd, "was registered already")
return
end
self.m_cmds[cmd] = cmd
end
function Template:reg_cmd_ex(obj, cmd, ...)
assert(not self[cmd], "reg cmd handler exsit")
self[cmd] = function(_, ...)
return obj[cmd](obj, ...)
end
self:reg_cmd(cmd, ...)
end
function Template:get_cmd_func(cmd)
local func = self[cmd]
if func then
return func
end
return self.m_cmds[cmd]
end
function Template:add_timer(ti, func, args)
return self._shiftimer:add(ti * 100, func, args)
end
function Template:remove_timer(tid)
return self._shiftimer:delete(tid)
end
return Template