bool CurrencyPair::Load(PacketBase& packet) { m_lstBids.clear(); m_lstOffers.clear(); if (!packet.ReadString(m_strCurrencyPair, 7)) return false; int nBidsCount; if (!packet.ReadInt(nBidsCount, 4)) return false; while (--nBidsCount >= 0) { BidOrOffer bid; if (!bid.Load(packet)) return false; m_lstBids.push_back(bid); } int nOffersCount; if (!packet.ReadInt(nOffersCount, 4)) return false; while (--nOffersCount >= 0) { BidOrOffer offer; if (!offer.Load(packet)) return false; m_lstOffers.push_back(offer); } return true; }
bool Dispatcher::parsing(Stream* raw) { while (raw->length() >= (int)sizeof(PacketBase)) { PacketBase* header = reinterpret_cast<PacketBase* >(raw->readptr()); if (header->getLength() >= raw->length()) { Stream* s = raw->split(header->getLength()); if (s == nullptr) { return false; } // need to thread sync _recvQueue.push_back(s); } else { break; } } return true; }
bool Order::Load(PacketBase& packet) { if (!packet.ReadDbl(m_lfAmount, 16)) return false; if (!packet.ReadDbl(m_lfMinQty, 16)) return false; if (!packet.ReadDbl(m_lfLotSize, 16)) return false; if (!packet.ReadString(m_strOrderID, 15)) return false; return true; }
bool ClientCurrencyPairMessage::Save ( PacketBase& packet ) const { if ( !MessageBase::Save ( packet ) ) return false; if ( !packet.WriteString ( m_strCurrencyPair, 7 ) ) return false; return true; }
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); }
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); } }
void PeerBase::DoPeerSend() { int32 realSndCount = 0; int32 curSndCount = 0; int32 dropCount = 0; PacketBase* pkt = m_PktSendList.Pop_Head(); while (pkt) { SocketBase* s = m_NetManger->GetSocket(pkt->GetSocketID()); if (s) { ++curSndCount; if (!s->SendPacket(pkt)) { if (!pkt->m_isDrop) { m_PktFailedList.Push_Back(pkt); pkt = m_PktSendList.Pop_Head(); continue; } ++dropCount; } ++realSndCount; s_PacketFactory.DestroyPacket(pkt); } if (curSndCount >= 50) { break; } pkt = m_PktSendList.Pop_Head(); } { PacketBase* failpkt = m_PktFailedList.Pop_Back(); while (failpkt) { m_PktSendList.Push_Back(failpkt); failpkt = m_PktFailedList.Pop_Back(); } } }
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; }
bool BidOrOffer::Load(PacketBase& packet) { m_lstOrders.clear(); if (!packet.ReadString(m_strBidPrice, 10)) return false; int nOrderCount; if (!packet.ReadInt(nOrderCount, 4)) return false; while (--nOrderCount >= 0) { Order order; if (!order.Load(packet)) return false; m_lstOrders.push_back(order); } return true; }
bool HotspotFxMarketSnapshotMessage::Load(PacketBase& packet) { m_lstCurrencyPairs.clear(); if (!packet.ReadInt(m_nMessageLength, 6)) return false; int nCurrencyPairCount; if (!packet.ReadInt(nCurrencyPairCount, 4)) return false; while (--nCurrencyPairCount >= 0) { hotspotfx::CurrencyPair currencyPair; if (!currencyPair.Load(packet)) return false; m_lstCurrencyPairs.push_back(currencyPair); } return MessageBase::Load(packet); }
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; }
bool HotspotFxNewOrderMessage::Load ( PacketBase& packet ) { if (!packet.ReadChar(m_cBuyOrSell)) return false; if (!packet.ReadString(m_strCurrencyPair, 7)) return false; if (!packet.ReadString(m_strOrderID, 15)) return false; if (!packet.ReadString(m_strPrice, 10)) return false; if (!packet.ReadDbl(m_lfAmount, 16)) return false; if (!packet.ReadDbl(m_lfMinQty, 16)) return false; if (!packet.ReadDbl(m_lfLotSize, 16)) return false; return MessageBase::Load(packet); }
bool NetChannel::FillPackets2Block(DataBufferArg& arg) { const uint32 cbHeader = sizeof(BlockHeadT); uint32 cbBuffer = CBBUFF; if( arg.cbBuffer < cbBuffer ) cbBuffer = arg.cbBuffer; if( cbBuffer > BLOCK_SIZE_MASK ) cbBuffer = BLOCK_SIZE_MASK; if( cbBuffer > SOCKET_MY_MAX_DATA_BLOCK_SIZE ) cbBuffer = SOCKET_MY_MAX_DATA_BLOCK_SIZE; char* pBuffer = m_sendPacketBuffer + cbHeader; char* pBuffer2 = m_SendPacketBuffer2 + cbHeader; cbBuffer -= cbHeader; uint32 cbData = 0; // 开始合并数据包处理 bool bFirstPacket = true; bool bEncrypt = true; PacketList& list = m_queueSendingPacket; int64 nPacketSend = 0; PacketBase* pPkt = list.Pop_Head(); while ( pPkt != NULL ) { pPkt->IsUseIndex( m_pMgr->UseIndexWhenSend() ); pPkt->SetPacketIndex( m_nIndexOfSend ); const uint32 PacketAttr = pPkt->GetStaticAttribute(); if( PacketBase::IsDisconnectCommand( PacketAttr ) ) { DisConnect(); FACTORY_DEL_PACKET( pPkt ); return false; } if( bFirstPacket ) { bEncrypt = PacketBase::ShouldEncrypt( PacketAttr ); bFirstPacket = false; } else { if( bEncrypt != PacketBase::ShouldEncrypt( PacketAttr ) ) break; } char pktContent[PACKET_MAX_SIZE]; size_t nPktSize = pPkt->WritePacket( pktContent ) - pktContent; assert( nPktSize < PACKET_MAX_SIZE && nPktSize > 0); if(cbData + nPktSize > cbBuffer) break; memcpy( pBuffer + cbData, pktContent, nPktSize); nPacketSend++; ++m_nIndexOfSend; cbData += (uint32)nPktSize; FACTORY_DEL_PACKET( pPkt ); pPkt = list.Pop_Head(); } m_pMgr->PacketsSend().Add(nPacketSend); if(pPkt != NULL) list.Push_Head(pPkt); if( cbData == 0 ) return false; bool bCompress = false; BlockHeadT blockFlag = 0; #if LZO_COMPRESS if( m_pGLZOCompressor ) { if( bCompress && cbData > 56 ) { uint32 dstLen = cbBuffer; if(!m_pGLZOCompressor->Compress( pBuffer, cbData, pBuffer2, dstLen) ) { assert(false); MyLog::error("Socket send packet size error, Compress Fail! sockid=[%d] size=[%u]!Will disconnect", GetID(), cbData); DisConnect(); return false; } if( dstLen < cbData ) { blockFlag |= BLOCK_FLAG_LZO; std::swap( pBuffer, pBuffer2); cbData = dstLen; } } } #endif #if USE_MY_BLOWFISH if(m_pBlowFishCipher) { if(bEncrypt) { blockFlag |= Block_FLAG_ENC; m_pBlowFishCipher->Encrypt( pBuffer, pBuffer2, nDataLen); std::swap( pBuffer, pBuffer2); } } #endif if( cbData > BLOCK_SIZE_MASK ) { MyLog::error("NetChannel::FillPackets2Block() Error , cbData > BLOCK_SIZE_MASK\n"); } arg.pBuffer = pBuffer - cbHeader; arg.cbData = cbHeader + cbData; BlockHeadT blockHead = (BlockHeadT)(cbData); blockHead |= blockFlag; memcpy( arg.pBuffer, &blockHead, cbHeader); return true; }
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; }