Exemplo n.º 1
0
bool PeerBase::PeerSend(int32 socketid, PacketBase* pkt)
{
	if (socketid < 0 || !pkt)
	{
		return false;
	}
	PacketBase* tmppkt = (PacketBase*)s_PacketFactory.CreatePacket(pkt->GetPacketID());
	if (!tmppkt)
	{
		return false;
	}
	char buffer[MAX_PACKET_SIZE];
	pkt->WritePacket(buffer);
	tmppkt->ReadPacket(buffer);
	tmppkt->SetSocketID(socketid);
	tmppkt->m_isDrop = tmppkt->m_isDrop;
	return m_PktSendList.Push_Back(tmppkt);
}
Exemplo n.º 2
0
void FSMExtendData::ProcessPacket(class PacketProcessor* pProccessor)
{
	char* pBuffer = m_vDataBuffer;

	for (int32 i = 0; i < m_nDataCnt; ++i)
	{
		const int32 nPktID = *((int32*)pBuffer);

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

		pBuffer = pPkt->ReadPacket(pBuffer);
		pProccessor->ProcessPacket(pPkt);

		FACTORY_DEL_PACKET(pPkt);
	}
}
Exemplo n.º 3
0
PacketBase* NetManager::DuplicatePacket(PacketBase& pkt)
{
	PacketBase* pPktTmp = (PacketBase*)PacketFactory::Instance().New( pkt.GetPacketID() );
	if( pPktTmp == NULL )
		return 0;

#if PACKET_USE_INDEX_DATA
	bool bOri = pkt.IsUseIndex();
	pkt.IsUseIndex(false);
	pPktTmp->IsUseIndex(false);
#endif

	char buffer[PACKET_MAX_SIZE];
	pkt.WritePacket(buffer);
	pPktTmp->ReadPacket(buffer);

#if PACKET_USE_INDEX_DATA
	pkt.IsUseIndex(bOri);
#endif

	return pPktTmp;
}
Exemplo n.º 4
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;
}