public void EnsureMessageEncodeAndDecodeAreCompatible(string key, string value) { var testMessage = new Message(key: key, value: value); var buffer = new byte[1024]; var encoder = new KafkaEncoder(buffer); Message.EncodeMessage(testMessage, encoder); var decoder = new KafkaDecoder(buffer); var result = Message.DecodeMessage(0, 0, decoder, encoder.Offset); Assert.That(testMessage.Key, Is.EqualTo(result.Key)); Assert.That(testMessage.Value, Is.EqualTo(result.Value)); }
public void DecodeMessageShouldThrowWhenCrcFails() { Assert.Throws(Is.TypeOf<FailCrcCheckException>(), () => { var testMessage = new Message(value: "kafka test message.", key: "test"); var buffer = new byte[1024]; var encoder = new KafkaEncoder(buffer); Message.EncodeMessage(testMessage, encoder); buffer[0] += 1; var decoder = new KafkaDecoder(buffer, 0, encoder.Offset); var result = Message.DecodeMessage(0, 0, decoder, encoder.Offset); }); }
public static byte[] CreateMessage(long offset, byte[] key, byte[] payload, byte magicByte = 0, byte attributes = 0) { var message = new Message { Attribute = attributes, MagicNumber = magicByte, Key = key, Value = payload }; var buffer = new byte[1024]; var encoder = new KafkaEncoder(buffer); Message.EncodeMessageSet(encoder, new[] { message }); var result = new byte[encoder.Offset]; Array.Copy(encoder.Buffer, result, encoder.Offset); return result; }
/// <summary> /// Decode messages from a payload and assign it a given kafka offset. /// </summary> /// <param name="offset">The offset represting the log entry from kafka of this message.</param> /// <param name="payload">The byte[] encode as a message from kafka.</param> /// <returns>The message</returns> /// <remarks>The return type is an Enumerable as the message could be a compressed message set.</remarks> internal static Message DecodeMessage(long offset, int partitionId, KafkaDecoder decoder, int messageSize) { var crc = decoder.ReadUInt32(); var calculatedCrc = Crc32Provider.Compute(decoder.Buffer, decoder.Offset, messageSize - 4); if (calculatedCrc != crc) { throw new FailCrcCheckException("Payload did not match CRC validation."); } var message = new Message { Meta = new MessageMetadata(offset, partitionId), MagicNumber = decoder.ReadByte(), Attribute = decoder.ReadByte(), Key = decoder.ReadBytes(), }; var codec = (MessageCodec)(ProtocolConstants.AttributeCodeMask & message.Attribute); switch (codec) { case MessageCodec.CodecNone: message.Value = decoder.ReadBytes(); break; default: throw new NotSupportedException(string.Format("Codec type of {0} is not supported.", codec)); } return message; }
/// <summary> /// Encodes a message object to byte[] /// </summary> /// <param name="message">Message data to encode.</param> /// <returns>Encoded byte[] representation of the message object.</returns> /// <remarks> /// Format: /// Crc (Int32), MagicByte (Byte), Attribute (Byte), Key (Byte[]), Value (Byte[]) /// </remarks> internal static KafkaEncoder EncodeMessage(Message message, KafkaEncoder encoder) { var marker = encoder.PrepareForCrc(); encoder .Write(message.MagicNumber) .Write(message.Attribute) .Write(message.Key) .Write(message.Value) .CalculateCrc(marker); return encoder; }