/// <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> public static byte[] EncodeMessage(Message message) { var body = new WriteByteStream(); body.Pack(new[] { message.MagicNumber }, new[] { message.Attribute }, message.Key.ToIntPrefixedBytes(), message.Value.ToIntPrefixedBytes()); var crc = Crc32Provider.ComputeHash(body.Payload()); body.Prepend(crc); return(body.Payload()); }
/// <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>Enumerable representing stream of messages decoded from byte[].</returns> /// <remarks>The return type is an Enumerable as the message could be a compressed message set.</remarks> public static IEnumerable <Message> DecodeMessage(long offset, byte[] payload) { var crc = payload.Take(4).ToArray(); var stream = new ReadByteStream(payload.Skip(4)); var hash = Crc32Provider.ComputeHash(stream.Payload); if (crc.SequenceEqual(hash) == false) { throw new FailCrcCheckException("Payload did not match CRC validation."); } var message = new Message { Meta = new MessageMetadata { Offset = offset }, MagicNumber = stream.ReadByte(), Attribute = stream.ReadByte(), Key = stream.ReadIntPrefixedBytes() }; var codec = (MessageCodec)(ProtocolConstants.AttributeCodeMask & message.Attribute); switch (codec) { case MessageCodec.CodecNone: message.Value = stream.ReadIntPrefixedBytes(); yield return(message); break; case MessageCodec.CodecGzip: var gZipData = stream.ReadIntPrefixedBytes(); foreach (var m in DecodeMessageSet(Compression.Unzip(gZipData))) { yield return(m); } break; default: throw new NotSupportedException(string.Format("Codec type of {0} is not supported.", codec)); } }