Skip to content

Commit 40caee9

Browse files
author
kbengine
committed
添加entityID分配机制。
git-svn-id: https://kbengine.svn.sourceforge.net/svnroot/kbengine@329 d26bdfbe-e24b-4f15-971f-8e0596e1bc08
1 parent 68aead5 commit 40caee9

23 files changed

Lines changed: 714 additions & 39 deletions

‎kbe/src/lib/network/endpoint.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class EndPoint
5959

6060
int bind(u_int16_t networkPort = 0, u_int32_t networkAddr = INADDR_ANY);
6161
int listen(int backlog = 5);
62-
int connect(u_int16_t networkPort, u_int32_t networkAddr = INADDR_BROADCAST);
63-
EndPoint* accept(u_int16_t * networkPort = NULL, u_int32_t * networkAddr = NULL);
62+
int connect(u_int16_t networkPort, u_int32_t networkAddr = INADDR_BROADCAST, bool autosetflags = true);
63+
EndPoint* accept(u_int16_t * networkPort = NULL, u_int32_t * networkAddr = NULL, bool autosetflags = true);
6464

6565
INLINE int send(const void * gramData, int gramSize);
6666
int recv(void * gramData, int gramSize);

‎kbe/src/lib/network/endpoint.ipp‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,17 +325,23 @@ INLINE int EndPoint::listen(int backlog)
325325
return ::listen(socket_, backlog);
326326
}
327327

328-
INLINE int EndPoint::connect(u_int16_t networkPort, u_int32_t networkAddr)
328+
INLINE int EndPoint::connect(u_int16_t networkPort, u_int32_t networkAddr, bool autosetflags)
329329
{
330330
sockaddr_in sin;
331331
sin.sin_family = AF_INET;
332332
sin.sin_port = networkPort;
333333
sin.sin_addr.s_addr = networkAddr;
334334

335-
return ::connect(socket_, (sockaddr*)&sin, sizeof(sin));
335+
int ret = ::connect(socket_, (sockaddr*)&sin, sizeof(sin));
336+
if(autosetflags)
337+
{
338+
setnonblocking(true);
339+
setnodelay(true);
340+
}
341+
return ret;
336342
}
337343

338-
INLINE EndPoint * EndPoint::accept(u_int16_t * networkPort, u_int32_t * networkAddr)
344+
INLINE EndPoint * EndPoint::accept(u_int16_t * networkPort, u_int32_t * networkAddr, bool autosetflags)
339345
{
340346
sockaddr_in sin;
341347
socklen_t sinLen = sizeof(sin);
@@ -349,9 +355,13 @@ INLINE EndPoint * EndPoint::accept(u_int16_t * networkPort, u_int32_t * networkA
349355
EndPoint * pNew = new EndPoint();
350356
pNew->setFileDescriptor(ret);
351357
pNew->addr(sin.sin_port, sin.sin_addr.s_addr);
352-
pNew->setnonblocking(true);
353-
pNew->setnodelay(true);
354-
358+
359+
if(autosetflags)
360+
{
361+
pNew->setnonblocking(true);
362+
pNew->setnodelay(true);
363+
}
364+
355365
if (networkPort != NULL) *networkPort = sin.sin_port;
356366
if (networkAddr != NULL) *networkAddr = sin.sin_addr.s_addr;
357367

‎kbe/src/lib/server/components.cpp‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Components _g_components;
4141

4242
//-------------------------------------------------------------------------------------
4343
Components::Components():
44-
pNetworkInterface_(NULL)
44+
_pNetworkInterface(NULL)
4545
{
4646
}
4747

@@ -84,6 +84,23 @@ void Components::addComponent(int32 uid, const char* username,
8484
else
8585
*cinfos = componentInfos;
8686

87+
_globalOrderLog[uid]++;
88+
89+
switch(componentType)
90+
{
91+
case BASEAPP_TYPE:
92+
_baseappGrouplOrderLog[uid]++;
93+
break;
94+
case CELLAPP_TYPE:
95+
_cellappGrouplOrderLog[uid]++;
96+
break;
97+
case LOGINAPP_TYPE:
98+
_loginappGrouplOrderLog[uid]++;
99+
break;
100+
default:
101+
break;
102+
};
103+
87104
INFO_MSG("Components::addComponent[%s], uid:%d, "
88105
"componentID:%"PRAppID", totalcount=%d\n",
89106
COMPONENT_NAME[(uint8)componentType], uid,
@@ -139,8 +156,8 @@ int Components::connectComponent(COMPONENT_TYPE componentType, int32 uid, COMPON
139156

140157
if(ret == 0)
141158
{
142-
pComponentInfos->pChannel = new Mercury::Channel(*pNetworkInterface_, pEndpoint, Mercury::Channel::INTERNAL);
143-
if(!pNetworkInterface_->registerChannel(pComponentInfos->pChannel))
159+
pComponentInfos->pChannel = new Mercury::Channel(*_pNetworkInterface, pEndpoint, Mercury::Channel::INTERNAL);
160+
if(!_pNetworkInterface->registerChannel(pComponentInfos->pChannel))
144161
{
145162
ERROR_MSG("Components::connectComponent: registerChannel(%s) is failed!\n",
146163
pComponentInfos->pChannel->c_str());
@@ -155,51 +172,51 @@ int Components::connectComponent(COMPONENT_TYPE componentType, int32 uid, COMPON
155172

156173
BaseappmgrInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(),
157174
Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(),
158-
pNetworkInterface_->intaddr().ip, pNetworkInterface_->intaddr().port,
159-
pNetworkInterface_->extaddr().ip, pNetworkInterface_->extaddr().port);
175+
_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
176+
_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
160177
}
161178
else if(componentType == CELLAPPMGR_TYPE)
162179
{
163180
bundle.newMessage(CellappmgrInterface::onRegisterNewApp);
164181

165182
CellappmgrInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(),
166183
Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(),
167-
pNetworkInterface_->intaddr().ip, pNetworkInterface_->intaddr().port,
168-
pNetworkInterface_->extaddr().ip, pNetworkInterface_->extaddr().port);
184+
_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
185+
_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
169186
}
170187
else if(componentType == CELLAPP_TYPE)
171188
{
172189
bundle.newMessage(CellappInterface::onRegisterNewApp);
173190

174191
CellappInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(),
175192
Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(),
176-
pNetworkInterface_->intaddr().ip, pNetworkInterface_->intaddr().port,
177-
pNetworkInterface_->extaddr().ip, pNetworkInterface_->extaddr().port);
193+
_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
194+
_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
178195
}
179196
else if(componentType == BASEAPP_TYPE)
180197
{
181198
bundle.newMessage(BaseappInterface::onRegisterNewApp);
182199

183200
BaseappInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(),
184201
Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(),
185-
pNetworkInterface_->intaddr().ip, pNetworkInterface_->intaddr().port,
186-
pNetworkInterface_->extaddr().ip, pNetworkInterface_->extaddr().port);
202+
_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
203+
_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
187204
}
188205
else if(componentType == DBMGR_TYPE)
189206
{
190207
bundle.newMessage(DbmgrInterface::onRegisterNewApp);
191208

192209
DbmgrInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(),
193210
Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(),
194-
pNetworkInterface_->intaddr().ip, pNetworkInterface_->intaddr().port,
195-
pNetworkInterface_->extaddr().ip, pNetworkInterface_->extaddr().port);
211+
_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
212+
_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
196213
}
197214
else
198215
{
199216
KBE_ASSERT(false && "invalid componentType.\n");
200217
}
201218

202-
bundle.send(*pNetworkInterface_, pComponentInfos->pChannel);
219+
bundle.send(*_pNetworkInterface, pComponentInfos->pChannel);
203220
}
204221
}
205222
else

‎kbe/src/lib/server/components.hpp‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Components : public Singleton<Components>
6363
Components();
6464
~Components();
6565

66-
void pNetworkInterface(Mercury::NetworkInterface * networkInterface){ pNetworkInterface_ = networkInterface; }
66+
void pNetworkInterface(Mercury::NetworkInterface * networkInterface){ _pNetworkInterface = networkInterface; }
6767

6868
void addComponent(int32 uid, const char* username,
6969
COMPONENT_TYPE componentType, COMPONENT_ID componentID,
@@ -81,16 +81,30 @@ class Components : public Singleton<Components>
8181
//const Components::ComponentInfos findComponent(COMPONENT_TYPE componentType, int32 uid);
8282

8383
int connectComponent(COMPONENT_TYPE componentType, int32 uid, COMPONENT_ID componentID);
84+
85+
typedef std::map<int32/*uid*/, int32/*lastorder*/> ORDER_LOG;
86+
ORDER_LOG& getGlobalOrderLog(){ return _globalOrderLog; }
87+
ORDER_LOG& getBaseappGroupOrderLog(){ return _baseappGrouplOrderLog; }
88+
ORDER_LOG& getCellappGroupOrderLog(){ return _cellappGrouplOrderLog; }
89+
ORDER_LOG& getLoginappGroupOrderLog(){ return _loginappGrouplOrderLog; }
8490
private:
85-
COMPONENTS _baseapps;
86-
COMPONENTS _cellapps;
87-
COMPONENTS _dbmgrs;
88-
COMPONENTS _loginapps;
89-
COMPONENTS _cellappmgrs;
90-
COMPONENTS _baseappmgrs;
91-
COMPONENTS _machines;
92-
COMPONENTS _centers;
93-
Mercury::NetworkInterface* pNetworkInterface_;
91+
COMPONENTS _baseapps;
92+
COMPONENTS _cellapps;
93+
COMPONENTS _dbmgrs;
94+
COMPONENTS _loginapps;
95+
COMPONENTS _cellappmgrs;
96+
COMPONENTS _baseappmgrs;
97+
COMPONENTS _machines;
98+
COMPONENTS _centers;
99+
100+
Mercury::NetworkInterface* _pNetworkInterface;
101+
102+
// 组件的全局启动次序log和组(相同的组件为一组, 如:所有baseapp为一个组)启动次序log
103+
// 注意:中途有死掉的app组件这里log并不去做减操作, 从使用意图来看也没有必要做这个匹配。
104+
ORDER_LOG _globalOrderLog;
105+
ORDER_LOG _baseappGrouplOrderLog;
106+
ORDER_LOG _cellappGrouplOrderLog;
107+
ORDER_LOG _loginappGrouplOrderLog;
94108
};
95109

96110
}

‎kbe/src/lib/server/serverapp.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class ServerApp :
9595

9696
void shutDown();
9797

98-
/* 注册一个新激活的baseapp或者cellapp或者dbmgr
98+
/* 网络接口
99+
注册一个新激活的baseapp或者cellapp或者dbmgr
99100
通常是一个新的app被启动了, 它需要向某些组件注册自己。
100101
*/
101102
virtual void onRegisterNewApp(Mercury::Channel* pChannel,

‎kbe/src/server/baseapp/baseapp.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ void Baseapp::onReqAllocEntityID(Mercury::Channel* pChannel, ENTITY_ID startID,
154154
idClient_.onAddRange(startID, endID);
155155
}
156156

157+
//-------------------------------------------------------------------------------------
158+
void Baseapp::onDbmgrInit(Mercury::Channel* pChannel,
159+
ENTITY_ID startID, ENTITY_ID endID, int32 startGlobalOrder, int32 startGroupOrder)
160+
{
161+
INFO_MSG("Baseapp::onDbmgrInit: entityID alloc(%d-%d), startGlobalOrder=%d, startGroupOrder=%d.\n",
162+
startID, endID, startGlobalOrder, startGroupOrder);
163+
}
164+
157165
//-------------------------------------------------------------------------------------
158166

159167
}

‎kbe/src/server/baseapp/baseapp.hpp‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ class Baseapp : public EntityApp,
7474
/** 网络接口
7575
请求分配一个ENTITY_ID段的回调
7676
*/
77-
void onReqAllocEntityID(Mercury::Channel* pChannel, int32 startID, int32 endID);
77+
void onReqAllocEntityID(Mercury::Channel* pChannel, ENTITY_ID startID, ENTITY_ID endID);
78+
79+
/** 网络接口
80+
dbmgr发送初始信息
81+
startID: 初始分配ENTITY_ID 段起始位置
82+
endID: 初始分配ENTITY_ID 段结束位置
83+
startGlobalOrder: 全局启动顺序 包括各种不同组件
84+
startGroupOrder: 组内启动顺序, 比如在所有baseapp中第几个启动。
85+
*/
86+
void onDbmgrInit(Mercury::Channel* pChannel,
87+
ENTITY_ID startID, ENTITY_ID endID, int32 startGlobalOrder, int32 startGroupOrder);
7888
protected:
7989
EntityIDClient idClient_;
8090
//Entities<Entity>* pEntities_; // 存储所有的entity的容器

‎kbe/src/server/baseapp/baseapp_interface.hpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ NETWORK_INTERFACE_DECLARE_BEGIN(BaseappInterface)
6464
ENTITY_ID, endID)
6565

6666

67+
// 某app请求获取一个entityID段的回调
68+
BASEAPP_MESSAGE_DECLARE_ARGS4(onDbmgrInit, MERCURY_FIXED_MESSAGE,
69+
ENTITY_ID, startID,
70+
ENTITY_ID, endID,
71+
int32, startGlobalOrder,
72+
int32, startGroupOrder)
73+
6774
NETWORK_INTERFACE_DECLARE_END()
6875

6976
#ifdef DEFINE_IN_INTERFACE

‎kbe/src/server/baseapp/main.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
4545
#define DEFINE_IN_INTERFACE
4646
#include "dbmgr/dbmgr_interface.hpp"
4747

48+
#undef DEFINE_IN_INTERFACE
49+
#include "loginapp/loginapp_interface.hpp"
50+
#define DEFINE_IN_INTERFACE
51+
#include "loginapp/loginapp_interface.hpp"
52+
4853
using namespace KBEngine;
4954

5055
int KBENGINE_MAIN(int argc, char* argv[])

‎kbe/src/server/baseappmgr/main.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
4545
#define DEFINE_IN_INTERFACE
4646
#include "dbmgr/dbmgr_interface.hpp"
4747

48+
#undef DEFINE_IN_INTERFACE
49+
#include "loginapp/loginapp_interface.hpp"
50+
#define DEFINE_IN_INTERFACE
51+
#include "loginapp/loginapp_interface.hpp"
52+
4853
using namespace KBEngine;
4954

5055
int KBENGINE_MAIN(int argc, char* argv[])

0 commit comments

Comments
 (0)