-
Notifications
You must be signed in to change notification settings - Fork 43
/
Entity.go
327 lines (260 loc) · 6.41 KB
/
Entity.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package entity
import (
"fmt"
"sync"
"zeus/dbservice"
"zeus/iserver"
"zeus/msgdef"
"zeus/msghandler"
"github.com/cihub/seelog"
"github.com/spf13/viper"
)
// iEntityState Entity的状态相关函数
type iEntityState interface {
OnInit()
OnAfterInit()
OnLoop()
OnDestroy()
IsDestroyed() bool
}
// iEntityState 留给后代作一些回调方法
type iEntityInit interface {
Init(interface{})
}
type iEntityLoop interface {
Loop()
}
type iEntityDestroy interface {
Destroy()
}
type iEntityInitFinish interface {
InitFinish()
}
// delayedSendMsg 需要延迟发送的消息结构
type delayedSendMsg struct {
srvType uint8
msg msgdef.IMsg
}
// Entity 代表服务器端一个可通讯对象
type Entity struct {
msghandler.IMsgHandlers
entityType string
entityID uint64
dbid uint64
cellID uint64
cellSrvType uint8
// real实体所在的服务器id,仅对ghost实体有效
realServerID uint64
//real entity所在的cellID
realCellID uint64
initParam interface{}
state byte
srvIDS map[uint8]*dbservice.EntitySrvInfo
srvIDSMux *sync.RWMutex
clientSess iserver.ISess
isDebug bool
realPtr interface{}
entitiesPtr iserver.IEntities
ieState iEntityState
// props relate
props map[string]*PropInfo
def *Def
dirtyPropList []*PropInfo
dirtySaveProps map[string]*PropInfo
dirtyProps map[uint8][]*PropInfo
ghostProps []*PropInfo
dirtyClientProps []*PropInfo
dirtyMRoleProps []*PropInfo
// props end
// 消息缓存, 在帧末发送的消息
delayedMsgs []*delayedSendMsg
}
// SetClient 设置客户端的连接
func (e *Entity) SetClient(s iserver.ISess) {
e.clientSess = s
if s == nil {
return
}
e.clientSess.SetMsgHandler(e.IMsgHandlers)
}
// GetClientSess 获取客户端连接
func (e *Entity) GetClientSess() iserver.ISess {
if e.clientSess != nil && !e.clientSess.IsClosed() {
return e.clientSess
}
return nil
}
// GetDBID 获取实体的DBID
func (e *Entity) GetDBID() uint64 {
return e.dbid
}
// GetID 获取实体ID
func (e *Entity) GetID() uint64 {
return e.entityID
}
// GetType 获取实体类型
func (e *Entity) GetType() string {
return e.entityType
}
// GetRealPtr 获取真实的后代对象的指针
func (e *Entity) GetRealPtr() interface{} {
return e.realPtr
}
// GetInitParam 获取初始化参数
func (e *Entity) GetInitParam() interface{} {
return e.initParam
}
// GetEntityState 获取当前状态
func (e *Entity) GetEntityState() uint8 {
return e.state
}
// IsGhost 是否是ghost实体
func (e *Entity) IsGhost() bool {
return e.realServerID != 0
}
func (e *Entity) SetRealServerID(srvID uint64) {
e.realServerID = srvID
}
// GetRealServerID 获取realServerID
func (e *Entity) GetRealServerID() uint64 {
return e.realServerID
}
func (e *Entity) SetRealCellID(cellID uint64) {
e.realCellID = cellID
}
// GetRealCellID
func (e *Entity) GetRealCellID() uint64 {
return e.realCellID
}
// GetEntities 获取包含自己的Entities指针
func (e *Entity) GetEntities() iserver.IEntities {
return e.entitiesPtr
}
// GetProxy 获取实体的代理对象
func (e *Entity) GetProxy() iserver.IEntityProxy {
return NewEntityProxy(iserver.GetSrvInst().GetSrvID(), e.GetCellID(), e.entityID)
}
// OnEntityCreated 初始化
func (e *Entity) OnEntityCreated(entityID uint64, entityType string, dbid uint64, cellID uint64, protoType interface{}, entities iserver.IEntities, initParam interface{}, syncInit bool, realServerID uint64) {
e.IMsgHandlers = msghandler.NewMsgHandlers()
e.entityType = entityType
e.entityID = entityID
e.dbid = dbid
e.cellID = cellID
e.initParam = initParam
e.realServerID = realServerID
e.realPtr = protoType
e.entitiesPtr = entities
e.state = iserver.Entity_State_Init
e.ieState = protoType.(iEntityState)
e.srvIDS = make(map[uint8]*dbservice.EntitySrvInfo)
e.srvIDSMux = &sync.RWMutex{}
e.props = make(map[string]*PropInfo)
e.dirtyPropList = make([]*PropInfo, 0, 1)
e.dirtySaveProps = make(map[string]*PropInfo)
e.dirtyProps = make(map[uint8][]*PropInfo)
e.ghostProps = make([]*PropInfo, 0, 1)
e.dirtyClientProps = make([]*PropInfo, 0, 1)
e.dirtyMRoleProps = make([]*PropInfo, 0, 1)
e.delayedMsgs = make([]*delayedSendMsg, 0, 1)
ps, ok := e.realPtr.(iserver.IEntityPropsSetter)
if ok {
ps.SetPropsSetter(e.realPtr.(iserver.IEntityProps))
}
if syncInit {
ies := e.ieState
ies.OnInit()
ies.OnAfterInit()
}
}
// OnEntityDestroyed 当Entity销毁时调用
func (e *Entity) OnEntityDestroyed() {
if e.state == iserver.Entity_State_Loop || e.state == iserver.Entity_State_Init {
e.state = iserver.Entity_State_Destroy
//e.reflushToDB()
}
}
// IsDestroyed 是否删除
func (e *Entity) IsDestroyed() bool {
return e.state == iserver.Entity_State_InValid
}
// MainLoop 主循环
func (e *Entity) MainLoop() {
defer func() {
if err := recover(); err != nil {
seelog.Error(err, e)
if viper.GetString("Config.Recover") == "0" {
panic(fmt.Sprintln(err, e))
}
}
}()
ies := e.ieState
switch e.state {
case iserver.Entity_State_Init:
{
ies.OnInit()
ies.OnAfterInit()
}
case iserver.Entity_State_Loop:
{
ies.OnLoop()
}
case iserver.Entity_State_Destroy:
{
ies.OnDestroy()
e.state = iserver.Entity_State_InValid
}
default:
{
// do nothing
}
}
}
// OnInit 初始化
func (e *Entity) OnInit() {
e.state = iserver.Entity_State_Loop
e.InitProp(GetDefs().GetDef(e.entityType))
e.RegMsgProc(e.GetRealPtr())
e.RegSrvID()
}
// OnAfterInit 后代的初始化
func (e *Entity) OnAfterInit() {
ii, ok := e.GetRealPtr().(iEntityInit)
if ok {
ii.Init(e.GetInitParam())
} else {
seelog.Error("the entity ", e.GetType(), " no init method")
}
}
// OnDestroy 销毁
func (e *Entity) OnDestroy() {
seelog.Info("OnDestroy the entity ", e.GetType(), ", ID: ", e.GetID())
if id, ok := e.GetRealPtr().(iEntityDestroy); ok {
id.Destroy()
}
if !e.IsGhost() {
e.UnregSrvID()
e.reflushToDB()
}
if e.clientSess != nil {
e.clientSess.Close()
}
e.state = iserver.Entity_State_InValid
}
// OnLoop 运行时
func (e *Entity) OnLoop() {
// 每帧处理顺序:
// 处理消息和业务逻辑, 在业务逻辑中更改属性, 发送RPC消息, 此时仅缓存需要发送的RPC消息
// 同步属性变化消息
// 真正发送RPC消息
e.DoMsg()
e.DoLooper()
e.ReflushDirtyProp()
e.FlushDelayedMsgs()
}
// DoLooper 调用后代类的loop函数
func (e *Entity) DoLooper() {
if ie, ok := e.GetRealPtr().(iEntityLoop); ok {
ie.Loop()
}
}