示例#1
0
        /// <summary>
        /// Decode a byte[] that represents a collection of messages.
        /// </summary>
        /// <param name="reader">The reader</param>
        /// <param name="codec">The codec of the containing messageset, if any</param>
        /// <returns>Enumerable representing stream of messages decoded from byte[]</returns>
        public static IImmutableList <Message> ReadMessages(this IKafkaReader reader, MessageCodec codec = MessageCodec.None)
        {
            var expectedLength = reader.ReadInt32();

            if (!reader.HasBytes(expectedLength))
            {
                throw new BufferUnderRunException($"Message set size of {expectedLength} is not fully available (codec {codec}).");
            }

            var messages      = ImmutableList <Message> .Empty;
            var finalPosition = reader.Position + expectedLength;

            while (reader.Position < finalPosition)
            {
                // this checks that we have at least the minimum amount of data to retrieve a header
                if (reader.HasBytes(MessageHeaderSize) == false)
                {
                    break;
                }

                var offset      = reader.ReadInt64();
                var messageSize = reader.ReadInt32();

                // if the stream does not have enough left in the payload, we got only a partial message
                if (reader.HasBytes(messageSize) == false)
                {
                    throw new BufferUnderRunException($"Message header size of {MessageHeaderSize} is not fully available (codec {codec}).");
                }

                try {
                    messages = messages.AddRange(reader.ReadMessage(messageSize, offset));
                } catch (EndOfStreamException ex) {
                    throw new BufferUnderRunException($"Message size of {messageSize} is not available (codec {codec}).", ex);
                }
            }
            return(messages);
        }
示例#2
0
        /// <inheritdoc />
        public IMemberAssignment DecodeAssignment(IKafkaReader reader)
        {
            var expectedLength = reader.ReadInt32();

            if (!reader.HasBytes(expectedLength))
            {
                throw new BufferUnderRunException($"{ProtocolType} Assignment size of {expectedLength} is not fully available.");
            }

            if (expectedLength == 0)
            {
                return(null);
            }
            return(DecodeAssignment(reader, expectedLength));
        }
示例#3
0
        /// <inheritdoc />
        public IMemberMetadata DecodeMetadata(string assignmentStrategy, IKafkaReader reader)
        {
            var expectedLength = reader.ReadInt32();

            if (!reader.HasBytes(expectedLength))
            {
                throw new BufferUnderRunException($"{ProtocolType} Metadata size of {expectedLength} is not fully available.");
            }

            if (expectedLength == 0)
            {
                return(null);
            }
            return(DecodeMetadata(assignmentStrategy, reader, expectedLength));
        }