Exemplo n.º 1
0
    // libeasy copy packet
    BasePacket* BasePacketStreamer::clone_packet(BasePacket* src)
    {
      int32_t pcode = src->getPCode();
      int64_t length = src->length();
      BasePacket* dest = dynamic_cast<BasePacket*>(_factory->createPacket(pcode));
      assert(NULL != dest);
      // Stream stream(length);
      dest->stream_.expand(length);
      int ret = src->serialize(dest->stream_);
      if (TFS_SUCCESS == ret)
      {
        ret = dest->deserialize(dest->stream_);
      }

      TBSYS_LOG_DW(ret, "clone packet, ret=%d, pcode=%d, length=%ld, src=%p, dest=%p",
          ret, pcode, length, src, dest);

      if (TFS_SUCCESS != ret)
      {
        tbsys::gDelete(dest);
        dest = NULL;
      }
      else
      {
        dest->setChannelId(src->getChannelId());
        dest->set_request(src->get_request());
        dest->set_direction(src->get_direction());
      }

      return dest;
    }
Exemplo n.º 2
0
    /** 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;
    }