public AppendRequest(String topic, Int32 partition, Message message) : base(RequestType.AppendRequest) { Topic = topic; Partition = partition; Messages = new List<Message>(1) { message }; }
public void WriteMessage(Message message) { _writer.Write(message.MessageLength); // 4 bytes signed integer value _writer.Write((byte) 1); // 1 byte "magic" number. Currently it is always '1'. _writer.Write(message.Crc); // 4 bytes CRC32 hash _writer.Write(message.Payload); // Actual message data }
/// <summary> /// Message that was correctly read, or null if end of stream found /// </summary> public Message ReadMessage() { try { // If this is the end of stream, return null if (_stream.Position == _stream.Length) return null; // Can we read 4 bytes? if (_stream.Position + 4 /* size of message length field */ > _stream.Length) return null; // Read 4 bytes that contains message length var messageLength = _reader.ReadInt32(); // If message length less or equlal zero - we probably read all messages if (messageLength <= 0) return null; // Can we read messageLength bytes? if (_stream.Position + messageLength > _stream.Length) return null; var message = new Message(); message.Magic = _reader.ReadByte(); message.Crc = _reader.ReadBytes(4); message.Payload = _reader.ReadBytes(Message.CalculatePayloadLength(messageLength)); // Validate message content message.Validate(); return message; } catch (EndOfStreamException) { return null; } }
/// <summary> /// Factory method for creating message with payload /// </summary> public static Message CreateMessage(byte[] payload) { using (var crc32 = new Crc32()) { var message = new Message(); message.Magic = 1; message.Payload = payload; message.Crc = crc32.ComputeHash(payload); return message; } }