コード例 #1
0
ファイル: DBManager.cpp プロジェクト: lubing521/NewProject
void CDBManager::OnMessage(stMsg* pmsg , eMsgPort eSenderPort , uint32_t nSessionID )
{
	// construct sql
	stArgData* pdata = GetReserverArgData() ;
	if ( pdata == NULL )
	{
		pdata = new stArgData ;
	}

	pdata->eFromPort = eSenderPort ;
	pdata->nSessionID = nSessionID ;

	stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
	pRequest->cOrder = eReq_Order_Normal ;
	pRequest->nRequestUID = pmsg->usMsgType ;
	pRequest->pUserData = pdata;
	pRequest->eType = eRequestType_Max ;
	pRequest->nSqlBufferLen = 0 ;

	switch( pmsg->usMsgType )
	{
	case MSG_REQUEST_CREATE_PLAYER_DATA:
		{
			stMsgRequestDBCreatePlayerData* pCreate = (stMsgRequestDBCreatePlayerData*)pmsg ;
			pdata->nExtenArg1 = pCreate->nUserUID ;

			uint16_t nRandID = rand() % 10000 ;
			pRequest->eType = eRequestType_Select ;
			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,
				"call CreateNewRegisterPlayerData(%d,'guest%d')",pCreate->nUserUID,nRandID) ;
		}
		break;
	case MSG_PLAYER_BASE_DATA:
		{
			stMsgDataServerGetBaseData* pRet = (stMsgDataServerGetBaseData*)pmsg ;
			pdata->nExtenArg1 = pRet->nUserUID ;
			pRequest->eType = eRequestType_Select ;
			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,sizeof(pRequest->pSqlBuffer),
				"SELECT * FROM playerbasedata WHERE userUID = '%d'",pRet->nUserUID) ;
		}
		break;
	case MSG_PLAYER_SAVE_PLAYER_INFO:
		{
			stMsgSavePlayerInfo* pRet = (stMsgSavePlayerInfo*)pmsg ;
			pRequest->eType = eRequestType_Update ;
			std::string strUploadPic = stMysqlField::UnIntArraryToString(pRet->vUploadedPic,MAX_UPLOAD_PIC) ;
			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,sizeof(pRequest->pSqlBuffer),
				"UPDATE playerbasedata SET playerName = '%s', signature = '%s',vUploadedPic = '%s',photoID = '%d' WHERE userUID = '%d'",pRet->vName,pRet->vSigure,strUploadPic.c_str(),pRet->nPhotoID,pRet->nUserUID) ;
		}
		break;
	case MSG_SAVE_PLAYER_MONEY:
		{
			stMsgSavePlayerMoney* pRet = (stMsgSavePlayerMoney*)pmsg ;
			pRequest->eType = eRequestType_Update ;
			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,sizeof(pRequest->pSqlBuffer),
				"UPDATE playerbasedata SET coin = '%I64d', diamond = '%d' WHERE userUID = '%d'",pRet->nCoin,pRet->nDiamoned,pRet->nUserUID) ;
		}
		break;
	case MSG_SAVE_PLAYER_TAXAS_DATA:
		{
			stMsgSavePlayerTaxaPokerData* pRet = (stMsgSavePlayerTaxaPokerData*)pmsg ;
			pRequest->eType = eRequestType_Update ;
			std::string strMaxcard = stMysqlField::UnIntArraryToString(pRet->vMaxCards,MAX_TAXAS_HOLD_CARD) ;
			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,sizeof(pRequest->pSqlBuffer),
				"UPDATE playerbasedata SET winTimes = '%d', loseTimes = '%d', singleWinMost = '%I64d', maxCard = '%s' WHERE userUID = '%d'",pRet->nWinTimes,pRet->nLoseTimes,pRet->nSingleWinMost,strMaxcard.c_str(),pRet->nUserUID) ;
		}
		break;
	case MSG_SAVE_COMMON_LOGIC_DATA:
		{
			stMsgSavePlayerCommonLoginData* pRet = (stMsgSavePlayerCommonLoginData*)pmsg ;
			pRequest->eType = eRequestType_Update ;
			std::string strJoinedClub = stMysqlField::UnIntArraryToString(pRet->vJoinedClubID,MAX_JOINED_CLUB_CNT) ;
			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,sizeof(pRequest->pSqlBuffer),
				"UPDATE playerbasedata SET mostCoinEver = '%I64d', vipLevel = '%d', nYesterdayCoinOffset = '%I64d', \
				nTodayCoinOffset = '%I64d',offlineTime = '%d',continueLoginDays = '%d',lastLoginTime = '%d',lastTakeCharityCoinTime = '%d', \
				longitude = '%f',latitude = '%f,vJoinedClubID = '%s' WHERE userUID = '%d' ",
				pRet->nMostCoinEver,pRet->nVipLevel,pRet->nYesterdayCoinOffset,pRet->nTodayCoinOffset,pRet->tOfflineTime,pRet->nContinueDays,pRet->tLastLoginTime,pRet->tLastTakeCharityCoinTime,pRet->dfLongitude,pRet->dfLatidue,
				strJoinedClub.c_str(),pRet->nUserUID) ;
		}
		break;
// 	case MSG_PLAYER_SAVE_BASE_DATA:
// 		{
// 			stMsgGameServerSaveBaseData* pSaveBaseData = (stMsgGameServerSaveBaseData*)pmsg ;
// 			pdata->nSessionID = pSaveBaseData->nSessionID ;
// 			pdata->nExtenArg1 = pSaveBaseData->stBaseData.nUserUID ;
// 
// 			char pMaxCards[sizeof(pSaveBaseData->stBaseData.vMaxCards)*2 + 1 ] = {0} ;
// 			m_pTheApp->GetDBThread()->EscapeString(pMaxCards,(char*)pSaveBaseData->stBaseData.vMaxCards,sizeof(pSaveBaseData->stBaseData.vMaxCards)) ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_Low ;
// 			pRequest->eType = eRequestType_Update ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer, 
// 			"UPDATE playerbasedata SET playerName = '%s', defaultPhotoID = '%u', isUploadPhoto = '%u', signature = '%s', sex = '%u', vipLevel = '%u',   \
// 			coin = '%I64u', diamond = '%u', winTimes = '%u', loseTimes = '%u', singleWinMost = '%I64u', maxCard = '%s',   \
// 			longitude = '%f', latitude = '%f', exp = '%u', offlineTime = '%u',noticeID = '%u',vipEndTime = '%u',\
// 			continueLoginDays = '%u',lastLoginTime = '%u',lastTakeCharityCoinTime = '%u',todayPlayTimes = '%u' , \
// 			yesterdayPlayTimes = '%u',todayWinCoin = '%I64d',yesterdayWinCoin = '%I64d', \
// 			takeMasterStudentRewardTime = '%u',rechargeTimes = '%u',curOnlineBoxID = '%u',onlineBoxPassedTime = '%u' WHERE userUID = '%u'", 
// 			pSaveBaseData->stBaseData.cName,pSaveBaseData->stBaseData.nDefaulPhotoID,pSaveBaseData->stBaseData.bIsUploadPhoto,pSaveBaseData->stBaseData.cSignature,pSaveBaseData->stBaseData.nSex,pSaveBaseData->stBaseData.nVipLevel,
// 			pSaveBaseData->stBaseData.nCoin,pSaveBaseData->stBaseData.nDiamoned,pSaveBaseData->stBaseData.nWinTimes,pSaveBaseData->stBaseData.nLoseTimes,pSaveBaseData->stBaseData.nSingleWinMost,pMaxCards,
// 			pSaveBaseData->stBaseData.dfLongitude,pSaveBaseData->stBaseData.dfLatidue,pSaveBaseData->stBaseData.nExp,(unsigned int)pSaveBaseData->stBaseData.tOfflineTime,pSaveBaseData->stBaseData.nNoticeID,pSaveBaseData->stBaseData.nVipEndTime,
// 			pSaveBaseData->stBaseData.nContinueDays,(unsigned int)pSaveBaseData->stBaseData.tLastLoginTime,(unsigned int)pSaveBaseData->stBaseData.tLastTakeCharityCoinTime,pSaveBaseData->stBaseData.nTodayPlayTimes,
// 			pSaveBaseData->stBaseData.nYesterdayPlayTimes,pSaveBaseData->stBaseData.nTodayWinCoin,pSaveBaseData->stBaseData.nYesterdayWinCoin,
// 			(unsigned int)pSaveBaseData->stBaseData.tTakeMasterStudentRewardTime,pSaveBaseData->stBaseData.nRechargeTimes,pSaveBaseData->stBaseData.nCurOnlineBoxID,pSaveBaseData->stBaseData.nOnlineBoxPassedTime,pSaveBaseData->stBaseData.nUserUID) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break ;
// 	case MSG_SAVE_PLAYER_COIN:
// 		{
// 			stMsgGameServerSavePlayerCoin* pSaveCoin = (stMsgGameServerSavePlayerCoin*)pmsg ;
// 			pdata->nSessionID = pSaveCoin->nSessionID ;
// 			pdata->nExtenArg1 = pSaveCoin->nUserUID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_Low ;
// 			pRequest->eType = eRequestType_Update ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"UPDATE gamedb.account SET nCoin = '%I64d', nDiamoned = '%d' WHERE UserUID = '%d'",pSaveCoin->nCoin,pSaveCoin->nDiamoned,pSaveCoin->nUserUID) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;  
// 	case MSG_SAVE_FRIEND_LIST:
// 		{
// 			stMsgGameServerSaveFirendList* pSaveFriend = (stMsgGameServerSaveFirendList*)pmsg ;
// 			pdata->nSessionID = pSaveFriend->nSessionID ;
// 			pdata->nExtenArg1 = pSaveFriend->nUserUID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_Low ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->eType = eRequestType_Update ;
// 
// 			char* pBuffer = (char*)pmsg ;
// 			pBuffer += sizeof(stMsgGameServerSaveFirendList);
// 
// 			char *pFriendListBuffer = new char[sizeof(stServerSaveFrienItem)*pSaveFriend->nFriendCount*2+1] ;
// 			memset(pFriendListBuffer,0,sizeof(sizeof(stServerSaveFrienItem)*pSaveFriend->nFriendCount*2+1));
// 			m_pTheApp->GetDBThread()->EscapeString(pFriendListBuffer,pBuffer,pSaveFriend->nFriendCount * sizeof(stServerSaveFrienItem) ) ;
// 			unsigned int nSaveTime = (unsigned int)time(NULL) ;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"UPDATE playerfriend SET friendCount = '%u',contentData = '%s',saveTime = '%u' WHERE userUID = '%d'",pSaveFriend->nFriendCount,pFriendListBuffer,nSaveTime,pSaveFriend->nUserUID) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 			delete[]pFriendListBuffer ;
// 		}
// 		break;
// 	case MSG_REQUEST_FRIEND_LIST:
// 		{
// 			stMsgGameServerRequestFirendList* pRequestFriend = (stMsgGameServerRequestFirendList*)pmsg ;
// 			pdata->nSessionID = pRequestFriend->nSessionID ;
// 			pdata->nExtenArg1 = 0 ;  // more than one time select ; 
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_High ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"SELECT * FROM playerfriend WHERE userUID = '%u'",pRequestFriend->nUserUID ) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;
// 	case MSG_REQUEST_FRIEND_BRIFDATA_LIST:
// 		{
// 			stMsgGameServerRequestFriendBrifDataList* pRet = (stMsgGameServerRequestFriendBrifDataList*)pmsg ;
// 			pdata->nSessionID = pRet->nSessionID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_High ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			char* pBffer = (char*)pRet ;
// 			pBffer += sizeof(stMsgGameServerRequestFriendBrifDataList);
// 			unsigned int * pUserUID = (unsigned int*)pBffer ;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"SELECT %s FROM playerfriend WHERE userUID = '%u'",PLAYER_BRIF_DATA,*pUserUID ) ;
// 			--pRet->nFriendCount ;
// 			while ( pRet->nFriendCount--)
// 			{
// 				pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"%s || userUID = '%u'",pRequest->pSqlBuffer,*pUserUID ) ;
// 				++pUserUID ;
// 			}
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;
// 	case MSG_PLAYER_SERACH_PEERS:
// 		{
// 			stMsgGameServerGetSearchFriendResult* pMsgRet = (stMsgGameServerGetSearchFriendResult*)pmsg ;
// 			pdata->nSessionID = pMsgRet->nSessionID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_High ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			char* pBuffer = new char[pMsgRet->nLen * 2 + 1 ] ;
// 			memset(pBuffer,0 ,pMsgRet->nLen * 2 + 1  ) ;
// 			m_pTheApp->GetDBThread()->EscapeString(pBuffer, (char*)pMsgRet + sizeof(stMsgGameServerGetSearchFriendResult),pMsgRet->nLen) ;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"SELECT %s FROM playerbasedata where userUID regexp( '%s' ) or playerName regexp('%s') limit 15;", PLAYER_BRIF_DATA,pBuffer, pBuffer ) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 			delete[] pBuffer ;
// 		}
// 		break;
// 	case MSG_PLAYER_REQUEST_SEARCH_PEER_DETAIL:
// 		{
// 			stMsgGameServerGetSearchedPeerDetail* pMsgRet = (stMsgGameServerGetSearchedPeerDetail*)pmsg ;
// 			pdata->nSessionID = pMsgRet->nSessionID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_High ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"SELECT %s FROM playerbasedata where userUID = %u", PLAYER_DETAIL_DATA,pMsgRet->nPeerUserUID) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;
// 	case MSG_PLAYER_REQUEST_FRIEND_DETAIL:
// 		{
// 			stMsgGameServerGetFriendDetail* pMsgRet = (stMsgGameServerGetFriendDetail*)pmsg ;
// 			pdata->nSessionID = pMsgRet->nSessionID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_High ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"SELECT %s FROM playerbasedata where userUID = %d", PLAYER_DETAIL_DATA,pMsgRet->nFriendUID) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;
// 	case MSG_PLAYER_GET_MAIL_LIST:
// 		{
// 			stMsgGameServerGetMailList* pMsgRet = (stMsgGameServerGetMailList*)pmsg;
// 			pdata->nSessionID = pMsgRet->nSessionID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_High ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"select * from mail where userUID = %u order by postTime desc limit %d",pMsgRet->nUserUID, MAX_KEEP_MAIL_COUNT ) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break; 
// 	case MSG_PLAYER_SAVE_MAIL:
// 		{
// 			stMsgGameServerSaveMail* pMsgRet = (stMsgGameServerSaveMail*)pmsg  ;
// 			pdata->nSessionID = pMsgRet->nSessionID ;
// 			pdata->nExtenArg1 = pMsgRet->nUserUID ;
// 			char* pmsgData = (char*)pmsg ;
// 			pmsgData += sizeof(stMsgGameServerSaveMail);
// 			stMail* pMailToSave = (stMail*)pmsgData ;
// 			pdata->nExtenArg2 = (unsigned int)pMailToSave->nMailUID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_Low ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			switch ( pMsgRet->nOperateType ) 
// 			{
// 			case eDBAct_Update:
// 				{
// 					pRequest->eType = eRequestType_Update ;
// 					pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"UPDATE mail SET processAct = '%d' WHERE mailUID = '%I64d'",pMailToSave->eProcessAct,pMailToSave->nMailUID ) ;
// 				}
// 				break;
// 			case eDBAct_Delete:
// 				{
// 					pRequest->eType = eRequestType_Delete ;
// 					pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"DELETE FROM mail WHERE mailUID ='%I64d' limit 1",pMailToSave->nMailUID ) ;
// 				}
// 				break;
// 			case eDBAct_Add:
// 				{
// 					pRequest->eType = eRequestType_Add ;
// 					char* pContent = new char[pMailToSave->nContentLen * 2 + 1 ] ;
// 					memset(pContent,0,pMailToSave->nContentLen * 2 + 1 );
// 					
// 					char* pBuffer = (char*)pMsgRet;
// 					pBuffer += sizeof(stMsgGameServerSaveMail);
// 					pBuffer += sizeof(stMail);
// 					m_pTheApp->GetDBThread()->EscapeString(pContent,pBuffer,pMailToSave->nContentLen ) ;
// 					CLogMgr::SharedLogMgr()->PrintLog("mail title content Len = %d",pMailToSave->nContentLen) ;
// 					pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"INSERT INTO mail (mailUID, userUID,postTime,mailType,mailContentLen,mailContent,processAct) VALUES ('%I64d', '%u','%u','%u','%u','%s','%u')",
// 						pMailToSave->nMailUID,pMsgRet->nUserUID,pMailToSave->nPostTime,pMailToSave->eType,pMailToSave->nContentLen,pContent,pMailToSave->eProcessAct) ;
// 					delete[] pContent ;
// 				}
// 				break;
// 			default:
// 				CLogMgr::SharedLogMgr()->ErrorLog("unknown save mail operation type !") ;
// 				pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"") ;
// 				break;
// 			}
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break; 
// 	case MSG_GAME_SERVER_GET_MAX_MAIL_UID:
// 		{
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_Normal ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = MSG_GAME_SERVER_GET_MAX_MAIL_UID;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"(select max(mail.mailUID) FROM mail)") ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;
// 	case MSG_REQUEST_ITEM_LIST:
// 		{
// 			stMsgGameServerRequestItemList* pMsgRet = (stMsgGameServerRequestItemList*)pmsg ;
// 			pdata->nSessionID = pMsgRet->nSessionID ;
// 			pdata->nExtenArg1 = pMsgRet->nUserUID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_High ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"select * from playeritems where userUID = %u",pMsgRet->nUserUID ) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;
// 	case MSG_SAVE_ITEM_LIST:
// 		{
// 			stMsgGameServerSaveItemList* pMsgRet = (stMsgGameServerSaveItemList*)pmsg ;
// 			pdata->nSessionID = pMsgRet->nSessionID ;
// 			pdata->nExtenArg1 = pMsgRet->nUserUID ;
// 			char* pBuffer = new char[pMsgRet->nOwnItemKindCount * sizeof(stPlayerItem) * 2 + 1] ;
// 			memset(pBuffer,0,pMsgRet->nOwnItemKindCount * sizeof(stPlayerItem) * 2 + 1);
// 			m_pTheApp->GetDBThread()->EscapeString(pBuffer, (((char*)pmsg) + sizeof(stMsgGameServerSaveItemList)),pMsgRet->nOwnItemKindCount * sizeof(stPlayerItem) ) ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_Low ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->eType = eRequestType_Update ;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"UPDATE playeritems SET itemsData = '%s',ownItemsKindCount = '%u' WHERE userUID = '%d'",pBuffer,pMsgRet->nOwnItemKindCount,pMsgRet->nUserUID ) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 			delete[] pBuffer ;
// 		}
// 		break; 
// 	case MSG_REQUEST_RANK:
// 		{
// 			stMsgGameServerRequestRank* pMsgRet = (stMsgGameServerRequestRank*)pmsg ;
// 			pdata->nExtenArg1 = pMsgRet->eType ;
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_Normal ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			switch ( pMsgRet->eType)
// 			{
// 			case eRank_AllCoin:
// 				{
// 					pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"select yesterdayWinCoin ,%s from playerbasedata where userUID !=0 order by coin desc limit %d",PLAYER_DETAIL_DATA,RANK_SHOW_PEER_COUNT) ;
// 				}
// 				break;
// 			case eRank_YesterDayWin:
// 				{
// 					unsigned int tYesterDay = (unsigned int)time(NULL) - 24 * 3600 ;
// 					//struct tm tmNow = *localtime(&tNow) ;
// 					//tmNow.tm_hour = 0 ;
// 					//tmNow.tm_min = 0 ;
// 					//tmNow.tm_sec = 0 ;
// 					//time_t tZero = mktime(&tmNow) ;
// 					pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"select yesterdayWinCoin ,%s from playerbasedata where (offlineTime >= '%u' || lastLoginTime >= '%u') order by todayWinCoin desc limit %d",PLAYER_DETAIL_DATA,tYesterDay,tYesterDay,RANK_SHOW_PEER_COUNT) ;
// 				}
// 				break;
// 			case eRank_SingleWinMost:
// 				{
// 					pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"select yesterdayWinCoin ,%s from playerbasedata where userUID !=0 order by singleWinMost desc limit %d",PLAYER_DETAIL_DATA,RANK_SHOW_PEER_COUNT) ;
// 				}
// 				break;
// 			default:
// 				CLogMgr::SharedLogMgr()->ErrorLog("unknown rank type to select type = %d",pMsgRet->eType) ;
// 				break;
// 			}
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;
// 	//case MSG_GET_SHOP_BUY_RECORD:
// 	//	{
// 	//		stMsgGameServerGetShopBuyRecord* pMsgRet = (stMsgGameServerGetShopBuyRecord*)pmsg ;
// 	//		pdata->nSessionID = pMsgRet->nSessionID ;
// 	//		pdata->nExtenArg1 = pMsgRet->nUserUID ;
// 
// 	//		stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 	//		pRequest->cOrder = eReq_Order_High ;
// 	//		pRequest->eType = eRequestType_Select ;
// 	//		pRequest->nRequestUID = pmsg->usMsgType ;
// 	//		pRequest->pUserData = pdata;
// 	//		pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"select * from playershopbuyrecord where nUserUID = %d",pMsgRet->nUserUID ) ;
// 	//		CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 	//	}
// 	//	break;
// 	//case MSG_SAVE_SHOP_BUY_RECORD:
// 	//	{
// 	//		stMsgGameServerSaveShopBuyRecord* pMsgRet = (stMsgGameServerSaveShopBuyRecord*)pmsg;
// 	//		pdata->nSessionID = pMsgRet->nSessionID ;
// 	//		pdata->nExtenArg1 = pMsgRet->nUserUID ;
// 
// 	//		stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 	//		pRequest->cOrder = eReq_Order_Low ;
// 	//		pRequest->nRequestUID = pmsg->usMsgType ;
// 	//		pRequest->pUserData = pdata;
// 
// 	//		char* pSaveBuffer = new char[pMsgRet->nBufferLen * 2 + 1 ] ;
// 	//		memset(pSaveBuffer,0,pMsgRet->nBufferLen * 2 + 1) ;
// 	//		char* pBuffer = (char*)pmsg ;
// 	//		pBuffer += sizeof(stMsgGameServerSaveShopBuyRecord);
// 	//		m_pTheApp->GetDBThread()->EscapeString(pSaveBuffer,pBuffer,pMsgRet->nBufferLen ) ;
// 	//		if ( pMsgRet->bAdd )
// 	//		{
// 	//			pRequest->eType = eRequestType_Add ;
// 	//			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"INSERT INTO `gamedb`.`playershopbuyrecord` (`nUserUID`, `pBuffer`) VALUES ('%d', '%s');",pMsgRet->nUserUID,pSaveBuffer) ;
// 	//		}
// 	//		else
// 	//		{
// 	//			pRequest->eType = eRequestType_Update ;
// 	//			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"UPDATE gamedb.playershopbuyrecord SET pBuffer = '%s' WHERE nUserUID = '%d'",pSaveBuffer,pMsgRet->nUserUID) ;
// 	//		}
// 	//		CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 	//		delete[] pSaveBuffer ;
// 	//	}
// 	//	break;
// 	case MSG_GAME_SERVER_SAVE_MISSION_DATA:
// 		{
// 			stMsgGameServerSaveMissionData* pMissionData = (stMsgGameServerSaveMissionData*)pmsg ;
// 			pdata->nSessionID = pMissionData->nSessionID ;
// 			pdata->nExtenArg1 = pMissionData->nUserUID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_Low ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 
// 			char* pSaveBuffer = new char[pMissionData->nMissonCount * sizeof(stMissionSate) * 2 + 1 ] ;
// 			memset(pSaveBuffer,0,pMissionData->nMissonCount * sizeof(stMissionSate) * 2 + 1) ;
// 			char* pBuffer = (char*)pmsg ;
// 			pBuffer += sizeof(stMsgGameServerSaveMissionData);
// 			m_pTheApp->GetDBThread()->EscapeString(pSaveBuffer,pBuffer,pMissionData->nMissonCount * sizeof(stMissionSate)) ;
// 			pRequest->eType = eRequestType_Update ;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"UPDATE playermission SET missionData = '%s',saveTime = '%u',missionCount = '%u' WHERE userUID = '%d'",pSaveBuffer,pMissionData->nSavetime,pMissionData->nMissonCount,pMissionData->nUserUID) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 			delete[] pSaveBuffer ;
// 		}
// 		break;
// 	case MSG_GAME_SERVER_GET_MISSION_DATA:
// 		{
// 			stMsgGameServerGetMissionData* pMsgRet = (stMsgGameServerGetMissionData*)pmsg ;
// 			pdata->nSessionID = pMsgRet->nSessionID ;
// 			pdata->nExtenArg1 = pMsgRet->nUserUID ;
// 
// 			stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
// 			pRequest->cOrder = eReq_Order_High ;
// 			pRequest->eType = eRequestType_Select ;
// 			pRequest->nRequestUID = pmsg->usMsgType ;
// 			pRequest->pUserData = pdata;
// 			pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"select * from playermission where userUID = %d",pMsgRet->nUserUID ) ;
// 			CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
// 		}
// 		break;
	default:
		{
			m_vReserverArgData.push_back(pdata) ;
			CLogMgr::SharedLogMgr()->ErrorLog("unknown msg type = %d",pmsg->usMsgType ) ;
		}
	}

	if ( pRequest->nSqlBufferLen == 0 || pRequest->eType == eRequestType_Max )
	{
		CLogMgr::SharedLogMgr()->ErrorLog("a request sql len = 0 , msg = %d" , pRequest->nRequestUID ) ;
		
		CDBRequestQueue::VEC_DBREQUEST v ;
		v.push_back(pRequest) ;
		CDBRequestQueue::SharedDBRequestQueue()->PushReserveRequest(v);
	}
	else
	{
		CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
	}
}
コード例 #2
0
ファイル: DBManager.cpp プロジェクト: xianyinchen/NewProject
void CDBManager::OnMessage(stMsg* pmsg , eMsgPort eSenderPort , uint32_t nSessionID )
{
    // construct sql
    stArgData* pdata = GetReserverArgData() ;
    if ( pdata == NULL )
    {
        pdata = new stArgData ;
    }

    pdata->eFromPort = eSenderPort ;
    pdata->nSessionID = nSessionID ;

    stDBRequest* pRequest = CDBRequestQueue::SharedDBRequestQueue()->GetReserveRequest();
    pRequest->cOrder = eReq_Order_Normal ;
    pRequest->nRequestUID = pmsg->usMsgType ;
    pRequest->pUserData = pdata;
    pRequest->eType = eRequestType_Max ;
    pRequest->nSqlBufferLen = 0 ;

    switch( pmsg->usMsgType )
    {
    case MSG_SAVE_LOG:
    {
        stMsgSaveLog* pCreate = (stMsgSaveLog*)pmsg ;
        pdata->nExtenArg1 = pCreate->nTargetID ;
        char* pStrBuffer = new char[pCreate->nJsonExtnerLen+1];
        memset(pStrBuffer,0,pCreate->nJsonExtnerLen+1);
        char* pS = (char*)&pCreate->nJsonExtnerLen ;
        pS += sizeof(pCreate->nJsonExtnerLen);
        memcpy(pStrBuffer,pS,pCreate->nJsonExtnerLen);
        pRequest->eType = eRequestType_Select ;
        pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,
                                            "call saveLog(%d,'%d','%s'",pCreate->nLogType,pCreate->nTargetID,pStrBuffer) ;
        for ( uint8_t nIdx = 0 ; nIdx < LOG_ARG_CNT; ++nIdx )
        {
            pRequest->nSqlBufferLen += sprintf((char*)(pRequest->pSqlBuffer + pRequest->nSqlBufferLen),
                                               ",'%I64d'",pCreate->vArg[nIdx]) ;
        }

        pRequest->nSqlBufferLen += sprintf((char*)(pRequest->pSqlBuffer + pRequest->nSqlBufferLen),
                                           ")") ;
        delete[] pStrBuffer ;
        pStrBuffer = nullptr;
    }
    break;
    case MSG_SAVE_PLAYER_ADVICE:
    {
        stMsgSavePlayerAdvice* pSave = (stMsgSavePlayerAdvice*)pmsg ;
        time_t nT = time(nullptr) ;
        char* pBuff = new char[pSave->nLen+1] ;
        memset(pBuff,0,pSave->nLen+1) ;
        memcpy(pBuff,(char*)pmsg + sizeof(stMsgSavePlayerAdvice) ,pSave->nLen );
        pRequest->eType = eRequestType_Add ;
        pRequest->nSqlBufferLen = sprintf_s(pRequest->pSqlBuffer,"INSERT INTO log_playeradvice (userUID, cotent,time,c_time) VALUES ('%u', '%s','%u','%s')",
                                            pSave->nUserUID,pBuff,(uint32_t)nT,ctime(&nT)) ;
        delete[] pBuff ;
        pBuff = nullptr ;
    }
    break;
    default:
    {
        m_vReserverArgData.push_back(pdata) ;
        CLogMgr::SharedLogMgr()->ErrorLog("unknown msg type = %d",pmsg->usMsgType ) ;
    }
    }

    if ( pRequest->nSqlBufferLen == 0 || pRequest->eType == eRequestType_Max )
    {
        CLogMgr::SharedLogMgr()->ErrorLog("a request sql len = 0 , msg = %d" , pRequest->nRequestUID ) ;

        CDBRequestQueue::VEC_DBREQUEST v ;
        v.push_back(pRequest) ;
        CDBRequestQueue::SharedDBRequestQueue()->PushReserveRequest(v);
    }
    else
    {
        CDBRequestQueue::SharedDBRequestQueue()->PushRequest(pRequest) ;
    }
}
コード例 #3
0
ファイル: DataBaseThread.cpp プロジェクト: BillXu/NewProject
bool CDataBaseThread::ProcessRequest()
{
	// process request here ;
	CDBRequestQueue* pRequestQueue = CDBRequestQueue::SharedDBRequestQueue();
	CDBRequestQueue::VEC_DBREQUEST vRequestOut ;
	CDBRequestQueue::VEC_DBRESULT vProcessedResult ;
	pRequestQueue->GetAllRequest(vRequestOut);
	CDBRequestQueue::VEC_DBREQUEST::iterator iter = vRequestOut.begin() ;
	stDBRequest* pRequest = NULL ;
	stDBResult* pResult = NULL ;
	MYSQL_RES *msqlResult = NULL ;
	MYSQL_ROW msqlrow;
	MYSQL_FIELD *msqlfield;
	for ( ; iter != vRequestOut.end(); ++iter )
	{
		pRequest = *iter ;
		pResult = new stDBResult;  // will be deleted after processed in the main thread .
		vProcessedResult.push_back(pResult);
		pResult->nRequestUID = pRequest->nRequestUID ;
		pResult->pUserData = pRequest->pUserData ;
		pResult->nAffectRow = 0 ;
		if ( mysql_real_query(m_pMySql,pRequest->pSqlBuffer,pRequest->nSqlBufferLen) )
		{
			printf("query DB Error Info , Operate UID = %d : %s . sql: = %s\n", pRequest->nRequestUID, mysql_error(m_pMySql),pRequest->pSqlBuffer);
			pResult->nAffectRow = 0 ;
			continue; 
		}

		do 
		{
			switch ( pRequest->eType )
			{
			case eRequestType_Add:
			case eRequestType_Delete:
			case eRequestType_Update:
				{
					pResult->nAffectRow = (unsigned int)mysql_affected_rows(m_pMySql);
					msqlResult = mysql_store_result( m_pMySql ); 
					mysql_free_result(msqlResult); 
				}
				break;
			case eRequestType_Select:
				{
					msqlResult = mysql_store_result(m_pMySql);

					if ( msqlResult == NULL )
					{
						mysql_free_result(msqlResult);
						continue;
					}

					if ( pResult->nAffectRow >= 1 )
					{
						if ( msqlResult != NULL )
						{
							printf("mysql_store_result Error Info , Operate UID = %d : %s why have more than one result type  result \n", pRequest->nRequestUID, mysql_error(m_pMySql));
						}
						//pResult->nAffectRow = 0 ;
						mysql_free_result(msqlResult);
						continue;
					}

					pResult->nAffectRow += (unsigned int)mysql_num_rows(msqlResult);
					// process row ;
					int nNumFiled = mysql_num_fields(msqlResult);
					while ( msqlrow = mysql_fetch_row(msqlResult))
					{
						mysql_field_seek(msqlResult,0); // reset o begin ;
						CMysqlRow* rowData = new CMysqlRow ;
						unsigned long* pLengths = mysql_fetch_lengths(msqlResult);
						for ( int i = 0 ; i < nNumFiled ; ++i )
						{
							msqlfield = mysql_fetch_field(msqlResult);
							if ( !msqlfield )
							{
								continue;
							}
							stMysqlField* pField = new stMysqlField(msqlfield->name,msqlfield->name_length) ;
							pField->nBufferLen = pLengths[i] ;
							bool bValide = true ;
							switch (msqlfield->type)
							{
							case MYSQL_TYPE_TINY: // char
							case MYSQL_TYPE_SHORT: // short 
							case MYSQL_TYPE_LONG: // int
							case MYSQL_TYPE_LONGLONG: // 64 bit int 
							case MYSQL_TYPE_FLOAT: // float 
							case MYSQL_TYPE_DOUBLE: // double 
							case MYSQL_TYPE_BLOB: // binary 
							case MYSQL_TYPE_VAR_STRING:  // string 
							case MYSQL_TYPE_INT24:
								{
									bValide = true;
								}
								break;
							default:
								{
									bValide = false ;
									printf("error DB request unsupport field Type : Type = %d : field Name: %s \n",msqlfield->type, pField->strFieldName.c_str()) ;
								}
							}

							if ( bValide )
							{
								pField->pBuffer = new char [pField->nBufferLen + 1 ] ;
								memset(pField->pBuffer,0,pField->nBufferLen + 1 );
								memcpy(pField->pBuffer,msqlrow[i],pLengths[i]);
							}
							else
							{
								delete pField ;
								printf("not support data type from db") ;
								assert(0&&"why support type not !");
								continue; 
							}
							rowData->PushFiled(pField);
						}
						pResult->vResultRows.push_back(rowData);
					}
					mysql_free_result(msqlResult);
				}
				break; 
			default:
				{
					printf("error DB request type, DB request UID = %d , Type = %d\n",pRequest->nRequestUID,pRequest->eType) ;
					continue; ;
				}
			}
		} while ( !mysql_next_result(m_pMySql) );
	}

	if ( vRequestOut.empty() == false )  // means mysql connection is not idle .
	{
		m_tNextMysqlPingTime = time(NULL) + MYSQL_PING_TIME;  // do a mysql_ping() on the connection if there has been a long time [since the last query ]
	}

	pRequestQueue->PushReserveRequest(vRequestOut);
	pRequestQueue->PushResult(vProcessedResult);
	return false ;
}