Repository navigation
Expand file tree
/
Copy pathcellappmgr.cpp
More file actions
493 lines (404 loc) · 14.2 KB
/
Copy pathcellappmgr.cpp
File metadata and controls
493 lines (404 loc) · 14.2 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cellappmgr.h"
#include "cellappmgr_interface.h"
#include "network/common.h"
#include "network/tcp_packet.h"
#include "network/udp_packet.h"
#include "network/message_handler.h"
#include "thread/threadpool.h"
#include "server/components.h"
#include "../../server/baseapp/baseapp_interface.h"
#include "../../server/cellapp/cellapp_interface.h"
#include "../../server/dbmgr/dbmgr_interface.h"
namespace KBEngine{
ServerConfig g_serverConfig;
KBE_SINGLETON_INIT(Cellappmgr);
class AppForwardItem : public ForwardItem
{
public:
virtual bool isOK()
{
// 必须存在一个准备好的进程
Components::COMPONENTS& cts = Components::getSingleton().getComponents(CELLAPP_TYPE);
Components::COMPONENTS::iterator ctiter = cts.begin();
for (; ctiter != cts.end(); ++ctiter)
{
if (Cellappmgr::getSingleton().componentReady((*ctiter).cid))
{
std::map< COMPONENT_ID, Cellapp >& cellapps = Cellappmgr::getSingleton().cellapps();
std::map< COMPONENT_ID, Cellapp >::iterator cellapp_iter = cellapps.find((*ctiter).cid);
if (cellapp_iter == cellapps.end())
continue;
if ((cellapp_iter->second.flags() & APP_FLAGS_NOT_PARTCIPATING_LOAD_BALANCING) > 0)
continue;
if (cellapp_iter->second.isDestroyed())
continue;
if (cellapp_iter->second.initProgress() < 1.f)
continue;
return true;
}
}
return false;
}
};
//-------------------------------------------------------------------------------------
Cellappmgr::Cellappmgr(Network::EventDispatcher& dispatcher,
Network::NetworkInterface& ninterface,
COMPONENT_TYPE componentType,
COMPONENT_ID componentID):
ServerApp(dispatcher, ninterface, componentType, componentID),
gameTimer_(),
forward_anywhere_cellapp_messagebuffer_(ninterface, CELLAPP_TYPE),
forward_cellapp_messagebuffer_(ninterface),
cellapps_(),
cellapp_cids_()
{
}
//-------------------------------------------------------------------------------------
Cellappmgr::~Cellappmgr()
{
cellapps_.clear();
cellapp_cids_.clear();
}
//-------------------------------------------------------------------------------------
bool Cellappmgr::run()
{
return ServerApp::run();
}
//-------------------------------------------------------------------------------------
std::map< COMPONENT_ID, Cellapp >& Cellappmgr::cellapps()
{
return cellapps_;
}
//-------------------------------------------------------------------------------------
void Cellappmgr::removeCellapp(COMPONENT_ID cid)
{
std::map< COMPONENT_ID, Cellapp >::iterator iter = cellapps_.find(cid);
if (iter != cellapps_.end())
{
WARNING_MSG(fmt::format("Cellappmgr::removeCellapp: erase cellapp[{0}], currsize={1}\n",
cid, (cellapps_.size() - 1)));
cellapps_.erase(iter);
std::vector<COMPONENT_ID>::iterator viter = cellapp_cids_.begin();
for (; viter != cellapp_cids_.end(); ++viter)
{
if ((*viter) == cid)
{
cellapp_cids_.erase(viter);
break;
}
}
updateBestCellapp();
}
}
//-------------------------------------------------------------------------------------
Cellapp& Cellappmgr::getCellapp(COMPONENT_ID cid)
{
std::map< COMPONENT_ID, Cellapp >::iterator iter = cellapps_.find(cid);
if (iter != cellapps_.end())
{
return iter->second;
}
// 添加了一个新的cellapp
Cellapp& cellapp = cellapps_[cid];
INFO_MSG(fmt::format("Cellappmgr::getCellapp: added new cellapp({0}).\n",
cid));
cellapp_cids_.push_back(cid);
return cellapp;
}
//-------------------------------------------------------------------------------------
void Cellappmgr::handleTimeout(TimerHandle handle, void * arg)
{
switch (reinterpret_cast<uintptr>(arg))
{
case TIMEOUT_GAME_TICK:
this->handleGameTick();
break;
default:
break;
}
ServerApp::handleTimeout(handle, arg);
}
//-------------------------------------------------------------------------------------
void Cellappmgr::onChannelDeregister(Network::Channel * pChannel)
{
// 如果是app死亡了
if(pChannel->isInternal())
{
Components::ComponentInfos* cinfo = Components::getSingleton().findComponent(pChannel);
if(cinfo)
{
cinfo->state = COMPONENT_STATE_STOP;
removeCellapp(cinfo->cid);
}
}
ServerApp::onChannelDeregister(pChannel);
}
//-------------------------------------------------------------------------------------
void Cellappmgr::updateBestCellapp()
{
bestCellappID_ = findFreeCellapp();
}
//-------------------------------------------------------------------------------------
void Cellappmgr::handleGameTick()
{
//time_t t = ::time(NULL);
//DEBUG_MSG("Cellappmgr::handleGameTick[%"PRTime"]:%u\n", t, time_);
++g_kbetime;
threadPool_.onMainThreadTick();
networkInterface().processChannels(&CellappmgrInterface::messageHandlers);
}
//-------------------------------------------------------------------------------------
bool Cellappmgr::initializeBegin()
{
return true;
}
//-------------------------------------------------------------------------------------
bool Cellappmgr::inInitialize()
{
return true;
}
//-------------------------------------------------------------------------------------
bool Cellappmgr::initializeEnd()
{
gameTimer_ = this->dispatcher().addTimer(1000000 / 50, this,
reinterpret_cast<void *>(TIMEOUT_GAME_TICK));
return true;
}
//-------------------------------------------------------------------------------------
void Cellappmgr::finalise()
{
gameTimer_.cancel();
forward_anywhere_cellapp_messagebuffer_.clear();
forward_cellapp_messagebuffer_.clear();
ServerApp::finalise();
}
//-------------------------------------------------------------------------------------
void Cellappmgr::forwardMessage(Network::Channel* pChannel, MemoryStream& s)
{
COMPONENT_ID sender_componentID, forward_componentID;
s >> sender_componentID >> forward_componentID;
Components::ComponentInfos* cinfos = Components::getSingleton().findComponent(forward_componentID);
KBE_ASSERT(cinfos != NULL && cinfos->pChannel != NULL);
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).append((char*)s.data() + s.rpos(), (int)s.length());
cinfos->pChannel->send(pBundle);
s.done();
}
//-------------------------------------------------------------------------------------
COMPONENT_ID Cellappmgr::findFreeCellapp(void)
{
std::map< COMPONENT_ID, Cellapp >::iterator iter = cellapps_.begin();
COMPONENT_ID cid = 0;
float minload = 1.f;
ENTITY_ID numEntities = 0x7fffffff;
for(; iter != cellapps_.end(); ++iter)
{
if ((iter->second.flags() & APP_FLAGS_NOT_PARTCIPATING_LOAD_BALANCING) > 0)
continue;
// 首先进程必须活着且初始化完毕
if(!iter->second.isDestroyed() && iter->second.initProgress() > 1.f)
{
// 如果没有任何实体则无条件分配
if(iter->second.numEntities() == 0)
return iter->first;
// 比较并记录负载最小的进程最终被分配
if(minload > iter->second.load() ||
(minload == iter->second.load() && numEntities > iter->second.numEntities()))
{
cid = iter->first;
numEntities = iter->second.numEntities();
minload = iter->second.load();
}
}
}
return cid;
}
//-------------------------------------------------------------------------------------
bool Cellappmgr::componentsReady()
{
Components::COMPONENTS& cts = Components::getSingleton().getComponents(CELLAPP_TYPE);
Components::COMPONENTS::iterator ctiter = cts.begin();
for(; ctiter != cts.end(); ++ctiter)
{
if((*ctiter).pChannel == NULL)
return false;
if((*ctiter).state != COMPONENT_STATE_RUN)
return false;
}
return true;
}
//-------------------------------------------------------------------------------------
bool Cellappmgr::componentReady(COMPONENT_ID cid)
{
Components::ComponentInfos* cinfos = Components::getSingleton().findComponent(CELLAPP_TYPE, cid);
if(cinfos == NULL || cinfos->pChannel == NULL || cinfos->state != COMPONENT_STATE_RUN)
return false;
return true;
}
//-------------------------------------------------------------------------------------
uint32 Cellappmgr::numLoadBalancingApp()
{
uint32 num = 0;
std::map< COMPONENT_ID, Cellapp >::iterator iter = cellapps_.begin();
for (; iter != cellapps_.end(); ++iter)
{
if ((iter->second.flags() & APP_FLAGS_NOT_PARTCIPATING_LOAD_BALANCING) > 0)
continue;
++num;
}
return num;
}
//-------------------------------------------------------------------------------------
void Cellappmgr::reqCreateInNewSpace(Network::Channel* pChannel, MemoryStream& s)
{
std::string entityType;
ENTITY_ID id;
COMPONENT_ID componentID;
bool hasClient;
// 如果cellappIndex为0,则代表不强制指定cellapp
// 非0的情况下,选择的cellapp可以用1,2,3,4来代替
// 假如预期有4个cellapp, 假如不够4个, 只有3个, 那么4代表1
uint32 cellappIndex = 0;
s >> entityType;
s >> id;
s >> cellappIndex;
s >> componentID;
s >> hasClient;
static SPACE_ID spaceID = 1;
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).newMessage(CellappInterface::onCreateInNewSpaceFromBaseapp);
(*pBundle) << entityType;
(*pBundle) << id;
(*pBundle) << spaceID++;
(*pBundle) << componentID;
(*pBundle) << hasClient;
(*pBundle).append(&s);
s.done();
uint32 cellappSize = cellapp_cids_.size();
if (cellappSize > 0)
{
updateBestCellapp();
// 选择特定的cellapp创建space
if (cellappIndex > 0)
{
uint32 index = (cellappIndex - 1) % cellappSize;
bestCellappID_ = cellapp_cids_[index];
}
else if (bestCellappID_ == 0 && numLoadBalancingApp() == 0)
{
ERROR_MSG(fmt::format("Cellappmgr::reqCreateInNewSpace: Unable to allocate cellapp for load balancing! entityType={}, entityID={}, componentID={}, cellappSize={}.\n",
entityType, id, componentID, cellappSize));
}
}
Components::ComponentInfos* cinfos = Components::getSingleton().findComponent(CELLAPP_TYPE, bestCellappID_);
if (cinfos == NULL || cinfos->pChannel == NULL || cinfos->state != COMPONENT_STATE_RUN)
{
WARNING_MSG("Cellappmgr::reqCreateInNewSpace: not found cellapp, message is buffered.\n");
ForwardItem* pFI = new AppForwardItem();
pFI->pHandler = NULL;
pFI->pBundle = pBundle;
if (cellappIndex == 0 || bestCellappID_ == 0)
forward_anywhere_cellapp_messagebuffer_.push(pFI);
else
forward_cellapp_messagebuffer_.push(bestCellappID_, pFI);
return;
}
else
{
cinfos->pChannel->send(pBundle);
}
std::map< COMPONENT_ID, Cellapp >::iterator cellapp_iter = cellapps_.find(bestCellappID_);
DEBUG_MSG(fmt::format("Cellappmgr::reqCreateInNewSpace: entityType={}, entityID={}, componentID={}, cellapp(cid={}, load={}, numEntities={}).\n",
entityType, id, componentID, bestCellappID_, cellapp_iter->second.load(), cellapp_iter->second.numEntities()));
// 预先将实体数量增加
if (cellapp_iter != cellapps_.end())
{
cellapp_iter->second.incNumEntities();
}
}
//-------------------------------------------------------------------------------------
void Cellappmgr::reqRestoreSpaceInCell(Network::Channel* pChannel, MemoryStream& s)
{
std::string entityType;
ENTITY_ID id;
COMPONENT_ID componentID;
SPACE_ID spaceID;
bool hasClient;
s >> entityType;
s >> id;
s >> componentID;
s >> spaceID;
s >> hasClient;
Network::Bundle* pBundle = Network::Bundle::createPoolObject();
(*pBundle).newMessage(CellappInterface::onRestoreSpaceInCellFromBaseapp);
(*pBundle) << entityType;
(*pBundle) << id;
(*pBundle) << spaceID;
(*pBundle) << componentID;
(*pBundle) << hasClient;
(*pBundle).append(&s);
s.done();
DEBUG_MSG(fmt::format("Cellappmgr::reqRestoreSpaceInCell: entityType={0}, entityID={1}, componentID={2}, spaceID={3}.\n",
entityType, id, componentID, spaceID));
Components::ComponentInfos* cinfos = Components::getSingleton().findComponent(CELLAPP_TYPE, bestCellappID_);
if(cinfos == NULL || cinfos->pChannel == NULL || cinfos->state != COMPONENT_STATE_RUN)
{
WARNING_MSG("Cellappmgr::reqRestoreSpaceInCell: not found cellapp, message is buffered.\n");
ForwardItem* pFI = new AppForwardItem();
pFI->pHandler = NULL;
pFI->pBundle = pBundle;
forward_anywhere_cellapp_messagebuffer_.push(pFI);
return;
}
else
{
cinfos->pChannel->send(pBundle);
}
// 预先将实体数量增加
std::map< COMPONENT_ID, Cellapp >::iterator cellapp_iter = cellapps_.find(bestCellappID_);
if (cellapp_iter != cellapps_.end())
{
cellapp_iter->second.incNumEntities();
}
}
//-------------------------------------------------------------------------------------
void Cellappmgr::updateCellapp(Network::Channel* pChannel, COMPONENT_ID componentID,
ENTITY_ID numEntities, float load, uint32 flags)
{
Cellapp& cellapp = getCellapp(componentID);
cellapp.load(load);
cellapp.numEntities(numEntities);
cellapp.flags(flags);
updateBestCellapp();
}
//-------------------------------------------------------------------------------------
void Cellappmgr::onCellappInitProgress(Network::Channel* pChannel, COMPONENT_ID cid, float progress)
{
if(progress > 1.f)
{
INFO_MSG(fmt::format("Cellappmgr::onCellappInitProgress: cid={0}, progress={1}.\n",
cid , (progress > 1.f ? 1.f : progress)));
Components::ComponentInfos* cinfos = Components::getSingleton().findComponent(cid);
if(cinfos)
cinfos->state = COMPONENT_STATE_RUN;
}
KBE_ASSERT(cellapps_.find(cid) != cellapps_.end());
getCellapp(cid).initProgress(progress);
}
//-------------------------------------------------------------------------------------
}