1 Star 0 Fork 0

lichun-123 / leaf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
skeleton.go 2.50 KB
一键复制 编辑 原始数据 按行查看 历史
lichun-123 提交于 2023-10-11 11:46 . proto update
package module
import (
"gitee.com/lichun-123/leaf/chanrpc"
"gitee.com/lichun-123/leaf/console"
g "gitee.com/lichun-123/leaf/go"
"gitee.com/lichun-123/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("invalid TimerDispatcherLen")
}
return s.dispatcher.AfterFunc(d, cb)
}
func (s *Skeleton) CronFunc(cronExpr *timer.CronExpr, cb func()) *timer.Cron {
if s.TimerDispatcherLen == 0 {
panic("invalid TimerDispatcherLen")
}
return s.dispatcher.CronFunc(cronExpr, cb)
}
func (s *Skeleton) Go(f func(), cb func()) {
if s.GoLen == 0 {
panic("invalid GoLen")
}
s.g.Go(f, cb)
}
func (s *Skeleton) NewLinearContext() *g.LinearContext {
if s.GoLen == 0 {
panic("invalid GoLen")
}
return s.g.NewLinearContext()
}
func (s *Skeleton) AsynCall(server *chanrpc.Server, id interface{}, args ...interface{}) {
if s.AsynCallLen == 0 {
panic("invalid AsynCallLen")
}
s.client.Attach(server)
s.client.AsynCall(id, args...)
}
func (s *Skeleton) RegisterChanRPC(id interface{}, f interface{}) {
if s.ChanRPCServer == nil {
panic("invalid ChanRPCServer")
}
s.server.Register(id, f)
}
func (s *Skeleton) RegisterCommand(name string, help string, f interface{}) {
console.Register(name, help, f, s.commandServer)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lichun-123/leaf.git
git@gitee.com:lichun-123/leaf.git
lichun-123
leaf
leaf
v0.0.35

搜索帮助

344bd9b3 5694891 D2dac590 5694891