예제 #1
0
파일: MessageTests.cs 프로젝트: Bny12/kafka
        public void ParseFromValid()
        {
            Crc32 crc32 = new Crc32();

            string payload = "kafka";
            byte magic = 0;
            byte[] payloadData = Encoding.UTF8.GetBytes(payload);
            byte[] payloadSize = BitConverter.GetBytes(payloadData.Length);
            byte[] checksum = crc32.ComputeHash(payloadData);
            byte[] messageData = new byte[payloadData.Length + 1 + payloadSize.Length + checksum.Length];

            Buffer.BlockCopy(payloadSize, 0, messageData, 0, payloadSize.Length);
            messageData[4] = magic;
            Buffer.BlockCopy(checksum, 0, messageData, payloadSize.Length + 1, checksum.Length);
            Buffer.BlockCopy(payloadData, 0, messageData, payloadSize.Length + 1 + checksum.Length, payloadData.Length);

            Message message = Message.ParseFrom(messageData);

            Assert.IsNotNull(message);
            Assert.AreEqual(magic, message.Magic);
            Assert.IsTrue(payloadData.SequenceEqual(message.Payload));
            Assert.IsTrue(checksum.SequenceEqual(message.Checksum));
        }
예제 #2
0
파일: Message.cs 프로젝트: thaingo/kafka
 /// <summary>
 /// Calculates the CRC32 checksum on the payload of the message.
 /// </summary>
 /// <returns>The checksum given the payload.</returns>
 private byte[] CalculateChecksum()
 {
     Crc32 crc32 = new Crc32();
     return crc32.ComputeHash(Payload);
 }
예제 #3
0
 /// <summary>
 /// Calculates the CRC32 checksum on the payload of the message.
 /// </summary>
 /// <returns>The checksum given the payload.</returns>
 private byte[] CalculateChecksum()
 {
     Crc32 crc32 = new Crc32();
     return crc32.ComputeHash(GetMessageBytesWithoutCrc());
 }