Skip to content

Commit f48a91f

Browse files
committed
dbmgr应该支持不同类型的billinghandler来完成各种第三方需求
1 parent 2b1bd5e commit f48a91f

17 files changed

Lines changed: 314 additions & 22 deletions

File tree

‎kbe/src/examples/testnetwork/main.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ void init_network(void)
438438
bundle2.newMessage(LoginappInterface::login);
439439
int8 tclient = 1;
440440
bundle2 << tclient;
441-
bundle2 << "phone";
441+
bindatas = "phone";
442+
bundle2.appendBlob(bindatas);
442443
bundle2 << accountname;
443444
bundle2 << "123456";
444445
bundle2.send(mysocket);

‎kbe/src/lib/network/address.cpp‎

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

2121

2222
#include "address.hpp"
23+
#include "endpoint.hpp"
2324
#include "helper/debug_helper.hpp"
2425

2526
namespace KBEngine {
@@ -60,6 +61,16 @@ void Address::onReclaimObject()
6061
port = 0;
6162
}
6263

64+
//-------------------------------------------------------------------------------------
65+
Address::Address(std::string ipArg, uint16 portArg):
66+
ip(0),
67+
port(htons(portArg))
68+
{
69+
u_int32_t addr;
70+
Mercury::EndPoint::convertAddress(ipArg.c_str(), addr);
71+
ip = (uint32)addr;
72+
}
73+
6374
//-------------------------------------------------------------------------------------
6475
int Address::writeToString(char * str, int length) const
6576
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Address : public PoolObject
4141

4242
Address();
4343
Address(uint32 ipArg, uint16 portArg);
44+
Address(std::string ipArg, uint16 portArg);
45+
4446
uint32 ip;
4547
uint16 port;
4648

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ namespace tcp{
6969
#define KBE_PORT_BROADCAST_DISCOVERY KBE_PORT_START + 87
7070
#define KBE_MACHINE_TCP_PORT KBE_PORT_START + 88
7171

72+
#define KBE_BILLING_TCP_PORT 30099
73+
7274
/*
7375
网络消息类型, 定长或者变长。
7476
如果需要自定义长度则在NETWORK_INTERFACE_DECLARE_BEGIN中声明时填入长度即可。

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class EndPoint : public PoolObject
4040
static void destroyObjPool();
4141
void onReclaimObject();
4242

43+
EndPoint(Address address);
4344
EndPoint(u_int32_t networkAddr = 0, u_int16_t networkPort = 0);
4445
virtual ~EndPoint();
4546
operator KBESOCKET() const;
@@ -65,11 +66,16 @@ class EndPoint : public PoolObject
6566
int setnodelay(bool nodelay = true);
6667

6768
int bind(u_int16_t networkPort = 0, u_int32_t networkAddr = INADDR_ANY);
69+
6870
int listen(int backlog = 5);
71+
6972
int connect(u_int16_t networkPort, u_int32_t networkAddr = INADDR_BROADCAST, bool autosetflags = true);
73+
int connect(bool autosetflags = true);
74+
7075
EndPoint* accept(u_int16_t * networkPort = NULL, u_int32_t * networkAddr = NULL, bool autosetflags = true);
7176

7277
INLINE int send(const void * gramData, int gramSize);
78+
7379
int recv(void * gramData, int gramSize);
7480
bool recvAll(void * gramData, int gramSize);
7581

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ socket_(-1)
3737
}
3838
}
3939

40+
INLINE EndPoint::EndPoint(Address address):
41+
#if KBE_PLATFORM == PLATFORM_WIN32
42+
socket_(INVALID_SOCKET)
43+
#else
44+
socket_(-1)
45+
#endif
46+
{
47+
if(address.ip > 0)
48+
{
49+
address_ = address;
50+
}
51+
}
52+
4053
INLINE EndPoint::~EndPoint()
4154
{
4255
this->close();
@@ -325,6 +338,11 @@ INLINE int EndPoint::listen(int backlog)
325338
return ::listen(socket_, backlog);
326339
}
327340

341+
INLINE int EndPoint::connect(bool autosetflags)
342+
{
343+
return connect(address_.port, address_.ip, autosetflags);
344+
}
345+
328346
INLINE int EndPoint::connect(u_int16_t networkPort, u_int32_t networkAddr, bool autosetflags)
329347
{
330348
sockaddr_in sin;
@@ -338,6 +356,7 @@ INLINE int EndPoint::connect(u_int16_t networkPort, u_int32_t networkAddr, bool
338356
setnonblocking(true);
339357
setnodelay(true);
340358
}
359+
341360
return ret;
342361
}
343362

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ KBE_SINGLETON_INIT(ServerConfig);
2929

3030
//-------------------------------------------------------------------------------------
3131
ServerConfig::ServerConfig():
32-
gameUpdateHertz_(10)
32+
gameUpdateHertz_(10),
33+
billingSystemAddr_(),
34+
billingSystem_type_("")
3335
{
3436
}
3537

@@ -121,6 +123,38 @@ bool ServerConfig::loadConfig(std::string fileName)
121123
rootNode = NULL;
122124
}
123125

126+
rootNode = xml->getRootNode("billingSystem");
127+
if(rootNode != NULL)
128+
{
129+
TiXmlNode* childnode = xml->enterNode(rootNode, "type");
130+
if(childnode)
131+
billingSystem_type_ = xml->getValStr(childnode);
132+
133+
std::string ip = "";
134+
childnode = NULL;
135+
childnode = xml->enterNode(rootNode, "host");
136+
if(childnode)
137+
ip = xml->getValStr(childnode);
138+
139+
uint16 port = 0;
140+
childnode = NULL;
141+
childnode = xml->enterNode(rootNode, "port");
142+
if(childnode)
143+
port = xml->getValInt(childnode);
144+
145+
if(billingSystem_type_.size() == 0)
146+
billingSystem_type_ = "normal";
147+
148+
if(ip.size() == 0)
149+
ip = "localhost";
150+
151+
if(port <= 0)
152+
port = KBE_BILLING_TCP_PORT;
153+
154+
Mercury::Address addr(ip, port);
155+
billingSystemAddr_ = addr;
156+
}
157+
124158
rootNode = xml->getRootNode("cellapp");
125159
if(rootNode != NULL)
126160
{

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ class ServerConfig : public Singleton<ServerConfig>
125125
const Mercury::Address& internalAddr, const Mercury::Address& externalAddr);
126126

127127
inline int16 gameUpdateHertz(void)const { return gameUpdateHertz_;}
128+
129+
inline Mercury::Address billingSystemAddr(void)const { return billingSystemAddr_;}
130+
131+
inline const char* billingSystemType()const { return billingSystem_type_.c_str(); }
128132
private:
129133
ENGINE_COMPONENT_INFO _cellAppInfo;
130134
ENGINE_COMPONENT_INFO _baseAppInfo;
@@ -141,7 +145,10 @@ class ServerConfig : public Singleton<ServerConfig>
141145
int16 gameUpdateHertz_;
142146

143147
// 每个客户端每秒占用的最大带宽
144-
uint32 bitsPerSecondToClient_;
148+
uint32 bitsPerSecondToClient_;
149+
150+
Mercury::Address billingSystemAddr_;
151+
std::string billingSystem_type_; // 计费系统类别
145152
};
146153

147154
#define g_kbeSrvConfig ServerConfig::getSingleton()

‎kbe/src/server/dbmgr/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
BIN = dbmgr
22
SRCS = \
3+
billinghandler \
34
dbmgr \
45
dbmgr_interface \
56
dbtasks \
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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 "billinghandler.hpp"
21+
#include "buffered_dbtasks.hpp"
22+
#include "db_threadpool.hpp"
23+
#include "thread/threadpool.hpp"
24+
#include "thread/threadguard.hpp"
25+
#include "server/serverconfig.hpp"
26+
#include "network/endpoint.hpp"
27+
#include "network/channel.hpp"
28+
29+
namespace KBEngine{
30+
31+
//-------------------------------------------------------------------------------------
32+
BillingHandler* BillingHandlerFactory::create(std::string type,
33+
thread::ThreadPool& threadPool,
34+
DBThreadPool& dbThreadPool)
35+
{
36+
if(type.size() == 0 || type == "normal")
37+
{
38+
return new BillingHandler_Normal(threadPool, dbThreadPool);
39+
}
40+
else if(type == "thirdparty")
41+
{
42+
return new BillingHandler_ThirdParty(threadPool, dbThreadPool);
43+
}
44+
45+
return NULL;
46+
}
47+
48+
//-------------------------------------------------------------------------------------
49+
BillingHandler::BillingHandler(thread::ThreadPool& threadPool, DBThreadPool& dbThreadPool):
50+
dbThreadPool_(dbThreadPool),
51+
threadPool_(threadPool)
52+
{
53+
}
54+
55+
//-------------------------------------------------------------------------------------
56+
BillingHandler::~BillingHandler()
57+
{
58+
}
59+
60+
//-------------------------------------------------------------------------------------
61+
BillingHandler_Normal::BillingHandler_Normal(thread::ThreadPool& threadPool, DBThreadPool& dbThreadPool):
62+
BillingHandler(threadPool, dbThreadPool)
63+
{
64+
}
65+
66+
//-------------------------------------------------------------------------------------
67+
BillingHandler_Normal::~BillingHandler_Normal()
68+
{
69+
}
70+
71+
//-------------------------------------------------------------------------------------
72+
bool BillingHandler_Normal::createAccount(Mercury::Channel* pChannel, std::string& accountName,
73+
std::string& password, std::string& datas)
74+
{
75+
dbThreadPool_.addTask(new DBTaskCreateAccount(pChannel->addr(),
76+
accountName, password, datas));
77+
78+
return true;
79+
}
80+
81+
//-------------------------------------------------------------------------------------
82+
bool BillingHandler_Normal::loginAccount(Mercury::Channel* pChannel, std::string& loginName,
83+
std::string& password, std::string& datas)
84+
{
85+
dbThreadPool_.addTask(new DBTaskAccountLogin(pChannel->addr(),
86+
loginName, password, datas));
87+
88+
return true;
89+
}
90+
91+
//-------------------------------------------------------------------------------------
92+
BillingHandler_ThirdParty::BillingHandler_ThirdParty(thread::ThreadPool& threadPool, DBThreadPool& dbThreadPool):
93+
BillingHandler(threadPool, dbThreadPool),
94+
pEndPoint_(NULL)
95+
{
96+
}
97+
98+
//-------------------------------------------------------------------------------------
99+
BillingHandler_ThirdParty::~BillingHandler_ThirdParty()
100+
{
101+
SAFE_RELEASE(pEndPoint_);
102+
}
103+
104+
//-------------------------------------------------------------------------------------
105+
bool BillingHandler_ThirdParty::createAccount(Mercury::Channel* pChannel, std::string& accountName,
106+
std::string& password, std::string& datas)
107+
{
108+
return true;
109+
}
110+
111+
//-------------------------------------------------------------------------------------
112+
bool BillingHandler_ThirdParty::loginAccount(Mercury::Channel* pChannel, std::string& loginName,
113+
std::string& password, std::string& datas)
114+
{
115+
return true;
116+
}
117+
118+
//-------------------------------------------------------------------------------------
119+
bool BillingHandler_ThirdParty::initialize()
120+
{
121+
Mercury::Address addr = g_kbeSrvConfig.billingSystemAddr();
122+
123+
pEndPoint_ = new Mercury::EndPoint(addr);
124+
125+
pEndPoint_->socket(SOCK_STREAM);
126+
if (!pEndPoint_->good())
127+
{
128+
ERROR_MSG("BillingHandler_ThirdParty::initialize: couldn't create a socket\n");
129+
return true;
130+
}
131+
132+
pEndPoint_->setnonblocking(true);
133+
pEndPoint_->setnodelay(true);
134+
return reconnect();
135+
}
136+
137+
//-------------------------------------------------------------------------------------
138+
bool BillingHandler_ThirdParty::reconnect()
139+
{
140+
if(pEndPoint_ == NULL)
141+
return false;
142+
143+
int trycount = 0;
144+
145+
while(true)
146+
{
147+
fd_set frds, fwds;
148+
struct timeval tv = { 0, 100000 }; // 100ms
149+
150+
FD_ZERO( &frds );
151+
FD_ZERO( &fwds );
152+
FD_SET((int)(*pEndPoint_), &frds);
153+
FD_SET((int)(*pEndPoint_), &fwds);
154+
155+
if(pEndPoint_->connect() == -1)
156+
{
157+
int selgot = select((*pEndPoint_)+1, &frds, &fwds, NULL, &tv);
158+
if(selgot > 0)
159+
{
160+
break;
161+
}
162+
163+
trycount++;
164+
165+
if(trycount > 3)
166+
{
167+
ERROR_MSG(boost::format("BillingHandler_ThirdParty::reconnect(): couldn't connect to:%1%\n") %
168+
pEndPoint_->addr().c_str());
169+
170+
return false;
171+
}
172+
}
173+
}
174+
175+
return true;
176+
}
177+
178+
//-------------------------------------------------------------------------------------
179+
180+
}

0 commit comments

Comments
 (0)