Example #1
0
void ISitableRoom::onGameDidEnd()
{
	for ( uint8_t nIdx = 0 ; nIdx < m_nSeatCnt ; ++nIdx )
	{
		auto pPlayer = m_vSitdownPlayers[nIdx] ;
		if ( pPlayer == nullptr )
		{
			continue;
		}

		if ( pPlayer->isHaveState(eRoomPeer_StayThisRound) && getDelegate() )
		{
			getDelegate()->onUpdatePlayerGameResult(this,pPlayer->getUserUID(),pPlayer->getGameOffset()) ;
			LOGFMTD("update room peer offset uid = %u, offset = %d",pPlayer->getUserUID(),pPlayer->getGameOffset());
		}

		if ( pPlayer->getNoneActTimes() >= getMaxNoneActTimeForStandUp() || (pPlayer->isDelayStandUp() || pPlayer->getCoin() < coinNeededToSitDown() || (getDelegate() && getDelegate()->isPlayerLoseReachMax(this,pPlayer->getUserUID())) ) )
		{
			playerDoStandUp(pPlayer);	
			pPlayer = nullptr ;
			m_vSitdownPlayers[nIdx] = nullptr ;
		}

		if ( pPlayer )
		{
			pPlayer->onGameEnd() ;
		}
	}
	m_vSortByPeerCardsAsc.clear();
	IRoom::onGameDidEnd() ;
}
Example #2
0
inline void START_MSG(const char * name, uint64 appuid)
{
	MachineInfos machineInfo;
	
	std::string s = (boost::format("---- %1% "
			"Version: %2%. "
			"ScriptVersion: %3%. "
			"Config: %4%. "
			"Built: %5% %6%. "
			"AppUID: %7%. "
			"UID: %8%. "
			"PID: %9% ----\n") %
		name % KBEVersion::versionString() % KBEVersion::scriptVersionString() %
		KBE_CONFIG % __TIME__ % __DATE__ %
		appuid % getUserUID() % getProcessPID()).str();

	INFO_MSG(s);
	
#if KBE_PLATFORM == PLATFORM_WIN32
	printf("%s", s.c_str());
#endif

	s = (boost::format("Server %1%: %2% with %3% RAM\n") %
		machineInfo.machineName().c_str() %
		machineInfo.cpuInfo().c_str() %
		machineInfo.memInfo().c_str() ).str();

	INFO_MSG(s);

#if KBE_PLATFORM == PLATFORM_WIN32
	printf("%s\n", s.c_str());
#endif

}
Example #3
0
//-------------------------------------------------------------------------------------
bool Componentbridge::process()
{
	if(state_ == 0)
	{
		// 如果是cellappmgr或者baseapmgrp则向machine请求获得dbmgr的地址
		Mercury::BundleBroadcast bhandler(networkInterface_, KBE_PORT_BROADCAST_DISCOVERY);

		bhandler.newMessage(MachineInterface::onBroadcastInterface);
		MachineInterface::onBroadcastInterfaceArgs8::staticAddToBundle(bhandler, getUserUID(), getUsername(), 
			componentType_, componentID_, 
			networkInterface_.intaddr().ip, networkInterface_.intaddr().port,
			networkInterface_.extaddr().ip, networkInterface_.extaddr().port);
		
		bhandler.broadcast();

		bhandler.close();
		state_ = 1;
		return true;
	}
	else
	{
		if(componentType_ != MACHINE_TYPE)
			if(!findInterfaces())
				return true;
	}

	return false;
}
Example #4
0
//-------------------------------------------------------------------------------------
void Baseapp::onGetEntityAppFromDbmgr(Mercury::Channel* pChannel, int32 uid, std::string& username, 
						int8 componentType, uint64 componentID, 
						uint32 intaddr, uint16 intport, uint32 extaddr, uint16 extport)
{
	if(pChannel->isExternal())
		return;

	EntityApp<Base>::onRegisterNewApp(pChannel, uid, username, componentType, componentID, 
									intaddr, intport, extaddr, extport);

	KBEngine::COMPONENT_TYPE tcomponentType = (KBEngine::COMPONENT_TYPE)componentType;

	Components::COMPONENTS cts = Componentbridge::getComponents().getComponents(DBMGR_TYPE);
	KBE_ASSERT(cts.size() >= 1);
	
	Components::ComponentInfos* cinfos = Componentbridge::getComponents().findComponent(tcomponentType, uid, componentID);
	cinfos->pChannel = NULL;

	int ret = Components::getSingleton().connectComponent(tcomponentType, uid, componentID);
	KBE_ASSERT(ret != -1);

	Mercury::Bundle bundle;

	switch(tcomponentType)
	{
	case BASEAPP_TYPE:
		bundle.newMessage(BaseappInterface::onRegisterNewApp);
		BaseappInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), BASEAPP_TYPE, componentID_, 
			this->getNetworkInterface().intaddr().ip, this->getNetworkInterface().intaddr().port, 
			this->getNetworkInterface().extaddr().ip, this->getNetworkInterface().extaddr().port);
		break;
	case CELLAPP_TYPE:
		bundle.newMessage(CellappInterface::onRegisterNewApp);
		CellappInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), BASEAPP_TYPE, componentID_, 
			this->getNetworkInterface().intaddr().ip, this->getNetworkInterface().intaddr().port, 
			this->getNetworkInterface().extaddr().ip, this->getNetworkInterface().extaddr().port);
		break;
	default:
		KBE_ASSERT(false && "no support!\n");
		break;
	};
	
	bundle.send(this->getNetworkInterface(), cinfos->pChannel);
}
Example #5
0
//-------------------------------------------------------------------------------------
void Cellapp::onGetEntityAppFromDbmgr(Mercury::Channel* pChannel, int32 uid, std::string& username, 
						int8 componentType, uint64 componentID, int8 globalorderID, int8 grouporderID,
						uint32 intaddr, uint16 intport, uint32 extaddr, uint16 extport, std::string& extaddrEx)
{
	EntityApp<Entity>::onRegisterNewApp(pChannel, uid, username, componentType, componentID, globalorderID, grouporderID,
									intaddr, intport, extaddr, extport, extaddrEx);

	KBEngine::COMPONENT_TYPE tcomponentType = (KBEngine::COMPONENT_TYPE)componentType;

	Components::COMPONENTS& cts = Componentbridge::getComponents().getComponents(DBMGR_TYPE);
	KBE_ASSERT(cts.size() >= 1);
	
	Components::ComponentInfos* cinfos = Componentbridge::getComponents().findComponent(tcomponentType, uid, componentID);
	cinfos->pChannel = NULL;

	int ret = Components::getSingleton().connectComponent(tcomponentType, uid, componentID);
	KBE_ASSERT(ret != -1);

	Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool().createObject();

	switch(tcomponentType)
	{
	case BASEAPP_TYPE:
		(*pBundle).newMessage(BaseappInterface::onRegisterNewApp);
		BaseappInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
			CELLAPP_TYPE, componentID_, startGlobalOrder_, startGroupOrder_,
			this->getNetworkInterface().intaddr().ip, this->getNetworkInterface().intaddr().port, 
			this->getNetworkInterface().extaddr().ip, this->getNetworkInterface().extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
		break;
	case CELLAPP_TYPE:
		(*pBundle).newMessage(CellappInterface::onRegisterNewApp);
		CellappInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
			CELLAPP_TYPE, componentID_, startGlobalOrder_, startGroupOrder_,
			this->getNetworkInterface().intaddr().ip, this->getNetworkInterface().intaddr().port, 
			this->getNetworkInterface().extaddr().ip, this->getNetworkInterface().extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
		break;
	default:
		KBE_ASSERT(false && "no support!\n");
		break;
	};
	
	(*pBundle).send(this->getNetworkInterface(), cinfos->pChannel);
	Mercury::Bundle::ObjPool().reclaimObject(pBundle);
}
Example #6
0
void ISitableRoom::onPlayerEnterRoom(stEnterRoomData* pEnterRoomPlayer,int8_t& nSubIdx )
{
	IRoom::onPlayerEnterRoom(pEnterRoomPlayer, nSubIdx);
	auto pp = getSitdownPlayerByUID(pEnterRoomPlayer->nUserUID);
	if (pp)
	{
		auto pStandPlayer = getPlayerByUserUID(pEnterRoomPlayer->nUserUID);
		if (pStandPlayer->nCoin >= pp->getCoin())
		{
			if (pStandPlayer->nCoin != pp->getCoin())
			{
				LOGFMTD("uid = %u reenter room coin is not the same . stand = %u , sit = %u", pp->getUserUID(), pStandPlayer->nCoin, pp->getCoin());
			}
			pStandPlayer->nCoin -= pp->getCoin();
		}
		else
		{
			LOGFMTE("uid = %u stand coin is few then sit , why ? but stand = %u , sit = %u", pp->getUserUID(), pStandPlayer->nCoin, pp->getCoin());
			pStandPlayer->nCoin = 0;
		}
		pp->reactive(pEnterRoomPlayer->nUserSessionID);
	}
}
Example #7
0
//-------------------------------------------------------------------------------------		
Components::ComponentInfos* Components::findComponent(COMPONENT_ID componentID)
{
	int idx = 0;
	int32 uid = getUserUID();

	while(true)
	{
		COMPONENT_TYPE ct = ALL_COMPONENT_TYPES[idx++];
		if(ct == UNKNOWN_COMPONENT_TYPE)
			break;

		ComponentInfos* cinfos = findComponent(ct, uid, componentID);
		if(cinfos != NULL)
		{
			return cinfos;
		}
	}

	return NULL;
}
Example #8
0
//-------------------------------------------------------------------------------------
void Machine::onBroadcastInterface(Mercury::Channel* pChannel, int32 uid, std::string& username,
								   int8 componentType, uint64 componentID, 
									uint32 intaddr, uint16 intport,
									uint32 extaddr, uint16 extport)
{
	if(componentType == MACHINE_TYPE)
	{
		if(uid == getUserUID() && 
			intaddr == this->getNetworkInterface().intaddr().ip)
		{
			return;
		}
	}
	else if(componentType == DBMGR_TYPE) // 如果是dbmgr重启了则清空所有这个uid的记录。
	{
		const Components::ComponentInfos* pinfos = 
			Componentbridge::getComponents().findComponent(DBMGR_TYPE, uid, componentID);
		
		// 做一个容错处理, windows下没做处理, 广播包会受到多次, linux不会出现。
		// 将来会对windows进行下机制进行调整此处则不会出现不会NULL。
		if(pinfos == NULL)
		{
			INFO_MSG("Machine::onBroadcastInterface: /-----------------------------reset kbengine.-------------------------/\n");
			Componentbridge::getComponents().clear(uid);
			INFO_MSG("Machine::onBroadcastInterface: /-----------------------------end reset kbengine.---------------------/\n");
		}
		else
		{
			return;
		}
	}

	INFO_MSG("Machine::onBroadcastInterface: uid:%d, username:%s, componentType:%s, "
			"componentID:%"PRAppID", intaddr:%s, intport:%u, extaddr:%s, extport:%u.\n", 
		uid, username.c_str(), COMPONENT_NAME_EX((COMPONENT_TYPE)componentType), componentID, 
		inet_ntoa((struct in_addr&)intaddr), ntohs(intport),
		extaddr != 0 ? inet_ntoa((struct in_addr&)extaddr) : "nonsupport", ntohs(extport));

	Componentbridge::getComponents().addComponent(uid, username.c_str(), 
		(KBEngine::COMPONENT_TYPE)componentType, componentID, intaddr, intport, extaddr, extport);
}
Example #9
0
bool ISitableRoom::onMessage( stMsg* prealMsg , eMsgPort eSenderPort , uint32_t nPlayerSessionID )
{
	if ( IRoom::onMessage(prealMsg,eSenderPort,nPlayerSessionID) )
	{
		return true ;
	}

	switch ( prealMsg->usMsgType )
	{
	case MSG_REQ_CUR_GAME_OFFSET:
		{
			auto pPlayer = getSitdownPlayerBySessionID(nPlayerSessionID) ;
			if ( pPlayer )
			{
				stMsgReqRobotCurGameOffsetRet msgback ;
				msgback.nCurGameOffset = pPlayer->getTotalGameOffset();
				sendMsgToPlayer(&msgback,sizeof(msgback),nPlayerSessionID) ;
				LOGFMTD("robot req cur offset = %d , uid = %u",msgback.nCurGameOffset,pPlayer->getUserUID());
			}
		}
		break;
	case MSG_ADD_TEMP_HALO:
		{
			auto pPlayer = getPlayerBySessionID(nPlayerSessionID) ;
			if ( pPlayer == nullptr )
			{
				LOGFMTE("not in room player add temp halo session id = %u",nPlayerSessionID);
				break;
			}

			//if ( pPlayer->nPlayerType == ePlayer_Normal )
			//{
			//	LOGFMTE("normal can not add temp halo");
			//	break;
			//}

			stMsgAddTempHalo* pRet = (stMsgAddTempHalo*)prealMsg ;
			if ( 0 == pRet->nTargetUID )
			{
				pRet->nTargetUID = pPlayer->nUserUID ;
			}

			auto psitpp = getSitdownPlayerByUID(pRet->nTargetUID) ;
			if ( psitpp )
			{
				psitpp->setTempHaloWeight(pRet->nTempHalo);
				LOGFMTD("uid = %u add temp halo = %u",pRet->nTargetUID,pRet->nTempHalo) ;
			}
			else
			{
				LOGFMTE("uid = %u not sit down why add temp halo",pPlayer->nUserUID);
			}
		}
		break;
	case MSG_PLAYER_SITDOWN:
		{
			stMsgPlayerSitDownRet msgBack ;
			msgBack.nRet = 0 ;

			stStandPlayer* pPlayer = getPlayerBySessionID(nPlayerSessionID) ;
			if ( !pPlayer )
			{
				LOGFMTE("palyer session id = %d ,not in this room so , can not sit down",nPlayerSessionID) ;
				msgBack.nRet = 3 ;
				sendMsgToPlayer(&msgBack,sizeof(msgBack),nPlayerSessionID) ;
				break; 
			}

			auto pp = getSitdownPlayerByUID(pPlayer->nUserUID);
			if ( pp )
			{
				LOGFMTE("session id = %d , already sit down , don't sit down again",nPlayerSessionID ) ;
				msgBack.nRet = 4 ;
				sendMsgToPlayer(&msgBack,sizeof(msgBack),nPlayerSessionID) ;
				break;
			}

			stMsgPlayerSitDown* pRet = (stMsgPlayerSitDown*)prealMsg ;
			if ( pRet->nTakeInCoin == 0 || pRet->nTakeInCoin > pPlayer->nCoin)
			{
				pRet->nTakeInCoin = pPlayer->nCoin ;
			}

			if ( pRet->nTakeInCoin < coinNeededToSitDown() )
			{
				msgBack.nRet = 1 ;
				sendMsgToPlayer(&msgBack,sizeof(msgBack),nPlayerSessionID) ;
				break; 
			}

			if ( isSeatIdxEmpty(pRet->nIdx) == false )
			{
				msgBack.nRet = 2 ;
				sendMsgToPlayer(&msgBack,sizeof(msgBack),nPlayerSessionID) ;
				break; 
			}

			auto sitDownPlayer = getReuseSitableRoomPlayerObject() ;
			sitDownPlayer->reset(pPlayer) ;
			pPlayer->nCoin -= pRet->nTakeInCoin ;
			sitDownPlayer->setCoin(pRet->nTakeInCoin) ;
			sitDownPlayer->doSitdown(pRet->nIdx) ;
			sitDownPlayer->setIdx(pRet->nIdx);
			sitDownPlayer->setState(eRoomPeer_WaitNextGame );
			m_vSitdownPlayers[pRet->nIdx] = sitDownPlayer ;

			// tell others ;
			stMsgRoomSitDown msgSitDown ;
			msgSitDown.nIdx = sitDownPlayer->getIdx() ;
			msgSitDown.nSitDownPlayerUserUID = sitDownPlayer->getUserUID() ;
			msgSitDown.nTakeInCoin = sitDownPlayer->getCoin() ;
			sendRoomMsg(&msgSitDown,sizeof(msgSitDown));

			onPlayerSitDown(sitDownPlayer) ;

			if ( pPlayer->nPlayerType == ePlayer_Robot )
			{
				LOGFMTD("robot uid = %d enter room",sitDownPlayer->getUserUID()) ;
				m_pRobotDispatchStrage->onRobotJoin(sitDownPlayer->getSessionID());
			}
		}
		break;
	case MSG_PLAYER_STANDUP:
		{
			stMsgPlayerStandUpRet msgBack ;
			msgBack.nRet = 0 ;
			auto player = getSitdownPlayerBySessionID(nPlayerSessionID) ;
			if ( player == nullptr )
			{
				msgBack.nRet = 1 ;
				sendMsgToPlayer(&msgBack,sizeof(msgBack),nPlayerSessionID) ;
				break; 
			}
			onPlayerWillStandUp(player);
		}
		break;
	default:
		return false;
	}
	return true ;
}
Example #10
0
//-------------------------------------------------------------------------------------
void Dbmgr::onRegisterNewApp(Mercury::Channel* pChannel, int32 uid, std::string& username, 
						int8 componentType, uint64 componentID, int8 globalorderID, int8 grouporderID,
						uint32 intaddr, uint16 intport, uint32 extaddr, uint16 extport)
{
	ServerApp::onRegisterNewApp(pChannel, uid, username, componentType, componentID, globalorderID, grouporderID,
						intaddr, intport, extaddr, extport);

	KBEngine::COMPONENT_TYPE tcomponentType = (KBEngine::COMPONENT_TYPE)componentType;
	
	std::string digest = EntityDef::md5().getDigestStr();
	int32 startGroupOrder = 1;
	int32 startGlobalOrder = Componentbridge::getComponents().getGlobalOrderLog()[getUserUID()];

	if(grouporderID > 0)
		startGroupOrder = grouporderID;

	if(globalorderID > 0)
		startGlobalOrder = globalorderID;

	// 下一步:
	// 如果是连接到dbmgr则需要等待接收app初始信息
	// 例如:初始会分配entityID段以及这个app启动的顺序信息(是否第一个baseapp启动)
	if(tcomponentType == BASEAPP_TYPE || 
		tcomponentType == CELLAPP_TYPE || 
		tcomponentType == LOGINAPP_TYPE)
	{
		Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool().createObject();
		
		switch(tcomponentType)
		{
		case BASEAPP_TYPE:
			{
				if(grouporderID <= 0)
					startGroupOrder = Componentbridge::getComponents().getBaseappGroupOrderLog()[getUserUID()];


				onGlobalDataClientLogon(pChannel, BASEAPP_TYPE);

				std::pair<ENTITY_ID, ENTITY_ID> idRange = idServer_.allocRange();
				(*pBundle).newMessage(BaseappInterface::onDbmgrInitCompleted);
				BaseappInterface::onDbmgrInitCompletedArgs6::staticAddToBundle((*pBundle), g_kbetime, idRange.first, 
					idRange.second, startGlobalOrder, startGroupOrder, digest);
			}
			break;
		case CELLAPP_TYPE:
			{
				if(grouporderID <= 0)
					startGroupOrder = Componentbridge::getComponents().getCellappGroupOrderLog()[getUserUID()];

				onGlobalDataClientLogon(pChannel, CELLAPP_TYPE);

				std::pair<ENTITY_ID, ENTITY_ID> idRange = idServer_.allocRange();
				(*pBundle).newMessage(CellappInterface::onDbmgrInitCompleted);
				CellappInterface::onDbmgrInitCompletedArgs6::staticAddToBundle((*pBundle), g_kbetime, idRange.first, 
					idRange.second, startGlobalOrder, startGroupOrder, digest);
			}
			break;
		case LOGINAPP_TYPE:
			if(grouporderID <= 0)
				startGroupOrder = Componentbridge::getComponents().getLoginappGroupOrderLog()[getUserUID()];

			(*pBundle).newMessage(LoginappInterface::onDbmgrInitCompleted);
			LoginappInterface::onDbmgrInitCompletedArgs3::staticAddToBundle((*pBundle), 
				startGlobalOrder, startGroupOrder, digest);

			break;
		default:
			break;
		}

		(*pBundle).send(networkInterface_, pChannel);
		Mercury::Bundle::ObjPool().reclaimObject(pBundle);
	}

	// 如果是baseapp或者cellapp则将自己注册到所有其他baseapp和cellapp
	if(tcomponentType == BASEAPP_TYPE || 
		tcomponentType == CELLAPP_TYPE)
	{
		KBEngine::COMPONENT_TYPE broadcastCpTypes[2] = {BASEAPP_TYPE, CELLAPP_TYPE};
		for(int idx = 0; idx < 2; idx++)
		{
			Components::COMPONENTS& cts = Components::getSingleton().getComponents(broadcastCpTypes[idx]);
			Components::COMPONENTS::iterator fiter = cts.begin();
			for(; fiter != cts.end(); fiter++)
			{
				if((*fiter).cid == componentID)
					continue;

				Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool().createObject();
				ENTITTAPP_COMMON_MERCURY_MESSAGE(broadcastCpTypes[idx], (*pBundle), onGetEntityAppFromDbmgr);
				
				if(tcomponentType == BASEAPP_TYPE)
				{
					BaseappInterface::onGetEntityAppFromDbmgrArgs10::staticAddToBundle((*pBundle), 
						uid, username, componentType, componentID, startGlobalOrder, startGroupOrder,
							intaddr, intport, extaddr, extport);
				}
				else
				{
					CellappInterface::onGetEntityAppFromDbmgrArgs10::staticAddToBundle((*pBundle), 
						uid, username, componentType, componentID, startGlobalOrder, startGroupOrder,
							intaddr, intport, extaddr, extport);
				}
				
				KBE_ASSERT((*fiter).pChannel != NULL);
				(*pBundle).send(networkInterface_, (*fiter).pChannel);
				Mercury::Bundle::ObjPool().reclaimObject(pBundle);
			}
		}
	}
}
Example #11
0
//-------------------------------------------------------------------------------------		
int Components::connectComponent(COMPONENT_TYPE componentType, int32 uid, COMPONENT_ID componentID, bool printlog)
{
	Components::ComponentInfos* pComponentInfos = findComponent(componentType, uid, componentID);
	if (pComponentInfos == NULL)
	{
		if (printlog)
		{
			ERROR_MSG(fmt::format("Components::connectComponent: not found componentType={}, uid={}, componentID={}!\n",
				COMPONENT_NAME_EX(componentType), uid, componentID));
		}

		return -1;
	}

	Network::EndPoint * pEndpoint = Network::EndPoint::createPoolObject();
	pEndpoint->socket(SOCK_STREAM);
	if (!pEndpoint->good())
	{
		if (printlog)
		{
			ERROR_MSG("Components::connectComponent: couldn't create a socket\n");
		}

		Network::EndPoint::reclaimPoolObject(pEndpoint);
		return -1;
	}

	pEndpoint->addr(*pComponentInfos->pIntAddr);
	int ret = pEndpoint->connect(pComponentInfos->pIntAddr->port, pComponentInfos->pIntAddr->ip);

	if(ret == 0)
	{
		Network::Channel* pChannel = Network::Channel::createPoolObject();
		bool ret = pChannel->initialize(*_pNetworkInterface, pEndpoint, Network::Channel::INTERNAL);
		if(!ret)
		{
			if (printlog)
			{
				ERROR_MSG(fmt::format("Components::connectComponent: initialize({}) is failed!\n",
					pChannel->c_str()));
			}

			pChannel->destroy();
			Network::Channel::reclaimPoolObject(pChannel);
			return -1;
		}

		pComponentInfos->pChannel = pChannel;
		pComponentInfos->pChannel->componentID(componentID);
		if(!_pNetworkInterface->registerChannel(pComponentInfos->pChannel))
		{
			if (printlog)
			{
				ERROR_MSG(fmt::format("Components::connectComponent: registerChannel({}) is failed!\n",
					pComponentInfos->pChannel->c_str()));
			}

			pComponentInfos->pChannel->destroy();
			Network::Channel::reclaimPoolObject(pComponentInfos->pChannel);

			// 此时不可强制释放内存,destroy中已经对其减引用
			// SAFE_RELEASE(pComponentInfos->pChannel);
			pComponentInfos->pChannel = NULL;
			return -1;
		}
		else
		{
			Network::Bundle* pBundle = Network::Bundle::createPoolObject();
			if(componentType == BASEAPPMGR_TYPE)
			{
				(*pBundle).newMessage(BaseappmgrInterface::onRegisterNewApp);
				
				BaseappmgrInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					componentType_, componentID_, 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == CELLAPPMGR_TYPE)
			{
				(*pBundle).newMessage(CellappmgrInterface::onRegisterNewApp);
				
				CellappmgrInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					componentType_, componentID_, 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == CELLAPP_TYPE)
			{
				(*pBundle).newMessage(CellappInterface::onRegisterNewApp);
				
				CellappInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					componentType_, componentID_, 
					g_componentGlobalOrder, g_componentGroupOrder,
						_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == BASEAPP_TYPE)
			{
				(*pBundle).newMessage(BaseappInterface::onRegisterNewApp);
				
				BaseappInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					componentType_, componentID_, 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == DBMGR_TYPE)
			{
				(*pBundle).newMessage(DbmgrInterface::onRegisterNewApp);
				
				DbmgrInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					componentType_, componentID_, 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == LOGGER_TYPE)
			{
				(*pBundle).newMessage(LoggerInterface::onRegisterNewApp);
				
				LoggerInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					componentType_, componentID_, 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else
			{
				KBE_ASSERT(false && "invalid componentType.\n");
			}

			pComponentInfos->pChannel->send(pBundle);
		}
	}
	else
	{
		if (printlog)
		{
			ERROR_MSG(fmt::format("Components::connectComponent: connect({}) is failed! {}.\n",
				pComponentInfos->pIntAddr->c_str(), kbe_strerror()));
		}

		Network::EndPoint::reclaimPoolObject(pEndpoint);
		return -1;
	}

	return ret;
}
Example #12
0
//-------------------------------------------------------------------------------------		
int Components::connectComponent(COMPONENT_TYPE componentType, int32 uid, COMPONENT_ID componentID)
{
	Components::ComponentInfos* pComponentInfos = findComponent(componentType, uid, componentID);
	KBE_ASSERT(pComponentInfos != NULL);

	Mercury::EndPoint * pEndpoint = new Mercury::EndPoint;
	pEndpoint->socket(SOCK_STREAM);
	if (!pEndpoint->good())
	{
		ERROR_MSG("Components::connectComponent: couldn't create a socket\n");
		delete pEndpoint;
		return -1;
	}

	pEndpoint->addr(*pComponentInfos->pIntAddr);
	int ret = pEndpoint->connect(pComponentInfos->pIntAddr->port, pComponentInfos->pIntAddr->ip);

	if(ret == 0)
	{
		pComponentInfos->pChannel = new Mercury::Channel(*_pNetworkInterface, pEndpoint, Mercury::Channel::INTERNAL);
		pComponentInfos->pChannel->componentID(componentID);
		if(!_pNetworkInterface->registerChannel(pComponentInfos->pChannel))
		{
			ERROR_MSG(boost::format("Components::connectComponent: registerChannel(%1%) is failed!\n") %
				pComponentInfos->pChannel->c_str());

			pComponentInfos->pChannel->destroy();
			pComponentInfos->pChannel = NULL;
			return -1;
		}
		else
		{
			Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool().createObject();
			if(componentType == BASEAPPMGR_TYPE)
			{
				(*pBundle).newMessage(BaseappmgrInterface::onRegisterNewApp);
				
				BaseappmgrInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == CELLAPPMGR_TYPE)
			{
				(*pBundle).newMessage(CellappmgrInterface::onRegisterNewApp);
				
				CellappmgrInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == CELLAPP_TYPE)
			{
				(*pBundle).newMessage(CellappInterface::onRegisterNewApp);
				
				CellappInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					g_componentGlobalOrder, g_componentGroupOrder,
						_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == BASEAPP_TYPE)
			{
				(*pBundle).newMessage(BaseappInterface::onRegisterNewApp);
				
				BaseappInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == DBMGR_TYPE)
			{
				(*pBundle).newMessage(DbmgrInterface::onRegisterNewApp);
				
				DbmgrInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else if(componentType == MESSAGELOG_TYPE)
			{
				(*pBundle).newMessage(MessagelogInterface::onRegisterNewApp);
				
				MessagelogInterface::onRegisterNewAppArgs11::staticAddToBundle((*pBundle), getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					g_componentGlobalOrder, g_componentGroupOrder,
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port, g_kbeSrvConfig.getConfig().externalAddress);
			}
			else
			{
				KBE_ASSERT(false && "invalid componentType.\n");
			}

			(*pBundle).send(*_pNetworkInterface, pComponentInfos->pChannel);
			Mercury::Bundle::ObjPool().reclaimObject(pBundle);
		}
	}
	else
	{
		ERROR_MSG(boost::format("Components::connectComponent: connect(%1%) is failed! %2%.\n") %
			pComponentInfos->pIntAddr->c_str() % kbe_strerror());

		return -1;
	}

	return ret;
}
Example #13
0
int kbeMainT(int argc, char * argv[], COMPONENT_TYPE componentType, 
			 int32 extlisteningPort_min = -1, int32 extlisteningPort_max = -1, const char * extlisteningInterface = "",
			 int32 intlisteningPort = 0, const char * intlisteningInterface = "")
{
	setEvns();
	startLeakDetection(componentType, g_componentID);

	g_componentType = componentType;
	DebugHelper::initHelper(componentType);
	INFO_MSG( "-----------------------------------------------------------------------------------------\n\n\n");

#ifdef USE_OPENSSL	
	KBEKey kbekey(Resmgr::getSingleton().matchPath("key/") + "kbengine_public.key", 
		Resmgr::getSingleton().matchPath("key/") + "kbengine_private.key");
#endif

	Resmgr::getSingleton().print();

	Mercury::EventDispatcher dispatcher;
	DebugHelper::getSingleton().pDispatcher(&dispatcher);

	const ChannelCommon& channelCommon = g_kbeSrvConfig.channelCommon();

	Mercury::g_SOMAXCONN = g_kbeSrvConfig.tcp_SOMAXCONN(g_componentType);

	Mercury::NetworkInterface networkInterface(&dispatcher, 
		extlisteningPort_min, extlisteningPort_max, extlisteningInterface, 
		channelCommon.extReadBufferSize, channelCommon.extWriteBufferSize,
		(intlisteningPort != -1) ? htons(intlisteningPort) : -1, intlisteningInterface,
		channelCommon.intReadBufferSize, channelCommon.intWriteBufferSize);
	
	DebugHelper::getSingleton().pNetworkInterface(&networkInterface);

	g_kbeSrvConfig.updateInfos(true, componentType, g_componentID, 
			networkInterface.intaddr(), networkInterface.extaddr());
	
	if(getUserUID() <= 0)
	{
		WARNING_MSG(boost::format("invalid UID(%1%) <= 0, please check UID for environment!\n") % getUserUID());
	}

	Componentbridge* pComponentbridge = new Componentbridge(networkInterface, componentType, g_componentID);
	SERVER_APP app(dispatcher, networkInterface, componentType, g_componentID);
	START_MSG(COMPONENT_NAME_EX(componentType), g_componentID);

	if(!app.initialize())
	{
		ERROR_MSG("app::initialize() is error!\n");
		SAFE_RELEASE(pComponentbridge);
		app.finalise();
		return -1;
	}
	
	INFO_MSG(boost::format("---- %1% is running ----\n") % COMPONENT_NAME_EX(componentType));
#if KBE_PLATFORM == PLATFORM_WIN32
	printf("[INFO]: %s", (boost::format("---- %1% is running ----\n") % COMPONENT_NAME_EX(componentType)).str().c_str());

	wchar_t exe_path[MAX_PATH];
	memset(exe_path, 0, MAX_PATH * sizeof(wchar_t));
	GetCurrentDirectory(MAX_PATH, exe_path);
	
	char* ccattr = strutil::wchar2char(exe_path);
	printf("Writing to: %s/logs/%s.*.log\n\n", ccattr, COMPONENT_NAME_EX(componentType));
	free(ccattr);
#endif

	int ret = app.run();

	SAFE_RELEASE(pComponentbridge);
	app.finalise();
	INFO_MSG(boost::format("%1%(%2%) has shut down.\n") % COMPONENT_NAME_EX(componentType) % g_componentID);
	return ret;
}
Example #14
0
 // return produced vice pool c**t this round ;
uint8_t CTaxasRoom::CaculateOneRoundPool()
{
	// check build vice pool
	uint8_t nBeforeVicePoolIdx = GetFirstCanUseVicePool().nIdx ;
	uint32_t nVicePool = 0 ;
	while ( true )
	{
		// find maybe pool 
		nVicePool = 0 ;
		for ( uint8_t nIdx = 0 ; nIdx < getSeatCount() ; ++nIdx )
		{
			auto pPlayer = (CTaxasPlayer*)getPlayerByIdx(nIdx);
			if ( pPlayer == nullptr || (pPlayer->isHaveState(eRoomPeer_WaitCaculate) == false ) )
			{
				continue;
			}

			if ( pPlayer->getCurActType() == eRoomPeerAction_AllIn && pPlayer->getBetCoinThisRound() > 0 )
			{
				if ( pPlayer->getBetCoinThisRound() < nVicePool || nVicePool == 0 )
				{
					nVicePool = pPlayer->getBetCoinThisRound() ;
				}
			}
		}

		if ( nVicePool == 0 )
		{
			break;
		}

		// real build pool;
		stVicePool& pPool = GetFirstCanUseVicePool();
		pPool.bUsed = true ;
		pPool.nCoin = m_nCurMainBetPool ;
		m_nCurMainBetPool = 0 ;
		
		// put player idx in pool ;
		LOGFMTD("build pool pool idx = %d",pPool.nIdx ) ;
		for ( uint8_t nIdx = 0 ; nIdx < getSeatCount() ; ++nIdx )
		{
			auto pPlayer = (CTaxasPlayer*)getPlayerByIdx(nIdx);
			if ( pPlayer == nullptr || (pPlayer->isHaveState(eRoomPeer_WaitCaculate) == false ) )
			{
				continue;
			}

			if ( pPlayer->getBetCoinThisRound() > 0 )
			{
				if ( pPlayer->getBetCoinThisRound() < nVicePool )
				{
					LOGFMTE("room id = %d , put vice pool coin not enough , why error error",getRoomID()) ;
				}
				pPool.nCoin += nVicePool ;
				pPlayer->setBetCoinThisRound(pPlayer->getBetCoinThisRound() - nVicePool ) ;
				pPool.vInPoolPlayerIdx.push_back(nIdx) ;
				LOGFMTD("put player into pool player Idx = %d, UID = %d",nIdx,pPlayer->getUserUID() ) ;
			}
		}
		LOGFMTI("pool idx = %d : coin = %u",pPool.nIdx,pPool.nCoin) ;
	}

	// build mian pool ;
	LOGFMTD("build main pool: " ) ;
	for ( uint8_t nIdx = 0 ; nIdx < getSeatCount() ; ++nIdx )
	{
		auto pPlayer = (CTaxasPlayer*)getPlayerByIdx(nIdx);
		if ( pPlayer == nullptr || (pPlayer->isHaveState(eRoomPeer_CanAct) == false ) )
		{
			continue;
		}

		if ( pPlayer->getBetCoinThisRound() > 0 )
		{
			m_nCurMainBetPool += pPlayer->getBetCoinThisRound() ;
			pPlayer->setBetCoinThisRound(0);
			LOGFMTD("put player into Main pool player Idx = %d, UID = %d",nIdx,pPlayer->getUserUID()) ;
		}
	}

	uint8_t nProducedVicePoolCnt = GetFirstCanUseVicePool().nIdx - nBeforeVicePoolIdx;
	LOGFMTI("oneRound Caculate over, mainPool = %u, newVicePool = %d",m_nCurMainBetPool,nProducedVicePoolCnt );

	// send msg tell client [ nBeforeVicePoolIdx, GetFirstCanUseVicePoolIdx() ); this set of pool idx are new produced ; not include the last 
	stMsgTaxasRoomOneBetRoundResult msgResult ;
	msgResult.nCurMainPool = m_nCurMainBetPool ;
	msgResult.nNewVicePoolCnt = nProducedVicePoolCnt ;
	memset(msgResult.vNewVicePool,0,sizeof(msgResult.vNewVicePool)) ;
	for ( uint8_t nIdx = nBeforeVicePoolIdx, nNewIdx = 0 ; nIdx < GetFirstCanUseVicePool().nIdx; ++nIdx )
	{
		msgResult.vNewVicePool[nNewIdx++] = m_vAllVicePools[nIdx].nCoin ;
	}
	sendRoomMsg(&msgResult,sizeof(msgResult)) ;
	return nProducedVicePoolCnt ;
}
Example #15
0
//-------------------------------------------------------------------------------------
bool Componentbridge::findInterfaces()
{
	int8 findComponentTypes[] = {UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, 
								UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE, UNKNOWN_COMPONENT_TYPE};

	switch(componentType_)
	{
	case CELLAPP_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = RESOURCEMGR_TYPE;
		findComponentTypes[2] = DBMGR_TYPE;
		findComponentTypes[3] = CELLAPPMGR_TYPE;
		findComponentTypes[4] = BASEAPPMGR_TYPE;
		break;
	case BASEAPP_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = RESOURCEMGR_TYPE;
		findComponentTypes[2] = DBMGR_TYPE;
		findComponentTypes[3] = BASEAPPMGR_TYPE;
		findComponentTypes[4] = CELLAPPMGR_TYPE;
		break;
	case BASEAPPMGR_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = DBMGR_TYPE;
		findComponentTypes[2] = CELLAPPMGR_TYPE;
		break;
	case CELLAPPMGR_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = DBMGR_TYPE;
		findComponentTypes[2] = BASEAPPMGR_TYPE;
		break;
	case LOGINAPP_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		findComponentTypes[1] = DBMGR_TYPE;
		findComponentTypes[2] = BASEAPPMGR_TYPE;
		break;
	case DBMGR_TYPE:
		findComponentTypes[0] = MESSAGELOG_TYPE;
		break;
	default:
		if(componentType_ != MESSAGELOG_TYPE && componentType_ != MACHINE_TYPE)
			findComponentTypes[0] = MESSAGELOG_TYPE;
		break;
	};

	int ifind = 0;
	srand(KBEngine::getSystemTime());
	uint16 nport = KBE_PORT_START + (rand() % 1000);

	while(findComponentTypes[ifind] != UNKNOWN_COMPONENT_TYPE)
	{
		if(dispatcher().isBreakProcessing())
			return false;

		int8 findComponentType = findComponentTypes[ifind];

		INFO_MSG("Componentbridge::process: finding %s...\n",
			COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
		
		Mercury::BundleBroadcast bhandler(networkInterface_, nport);
		if(!bhandler.good())
		{
			KBEngine::sleep(10);
			nport = KBE_PORT_START + (rand() % 1000);
			continue;
		}

		if(bhandler.pCurrPacket() != NULL)
		{
			bhandler.pCurrPacket()->resetPacket();
		}

		bhandler.newMessage(MachineInterface::onFindInterfaceAddr);
		MachineInterface::onFindInterfaceAddrArgs6::staticAddToBundle(bhandler, getUserUID(), getUsername(), 
			componentType_, findComponentType, networkInterface_.intaddr().ip, bhandler.epListen().addr().port);

		if(!bhandler.broadcast())
		{
			ERROR_MSG("Componentbridge::process: broadcast error!\n");
			return false;
		}
	
		MachineInterface::onBroadcastInterfaceArgs8 args;
		if(bhandler.receive(&args, 0, 1000000))
		{
			if(args.componentType == UNKNOWN_COMPONENT_TYPE)
			{
				//INFO_MSG("Componentbridge::process: not found %s, try again...\n",
				//	COMPONENT_NAME_EX(findComponentType));
				
				KBEngine::sleep(1000);
				
				// 如果是这些辅助组件没找到则跳过
				if(findComponentType == MESSAGELOG_TYPE || findComponentType == RESOURCEMGR_TYPE)
				{
					WARNING_MSG("Componentbridge::process: not found %s!\n",
						COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

					findComponentTypes[ifind] = -1; // 跳过标志

					ifind++;
				}

				continue;
			}

			INFO_MSG("Componentbridge::process: found %s, addr:%s:%u\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)args.componentType), inet_ntoa((struct in_addr&)args.intaddr), ntohs(args.intaddr));

			Componentbridge::getComponents().addComponent(args.uid, args.username.c_str(), 
				(KBEngine::COMPONENT_TYPE)args.componentType, args.componentID, args.intaddr, args.intport, args.extaddr, args.extport);
			
			// 防止接收到的数据不是想要的数据
			if(findComponentType == args.componentType)
			{
				ifind++;
			}
			else
			{
				ERROR_MSG("Componentbridge::process: %s not found. receive data is error!\n", COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
			}
		}
		else
		{
			ERROR_MSG("Componentbridge::process: receive error!\n");
			return false;
		}
	}
	
	ifind = 0;

	// 开始注册到所有的组件
	while(findComponentTypes[ifind] != UNKNOWN_COMPONENT_TYPE)
	{
		if(dispatcher().isBreakProcessing())
			return false;

		int8 findComponentType = findComponentTypes[ifind++];
		
		if(findComponentType == -1)
			continue;

		INFO_MSG("Componentbridge::process: register self to %s...\n",
			COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

		if(getComponents().connectComponent(static_cast<COMPONENT_TYPE>(findComponentType), getUserUID(), 0) != 0)
		{
			ERROR_MSG("Componentbridge::register self to %s is error!\n",
			COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

			dispatcher().breakProcessing();
			return false;
		}
	}

	return true;
}
Example #16
0
//-------------------------------------------------------------------------------------		
int Components::connectComponent(COMPONENT_TYPE componentType, int32 uid, COMPONENT_ID componentID)
{
	Components::ComponentInfos* pComponentInfos = findComponent(componentType, uid, componentID);
	KBE_ASSERT(pComponentInfos != NULL);

	Mercury::EndPoint * pEndpoint = new Mercury::EndPoint;
	pEndpoint->socket(SOCK_STREAM);
	if (!pEndpoint->good())
	{
		ERROR_MSG("Components::connectComponent: couldn't create a socket\n");
		delete pEndpoint;
		return -1;
	}

	pEndpoint->addr(*pComponentInfos->pIntAddr);
	int ret = pEndpoint->connect(pComponentInfos->pIntAddr->port, pComponentInfos->pIntAddr->ip);

	if(ret == 0)
	{
		pComponentInfos->pChannel = new Mercury::Channel(*_pNetworkInterface, pEndpoint, Mercury::Channel::INTERNAL);
		if(!_pNetworkInterface->registerChannel(pComponentInfos->pChannel))
		{
			ERROR_MSG("Components::connectComponent: registerChannel(%s) is failed!\n",
				pComponentInfos->pChannel->c_str());

			delete pComponentInfos->pChannel;
			pComponentInfos->pChannel = NULL;
			return -1;
		}
		else
		{
			Mercury::Bundle bundle(pComponentInfos->pChannel);
			if(componentType == BASEAPPMGR_TYPE)
			{
				bundle.newMessage(BaseappmgrInterface::onRegisterNewApp);
				
				BaseappmgrInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
			}
			else if(componentType == CELLAPPMGR_TYPE)
			{
				bundle.newMessage(CellappmgrInterface::onRegisterNewApp);
				
				CellappmgrInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
			}
			else if(componentType == CELLAPP_TYPE)
			{
				bundle.newMessage(CellappInterface::onRegisterNewApp);
				
				CellappInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
						_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
			}
			else if(componentType == BASEAPP_TYPE)
			{
				bundle.newMessage(BaseappInterface::onRegisterNewApp);
				
				BaseappInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
			}
			else if(componentType == DBMGR_TYPE)
			{
				bundle.newMessage(DbmgrInterface::onRegisterNewApp);
				
				DbmgrInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
			}
			else if(componentType == MESSAGELOG_TYPE)
			{
				bundle.newMessage(MessagelogInterface::onRegisterNewApp);
				
				MessagelogInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
			}
			else if(componentType == RESOURCEMGR_TYPE)
			{
				bundle.newMessage(ResourcemgrInterface::onRegisterNewApp);
				
				ResourcemgrInterface::onRegisterNewAppArgs8::staticAddToBundle(bundle, getUserUID(), getUsername(), 
					Componentbridge::getSingleton().componentType(), Componentbridge::getSingleton().componentID(), 
					_pNetworkInterface->intaddr().ip, _pNetworkInterface->intaddr().port,
					_pNetworkInterface->extaddr().ip, _pNetworkInterface->extaddr().port);
			}
			else
			{
				KBE_ASSERT(false && "invalid componentType.\n");
			}

			bundle.send(*_pNetworkInterface, pComponentInfos->pChannel);
		}
	}
	else
	{
		ERROR_MSG("Components::connectComponent: connect(%s) is failed! %s.\n",
			pComponentInfos->pIntAddr->c_str(), kbe_strerror());

		return -1;
	}

	return ret;
}
Example #17
0
void CTaxasRoom::CaculateVicePool(stVicePool& pPool )
{
	if ( pPool.nCoin == 0 )
	{
		LOGFMTE("why this pool coin is 0 ? players = %d room id = %d ",pPool.vInPoolPlayerIdx.size(),getRoomID()) ;
		return ;
	}

	if ( pPool.vInPoolPlayerIdx.empty() )
	{
		LOGFMTE("why pool coin = %u , peers is 0 room id = %d  ",pPool.nCoin,getRoomID() ) ;
	}

	// find winner ;
	if ( pPool.vInPoolPlayerIdx.size() == 1 )
	{
		uint8_t nPeerIdx = pPool.vInPoolPlayerIdx[0] ;
		auto pPlayer = (CTaxasPlayer*)getPlayerByIdx(nPeerIdx);
		if ( pPlayer == nullptr )
		{
			LOGFMTE("why this winner idx is invalid = %d, system got coin = %u",nPeerIdx,pPool.nCoin ) ;
			return ;
		}

		if ( pPlayer->isHaveState(eRoomPeer_WaitCaculate) == false )
		{
			LOGFMTE("why this winner idx state is invalid = %d, system got coin = %u",nPeerIdx,pPool.nCoin ) ;
			return ;
		}
		pPool.vWinnerIdxs.push_back( nPeerIdx ) ;
		pPlayer->addWinCoinThisGame(pPool.nCoin);
		return ;
	}

	// pk card
	if ( IsPublicDistributeFinish() == false )
	{
		LOGFMTE("public is not finish how to pk card ? error room id = %d",getRoomID());
		return ;
	}

	CTaxasPlayer* pWiner = nullptr ;
	for ( int8_t nIdx = getSortedPlayerCnt() - 1 ; nIdx >= 0; --nIdx )
	{
		CTaxasPlayer* pData = (CTaxasPlayer*)getSortedPlayerByIdx(nIdx);
		if ( pData == nullptr || pData->isHaveState( eRoomPeer_WaitCaculate ) == false )
		{
			continue; ;
		}

		if ( pPool.isPlayerInThisPool(pData->getIdx()) == false )
		{
			continue;
		}

		if ( pPool.vWinnerIdxs.empty() )
		{
			pPool.vWinnerIdxs.push_back(pData->getIdx()) ;
			pWiner = pData ;
			continue;
		}

		if ( pWiner->getPeerCard()->pk(pData->getPeerCard()) != IPeerCard::PK_RESULT_EQUAL ) // not the same , means small 
		{
			break ;
		}

		pPool.vWinnerIdxs.push_back(pData->getIdx()) ;
	}

	// give coin 
	if ( pPool.vWinnerIdxs.empty() )
	{
		LOGFMTE("why room id = %d pool idx = %d winner is empty , system got coin = %u ",getRoomID(),pPool.nIdx,pPool.nCoin ) ;
		return ;
	}

	uint8_t nElasCoin = uint8_t(pPool.nCoin % pPool.vWinnerIdxs.size()) ;
	pPool.nCoin -= nElasCoin ;
	if ( nElasCoin > 0 )
	{
		addTotoalProfit(nElasCoin);
		LOGFMTI("system got the elaps coin = %d, room id = %d , pool idx = %d ",nElasCoin,getRoomID(),pPool.nIdx ) ;
	}
	uint32_t nCoinPerWinner = pPool.nCoin / pPool.vWinnerIdxs.size() ;
	for ( uint8_t nIdx = 0 ; nIdx < pPool.vWinnerIdxs.size(); ++nIdx )
	{
		uint8_t nPeerIdx = pPool.vWinnerIdxs[nIdx];
		auto pPlayer = (CTaxasPlayer*)getPlayerByIdx(nPeerIdx);
		pPlayer->addWinCoinThisGame(nCoinPerWinner);
		LOGFMTI("player use uid = %d win coin = %u , from pool idx = %d, room id = %d",pPlayer->getUserUID(),nCoinPerWinner,pPool.nIdx,getRoomID()) ;
	}
}
Example #18
0
//-------------------------------------------------------------------------------------
void Dbmgr::onRegisterNewApp(Network::Channel* pChannel, int32 uid, std::string& username, 
						COMPONENT_TYPE componentType, COMPONENT_ID componentID, COMPONENT_ORDER globalorderID, COMPONENT_ORDER grouporderID,
						uint32 intaddr, uint16 intport, uint32 extaddr, uint16 extport, std::string& extaddrEx)
{
	if(pChannel->isExternal())
		return;

	ServerApp::onRegisterNewApp(pChannel, uid, username, componentType, componentID, globalorderID, grouporderID,
						intaddr, intport, extaddr, extport, extaddrEx);

	KBEngine::COMPONENT_TYPE tcomponentType = (KBEngine::COMPONENT_TYPE)componentType;
	
	COMPONENT_ORDER startGroupOrder = 1;
	COMPONENT_ORDER startGlobalOrder = Components::getSingleton().getGlobalOrderLog()[getUserUID()];

	if(grouporderID > 0)
		startGroupOrder = grouporderID;

	if(globalorderID > 0)
		startGlobalOrder = globalorderID;

	if(pSyncAppDatasHandler_ == NULL)
		pSyncAppDatasHandler_ = new SyncAppDatasHandler(this->networkInterface());

	// 下一步:
	// 如果是连接到dbmgr则需要等待接收app初始信息
	// 例如:初始会分配entityID段以及这个app启动的顺序信息(是否第一个baseapp启动)
	if(tcomponentType == BASEAPP_TYPE || 
		tcomponentType == CELLAPP_TYPE || 
		tcomponentType == LOGINAPP_TYPE)
	{
		switch(tcomponentType)
		{
		case BASEAPP_TYPE:
			{
				if(grouporderID <= 0)
					startGroupOrder = Components::getSingleton().getBaseappGroupOrderLog()[getUserUID()];
			}
			break;
		case CELLAPP_TYPE:
			{
				if(grouporderID <= 0)
					startGroupOrder = Components::getSingleton().getCellappGroupOrderLog()[getUserUID()];
			}
			break;
		case LOGINAPP_TYPE:
			if(grouporderID <= 0)
				startGroupOrder = Components::getSingleton().getLoginappGroupOrderLog()[getUserUID()];

			break;
		default:
			break;
		}
	}

	pSyncAppDatasHandler_->pushApp(componentID, startGroupOrder, startGlobalOrder);

	// 如果是baseapp或者cellapp则将自己注册到所有其他baseapp和cellapp
	if(tcomponentType == BASEAPP_TYPE || 
		tcomponentType == CELLAPP_TYPE)
	{
		KBEngine::COMPONENT_TYPE broadcastCpTypes[2] = {BASEAPP_TYPE, CELLAPP_TYPE};
		for(int idx = 0; idx < 2; ++idx)
		{
			Components::COMPONENTS& cts = Components::getSingleton().getComponents(broadcastCpTypes[idx]);
			Components::COMPONENTS::iterator fiter = cts.begin();
			for(; fiter != cts.end(); ++fiter)
			{
				if((*fiter).cid == componentID)
					continue;

				Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject();
				ENTITTAPP_COMMON_NETWORK_MESSAGE(broadcastCpTypes[idx], (*pBundle), onGetEntityAppFromDbmgr);
				
				if(tcomponentType == BASEAPP_TYPE)
				{
					BaseappInterface::onGetEntityAppFromDbmgrArgs11::staticAddToBundle((*pBundle), 
						uid, username, componentType, componentID, startGlobalOrder, startGroupOrder,
							intaddr, intport, extaddr, extport, g_kbeSrvConfig.getConfig().externalAddress);
				}
				else
				{
					CellappInterface::onGetEntityAppFromDbmgrArgs11::staticAddToBundle((*pBundle), 
						uid, username, componentType, componentID, startGlobalOrder, startGroupOrder,
							intaddr, intport, extaddr, extport, g_kbeSrvConfig.getConfig().externalAddress);
				}
				
				KBE_ASSERT((*fiter).pChannel != NULL);
				(*fiter).pChannel->send(pBundle);
			}
		}
	}
}
Example #19
0
//-------------------------------------------------------------------------------------
bool Componentbridge::findInterfaces()
{
	if(state_ == 1)
	{
		srand(KBEngine::getSystemTime());
		uint16 nport = KBE_PORT_START + (rand() % 1000);

		while(findComponentTypes_[findIdx_] != UNKNOWN_COMPONENT_TYPE)
		{
			if(dispatcher().isBreakProcessing())
				return false;

			int8 findComponentType = findComponentTypes_[findIdx_];

			INFO_MSG("Componentbridge::process: finding %s...\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
			
			Mercury::BundleBroadcast bhandler(networkInterface_, nport);
			if(!bhandler.good())
			{
				return false;
			}

			bhandler.itry(0);
			if(bhandler.pCurrPacket() != NULL)
			{
				bhandler.pCurrPacket()->resetPacket();
			}

			bhandler.newMessage(MachineInterface::onFindInterfaceAddr);
			MachineInterface::onFindInterfaceAddrArgs6::staticAddToBundle(bhandler, getUserUID(), getUsername(), 
				componentType_, findComponentType, networkInterface_.intaddr().ip, bhandler.epListen().addr().port);

			if(!bhandler.broadcast())
			{
				ERROR_MSG("Componentbridge::process: broadcast error!\n");
				return false;
			}
		
			MachineInterface::onBroadcastInterfaceArgs8 args;
			if(bhandler.receive(&args, 0, 10000000))
			{
				if(args.componentType == UNKNOWN_COMPONENT_TYPE)
				{
					//INFO_MSG("Componentbridge::process: not found %s, try again...\n",
					//	COMPONENT_NAME_EX(findComponentType));
					
					// 如果是这些辅助组件没找到则跳过
					if(findComponentType == MESSAGELOG_TYPE || findComponentType == RESOURCEMGR_TYPE)
					{
						WARNING_MSG("Componentbridge::process: not found %s!\n",
							COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

						findComponentTypes_[findIdx_] = -1; // 跳过标志

						findIdx_++;
					}

					return false;
				}

				INFO_MSG("Componentbridge::process: found %s, addr:%s:%u\n",
					COMPONENT_NAME_EX((COMPONENT_TYPE)args.componentType), 
					inet_ntoa((struct in_addr&)args.intaddr), ntohs(args.intaddr));

				Componentbridge::getComponents().addComponent(args.uid, args.username.c_str(), 
					(KBEngine::COMPONENT_TYPE)args.componentType, args.componentID, 
					args.intaddr, args.intport, args.extaddr, args.extport);
				
				// 防止接收到的数据不是想要的数据
				if(findComponentType == args.componentType)
				{
					findIdx_++;
				}
				else
				{
					ERROR_MSG("Componentbridge::process: %s not found. receive data is error!\n", 
						COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
				}
			}
			else
			{
				ERROR_MSG("Componentbridge::process: receive error!\n");

				// 如果是这些辅助组件没找到则跳过
				if(findComponentType == MESSAGELOG_TYPE || findComponentType == RESOURCEMGR_TYPE)
				{
					WARNING_MSG("Componentbridge::process: not found %s!\n",
						COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

					findComponentTypes_[findIdx_] = -1; // 跳过标志

					findIdx_++;
				}

				return false;
			}
		}

		state_ = 2;
		findIdx_ = 0;
		return false;
	}

	if(state_ == 2)
	{
		// 开始注册到所有的组件
		while(findComponentTypes_[findIdx_] != UNKNOWN_COMPONENT_TYPE)
		{
			if(dispatcher().isBreakProcessing())
				return false;

			int8 findComponentType = findComponentTypes_[findIdx_];
			
			if(findComponentType == -1)
			{
				findIdx_++;
				return false;
			}

			INFO_MSG("Componentbridge::process: register self to %s...\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));

			if(getComponents().connectComponent(static_cast<COMPONENT_TYPE>(findComponentType), getUserUID(), 0) != 0)
			{
				ERROR_MSG("Componentbridge::register self to %s is error!\n",
				COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType));
				//dispatcher().breakProcessing();
				return false;
			}

			findIdx_++;
			return false;
		}
	}

	return true;
}