Пример #1
0
  /**
   * 服务端使用 检查消息并返回消息大小
   *
   * @return 0 表示走到文件末尾 >0 正常消息 -1 消息校验失败
   */
  public DispatchRequest checkMessageAndReturnSize(
      java.nio.ByteBuffer byteBuffer, final boolean checkCRC, final boolean readBody) {
    try {
      java.nio.ByteBuffer byteBufferMessage =
          ((DefaultAppendMessageCallback) this.appendMessageCallback).getMsgStoreItemMemory();
      byte[] bytesContent = byteBufferMessage.array();

      // 1 TOTALSIZE
      int totalSize = byteBuffer.getInt();

      // 2 MAGICCODE
      int magicCode = byteBuffer.getInt();
      switch (magicCode) {
        case MessageMagicCode:
          break;
        case BlankMagicCode:
          return new DispatchRequest(0);
        default:
          log.warn("found a illegal magic code 0x" + Integer.toHexString(magicCode));
          return new DispatchRequest(-1);
      }

      // 3 BODYCRC
      int bodyCRC = byteBuffer.getInt();

      // 4 QUEUEID
      int queueId = byteBuffer.getInt();

      // 5 FLAG
      int flag = byteBuffer.getInt();
      flag = flag + 0;

      // 6 QUEUEOFFSET
      long queueOffset = byteBuffer.getLong();

      // 7 PHYSICALOFFSET
      long physicOffset = byteBuffer.getLong();

      // 8 SYSFLAG
      int sysFlag = byteBuffer.getInt();

      // 9 BORNTIMESTAMP
      long bornTimeStamp = byteBuffer.getLong();
      bornTimeStamp = bornTimeStamp + 0;

      // 10 BORNHOST(IP+PORT)
      byteBuffer.get(bytesContent, 0, 8);

      // 11 STORETIMESTAMP
      long storeTimestamp = byteBuffer.getLong();

      // 12 STOREHOST(IP+PORT)
      byteBuffer.get(bytesContent, 0, 8);

      // 13 RECONSUMETIMES
      int reconsumeTimes = byteBuffer.getInt();

      // 14 Prepared Transaction Offset
      long preparedTransactionOffset = byteBuffer.getLong();

      // 15 BODY
      int bodyLen = byteBuffer.getInt();
      if (bodyLen > 0) {
        if (readBody) {
          byteBuffer.get(bytesContent, 0, bodyLen);

          // 校验CRC
          if (checkCRC) {
            int crc = UtilAll.crc32(bytesContent, 0, bodyLen);
            if (crc != bodyCRC) {
              log.warn("CRC check failed " + crc + " " + bodyCRC);
              return new DispatchRequest(-1);
            }
          }
        } else {
          byteBuffer.position(byteBuffer.position() + bodyLen);
        }
      }

      // 16 TOPIC
      byte topicLen = byteBuffer.get();
      byteBuffer.get(bytesContent, 0, topicLen);
      String topic = new String(bytesContent, 0, topicLen);

      long tagsCode = 0;
      String keys = "";

      // 17 properties
      short propertiesLength = byteBuffer.getShort();
      if (propertiesLength > 0) {
        byteBuffer.get(bytesContent, 0, propertiesLength);
        String properties = new String(bytesContent, 0, propertiesLength);
        Map<String, String> propertiesMap = MessageDecoder.string2messageProperties(properties);

        keys = propertiesMap.get(MessageConst.PROPERTY_KEYS);
        String tags = propertiesMap.get(MessageConst.PROPERTY_TAGS);
        if (tags != null && tags.length() > 0) {
          tagsCode =
              MessageExtBrokerInner.tagsString2tagsCode(
                  MessageExt.parseTopicFilterType(sysFlag), tags);
        }
      }

      return new DispatchRequest( //
          topic, // 1
          queueId, // 2
          physicOffset, // 3
          totalSize, // 4
          tagsCode, // 5
          storeTimestamp, // 6
          queueOffset, // 7
          keys, // 8
          sysFlag, // 9
          0L, // 10
          preparedTransactionOffset, // 11
          null // 12
          );
    } catch (BufferUnderflowException e) {
      byteBuffer.position(byteBuffer.limit());
    } catch (Exception e) {
      byteBuffer.position(byteBuffer.limit());
    }

    return new DispatchRequest(-1);
  }