Exemplo n.º 1
0
bool NetManager::SendPacket(PacketBase& pkt, int32 nSocketID)
{
	bool bSuceed = false;

	NetChannelBase* pChannel = GetChannel( nSocketID );
	if ( pChannel )
	{
		PacketBase* pPkt = DuplicatePacket(pkt);
		if(!pPkt)
			MyLog::error("NetManager::SendPacket failed to DuplicatePacket [%s]", pkt.GetClassName() );
		else
		{
			pPkt->SetSocketID( nSocketID );
			pChannel->AppendPacket( pPkt );
			bSuceed = true;
		}
		FreeChannel(pChannel);
	}

	return bSuceed;
}
Exemplo n.º 2
0
bool NetChannel::ParsePacketsInBlock(BlockHeadT blockHead, char* pBuffer, uint32 nBlockSize)
{
	char* pTempBuffer = m_recvPacketBuffer;

#if LZO_COMPRESS
	if( m_pGLZOCompressor && ( 0 != (blockHead & BLOCK_FLAG_LZO)))
	{
		uint32 nDestLen = CBBUFF;

		if( !m_pGLZOCompressor->DeCompress( pBuffer, nBlockSize, pTempBuffer, nDestLen))
		{
			MyLog::message("NetChannel::ParsePacketsInBlock Fail to DeCompress");
			return false;
		}

		nBlockSize = nDestLen;
		std::swap( pBuffer, pTempBuffer);
	}
#endif

	uint32 nDataLen = nBlockSize;

	int64 nPacketRecved = 0;
	bool bError = false;
	while( nDataLen > 0 && !bError )
	{
		const int32 nPktID = *((int32*)pBuffer);

		PacketBase* pPkt = (PacketBase*)PacketFactory::Instance().New(nPktID);

		if(pPkt)
		{
		#if PACKET_USE_INDEX_DATA
			pPkt->IsUseIndex( m_pMgr->UseIndexWhenRecv());
		#endif
			++nPacketRecved;

			IRecvPacketFilter* pFilter = m_pMgr->GetRecvPacketFilter();

			if( pFilter && (!pFilter->Thread_CheckPacketTypeValid(*pPkt)))
			{
				bError = true;

				const char* pPktName = pPkt->GetClassName();
				MyLog::error("NetChannel::RecvPacket() Packet Type Check Failed, Will Disconnect Socket! pktName=[%s] pktID=[%d]", \
					pPktName ? pPktName : "UNKOWN", pPkt->GetPacketID());
			}

			if(!bError)
			{
				char* pPacketEnd = pPkt->ReadPacket(pBuffer);
				if( 0 == pPacketEnd )
				{
					bError = true;
					const char* pPktName = pPkt->ReadPacket(pBuffer);
					MyLog::message("NetChannel::RecvPacket() Read Packet Failed, Will Disconnect Socket! pktName=[%s] pktID=[%d]", \
						pPktName ? pPktName :  "UNKOWN", pPkt->GetPacketID());
				}
				else
				{
					uint32 nPktDataLen = uint32(pPacketEnd - pBuffer);
					if( nPktDataLen > nDataLen )
					{
						bError = true;
						const char* pPktName = pPkt->GetClassName();
						MyLog::message("NetChannel::RecvPacket() Read Packet Failed, Will Disconnect Socket! pktName=[%s] pktID=[%d]", \
							pPktName ? pPktName :  "UNKOWN", pPkt->GetPacketID());
					}
					else
					{
						pBuffer += nPktDataLen;
						nDataLen -= nPktDataLen;
					}
				}
			}

			if(!bError)
			{
				MyLog::message("Recv Packet sock= %d id= %d \n", m_nID, nPktID);

				pPkt->SetSocketID( m_nID );

#if PACKET_USE_INDEX_DATA
				if( pPkt->IsUseIndex() && ( pPkt->GetPacketIndex() != m_nIndexOfRecv++ ))
				{
					MyLog::error("NetChannel::ParsePacketsInBlock Bad Index Recv! sock = [%d],pkt=[%s]", GetID(), pPkt->GetClass());
					bError = true;
					FACTORY_DEL_PACKET(pPkt);
				}
				else
#endif
				{
					OnPacketParsed(pPkt);
				}
			}
			else
			{
				FACTORY_DEL_PACKET(pPkt);
			}
		}
		else
		{
			bError = true;
			FactoryBase_Arg0* pFactory = PacketFactory::Instance().GetFactoryByPacketID(nPktID);
			if(pFactory)
			{
				MyLog::error("Packet Factory Create Packet name [%s] id[%d] fail", pFactory->ClassName(), nPktID);
			}
			else
			{
				MyLog::error("Packet Factroy Create Packet id[%d] fail", nPktID);
			}
		}
	}

	m_pMgr->PacketsRecv().Add(nPacketRecved);

	if(bError)
		DisConnect();

	return !bError;
}