forked from name5566/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskeleton.go
121 lines (102 loc) · 2.49 KB
/
skeleton.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package module
import (
"github.com/name5566/leaf/chanrpc"
"github.com/name5566/leaf/console"
"github.com/name5566/leaf/go"
"github.com/name5566/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)
}