Search in sources :

Example 1 with Action

use of io.aeron.logbuffer.ControlledFragmentHandler.Action in project Aeron by real-logic.

the class Image method controlledPoll.

/**
     * Poll for new messages in a stream. If new messages are found beyond the last consumed position then they
     * will be delivered to the {@link ControlledFragmentHandler} up to a limited number of fragments as specified.
     *
     * To assemble messages that span multiple fragments then use {@link ControlledFragmentAssembler}.
     *
     * @param fragmentHandler to which message fragments are delivered.
     * @param fragmentLimit   for the number of fragments to be consumed during one polling operation.
     * @return the number of fragments that have been consumed.
     * @see ControlledFragmentAssembler
     */
public int controlledPoll(final ControlledFragmentHandler fragmentHandler, final int fragmentLimit) {
    if (isClosed) {
        return 0;
    }
    long position = subscriberPosition.get();
    int termOffset = (int) position & termLengthMask;
    int resultingOffset = termOffset;
    int fragmentsRead = 0;
    final UnsafeBuffer termBuffer = activeTermBuffer(position);
    try {
        final int capacity = termBuffer.capacity();
        do {
            final int length = frameLengthVolatile(termBuffer, resultingOffset);
            if (length <= 0) {
                break;
            }
            final int frameOffset = resultingOffset;
            final int alignedLength = BitUtil.align(length, FRAME_ALIGNMENT);
            resultingOffset += alignedLength;
            if (!isPaddingFrame(termBuffer, frameOffset)) {
                header.buffer(termBuffer);
                header.offset(frameOffset);
                final Action action = fragmentHandler.onFragment(termBuffer, frameOffset + HEADER_LENGTH, length - HEADER_LENGTH, header);
                ++fragmentsRead;
                if (action == BREAK) {
                    break;
                } else if (action == ABORT) {
                    --fragmentsRead;
                    resultingOffset = frameOffset;
                    break;
                } else if (action == COMMIT) {
                    position += alignedLength;
                    termOffset = resultingOffset;
                    subscriberPosition.setOrdered(position);
                }
            }
        } while (fragmentsRead < fragmentLimit && resultingOffset < capacity);
    } catch (final Throwable t) {
        errorHandler.onError(t);
    }
    updatePosition(position, termOffset, resultingOffset);
    return fragmentsRead;
}
Also used : Action(io.aeron.logbuffer.ControlledFragmentHandler.Action) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Aggregations

Action (io.aeron.logbuffer.ControlledFragmentHandler.Action)1 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)1