Exemplo n.º 1
0
        // From : https://github.com/confluentinc/confluent-kafka-dotnet/issues/1307
        static string DecodeMemberAssignment(byte[] b)
        {
            /*
             * https://kafka.apache.org/protocol
             * STRING	Represents a sequence of characters. First the length N is given as an INT16. Then N bytes follow which are the UTF-8 encoding of the character sequence. Length must not be negative.
             * INT16	Represents an integer between -2^15 and 2^15-1 inclusive. The values are encoded using two bytes in network byte order (big-endian).
             * INT32	Represents an integer between -2^31 and 2^31-1 inclusive. The values are encoded using four bytes in network byte order (big-endian).
             * BYTES	Represents a raw sequence of bytes. First the length N is given as an INT32. Then N bytes follow.
             *
             * https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
             * MemberAssignment => Version PartitionAssignment
             * Version => int16
             * PartitionAssignment => [Topic [Partition]]
             *  Topic => string
             *  Partition => int32
             * UserData => bytes
             *
             * Note: [] probably denotes a sequence of same type items, begining with Int32 of the # of such items
             */

            UTF8Encoding  enc = new UTF8Encoding();
            StringBuilder s   = new StringBuilder();

            try
            {
                short version = SwapEndianness(BitConverter.ToInt16(b, 0));
                int   num_topic_assignments = SwapEndianness(BitConverter.ToInt32(b, 2));
                int   i = 6;
                for (int t = 0; t < num_topic_assignments; t++)
                {
                    short  topic_len = SwapEndianness(BitConverter.ToInt16(b, i));
                    byte[] str       = new byte[topic_len];
                    Array.Copy(b, i + 2, str, 0, topic_len);
                    string topic = enc.GetString(str);
                    i += (topic_len + 2);
                    int num_partition = SwapEndianness(BitConverter.ToInt32(b, i));
                    if (s.Length > 0)
                    {
                        s.Append($"; ");
                    }
                    s.Append($"{topic}: ");
                    for (int j = 0; j < num_partition; j++)
                    {
                        i += 4;
                        s.Append(
                            $"{SwapEndianness(BitConverter.ToInt32(b, i))}{(j < num_partition - 1 ? "," : "")}");
                    }
                }

                return(s.ToString());
            }
            catch
            {
                return("");
            }
        }
Exemplo n.º 2
0
        public void ReadableSwap_GivenUint_ReturnUintWithSwappedEndianness(uint value, uint expected)
        {
            // Arrange
            var swapEndianness = new SwapEndianness();

            // Act
            var result = swapEndianness.ReadableSwap(value);

            // Assert
            result.Should().Be(expected);
        }