|
| 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