-
Notifications
You must be signed in to change notification settings - Fork 1
/
MiniActor.go
82 lines (66 loc) · 1.68 KB
/
MiniActor.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
package goactor
import (
_ "reflect"
"sync"
"github.com/xuexihuang/goactor/chanrpc"
"github.com/xuexihuang/goactor/log"
"github.com/xuexihuang/goactor/module"
"time"
)
type MiniActor struct {
*module.Skeleton
closeSig chan bool
data interface{}
wg sync.WaitGroup
}
func (mc *MiniActor) run() {
mc.Run(mc.closeSig)
mc.wg.Done()
}
func (mc *MiniActor) OnInit() { //用来初始化data使用,并且注册消息和回调函数
//miniactor.OnRegister("C2GSLogin", HandleC2GSLogin2)//demo,注册消息和处理函数,
}
func (mc *MiniActor) OnDestroy() { //析构data使用
}
func OnNew() *MiniActor {
miniactor := MiniActor{}
miniactor.Skeleton = &module.Skeleton{
GoLen: 10000,//用来传输参数channel的长度
TimerDispatcherLen: 10000,//没用到
AsynCallLen: 10000,//没用到
ChanRPCServer: chanrpc.NewServer(10000),
}
miniactor.Skeleton.Init()
miniactor.closeSig = make(chan bool, 1)
miniactor.wg.Add(1)
miniactor.OnInit()
go miniactor.run()
return &miniactor
}
func (mc *MiniActor) OnRegister(m interface{}, h interface{}) {//注册对于消息的处理函数
mc.Skeleton.RegisterChanRPC(m, h)
}
func (m *MiniActor) OnEnd() {
defer func() {
if r := recover(); r != nil {
log.Error("%v", r)
}
}()
m.closeSig <- true
m.wg.Wait()
m.OnDestroy()
}
/* 这是测试例子,在模块内部注册处理函数,例子中在main函数注册的,其实应该在OnInit注册
func HandleC2GSLogin2(i []interface{}) {
m := i[0].(int)
log.Debug("%v", m)
}
func main() {
miniactor := OnNew()//动态创建一个actor实例
miniactor.OnRegister("C2GSLogin", HandleC2GSLogin2)//注册消息和处理函数
miniactor.Skeleton.ChanRPCServer.Go("C2GSLogin", 1212)//发送消息
time.Sleep(time.Second * 10)
miniactor.OnEnd()
miniactor = nil
}
*/