예제 #1
0
        public static int Write(this IKafkaWriter writer, IEnumerable <Message> messages, MessageCodec codec)
        {
            if (codec == MessageCodec.None)
            {
                using (writer.MarkForLength()) {
                    writer.Write(messages);
                }
                return(0);
            }
            using (var messageWriter = new KafkaWriter()) {
                messageWriter.Write(messages);
                var messageSet = messageWriter.ToSegment(false);

                using (writer.MarkForLength()) {             // messageset
                    writer.Write(0L);                        // offset
                    using (writer.MarkForLength()) {         // message
                        using (writer.MarkForCrc()) {
                            writer.Write((byte)0)            // message version
                            .Write((byte)codec)              // attribute
                            .Write(-1);                      // key  -- null, so -1 length
                            using (writer.MarkForLength()) { // value
                                var initialPosition = writer.Position;
                                writer.WriteCompressed(messageSet, codec);
                                var compressedMessageLength = writer.Position - initialPosition;
                                return(messageSet.Count - compressedMessageLength);
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
 public void WriteTo(IKafkaWriter writer)
 {
     using (writer.MarkForCrc()) {
         writer.Write(MessageVersion)
         .Write((byte)0);
         if (MessageVersion >= 1)
         {
             writer.Write(Timestamp.GetValueOrDefault(DateTimeOffset.UtcNow).ToUnixTimeMilliseconds());
         }
         writer.Write(Key)
         .Write(Value);
     }
 }
예제 #3
0
 /// <summary>
 /// Encodes a message object
 /// </summary>
 /// <param name="writer">The writer</param>
 /// <param name="message">Message data to encode.</param>
 /// <param name="includeLength">Whether to include the length at the start</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 IKafkaWriter Write(this IKafkaWriter writer, Message message, bool includeLength = true)
 {
     using (includeLength ? writer.MarkForLength() : Disposable.None) {
         using (writer.MarkForCrc()) {
             writer.Write(message.MessageVersion)
             .Write(message.Attribute);
             if (message.MessageVersion >= 1)
             {
                 writer.Write(message.Timestamp.GetValueOrDefault(DateTime.UtcNow).ToUnixEpochMilliseconds());
             }
             writer.Write(message.Key)
             .Write(message.Value);
         }
     }
     return(writer);
 }