Skip to content

Commit 87d3895

Browse files
committed
baseappmgr增加负载平衡算法
1 parent 76eb45f commit 87d3895

8 files changed

Lines changed: 142 additions & 2 deletions

File tree

‎kbe/res/server/kbengine_defs.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
<archivePeriod> 100 </archivePeriod> <!-- Type: Float -->
5454
<backupPeriod> 10 </backupPeriod> <!-- Type: Float -->
5555
<backUpUndefinedProperties> 0 </backUpUndefinedProperties> <!-- Type: Boolean -->
56+
57+
<loadSmoothingBias> 0.5 </loadSmoothingBias>
5658
</baseapp>
5759

5860
<cellappmgr>

‎kbe/src/server/baseappmgr/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
BIN = baseappmgr
22
SRCS = \
3+
baseapp \
34
baseappmgr \
45
baseappmgr_interface \
56
main
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This source file is part of KBEngine
3+
For the latest info, see http://www.kbengine.org/
4+
5+
Copyright (c) 2008-2012 KBEngine.
6+
7+
KBEngine is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU Lesser General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
KBEngine is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public License
18+
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
#include "baseapp.hpp"
21+
22+
namespace KBEngine{
23+
24+
//-------------------------------------------------------------------------------------
25+
Baseapp::Baseapp():
26+
numBases_(0),
27+
numProxices_(0),
28+
load_(0.f)
29+
{
30+
}
31+
32+
//-------------------------------------------------------------------------------------
33+
Baseapp::~Baseapp()
34+
{
35+
}
36+
37+
38+
//-------------------------------------------------------------------------------------
39+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
This source file is part of KBEngine
3+
For the latest info, see http://www.kbengine.org/
4+
5+
Copyright (c) 2008-2012 KBEngine.
6+
7+
KBEngine is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU Lesser General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
KBEngine is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public License
18+
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef __BASEAPPMGR_BASEAPP_H__
22+
#define __BASEAPPMGR_BASEAPP_H__
23+
24+
// common include
25+
// #define NDEBUG
26+
#include "cstdkbe/cstdkbe.hpp"
27+
#include "helper/debug_helper.hpp"
28+
29+
namespace KBEngine{
30+
31+
class Baseapp
32+
{
33+
public:
34+
Baseapp();
35+
virtual ~Baseapp();
36+
37+
ENTITY_ID numBases()const { return numBases_; }
38+
void numBases(ENTITY_ID num) { numBases_ = num; }
39+
40+
ENTITY_ID numProxices()const { return numProxices_; }
41+
void numProxices(ENTITY_ID num) { numProxices_ = num; }
42+
43+
float load()const { return load_; }
44+
void load(int32 v) { load_ = v; }
45+
46+
protected:
47+
ENTITY_ID numBases_;
48+
ENTITY_ID numProxices_;
49+
float load_;
50+
};
51+
52+
}
53+
#endif

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ Baseappmgr::Baseappmgr(Mercury::EventDispatcher& dispatcher,
4646
COMPONENT_ID componentID):
4747
ServerApp(dispatcher, ninterface, componentType, componentID),
4848
gameTimer_(),
49-
forward_baseapp_messagebuffer_(ninterface, BASEAPP_TYPE)
49+
forward_baseapp_messagebuffer_(ninterface, BASEAPP_TYPE),
50+
bestBaseappID_(0)
5051
{
5152
}
5253

@@ -83,6 +84,7 @@ void Baseappmgr::handleGameTick()
8384
//DEBUG_MSG("CellApp::handleGameTick[%"PRTime"]:%u\n", t, time_);
8485

8586
g_kbetime++;
87+
updateBestBaseapp();
8688
threadPool_.onMainThreadTick();
8789
getNetworkInterface().handleChannels(&BaseappmgrInterface::messageHandlers);
8890
}
@@ -130,6 +132,24 @@ void Baseappmgr::forwardMessage(Mercury::Channel* pChannel, MemoryStream& s)
130132
Mercury::Bundle::ObjPool().reclaimObject(pBundle);
131133
}
132134

135+
//-------------------------------------------------------------------------------------
136+
void Baseappmgr::updateBaseapp(Mercury::Channel* pChannel,
137+
ENTITY_ID numBases, ENTITY_ID numProxices, float load)
138+
{
139+
}
140+
141+
//-------------------------------------------------------------------------------------
142+
COMPONENT_ID Baseappmgr::findFreeBaseapp()
143+
{
144+
return 0;
145+
}
146+
147+
//-------------------------------------------------------------------------------------
148+
void Baseappmgr::updateBestBaseapp()
149+
{
150+
bestBaseappID_ = findFreeBaseapp();
151+
}
152+
133153
//-------------------------------------------------------------------------------------
134154
void Baseappmgr::reqCreateBaseAnywhere(Mercury::Channel* pChannel, MemoryStream& s)
135155
{
@@ -149,7 +169,7 @@ void Baseappmgr::reqCreateBaseAnywhere(Mercury::Channel* pChannel, MemoryStream&
149169
forward_baseapp_messagebuffer_.push(pFI);
150170
return;
151171
}
152-
172+
153173
static uint32 currentBaseappIndex = 0;
154174
if(currentBaseappIndex > componentSize - 1)
155175
currentBaseappIndex = 0;

‎kbe/src/server/baseappmgr/baseappmgr.hpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
2323
#define __BASEAPPMGR_H__
2424

2525
// common include
26+
#include "baseapp.hpp"
2627
#include "server/kbemain.hpp"
2728
#include "server/serverapp.hpp"
2829
#include "server/idallocate.hpp"
@@ -68,6 +69,9 @@ class Baseappmgr : public ServerApp,
6869
bool initializeEnd();
6970
void finalise();
7071

72+
COMPONENT_ID findFreeBaseapp();
73+
void updateBestBaseapp();
74+
7175
/** 网络接口
7276
收到baseapp::createBaseAnywhere请求在某个空闲的baseapp上创建一个baseEntity
7377
@param sp: 这个数据包中存储的是 entityType : entity的类别, entities.xml中的定义的。
@@ -101,9 +105,16 @@ class Baseappmgr : public ServerApp,
101105
*/
102106
void onPendingAccountGetBaseappAddr(Mercury::Channel* pChannel,
103107
std::string& accountName, uint32 addr, uint16 port);
108+
109+
/** 网络接口
110+
更新baseapp情况。
111+
*/
112+
void updateBaseapp(Mercury::Channel* pChannel,
113+
ENTITY_ID numBases, ENTITY_ID numProxices, float load);
104114
protected:
105115
TimerHandle gameTimer_;
106116
ForwardAnywhere_MessageBuffer forward_baseapp_messagebuffer_;
117+
COMPONENT_ID bestBaseappID_;
107118
};
108119

109120
}

‎kbe/src/server/baseappmgr/baseappmgr90.vcproj‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@
179179
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
180180
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
181181
>
182+
<File
183+
RelativePath=".\baseapp.cpp"
184+
>
185+
</File>
182186
<File
183187
RelativePath=".\baseappmgr.cpp"
184188
>
@@ -203,6 +207,10 @@
203207
Filter="h;hpp;hxx;hm;inl;inc;xsd"
204208
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
205209
>
210+
<File
211+
RelativePath=".\baseapp.hpp"
212+
>
213+
</File>
206214
<File
207215
RelativePath=".\baseappmgr.hpp"
208216
>

‎kbe/src/server/baseappmgr/baseappmgr_interface.hpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ NETWORK_INTERFACE_DECLARE_BEGIN(BaseappmgrInterface)
9595
// 请求关闭服务器
9696
BASEAPPMGR_MESSAGE_DECLARE_STREAM(reqCloseServer, MERCURY_VARIABLE_MESSAGE)
9797

98+
// 获取到baseapp的地址。
99+
BASEAPPMGR_MESSAGE_DECLARE_ARGS3(updateBaseapp, MERCURY_FIXED_MESSAGE,
100+
ENTITY_ID, numBases,
101+
ENTITY_ID, numProxices,
102+
float, load)
103+
98104
NETWORK_INTERFACE_DECLARE_END()
99105

100106
#ifdef DEFINE_IN_INTERFACE

0 commit comments

Comments
 (0)