Beispiel #1
0
  private void applyDefaultHeader(
      final UnsafeBuffer buffer,
      final int termOffset,
      final int frameLength,
      final long defaultVersionFlagsType,
      final long defaultSessionId) {
    long lengthVersionFlagsType;
    long termOffsetAndSessionId;

    if (ByteOrder.nativeOrder() == LITTLE_ENDIAN) {
      lengthVersionFlagsType = defaultVersionFlagsType | ((-frameLength) & 0xFFFF_FFFFL);
      termOffsetAndSessionId = defaultSessionId | termOffset;
    } else {
      lengthVersionFlagsType =
          (((reverseBytes(-frameLength)) & 0xFFFF_FFFFL) << 32) | defaultVersionFlagsType;
      termOffsetAndSessionId =
          (((reverseBytes(termOffset)) & 0xFFFF_FFFFL) << 32) | defaultSessionId;
    }

    buffer.putLongOrdered(termOffset + FRAME_LENGTH_FIELD_OFFSET, lengthVersionFlagsType);
    UnsafeAccess.UNSAFE.storeFence();

    buffer.putLong(termOffset + TERM_OFFSET_FIELD_OFFSET, termOffsetAndSessionId);

    // read the stream(int) and term(int), this is the mutable part of the default header
    final long streamAndTermIds = defaultHeader.getLong(STREAM_ID_FIELD_OFFSET);
    buffer.putLong(termOffset + STREAM_ID_FIELD_OFFSET, streamAndTermIds);
  }
  /** {@inheritDoc} */
  public boolean write(
      final int msgTypeId, final DirectBuffer srcBuffer, final int srcIndex, final int length) {
    checkTypeId(msgTypeId);
    checkMsgLength(length);

    boolean isSuccessful = false;

    final AtomicBuffer buffer = this.buffer;
    final int recordLength = length + HEADER_LENGTH;
    final int requiredCapacity = align(recordLength, ALIGNMENT);
    final int recordIndex = claimCapacity(buffer, requiredCapacity);

    if (INSUFFICIENT_CAPACITY != recordIndex) {
      buffer.putLongOrdered(recordIndex, makeHeader(-recordLength, msgTypeId));
      UnsafeAccess.UNSAFE.storeFence();

      buffer.putBytes(encodedMsgOffset(recordIndex), srcBuffer, srcIndex, length);
      buffer.putIntOrdered(lengthOffset(recordIndex), recordLength);

      isSuccessful = true;
    }

    return isSuccessful;
  }