void Channel::processPackets(MessageHandlers* pMsgHandlers) { mLastTickBytesReceived = 0; mLastTickBytesSent = 0; if (this->isDestroyed()) { // ERROR_MSG(fmt::format("Channel::processPackets({}): channel[{:p}] is destroyed.\n", // this->c_str(), (void*)this)); return; } if(this->isCondemn()) { // ERROR_MSG(fmt::format("Channel::processPackets({}): channel[{:p}] is condemn.\n", // this->c_str(), (void*)this)); return; } if(!hasHandshake()) { handshake(); } try { BufferedReceives::iterator packetIter = mBufferedReceives.begin(); for(; packetIter != mBufferedReceives.end(); ++packetIter) { Packet* pPacket = (*packetIter); mpPacketReader->processMessages(pMsgHandlers, pPacket); reclaimPacket(pPacket->isTCPPacket(), pPacket); } } catch(MemoryStreamException &) { // MessageHandler* pMsgHandler = pMsgHandlers->find(mpPacketReader->currMsgID()); // WARNING_MSG(fmt::format("Channel::processPackets({}): packet invalid. currMsg=(name={}, id={}, len={}), currMsgLen={}\n", // this->c_str() // , (pMsgHandler == NULL ? "unknown" : pMsgHandler->name) // , mpPacketReader->currMsgID() // , (pMsgHandler == NULL ? -1 : pMsgHandler->msgLen) // , mpPacketReader->currMsgLen())); mpPacketReader->currMsgID(0); mpPacketReader->currMsgLen(0); condemn(); } mBufferedReceives.clear(); }
//------------------------------------------------------------------------------------- void Channel::processPackets(KBEngine::Mercury::MessageHandlers* pMsgHandlers) { lastTickBytesReceived_ = 0; if(pMsgHandlers_ != NULL) { pMsgHandlers = pMsgHandlers_; } if (this->isDestroyed()) { ERROR_MSG(fmt::format("Channel::processPackets({}): channel[{:p}] is destroyed.\n", this->c_str(), (void*)this)); return; } if(this->isCondemn()) { ERROR_MSG(fmt::format("Channel::processPackets({}): channel[{:p}] is condemn.\n", this->c_str(), (void*)this)); //this->destroy(); return; } if(pPacketReader_ == NULL) { handshake(); } uint8 idx = bufferedReceivesIdx_; bufferedReceivesIdx_ = 1 - bufferedReceivesIdx_; try { BufferedReceives::iterator packetIter = bufferedReceives_[idx].begin(); for(; packetIter != bufferedReceives_[idx].end(); packetIter++) { Packet* pPacket = (*packetIter); pPacketReader_->processMessages(pMsgHandlers, pPacket); if(pPacket->isTCPPacket()) TCPPacket::ObjPool().reclaimObject(static_cast<TCPPacket*>(pPacket)); else UDPPacket::ObjPool().reclaimObject(static_cast<UDPPacket*>(pPacket)); } }catch(MemoryStreamException &) { Mercury::MessageHandler* pMsgHandler = pMsgHandlers->find(pPacketReader_->currMsgID()); WARNING_MSG(fmt::format("Channel::processPackets({}): packet invalid. currMsg=(name={}, id={}, len={}), currMsgLen={}\n", this->c_str() , (pMsgHandler == NULL ? "unknown" : pMsgHandler->name) , pPacketReader_->currMsgID() , (pMsgHandler == NULL ? -1 : pMsgHandler->msgLen) , pPacketReader_->currMsgLen())); pPacketReader_->currMsgID(0); pPacketReader_->currMsgLen(0); condemn(); } bufferedReceives_[idx].clear(); this->send(); }
//------------------------------------------------------------------------------------- void Channel::handleMessage(KBEngine::Mercury::MessageHandlers* pMsgHandlers) { if (this->isDestroyed()) { ERROR_MSG("Channel::handleMessage(%s): channel[%p] is destroyed.\n", this->c_str(), this); return; } if(this->isCondemn()) { ERROR_MSG("Channel::handleMessage(%s): channel[%p] is condemn.\n", this->c_str(), this); //this->destroy(); return; } try { BufferedReceives::iterator packetIter = bufferedReceives_.begin(); for(; packetIter != bufferedReceives_.end(); packetIter++) { Packet* pPacket = (*packetIter); while(pPacket->totalSize() > 0) { if(fragmentDatasFlag_ == FRAGMENT_DATA_UNKNOW) { if(currMsgID_ == 0) { if(MERCURY_MESSAGE_ID_SIZE > 1 && pPacket->opsize() < MERCURY_MESSAGE_ID_SIZE) { writeFragmentMessage(FRAGMENT_DATA_MESSAGE_ID, pPacket, MERCURY_MESSAGE_ID_SIZE); break; } (*pPacket) >> currMsgID_; pPacket->messageID(currMsgID_); } Mercury::MessageHandler* pMsgHandler = pMsgHandlers->find(currMsgID_); if(pMsgHandler == NULL) { TRACE_BUNDLE_DATA(true, pPacket, pMsgHandler, pPacket->totalSize()); WARNING_MSG("Channel::handleMessage: invalide msgID=%d, msglen=%d, from %s.\n", currMsgID_, pPacket->totalSize(), c_str()); currMsgID_ = 0; currMsgLen_ = 0; condemn(); break; } TRACE_BUNDLE_DATA(true, pPacket, pMsgHandler, pPacket->totalSize()); // 如果没有可操作的数据了则退出等待下一个包处理。 //if(pPacket->opsize() == 0) // 可能是一个无参数数据包 // break; if(currMsgLen_ == 0) { if(pMsgHandler->msgLen == MERCURY_VARIABLE_MESSAGE || g_packetAlwaysContainLength) { // 如果长度信息不完整, 则等待下一个包处理 if(pPacket->opsize() < MERCURY_MESSAGE_LENGTH_SIZE) { writeFragmentMessage(FRAGMENT_DATA_MESSAGE_LENGTH, pPacket, MERCURY_MESSAGE_LENGTH_SIZE); break; } else (*pPacket) >> currMsgLen_; } else currMsgLen_ = pMsgHandler->msgLen; } if(currMsgLen_ > MERCURY_MESSAGE_MAX_SIZE / 2) { WARNING_MSG("Channel::handleMessage(%s): msglen is error! msgID=%d, msglen=(%d:%d), from %s.\n", pMsgHandler->name.c_str(), currMsgID_, currMsgLen_, pPacket->totalSize(), c_str()); currMsgLen_ = 0; condemn(); break; } if(pFragmentStream_ != NULL) { pMsgHandler->handle(this, *pFragmentStream_); MemoryStream::ObjPool().reclaimObject(pFragmentStream_); pFragmentStream_ = NULL; } else { if(pPacket->opsize() < currMsgLen_) { writeFragmentMessage(FRAGMENT_DATA_MESSAGE_BODY, pPacket, currMsgLen_); break; } // 临时设置有效读取位, 防止接口中溢出操作 size_t wpos = pPacket->wpos(); // size_t rpos = pPacket->rpos(); size_t frpos = pPacket->rpos() + currMsgLen_; pPacket->wpos(frpos); pMsgHandler->handle(this, *pPacket); // 防止handle中没有将数据导出获取非法操作 if(currMsgLen_ > 0) { if(frpos != pPacket->rpos()) { CRITICAL_MSG("Channel::handleMessage(%s): rpos(%d) invalid, expect=%d. msgID=%d, msglen=%d.\n", pMsgHandler->name.c_str(), pPacket->rpos(), frpos, currMsgID_, currMsgLen_); pPacket->rpos(frpos); } } pPacket->wpos(wpos); } currMsgID_ = 0; currMsgLen_ = 0; } else { mergeFragmentMessage(pPacket); } } if(pPacket->isTCPPacket()) TCPPacket::ObjPool().reclaimObject(static_cast<TCPPacket*>(pPacket)); else UDPPacket::ObjPool().reclaimObject(static_cast<UDPPacket*>(pPacket)); }