0 Star 0 Fork 0

蒋佳李/leaf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
skeleton.go 2.73 KB
一键复制 编辑 原始数据 按行查看 历史
蒋佳李 提交于 2023-02-28 22:10 . 删除console、cluster功能
package module
import (
"gitee.com/jiangjiali/leaf/chanrpc"
"gitee.com/jiangjiali/leaf/go"
"gitee.com/jiangjiali/leaf/timer"
"time"
)
type Skeleton struct {
GoLen int
TimerDispatcherLen int
AsynCallLen int
ChanRPCServer *chanrpc.Server
g *g.Go
dispatcher *timer.Dispatcher
client *chanrpc.Client
server *chanrpc.Server
commandServer *chanrpc.Server
}
func (s *Skeleton) Init() {
if s.GoLen <= 0 {
s.GoLen = 0
}
if s.TimerDispatcherLen <= 0 {
s.TimerDispatcherLen = 0
}
if s.AsynCallLen <= 0 {
s.AsynCallLen = 0
}
s.g = g.New(s.GoLen)
s.dispatcher = timer.NewDispatcher(s.TimerDispatcherLen)
s.client = chanrpc.NewClient(s.AsynCallLen)
s.server = s.ChanRPCServer
if s.server == nil {
s.server = chanrpc.NewServer(0)
}
s.commandServer = chanrpc.NewServer(0)
}
func (s *Skeleton) Run(closeSig chan bool) {
for {
select {
case <-closeSig:
s.commandServer.Close()
s.server.Close()
for !s.g.Idle() || !s.client.Idle() {
s.g.Close()
s.client.Close()
}
return
case ri := <-s.client.ChanAsynRet:
s.client.Cb(ri)
case ci := <-s.server.ChanCall:
s.server.Exec(ci)
case ci := <-s.commandServer.ChanCall:
s.commandServer.Exec(ci)
case cb := <-s.g.ChanCb:
s.g.Cb(cb)
case t := <-s.dispatcher.ChanTimer:
t.Cb()
}
}
}
func (s *Skeleton) AfterFunc(d time.Duration, cb func()) *timer.Timer {
if s.TimerDispatcherLen == 0 {
panic(any("invalid TimerDispatcherLen"))
}
return s.dispatcher.AfterFunc(d, cb)
}
func (s *Skeleton) CronFunc(cronExpr *timer.CronExpr, cb func()) *timer.Cron {
if s.TimerDispatcherLen == 0 {
panic(any("invalid TimerDispatcherLen"))
}
return s.dispatcher.CronFunc(cronExpr, cb)
}
// LoopFunc 由于循环队列只支持cron表达式,而cron表达式最少只有到秒级别,游戏中同步一般都是毫秒级别,所以在此添加毫秒级别的循环队列
func (s *Skeleton) LoopFunc(d time.Duration, cb func()) *timer.Cron {
if s.TimerDispatcherLen == 0 {
panic(any("invalid TimerDispatcherLen"))
}
return s.dispatcher.LoopFunc(d, cb)
}
func (s *Skeleton) Go(f func(), cb func()) {
if s.GoLen == 0 {
panic(any("invalid GoLen"))
}
s.g.Go(f, cb)
}
func (s *Skeleton) NewLinearContext() *g.LinearContext {
if s.GoLen == 0 {
panic(any("invalid GoLen"))
}
return s.g.NewLinearContext()
}
func (s *Skeleton) AsynCall(server *chanrpc.Server, id interface{}, args ...interface{}) {
if s.AsynCallLen == 0 {
panic(any("invalid AsynCallLen"))
}
s.client.Attach(server)
s.client.AsynCall(id, args...)
}
func (s *Skeleton) RegisterChanRPC(id interface{}, f interface{}) {
if s.ChanRPCServer == nil {
panic(any("invalid ChanRPCServer"))
}
s.server.Register(id, f)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jiangjiali/leaf.git
git@gitee.com:jiangjiali/leaf.git
jiangjiali
leaf
leaf
v1.1.43

搜索帮助