Skip to content

Commit e14d736

Browse files
committed
notify函数可以自定义
1 parent 49b94cc commit e14d736

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

peer/http/acceptor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (self *httpAcceptor) ServeHTTP(res http.ResponseWriter, req *http.Request)
133133

134134
if err != nil {
135135

136+
// 或者是普通消息没有Handled
136137
log.Warnf("#http.recv(%s) '%s' %s | [%d] Not found",
137138
self.Name(),
138139
req.Method,

timer/loop.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88

99
// 轻量级的持续Tick循环
1010
type Loop struct {
11-
Context interface{}
12-
Duration time.Duration
13-
userCallback func(*Loop)
11+
Context interface{}
12+
Duration time.Duration
13+
notifyCallback func(*Loop)
1414

1515
running int64
1616

@@ -71,12 +71,26 @@ func (self *Loop) Stop() {
7171
self.setRunning(false)
7272
}
7373

74+
func (self *Loop) Resume() {
75+
76+
self.setRunning(true)
77+
}
78+
7479
// 马上调用一次用户回调
7580
func (self *Loop) Notify() *Loop {
76-
self.userCallback(self)
81+
self.notifyCallback(self)
82+
return self
83+
}
84+
85+
func (self *Loop) SetNotifyFunc(notifyCallback func(*Loop)) *Loop {
86+
self.notifyCallback = notifyCallback
7787
return self
7888
}
7989

90+
func (self *Loop) NotifyFunc() func(*Loop) {
91+
return self.notifyCallback
92+
}
93+
8094
func tick(ctx interface{}, nextLoop bool) {
8195

8296
loop := ctx.(*Loop)
@@ -92,13 +106,13 @@ func tick(ctx interface{}, nextLoop bool) {
92106

93107
// 执行一个循环, 持续调用callback, 周期是duration
94108
// context: 将context上下文传递到带有context指针的函数回调中
95-
func NewLoop(q cellnet.EventQueue, duration time.Duration, callback func(*Loop), context interface{}) *Loop {
109+
func NewLoop(q cellnet.EventQueue, duration time.Duration, notifyCallback func(*Loop), context interface{}) *Loop {
96110

97111
self := &Loop{
98-
Context: context,
99-
Duration: duration,
100-
userCallback: callback,
101-
Queue: q,
112+
Context: context,
113+
Duration: duration,
114+
notifyCallback: notifyCallback,
115+
Queue: q,
102116
}
103117

104118
return self

0 commit comments

Comments
 (0)