int BasePacketStreamer::encode_handler(easy_request_t *r, void *packet) { BasePacket* bp = (BasePacket*)packet; int32_t pcode = bp->getPCode(); int64_t length = bp->length(); TBSYS_LOG(DEBUG, "encode packet, pcode=%d, length=%ld, chid=%d", pcode, length, bp->getChannelId()); if (EASY_TYPE_CLIENT == r->ms->c->type) { uint32_t chid = ((easy_session_t*)r->ms)->packet_id; bp->setChannelId(chid); } else { if (((easy_message_t*)r->ms)->status == EASY_MESG_DESTROY) { return EASY_ERROR; } if (r->retcode == EASY_ABORT) { r->ms->c->pool->ref--; easy_atomic_dec(&r->ms->pool->ref); r->retcode = EASY_OK; } } easy_list_t list; easy_list_init(&list); Stream output(r->ms->pool, &list); output.reserve(TFS_PACKET_HEADER_V1_SIZE + bp->length()); output.set_int32(TFS_PACKET_FLAG_V1); char* len_pos = output.get_free(); // length() may not equal real data length output.set_int32(bp->length()); output.set_int16(bp->getPCode()); output.set_int16(TFS_PACKET_VERSION_V2); output.set_int64(bp->getChannelId()); output.set_int32(0); // crc, place holder if (TFS_SUCCESS != bp->serialize(output)) { return EASY_ERROR; } // use real length to replace header length if (bp->length() != output.get_data_length() - TFS_PACKET_HEADER_V1_SIZE) { // help to detect serialize problem TBSYS_LOG(DEBUG, "length()=%ld not equals to serialize()=%ld", bp->length(), output.get_data_length() - TFS_PACKET_HEADER_V1_SIZE); int64_t pos = 0; Serialization::set_int32(len_pos, INT_SIZE, pos, output.get_data_length() - TFS_PACKET_HEADER_V1_SIZE); } easy_request_addbuf_list(r, &list); return EASY_OK; }
/** handle single packet */ tbnet::IPacketHandler::HPRetCode KvRootServer::handlePacket(tbnet::Connection *connection, tbnet::Packet *packet) { tbnet::IPacketHandler::HPRetCode hret = tbnet::IPacketHandler::FREE_CHANNEL; bool bret = NULL != connection && NULL != packet; if (bret) { TBSYS_LOG(DEBUG, "receive pcode : %d", packet->getPCode()); if (!packet->isRegularPacket()) { bret = false; TBSYS_LOG(WARN, "control packet, pcode: %d", dynamic_cast<tbnet::ControlPacket*>(packet)->getCommand()); } if (bret) { BasePacket* bpacket = dynamic_cast<BasePacket*>(packet); bpacket->set_connection(connection); bpacket->setExpireTime(MAX_RESPONSE_TIME); bpacket->set_direction(static_cast<DirectionStatus>(bpacket->get_direction()|DIRECTION_RECEIVE)); if (bpacket->is_enable_dump()) { bpacket->dump(); } int32_t pcode = bpacket->getPCode(); int32_t iret = common::TFS_SUCCESS; if (common::TFS_SUCCESS == iret) { hret = tbnet::IPacketHandler::KEEP_CHANNEL; switch (pcode) { case REQ_KV_RT_MS_KEEPALIVE_MESSAGE: ms_rs_heartbeat_workers_.push(bpacket, 0/* no limit */, false/* no block */); break; default: if (!main_workers_.push(bpacket, work_queue_size_)) { bpacket->reply_error_packet(TBSYS_LOG_LEVEL(ERROR),STATUS_MESSAGE_ERROR, "%s, task message beyond max queue size, discard", get_ip_addr()); bpacket->free(); } break; } } else { bpacket->free(); TBSYS_LOG(WARN, "the msg: %d will be ignored", pcode); } } } return hret; }