forked from davyxu/cellnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.go
228 lines (174 loc) · 3.86 KB
/
event.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package cellnet
import (
"fmt"
"reflect"
"sync/atomic"
)
type EventType int32
const (
Event_None EventType = iota
Event_Connected
Event_ConnectFailed
Event_Accepted
Event_AcceptFailed
Event_Closed
Event_Recv
Event_Send
)
func (self EventType) String() string {
switch self {
case Event_Recv:
return "recv"
case Event_Send:
return "send"
case Event_Connected:
return "connected"
case Event_ConnectFailed:
return "connectfailed"
case Event_Accepted:
return "accepted"
case Event_AcceptFailed:
return "acceptfailed"
case Event_Closed:
return "closed"
}
return fmt.Sprintf("unknown(%d)", self)
}
type Result int32
const (
Result_OK Result = iota
Result_SocketError // 网络错误
Result_SocketTimeout // Socket超时
Result_PackageCrack // 封包破损
Result_CodecError
Result_RequestClose // 请求关闭
Result_NextChain
Result_RPCTimeout
)
// 会话事件
type Event struct {
UID int64
Type EventType // 事件类型
MsgID uint32 // 消息ID
Msg interface{} // 消息对象
Data []byte // 消息序列化后的数据
Tag interface{} // 事件的连接, 一个处理流程后被Reset
TransmitTag interface{} // 接收过程可以传递到发送过程, 不会被清空
Ses Session // 会话
ChainSend *HandlerChain // 发送handler override
r Result // 出现错误, 将结束ChainCall
chainid int64 // 所在链, 调试用
}
func (self *Event) Clone() *Event {
c := &Event{
UID: self.UID,
Type: self.Type,
MsgID: self.MsgID,
Msg: self.Msg,
Tag: self.Tag,
TransmitTag: self.TransmitTag,
Ses: self.Ses,
ChainSend: self.ChainSend,
Data: make([]byte, len(self.Data)),
}
copy(c.Data, self.Data)
return c
}
func (self *Event) Result() Result {
return self.r
}
func (self *Event) SetResult(r Result) {
self.r = r
}
// 兼容普通消息发送和rpc消息返回, 推荐
func (self *Event) Send(data interface{}) {
if self.Ses == nil {
return
}
ev := NewEvent(Event_Send, self.Ses)
ev.Msg = data
ev.TransmitTag = self.TransmitTag
if self.ChainSend != nil {
// 由接收方提供的发送链继续传递
ev.ChainSend = self.ChainSend
} else {
// 默认没有, 使用peer的发送链
ev.ChainSend = self.Ses.FromPeer().ChainSend()
}
self.Ses.RawSend(ev)
}
func (self *Event) PeerName() string {
if self.Ses == nil {
return ""
}
name := self.Ses.FromPeer().Name()
if name != "" {
return name
}
return self.Ses.FromPeer().Address()
}
func (self *Event) SessionID() int64 {
if self.Ses == nil {
return 0
}
return self.Ses.ID()
}
func (self *Event) MsgSize() int {
if self.Data == nil {
return 0
}
return len(self.Data)
}
func (self *Event) MsgString() string {
if self.Msg == nil {
return ""
}
if stringer, ok := self.Msg.(interface {
String() string
}); ok {
return stringer.String()
}
return ""
}
func (self *Event) MsgName() string {
meta := MessageMetaByID(self.MsgID)
if meta == nil {
return ""
}
return meta.Name
}
func (self *Event) FromMessage(msg interface{}) *Event {
var err error
self.Data, self.MsgID, err = EncodeMessage(msg)
if err != nil {
log.Debugln(err, *self)
}
// Data+ID / Msg 二选一
self.Msg = nil
return self
}
// 根据消息内容, 自动填充其他部分, 以方便输出日志
func (self *Event) Parse() {
if self.Msg == nil && self.Data != nil && self.MsgID != 0 {
self.Msg, _ = DecodeMessage(self.MsgID, self.Data)
} else if self.MsgID == 0 && self.Msg != nil {
meta := MessageMetaByType(reflect.TypeOf(self.Msg))
if meta != nil {
self.MsgID = meta.ID
}
}
}
func NewEvent(t EventType, s Session) *Event {
self := &Event{
Type: t,
Ses: s,
}
if EnableHandlerLog {
self.UID = genSesEvUID()
}
return self
}
var evuid int64
func genSesEvUID() int64 {
return atomic.AddInt64(&evuid, 1)
}