Пример #1
0
        public void ShouldNotReadLastMessageWhenPadding()
        {
            const int msgLength          = 1;
            int       frameLength        = HEADER_LENGTH + msgLength;
            int       alignedFrameLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            int       frameOffset        = TERM_BUFFER_CAPACITY - alignedFrameLength;
            long      startingPosition   = LogBufferDescriptor.ComputePosition(
                INITIAL_TERM_ID, frameOffset, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID);

            A.CallTo(() => termBuffer.GetIntVolatile(frameOffset)).Returns(frameLength);
            A.CallTo(() => termBuffer.GetShort(FrameDescriptor.TypeOffset(frameOffset)))
            .Returns((short)FrameDescriptor.PADDING_FRAME_TYPE);
            A.CallTo(() => subscriberPosition.GetVolatile()).Returns(startingPosition);

            int readOutcome = TermReader.Read(
                termBuffer,
                frameOffset,
                handler,
                int.MaxValue,
                header,
                errorHandler,
                startingPosition,
                subscriberPosition);

            Assert.AreEqual(0, readOutcome);

            A.CallTo(() => termBuffer.GetIntVolatile(frameOffset)).MustHaveHappened()
            .Then(A.CallTo(() => subscriberPosition.SetOrdered(TERM_BUFFER_CAPACITY)).MustHaveHappened());

            A.CallTo(() => handler.OnFragment(A <UnsafeBuffer> ._, A <int> ._, A <int> ._, A <Header> ._))
            .MustNotHaveHappened();
        }
Пример #2
0
        public void ShouldFragmentMessageOverTwoFrames()
        {
            int          msgLength        = MaxPayloadLength + 1;
            int          headerLength     = _defaultHeader.Capacity;
            int          frameLength      = headerLength + 1;
            int          requiredCapacity = BitUtil.Align(headerLength + 1, FrameDescriptor.FRAME_ALIGNMENT) + MaxFrameLength;
            UnsafeBuffer buffer           = new UnsafeBuffer(new byte[msgLength]);
            int          tail             = 0;

            _logMetaDataBuffer.PutLong(TermTailCounterOffset, LogBufferDescriptor.PackTail(TermID, tail));

            Assert.That(_termAppender.AppendFragmentedMessage(_headerWriter, buffer, 0, msgLength, MaxPayloadLength, RVS), Is.EqualTo((long)requiredCapacity));

            Assert.AreEqual(LogBufferDescriptor.RawTailVolatile(_logMetaDataBuffer, PartionIndex), LogBufferDescriptor.PackTail(TermID, tail + requiredCapacity));

            A.CallTo(() => _headerWriter.Write(_termBuffer, tail, MaxFrameLength, TermID)).MustHaveHappened()
            .Then(A.CallTo(() => _termBuffer.PutBytes(tail + headerLength, buffer, 0, MaxPayloadLength)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutByte(FrameDescriptor.FlagsOffset(tail), FrameDescriptor.BEGIN_FRAG_FLAG)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutLong(tail + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, RV)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutIntOrdered(tail, MaxFrameLength)).MustHaveHappened())
            .Then(A.CallTo(() => _headerWriter.Write(_termBuffer, MaxFrameLength, frameLength, TermID)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutBytes(MaxFrameLength + headerLength, buffer, MaxPayloadLength, 1)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutByte(FrameDescriptor.FlagsOffset(MaxFrameLength), FrameDescriptor.END_FRAG_FLAG)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutLong(MaxFrameLength + DataHeaderFlyweight.RESERVED_VALUE_OFFSET, RV)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutIntOrdered(MaxFrameLength, frameLength)).MustHaveHappened());
        }
Пример #3
0
        internal ConcurrentPublication(
            ClientConductor clientConductor,
            string channel,
            int streamId,
            int sessionId,
            IReadablePosition positionLimit,
            int channelStatusId,
            LogBuffers logBuffers,
            long originalRegistrationId,
            long registrationId)
            : base(
                clientConductor,
                channel,
                streamId,
                sessionId,
                positionLimit,
                channelStatusId,
                logBuffers,
                originalRegistrationId,
                registrationId,
                FrameDescriptor.ComputeMaxMessageLength(logBuffers.TermLength())
                )
        {
            var buffers = logBuffers.DuplicateTermBuffers();

            for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
            {
                _termAppenders[i] = new TermAppender(buffers[i], _logMetaDataBuffer, i);
            }
        }
Пример #4
0
        public bool GetSnapshot(ImageFormat imageFormat, string filename)
        {
            int rawImageSize = DetermineRawImageSize();

            byte[] buf = new byte[rawImageSize];

            Api.SetStreamState(m_hCamera, StreamState.Start);

            FrameDescriptor frameDesc = new FrameDescriptor();

            ReturnCode rc = Api.GetNextFrame(m_hCamera, buf.Length, buf, ref frameDesc);

            Api.SetStreamState(m_hCamera, StreamState.Stop);

            // How big a buffer do we need for the converted image?
            int destBufferSize = 0;

            rc = Api.FormatImage(buf, ref frameDesc, imageFormat, null, ref destBufferSize);
            byte[] dstBuf = new byte[destBufferSize];
            rc = Api.FormatImage(buf, ref frameDesc, imageFormat, dstBuf, ref destBufferSize);

            // Save the data to a binary file
            FileStream   fStream = new FileStream(filename, FileMode.OpenOrCreate);
            BinaryWriter bw      = new BinaryWriter(fStream);

            bw.Write(dstBuf);
            bw.Close();
            fStream.Close();

            return(true);
        }
Пример #5
0
        public void ShouldReadBlockOfThreeMessagesThatFillBuffer()
        {
            const int offset               = 0;
            int       limit                = _termBuffer.Capacity;
            const int messageLength        = 50;
            int       alignedMessageLength = BitUtil.Align(messageLength, FrameDescriptor.FRAME_ALIGNMENT);
            int       thirdMessageLength   = limit - (alignedMessageLength * 2);

            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(offset)))
            .Returns(messageLength);
            A.CallTo(() => _termBuffer.GetShort(FrameDescriptor.TypeOffset(offset)))
            .Returns((short)HeaderFlyweight.HDR_TYPE_DATA);
            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(alignedMessageLength)))
            .Returns(messageLength);
            A.CallTo(() => _termBuffer.GetShort(FrameDescriptor.TypeOffset(alignedMessageLength)))
            .Returns((short)HeaderFlyweight.HDR_TYPE_DATA);
            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(alignedMessageLength * 2)))
            .Returns(thirdMessageLength);
            A.CallTo(() => _termBuffer.GetShort(FrameDescriptor.TypeOffset(alignedMessageLength * 2)))
            .Returns((short)HeaderFlyweight.HDR_TYPE_DATA);

            int newOffset = TermBlockScanner.Scan(_termBuffer, offset, limit);

            Assert.That(newOffset, Is.EqualTo(limit));
        }
Пример #6
0
 internal Publication(
     ClientConductor clientConductor,
     string channel,
     int streamId,
     int sessionId,
     IReadablePosition positionLimit,
     int channelStatusId,
     LogBuffers logBuffers,
     long originalRegistrationId,
     long registrationId)
 {
     _logMetaDataBuffer   = logBuffers.MetaDataBuffer();
     TermBufferLength     = logBuffers.TermLength();
     MaxMessageLength     = FrameDescriptor.ComputeMaxMessageLength(TermBufferLength);
     MaxPayloadLength     = LogBufferDescriptor.MtuLength(_logMetaDataBuffer) - DataHeaderFlyweight.HEADER_LENGTH;
     _maxPossiblePosition = TermBufferLength * (1L << 31);
     _conductor           = clientConductor;
     Channel                 = channel;
     StreamId                = streamId;
     SessionId               = sessionId;
     InitialTermId           = LogBufferDescriptor.InitialTermId(_logMetaDataBuffer);
     _originalRegistrationId = originalRegistrationId;
     RegistrationId          = registrationId;
     _positionLimit          = positionLimit;
     _channelStatusId        = channelStatusId;
     _logBuffers             = logBuffers;
     PositionBitsToShift     = LogBufferDescriptor.PositionBitsToShift(TermBufferLength);
     _headerWriter           = new HeaderWriter(LogBufferDescriptor.DefaultFrameHeader(_logMetaDataBuffer));
 }
Пример #7
0
        internal Publication(ClientConductor clientConductor, string channel, int streamId, int sessionId, IReadablePosition positionLimit, LogBuffers logBuffers, long registrationId)
        {
            var buffers           = logBuffers.AtomicBuffers();
            var logMetaDataBuffer = buffers[LogBufferDescriptor.LOG_META_DATA_SECTION_INDEX];

            for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
            {
                _termAppenders[i] = new TermAppender(buffers[i], buffers[i + LogBufferDescriptor.PARTITION_COUNT]);
            }

            var termLength = logBuffers.TermLength();

            _maxPayloadLength    = LogBufferDescriptor.MtuLength(logMetaDataBuffer) - DataHeaderFlyweight.HEADER_LENGTH;
            MaxMessageLength     = FrameDescriptor.ComputeMaxMessageLength(termLength);
            _clientConductor     = clientConductor;
            Channel              = channel;
            StreamId             = streamId;
            SessionId            = sessionId;
            InitialTermId        = LogBufferDescriptor.InitialTermId(logMetaDataBuffer);
            _logMetaDataBuffer   = logMetaDataBuffer;
            RegistrationId       = registrationId;
            _positionLimit       = positionLimit;
            _logBuffers          = logBuffers;
            _positionBitsToShift = IntUtil.NumberOfTrailingZeros(termLength);
            _headerWriter        = new HeaderWriter(LogBufferDescriptor.DefaultFrameHeader(logMetaDataBuffer));
        }
Пример #8
0
        /// <summary>
        /// Poll for new messages in a stream. If new messages are found beyond the last consumed position then they
        /// will be delivered to the <seealso cref="FragmentHandler"/> up to a limited number of fragments as specified or
        /// the maximum position specified.
        /// <para>
        /// Use a <seealso cref="FragmentAssembler"/> to assemble messages which span multiple fragments.
        ///
        /// </para>
        /// </summary>
        /// <param name="handler">       to which message fragments are delivered. </param>
        /// <param name="limitPosition"> to consume messages up to. </param>
        /// <param name="fragmentLimit"> for the number of fragments to be consumed during one polling operation. </param>
        /// <returns> the number of fragments that have been consumed. </returns>
        /// <seealso cref="FragmentAssembler" />
        /// <seealso cref="ImageFragmentAssembler" />
        public int BoundedPoll(IFragmentHandler handler, long limitPosition, int fragmentLimit)
        {
            if (_isClosed)
            {
                return(0);
            }

            int          fragmentsRead   = 0;
            long         initialPosition = _subscriberPosition.Get();
            int          initialOffset   = (int)initialPosition & _termLengthMask;
            int          offset          = initialOffset;
            UnsafeBuffer termBuffer      = ActiveTermBuffer(initialPosition);
            int          limitOffset     = (int)Math.Min(termBuffer.Capacity, (limitPosition - initialPosition) + offset);
            Header       header          = _header;

            header.Buffer = termBuffer;

            try
            {
                while (fragmentsRead < fragmentLimit && offset < limitOffset)
                {
                    int length = FrameDescriptor.FrameLengthVolatile(termBuffer, offset);
                    if (length <= 0)
                    {
                        break;
                    }

                    int frameOffset   = offset;
                    int alignedLength = BitUtil.Align(length, FrameDescriptor.FRAME_ALIGNMENT);
                    offset += alignedLength;

                    if (FrameDescriptor.IsPaddingFrame(termBuffer, frameOffset))
                    {
                        continue;
                    }

                    header.Offset = frameOffset;
                    handler.OnFragment(termBuffer, frameOffset + DataHeaderFlyweight.HEADER_LENGTH, length - DataHeaderFlyweight.HEADER_LENGTH, header);
                    ++fragmentsRead;
                }
            }
            catch (Exception t)
            {
                _errorHandler(t);
            }
            finally
            {
                long resultingPosition = initialPosition + (offset - initialOffset);
                if (resultingPosition > initialPosition)
                {
                    _subscriberPosition.SetOrdered(resultingPosition);
                }
            }

            return(fragmentsRead);
        }
Пример #9
0
        public void AddFrame(string animationName, int x, int y, int width, int height, double delay, int nextFrame)
        {
            FrameDescriptor f = new FrameDescriptor();

            f.animationName = animationName;
            f.x = x;
            f.y = y;
            f.height = height;
            f.width = width;
            f.delay = delay;
            f.nextFrame = nextFrame;

            frames.Add(f);
        }
        public void ShouldReadOneMessageOnLimit()
        {
            const int offset = 0;
            const int messageLength = 50;
            int alignedMessageLength = BitUtil.Align(messageLength, FrameDescriptor.FRAME_ALIGNMENT);
            int limit = alignedMessageLength;

            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(offset)))
                .Returns(messageLength);

            int newOffset = TermBlockScanner.Scan(_termBuffer, offset, limit);

            Assert.AreEqual(alignedMessageLength, newOffset);
        }
Пример #11
0
        public void ShouldReadFirstMessage()
        {
            const int offset               = 0;
            int       limit                = _termBuffer.Capacity;
            const int messageLength        = 50;
            int       alignedMessageLength = BitUtil.Align(messageLength, FrameDescriptor.FRAME_ALIGNMENT);

            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(offset)))
            .Returns(messageLength);

            int newOffset = TermBlockScanner.Scan(_termBuffer, offset, limit);

            Assert.That(newOffset, Is.EqualTo(alignedMessageLength));
        }
Пример #12
0
        internal ExclusivePublication(
            ClientConductor clientConductor,
            string channel,
            int streamId,
            int sessionId,
            IReadablePosition positionLimit,
            LogBuffers logBuffers,
            long originalRegistrationId,
            long registrationId)
        {
            var buffers           = logBuffers.TermBuffers();
            var logMetaDataBuffer = logBuffers.MetaDataBuffer();

            for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
            {
                _termAppenders[i] = new ExclusiveTermAppender(buffers[i], logMetaDataBuffer, i);
            }

            var termLength = logBuffers.TermLength();

            _termBufferLength    = termLength;
            MaxPayloadLength     = LogBufferDescriptor.MtuLength(logMetaDataBuffer) - DataHeaderFlyweight.HEADER_LENGTH;
            MaxMessageLength     = FrameDescriptor.ComputeExclusiveMaxMessageLength(termLength);
            _maxPossiblePosition = termLength * (1L << 31);
            _conductor           = clientConductor;
            Channel   = channel;
            StreamId  = streamId;
            SessionId = sessionId;

            _logMetaDataBuffer      = logMetaDataBuffer;
            _originalRegistrationId = originalRegistrationId;
            RegistrationId          = registrationId;
            _positionLimit          = positionLimit;
            _logBuffers             = logBuffers;
            _positionBitsToShift    = IntUtil.NumberOfTrailingZeros(termLength);
            _headerWriter           = new HeaderWriter(LogBufferDescriptor.DefaultFrameHeader(logMetaDataBuffer));
            InitialTermId           = LogBufferDescriptor.InitialTermId(logMetaDataBuffer);

            var activeIndex = LogBufferDescriptor.ActivePartitionIndex(logMetaDataBuffer);

            _activePartitionIndex = activeIndex;

            long rawTail = LogBufferDescriptor.RawTail(_logMetaDataBuffer, activeIndex);

            _termId            = LogBufferDescriptor.TermId(rawTail);
            _termOffset        = LogBufferDescriptor.TermOffset(rawTail, termLength);
            _termBeginPosition =
                LogBufferDescriptor.ComputeTermBeginPosition(_termId, _positionBitsToShift, InitialTermId);
        }
        public void ShouldReadFirstMessage()
        {
            const int offset = 0;
            int limit = _termBuffer.Capacity;
            const int messageLength = 50;
            int alignedMessageLength = BitUtil.Align(messageLength, FrameDescriptor.FRAME_ALIGNMENT);

            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(offset)))
                .Returns(messageLength);
            A.CallTo(() => _termBuffer.GetShort(FrameDescriptor.TypeOffset(offset)))
                .Returns((short)HeaderFlyweight.HDR_TYPE_DATA);

            int newOffset = TermBlockScanner.Scan(_termBuffer, offset, limit);
            Assert.AreEqual(alignedMessageLength, newOffset);
        }
Пример #14
0
        public void ShouldInsertLastFrameIntoBuffer()
        {
            int          frameLength = BitUtil.Align(256, FrameDescriptor.FRAME_ALIGNMENT);
            const int    srcOffset   = 0;
            int          tail        = TERM_BUFFER_CAPACITY - frameLength;
            int          termOffset  = tail;
            UnsafeBuffer packet      = new UnsafeBuffer(new byte[frameLength]);

            packet.PutShort(FrameDescriptor.TypeOffset(srcOffset), (short)FrameDescriptor.PADDING_FRAME_TYPE);
            packet.PutInt(srcOffset, frameLength);

            TermRebuilder.Insert(_termBuffer, termOffset, packet, frameLength);

            A.CallTo(() => _termBuffer.PutBytes(tail + DataHeaderFlyweight.HEADER_LENGTH, packet, srcOffset + DataHeaderFlyweight.HEADER_LENGTH, frameLength - DataHeaderFlyweight.HEADER_LENGTH)).MustHaveHappened();
        }
Пример #15
0
        private ReturnCode GetNextFrame(ref FrameDescriptor frameDesc, byte[] buf)
        {
            ReturnCode rc        = ReturnCode.UnknownError;
            const int  NUM_TRIES = 4;

            for (int i = 0; i < NUM_TRIES; i++)
            {
                rc = Api.GetNextFrame(m_hCamera, buf.Length, buf, ref frameDesc);
                if (Api.IsSuccess(rc))
                {
                    return(rc);
                }
            }
            return(rc);
        }
Пример #16
0
        public void ShouldReadFirstMessage()
        {
            const int msgLength   = 1;
            int       frameLength = HEADER_LENGTH + msgLength;
            const int termOffset  = 0;

            A.CallTo(() => termBuffer.GetIntVolatile(0))
            .Returns(frameLength);
            A.CallTo(() => termBuffer.GetShort(FrameDescriptor.TypeOffset(0)))
            .Returns((short)HeaderFlyweight.HDR_TYPE_DATA);

            long readOutcome = TermReader.Read(termBuffer, termOffset, handler, int.MaxValue, header, errorHandler);

            Assert.That(TermReader.FragmentsRead(readOutcome), Is.EqualTo(1));

            A.CallTo(() => termBuffer.GetIntVolatile(0)).MustHaveHappened();
            A.CallTo(() => handler(termBuffer, HEADER_LENGTH, msgLength, A <Header> ._)).MustHaveHappened();
        }
Пример #17
0
        public void ShouldReadLastMessage()
        {
            const int msgLength          = 1;
            int       frameLength        = HEADER_LENGTH + msgLength;
            int       alignedFrameLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            int       frameOffset        = TERM_BUFFER_CAPACITY - alignedFrameLength;

            A.CallTo(() => termBuffer.GetIntVolatile(frameOffset)).Returns(frameLength);
            A.CallTo(() => termBuffer.GetShort(FrameDescriptor.TypeOffset(frameOffset))).Returns((short)HeaderFlyweight.HDR_TYPE_DATA);

            long readOutcome = TermReader.Read(termBuffer, frameOffset, handler, int.MaxValue, header, errorHandler);

            Assert.That(TermReader.FragmentsRead(readOutcome), Is.EqualTo(1));
            Assert.That(TermReader.Offset(readOutcome), Is.EqualTo(TERM_BUFFER_CAPACITY));

            A.CallTo(() => termBuffer.GetIntVolatile(frameOffset)).MustHaveHappened()
            .Then(A.CallTo(() => handler(termBuffer, frameOffset + HEADER_LENGTH, msgLength, A <Header> ._)).MustHaveHappened());
        }
Пример #18
0
        public void ShouldNotReadLastMessageWhenPadding()
        {
            const int msgLength          = 1;
            int       frameLength        = HEADER_LENGTH + msgLength;
            int       alignedFrameLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            int       frameOffset        = TERM_BUFFER_CAPACITY - alignedFrameLength;

            A.CallTo(() => termBuffer.GetIntVolatile(frameOffset)).Returns(frameLength);
            A.CallTo(() => termBuffer.GetShort(FrameDescriptor.TypeOffset(frameOffset))).Returns((short)FrameDescriptor.PADDING_FRAME_TYPE);

            long readOutcome = TermReader.Read(termBuffer, frameOffset, handler, int.MaxValue, header, errorHandler);

            Assert.That(TermReader.FragmentsRead(readOutcome), Is.EqualTo(0));
            Assert.That(TermReader.Offset(readOutcome), Is.EqualTo(TERM_BUFFER_CAPACITY));

            A.CallTo(() => termBuffer.GetIntVolatile(frameOffset)).MustHaveHappened();
            A.CallTo(() => handler(A <IDirectBuffer> ._, A <int> ._, A <int> ._, A <Header> ._)).MustNotHaveHappened();
        }
Пример #19
0
        internal ExclusivePublication(
            ClientConductor clientConductor,
            string channel,
            int streamId,
            int sessionId,
            IReadablePosition positionLimit,
            int channelStatusId,
            LogBuffers logBuffers,
            long originalRegistrationId,
            long registrationId)
            : base(
                clientConductor,
                channel,
                streamId,
                sessionId,
                positionLimit,
                channelStatusId,
                logBuffers,
                originalRegistrationId,
                registrationId,
                FrameDescriptor.ComputeExclusiveMaxMessageLength(logBuffers.TermLength())
                )
        {
            var buffers           = logBuffers.DuplicateTermBuffers();
            var logMetaDataBuffer = logBuffers.MetaDataBuffer();

            for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++)
            {
                _termAppenders[i] = new ExclusiveTermAppender(buffers[i], logMetaDataBuffer, i);
            }

            var termCount = LogBufferDescriptor.ActiveTermCount(logMetaDataBuffer);
            var index     = LogBufferDescriptor.IndexByTermCount(termCount);

            _activePartitionIndex = index;

            var rawTail = LogBufferDescriptor.RawTail(_logMetaDataBuffer, index);

            _termId            = LogBufferDescriptor.TermId(rawTail);
            _termOffset        = LogBufferDescriptor.TermOffset(rawTail);
            _termBeginPosition =
                LogBufferDescriptor.ComputeTermBeginPosition(_termId, _positionBitsToShift, InitialTermId);
        }
Пример #20
0
        public void ShouldPadLogWhenAppendingWithInsufficientRemainingCapacity()
        {
            const int    msgLength         = 120;
            int          headerLength      = _defaultHeader.Capacity;
            int          requiredFrameSize = BitUtil.Align(headerLength + msgLength, FrameDescriptor.FRAME_ALIGNMENT);
            int          tailValue         = TermBufferLength - BitUtil.Align(msgLength, FrameDescriptor.FRAME_ALIGNMENT);
            UnsafeBuffer buffer            = new UnsafeBuffer(new byte[128]);
            int          frameLength       = TermBufferLength - tailValue;

            _logMetaDataBuffer.PutLong(TermTailCounterOffset, LogBufferDescriptor.PackTail(TermID, tailValue));

            Assert.That(_termAppender.AppendUnfragmentedMessage(_headerWriter, buffer, 0, msgLength, RVS, TermID), Is.EqualTo(TermAppender.FAILED));

            Assert.AreEqual(LogBufferDescriptor.RawTailVolatile(_logMetaDataBuffer, PartionIndex), LogBufferDescriptor.PackTail(TermID, tailValue + requiredFrameSize));

            A.CallTo(() => _headerWriter.Write(_termBuffer, tailValue, frameLength, TermID)).MustHaveHappened()
            .Then(A.CallTo(() => _termBuffer.PutShort(FrameDescriptor.TypeOffset(tailValue), FrameDescriptor.PADDING_FRAME_TYPE)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutIntOrdered(tailValue, frameLength)).MustHaveHappened());
        }
Пример #21
0
        public void ShouldReadFirstMessage()
        {
            const int msgLength          = 1;
            int       frameLength        = HEADER_LENGTH + msgLength;
            int       alignedFrameLength = BitUtil.Align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            const int termOffset         = 0;

            A.CallTo(() => termBuffer.GetIntVolatile(0))
            .Returns(frameLength);
            A.CallTo(() => termBuffer.GetShort(FrameDescriptor.TypeOffset(0)))
            .Returns((short)HeaderFlyweight.HDR_TYPE_DATA);

            int readOutcome = TermReader.Read(termBuffer, termOffset, handler, int.MaxValue, header, errorHandler, 0, subscriberPosition);

            Assert.That(TermReader.FragmentsRead(readOutcome), Is.EqualTo(1));

            A.CallTo(() => termBuffer.GetIntVolatile(0)).MustHaveHappened()
            .Then(A.CallTo(() => handler.OnFragment(termBuffer, HEADER_LENGTH, msgLength, A <Header> ._)).MustHaveHappened())
            .Then(A.CallTo(() => subscriberPosition.SetOrdered(alignedFrameLength)).MustHaveHappened());
        }
Пример #22
0
        public void ShouldReadBlockOfOneMessageThenPadding()
        {
            const int offset               = 0;
            int       limit                = _termBuffer.Capacity;
            int       messageLength        = 50;
            int       alignedMessageLength = BitUtil.Align(messageLength, FrameDescriptor.FRAME_ALIGNMENT);

            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(offset))).Returns(messageLength);
            A.CallTo(() => _termBuffer.GetShort(FrameDescriptor.TypeOffset(offset))).Returns((short)HeaderFlyweight.HDR_TYPE_DATA);
            A.CallTo(() => _termBuffer.GetIntVolatile(FrameDescriptor.LengthOffset(alignedMessageLength))).Returns(messageLength);
            A.CallTo(() => _termBuffer.GetShort(FrameDescriptor.TypeOffset(alignedMessageLength))).Returns((short)HeaderFlyweight.HDR_TYPE_PAD);

            int firstOffset = TermBlockScanner.Scan(_termBuffer, offset, limit);

            Assert.That(firstOffset, Is.EqualTo(alignedMessageLength));

            int secondOffset = TermBlockScanner.Scan(_termBuffer, firstOffset, limit);

            Assert.That(secondOffset, Is.EqualTo(alignedMessageLength * 2));
        }
Пример #23
0
        public void ShouldPadLogAndTripWhenAppendingWithInsufficientRemainingCapacity()
        {
            const int    msgLength         = 120;
            int          headerLength      = _defaultHeader.Capacity;
            int          requiredFrameSize = BitUtil.Align(headerLength + msgLength, FrameDescriptor.FRAME_ALIGNMENT);
            int          tailValue         = TermBufferLength - BitUtil.Align(msgLength, FrameDescriptor.FRAME_ALIGNMENT);
            UnsafeBuffer buffer            = new UnsafeBuffer(new byte[128]);
            int          frameLength       = TermBufferLength - tailValue;

            A.CallTo(() => _metaDataBuffer.GetAndAddLong(LogBufferDescriptor.TERM_TAIL_COUNTER_OFFSET, requiredFrameSize)).Returns(TermAppender.Pack(TermID, tailValue));

            long expectResult = TermAppender.Pack(TermID, TermAppender.TRIPPED);

            Assert.That(_termAppender.AppendUnfragmentedMessage(_headerWriter, buffer, 0, msgLength), Is.EqualTo(expectResult));

            A.CallTo(() => _metaDataBuffer.GetAndAddLong(LogBufferDescriptor.TERM_TAIL_COUNTER_OFFSET, requiredFrameSize)).MustHaveHappened()
            .Then(A.CallTo(() => _headerWriter.Write(_termBuffer, tailValue, frameLength, TermID)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutShort(FrameDescriptor.TypeOffset(tailValue), (short)FrameDescriptor.PADDING_FRAME_TYPE)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutIntOrdered(tailValue, frameLength)).MustHaveHappened());
        }
Пример #24
0
            /// <summary>
            /// Try to gap fill the current term at a given offset if the gap contains no data.
            ///
            /// Note: the gap offset plus gap length must end on a <seealso cref="FrameDescriptor.FRAME_ALIGNMENT"/> boundary.
            /// </summary>
            /// <param name="logMetaDataBuffer"> containing the default headers </param>
            /// <param name="termBuffer">        to gap fill </param>
            /// <param name="termId">            for the current term. </param>
            /// <param name="gapOffset">         to fill from </param>
            /// <param name="gapLength">         to length of the gap. </param>
            /// <returns> true if the gap has been filled with a padding record or false if data found. </returns>
            public static bool TryFillGap(UnsafeBuffer logMetaDataBuffer, UnsafeBuffer termBuffer, int termId, int gapOffset, int gapLength)
            {
                int offset = gapOffset + gapLength - FrameDescriptor.FRAME_ALIGNMENT;

                while (offset >= gapOffset)
                {
                    if (0 != termBuffer.GetInt(offset))
                    {
                        return(false);
                    }

                    offset -= FrameDescriptor.FRAME_ALIGNMENT;
                }

                LogBufferDescriptor.ApplyDefaultHeader(logMetaDataBuffer, termBuffer, gapOffset);
                FrameDescriptor.FrameType(termBuffer, gapOffset, HeaderFlyweight.HDR_TYPE_PAD);
                FrameDescriptor.FrameTermOffset(termBuffer, gapOffset);
                FrameDescriptor.FrameTermId(termBuffer, gapOffset, termId);
                FrameDescriptor.FrameLengthOrdered(termBuffer, gapOffset, gapLength);

                return(true);
            }
Пример #25
0
        public void ShouldFragmentMessageOverTwoFrames()
        {
            int          msgLength        = MaxPayloadLength + 1;
            int          headerLength     = _defaultHeader.Capacity;
            int          frameLength      = headerLength + 1;
            int          requiredCapacity = BitUtil.Align(headerLength + 1, FrameDescriptor.FRAME_ALIGNMENT) + MaxFrameLength;
            UnsafeBuffer buffer           = new UnsafeBuffer(new byte[msgLength]);
            int          tail             = 0;

            A.CallTo(() => _metaDataBuffer.GetAndAddLong(LogBufferDescriptor.TERM_TAIL_COUNTER_OFFSET, requiredCapacity)).Returns(TermAppender.Pack(TermID, tail));

            Assert.That(_termAppender.AppendFragmentedMessage(_headerWriter, buffer, 0, msgLength, MaxPayloadLength), Is.EqualTo((long)requiredCapacity));

            A.CallTo(() => _metaDataBuffer.GetAndAddLong(LogBufferDescriptor.TERM_TAIL_COUNTER_OFFSET, requiredCapacity)).MustHaveHappened()
            .Then(A.CallTo(() => _headerWriter.Write(_termBuffer, tail, MaxFrameLength, TermID)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutBytes(tail + headerLength, buffer, 0, MaxPayloadLength)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutByte(FrameDescriptor.FlagsOffset(tail), FrameDescriptor.BEGIN_FRAG_FLAG)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutIntOrdered(tail, MaxFrameLength)).MustHaveHappened())

            .Then(A.CallTo(() => _headerWriter.Write(_termBuffer, MaxFrameLength, frameLength, TermID)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutBytes(MaxFrameLength + headerLength, buffer, MaxPayloadLength, 1)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutByte(FrameDescriptor.FlagsOffset(MaxFrameLength), FrameDescriptor.END_FRAG_FLAG)).MustHaveHappened())
            .Then(A.CallTo(() => _termBuffer.PutIntOrdered(MaxFrameLength, frameLength)).MustHaveHappened());
        }
Пример #26
0
        public byte[] GetBuffer(ImageFormat imageFormat, string filename)
        {
            int rawImageSize = DetermineRawImageSize();

            byte[] buf = new byte[rawImageSize];

            Api.SetStreamState(m_hCamera, StreamState.Start);

            FrameDescriptor frameDesc = new FrameDescriptor();

            ReturnCode rc = Api.GetNextFrame(m_hCamera, buf.Length, buf, ref frameDesc);

            Api.SetStreamState(m_hCamera, StreamState.Stop);

            // How big a buffer do we need for the converted image?
            int destBufferSize = 0;

            rc = Api.FormatImage(buf, ref frameDesc, imageFormat, null, ref destBufferSize);
            byte[] dstBuf = new byte[destBufferSize];
            rc = Api.FormatImage(buf, ref frameDesc, imageFormat, dstBuf, ref destBufferSize);


            return(dstBuf);
        }
Пример #27
0
        /// <summary>
        /// Peek for new messages in a stream by scanning forward from an initial position. If new messages are found then
        /// they will be delivered to the <seealso cref="IControlledFragmentHandler"/> up to a limited position.
        ///
        /// Use a <seealso cref="ControlledFragmentAssembler"/> to assemble messages which span multiple fragments. Scans must also
        /// start at the beginning of a message so that the assembler is reset.
        ///
        /// </summary>
        /// <param name="initialPosition"> from which to peek forward. </param>
        /// <param name="handler"> to which message fragments are delivered. </param>
        /// <param name="limitPosition">   up to which can be scanned. </param>
        /// <returns> the resulting position after the scan terminates which is a complete message. </returns>
        /// <seealso cref="ControlledFragmentAssembler"/>
        /// <seealso cref="ImageControlledFragmentAssembler"/>
        public long ControlledPeek(long initialPosition, IControlledFragmentHandler handler, long limitPosition)
        {
            if (_isClosed)
            {
                return(initialPosition);
            }

            ValidatePosition(initialPosition);
            if (initialPosition >= limitPosition)
            {
                return(initialPosition);
            }

            int          initialOffset = (int)initialPosition & _termLengthMask;
            int          offset        = initialOffset;
            long         position      = initialPosition;
            UnsafeBuffer termBuffer    = ActiveTermBuffer(initialPosition);
            var          header        = _header;
            int          limitOffset   = (int)Math.Min(termBuffer.Capacity, (limitPosition - initialPosition) + offset);

            _header.Buffer = termBuffer;
            long resultingPosition = initialPosition;

            try
            {
                while (offset < limitOffset)
                {
                    int length = FrameDescriptor.FrameLengthVolatile(termBuffer, offset);
                    if (length <= 0)
                    {
                        break;
                    }

                    int frameOffset = offset;
                    offset += BitUtil.Align(length, FrameDescriptor.FRAME_ALIGNMENT);

                    if (FrameDescriptor.IsPaddingFrame(termBuffer, frameOffset))
                    {
                        position         += (offset - initialOffset);
                        initialOffset     = offset;
                        resultingPosition = position;

                        continue;
                    }

                    _header.Offset = frameOffset;


                    var action = handler.OnFragment(
                        termBuffer,
                        frameOffset + DataHeaderFlyweight.HEADER_LENGTH,
                        length - DataHeaderFlyweight.HEADER_LENGTH,
                        _header);

                    if (action == ControlledFragmentHandlerAction.ABORT)
                    {
                        break;
                    }

                    position     += (offset - initialOffset);
                    initialOffset = offset;

                    if ((_header.Flags & FrameDescriptor.END_FRAG_FLAG) == FrameDescriptor.END_FRAG_FLAG)
                    {
                        resultingPosition = position;
                    }

                    if (action == ControlledFragmentHandlerAction.BREAK)
                    {
                        break;
                    }
                }
            }
            catch (Exception t)
            {
                _errorHandler(t);
            }

            return(resultingPosition);
        }
Пример #28
0
        /// <summary>
        /// Poll for new messages in a stream. If new messages are found beyond the last consumed position then they
        /// will be delivered to the <seealso cref="IControlledFragmentHandler"/> up to a limited number of fragments as specified.
        ///
        /// Use a <see cref="ControlledFragmentAssembler"/>. to assemble messages which span multiple fragments.
        ///
        /// </summary>
        /// <param name="handler"> to which message fragments are delivered. </param>
        /// <param name="fragmentLimit">   for the number of fragments to be consumed during one polling operation. </param>
        /// <returns> the number of fragments that have been consumed. </returns>
        /// <seealso cref="ControlledFragmentAssembler" />
        /// <seealso cref="ImageControlledFragmentAssembler" />
        public int ControlledPoll(IControlledFragmentHandler handler, int fragmentLimit)
        {
            if (_isClosed)
            {
                return(0);
            }

            var fragmentsRead   = 0;
            var initialPosition = _subscriberPosition.Get();
            var initialOffset   = (int)initialPosition & _termLengthMask;
            var offset          = initialOffset;
            var termBuffer      = ActiveTermBuffer(initialPosition);
            int capacity        = termBuffer.Capacity;
            var header          = _header;

            header.Buffer = termBuffer;

            try
            {
                while (fragmentsRead < fragmentLimit && offset < capacity)
                {
                    var length = FrameDescriptor.FrameLengthVolatile(termBuffer, offset);
                    if (length <= 0)
                    {
                        break;
                    }

                    var frameOffset   = offset;
                    var alignedLength = BitUtil.Align(length, FrameDescriptor.FRAME_ALIGNMENT);
                    offset += alignedLength;

                    if (FrameDescriptor.IsPaddingFrame(termBuffer, frameOffset))
                    {
                        continue;
                    }

                    header.Offset = frameOffset;

                    var action = handler.OnFragment(
                        termBuffer,
                        frameOffset + DataHeaderFlyweight.HEADER_LENGTH,
                        length - DataHeaderFlyweight.HEADER_LENGTH,
                        header);

                    if (action == ControlledFragmentHandlerAction.ABORT)
                    {
                        offset -= alignedLength;
                        break;
                    }

                    ++fragmentsRead;

                    if (action == ControlledFragmentHandlerAction.BREAK)
                    {
                        break;
                    }
                    else if (action == ControlledFragmentHandlerAction.COMMIT)
                    {
                        initialPosition += (offset - initialOffset);
                        initialOffset    = offset;
                        _subscriberPosition.SetOrdered(initialPosition);
                    }
                }
            }
            catch (Exception t)
            {
                _errorHandler(t);
            }
            finally
            {
                long resultingPosition = initialPosition + (offset - initialOffset);
                if (resultingPosition > initialPosition)
                {
                    _subscriberPosition.SetOrdered(resultingPosition);
                }
            }

            return(fragmentsRead);
        }
Пример #29
0
 internal unsafe void ExpandDescriptor(OverflowFrameDescriptor *overflow, ref FrameDescriptor res)
 {
 }
Пример #30
0
 public void ShouldReportMaxMessageLength()
 {
     Assert.AreEqual(_publication.MaxMessageLength, FrameDescriptor.ComputeMaxMessageLength(LogBufferDescriptor.TERM_MIN_LENGTH));
 }
Пример #31
0
        /// <summary>
        /// Poll for new messages in a stream. If new messages are found beyond the last consumed position then they
        /// will be delivered to the <seealso cref="IControlledFragmentHandler"/> up to a limited number of fragments as specified or
        /// the maximum position specified.
        /// <para>
        /// Use a <seealso cref="IControlledFragmentHandler"/> to assemble messages which span multiple fragments.
        ///
        /// </para>
        /// </summary>
        /// <param name="fragmentHandler"> to which message fragments are delivered. </param>
        /// <param name="maxPosition">     to consume messages up to. </param>
        /// <param name="fragmentLimit">   for the number of fragments to be consumed during one polling operation. </param>
        /// <returns> the number of fragments that have been consumed. </returns>
        /// <seealso cref="ControlledFragmentAssembler"/>
        /// <seealso cref="ImageControlledFragmentAssembler"/>
        public virtual int BoundedControlledPoll(IControlledFragmentHandler fragmentHandler, long maxPosition,
                                                 int fragmentLimit)
        {
            if (_isClosed)
            {
                return(0);
            }

            var fragmentsRead   = 0;
            var initialPosition = _subscriberPosition.Get();
            var initialOffset   = (int)initialPosition & _termLengthMask;
            var resultingOffset = initialOffset;
            var termBuffer      = ActiveTermBuffer(initialPosition);
            var endOffset       = (int)Math.Min(termBuffer.Capacity, (maxPosition - initialPosition + initialOffset));

            _header.Buffer = termBuffer;

            try
            {
                while (fragmentsRead < fragmentLimit && resultingOffset < endOffset)
                {
                    int length = FrameDescriptor.FrameLengthVolatile(termBuffer, resultingOffset);
                    if (length <= 0)
                    {
                        break;
                    }

                    int frameOffset   = resultingOffset;
                    int alignedLength = BitUtil.Align(length, FrameDescriptor.FRAME_ALIGNMENT);
                    resultingOffset += alignedLength;

                    if (FrameDescriptor.IsPaddingFrame(termBuffer, frameOffset))
                    {
                        continue;
                    }

                    _header.Offset = frameOffset;

                    var action = fragmentHandler.OnFragment(termBuffer,
                                                            frameOffset + DataHeaderFlyweight.HEADER_LENGTH,
                                                            length - DataHeaderFlyweight.HEADER_LENGTH, _header);

                    if (action == ControlledFragmentHandlerAction.ABORT)
                    {
                        resultingOffset -= alignedLength;
                        break;
                    }

                    ++fragmentsRead;

                    if (action == ControlledFragmentHandlerAction.BREAK)
                    {
                        break;
                    }
                    else if (action == ControlledFragmentHandlerAction.COMMIT)
                    {
                        initialPosition += (resultingOffset - initialOffset);
                        initialOffset    = resultingOffset;
                        _subscriberPosition.SetOrdered(initialPosition);
                    }
                }
            }
            catch (Exception t)
            {
                _errorHandler(t);
            }
            finally
            {
                long resultingPosition = initialPosition + (resultingOffset - initialOffset);
                if (resultingPosition > initialPosition)
                {
                    _subscriberPosition.SetOrdered(resultingPosition);
                }
            }

            return(fragmentsRead);
        }