Beispiel #1
0
        /// <summary>
        /// Parse the response bytes.
        /// </summary>
        /// <param name="response">The bytes of the response.</param>
        public override void Parse(byte[] response)
        {
            // ----------------------------------------------\\
            // ListGroupsResponse => ErrorCode Groups
            //  ErrorCode => int16
            //  Groups => [GroupId ProtocolType]
            //      GroupId => string
            //      ProtocolType => string
            // ----------------------------------------------\\

            using (var stream = new MemoryStream(response))
            {
                var reader = new BinaryReader(stream);

                // errorCode
                var errorCodeBytes = new byte[2];
                reader.Read(errorCodeBytes, 0, 2);
                this.ErrorCode = KafkaProtocolPrimitiveType.GetInt16(errorCodeBytes);

                // [groups]
                var groupSizeBytes = new byte[4];
                reader.Read(groupSizeBytes, 0, 4);
                var numOfGroups = KafkaProtocolPrimitiveType.GetInt32(groupSizeBytes);
                this.Groups = new KafkaProtocolConsumerListGroupResponseGroupInfo[numOfGroups];

                for (int i = 0; i < numOfGroups; i++)
                {
                    this.Groups[i] = new KafkaProtocolConsumerListGroupResponseGroupInfo();

                    // groupId : 2bytes , string
                    var groupIdSizeBytes = new byte[2];
                    reader.Read(groupIdSizeBytes, 0, 2);
                    var groupIdSize  = KafkaProtocolPrimitiveType.GetInt16(groupIdSizeBytes);
                    var groupIdBytes = new byte[groupIdSize];
                    reader.Read(groupIdBytes, 0, groupIdSize);
                    this.Groups[i].GroupId = Encoding.UTF8.GetString(groupIdBytes);

                    // protocolType : 2bytes , string
                    var protocolSizeBytes = new byte[2];
                    reader.Read(protocolSizeBytes, 0, 2);
                    var protocolSize  = KafkaProtocolPrimitiveType.GetInt16(protocolSizeBytes);
                    var protocolBytes = new byte[protocolSize];
                    reader.Read(protocolBytes, 0, protocolSize);
                    this.Groups[i].ProtocolType = Encoding.UTF8.GetString(protocolBytes);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Parse the response in bytes.
        /// </summary>
        /// <param name="response">The bytes of the response.</param>
        public override void Parse(byte[] response)
        {
            //-------------------------------------------------------------------\\
            // MetadataResponse => [Broker][TopicMetadata]
            //  Broker => NodeId Host Port  (any number of brokers may be returned)
            //    NodeId => int32
            //    Host => string
            //    Port => int32
            //  TopicMetadata => TopicErrorCode TopicName [PartitionMetadata]
            //    TopicErrorCode => int16
            //  PartitionMetadata => PartitionErrorCode PartitionId Leader Replicas Isr
            //    PartitionErrorCode => int16
            //    PartitionId => int32
            //    Leader => int32
            //    Replicas => [int32]
            //    Isr => [int32]
            //-------------------------------------------------------------------\\

            using (var stream = new MemoryStream(response))
            {
                var reader = new BinaryReader(stream);

                // response body
                // [broker]
                var brokersSizeBytes = new byte[4];
                reader.Read(brokersSizeBytes, 0, 4);
                var numOfBrokers = KafkaProtocolPrimitiveType.GetInt32(brokersSizeBytes);
                this.BrokerInfos = new KafkaProtocolTopicMetadataResponseBrokerInfo[numOfBrokers];
                for (int i = 0; i < numOfBrokers; i++)
                {
                    // brokerId , 4bytes , Int32
                    var brokerIdBytes = new byte[4];
                    reader.Read(brokerIdBytes, 0, 4);
                    var brokerId = KafkaProtocolPrimitiveType.GetInt32(brokerIdBytes);

                    // host , string
                    var hostSizeBytes = new byte[2];
                    reader.Read(hostSizeBytes, 0, 2);
                    var hostSize  = KafkaProtocolPrimitiveType.GetInt16(hostSizeBytes);
                    var hostBytes = new byte[hostSize];
                    reader.Read(hostBytes, 0, hostSize);
                    var hostName = Encoding.UTF8.GetString(hostBytes, 0, hostSize);

                    // port, 4bytes, int32
                    var portBytes = new byte[4];
                    reader.Read(portBytes, 0, 4);
                    var portNum = KafkaProtocolPrimitiveType.GetInt32(portBytes);

                    this.BrokerInfos[i] = new KafkaProtocolTopicMetadataResponseBrokerInfo()
                    {
                        Host   = hostName,
                        NodeId = brokerId,
                        Port   = portNum
                    };
                }

                // [topicmetadata]
                var topicMetadataSizeBytes = new byte[4];
                reader.Read(topicMetadataSizeBytes, 0, 4);
                var numOfTopicMetadatas = KafkaProtocolPrimitiveType.GetInt32(topicMetadataSizeBytes);
                this.TopicInfos = new KafkaProtocolTopicMetadataResponseTopicInfo[numOfTopicMetadatas];
                for (int i = 0; i < numOfTopicMetadatas; i++)
                {
                    this.TopicInfos[i] = new KafkaProtocolTopicMetadataResponseTopicInfo();

                    // errorCode, 2bytes, int16
                    var topicErrorCodeBytes = new byte[2];
                    reader.Read(topicErrorCodeBytes, 0, 2);
                    var errorCode = KafkaProtocolPrimitiveType.GetInt16(topicErrorCodeBytes);
                    this.TopicInfos[i].TopicErrorCode = errorCode;

                    // topicname, string
                    var topicNameSizeBytes = new byte[2];
                    reader.Read(topicNameSizeBytes, 0, 2);
                    var topicNameSize  = KafkaProtocolPrimitiveType.GetInt16(topicNameSizeBytes);
                    var topicNameBytes = new byte[topicNameSize];
                    reader.Read(topicNameBytes, 0, topicNameSize);
                    var topicName = Encoding.UTF8.GetString(topicNameBytes);
                    this.TopicInfos[i].TopicName = topicName;

                    // [partitionmetadata]
                    var partitionMetadataSizeBytes = new byte[4];
                    reader.Read(partitionMetadataSizeBytes, 0, 4);
                    var numOfPartitionMetadatas = KafkaProtocolPrimitiveType.GetInt32(partitionMetadataSizeBytes);
                    this.TopicInfos[i].PartitionInfos = new KafkaProtocolTopicMetadataResponsePartitionInfo[numOfPartitionMetadatas];
                    for (int j = 0; j < numOfPartitionMetadatas; j++)
                    {
                        this.TopicInfos[i].PartitionInfos[j] = new KafkaProtocolTopicMetadataResponsePartitionInfo();

                        // errorCode
                        var partitionErrorCodeBytes = new byte[2];
                        reader.Read(partitionErrorCodeBytes, 0, 2);
                        var partitionErrorCode = KafkaProtocolPrimitiveType.GetInt16(partitionErrorCodeBytes);
                        this.TopicInfos[i].PartitionInfos[j].PartitionErrorCode = partitionErrorCode;

                        // partitionId
                        var partitionIdBytes = new byte[4];
                        reader.Read(partitionIdBytes, 0, 4);
                        var partitionId = KafkaProtocolPrimitiveType.GetInt32(partitionIdBytes);
                        this.TopicInfos[i].PartitionInfos[j].PartitionId = partitionId;

                        // leader
                        var leaderBytes = new byte[4];
                        reader.Read(leaderBytes, 0, 4);
                        var leaderId = KafkaProtocolPrimitiveType.GetInt32(leaderBytes);
                        this.TopicInfos[i].PartitionInfos[j].Leader = leaderId;

                        // [replica]
                        var replicaSizeBytes = new byte[4];
                        reader.Read(replicaSizeBytes, 0, 4);
                        var numOfReplicas = KafkaProtocolPrimitiveType.GetInt32(replicaSizeBytes);
                        this.TopicInfos[i].PartitionInfos[j].Replicas = new int[numOfReplicas];
                        for (int k = 0; k < numOfReplicas; k++)
                        {
                            var replicaIdBytes = new byte[4];
                            reader.Read(replicaIdBytes, 0, 4);
                            var replicaId = KafkaProtocolPrimitiveType.GetInt32(replicaIdBytes);
                            this.TopicInfos[i].PartitionInfos[j].Replicas[k] = replicaId;
                        }

                        // [Isr]
                        var isrSizeBytes = new byte[4];
                        reader.Read(isrSizeBytes, 0, 4);
                        var numOfIsrs = KafkaProtocolPrimitiveType.GetInt32(isrSizeBytes);
                        this.TopicInfos[i].PartitionInfos[j].Isr = new int[numOfIsrs];
                        for (int k = 0; k < numOfIsrs; k++)
                        {
                            var isrIdBytes = new byte[4];
                            reader.Read(isrIdBytes, 0, 4);
                            var isrId = KafkaProtocolPrimitiveType.GetInt32(isrIdBytes);
                            this.TopicInfos[i].PartitionInfos[j].Isr[k] = isrId;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Parse the response bytes.
        /// </summary>
        /// <param name="response">The bytes of the response.</param>
        public override void Parse(byte[] response)
        {
            using (var stream = new MemoryStream(response))
            {
                var reader = new BinaryReader(stream);

                var sizeBytes = new byte[4];
                reader.Read(sizeBytes, 0, 4);
                var numOfGroupInfos = KafkaProtocolPrimitiveType.GetInt32(sizeBytes);

                this.GroupInfos = new KafkaProtocolConsumerDescribeGroupsResponseGroupInfo[numOfGroupInfos];
                for (int i = 0; i < numOfGroupInfos; i++)
                {
                    GroupInfos[i] = new KafkaProtocolConsumerDescribeGroupsResponseGroupInfo();

                    // error code
                    var errorCodeBytes = new byte[2];
                    reader.Read(errorCodeBytes, 0, 2);
                    GroupInfos[i].ErrorCode = KafkaProtocolPrimitiveType.GetInt16(errorCodeBytes);

                    // groupId
                    var groupIdSizeBytes = new byte[2];
                    reader.Read(groupIdSizeBytes, 0, 2);
                    var groupIdSize  = KafkaProtocolPrimitiveType.GetInt16(groupIdSizeBytes);
                    var groupIdBytes = new byte[groupIdSize];
                    reader.Read(groupIdBytes, 0, groupIdSize);
                    GroupInfos[i].GroupId = Encoding.UTF8.GetString(groupIdBytes);

                    // state
                    var stateSizeBytes = new byte[2];
                    reader.Read(stateSizeBytes, 0, 2);
                    var stateSize  = KafkaProtocolPrimitiveType.GetInt16(stateSizeBytes);
                    var stateBytes = new byte[stateSize];
                    reader.Read(stateBytes, 0, stateSize);
                    GroupInfos[i].State = Encoding.UTF8.GetString(stateBytes);

                    // protocol type
                    var protocolTypeSizeBytes = new byte[2];
                    reader.Read(protocolTypeSizeBytes, 0, 2);
                    var protocolTypeSize  = KafkaProtocolPrimitiveType.GetInt16(protocolTypeSizeBytes);
                    var protocolTypeBytes = new byte[protocolTypeSize];
                    reader.Read(protocolTypeBytes, 0, protocolTypeSize);
                    GroupInfos[i].ProtocolType = Encoding.UTF8.GetString(protocolTypeBytes);

                    // protocol
                    var protocolSizeBytes = new byte[2];
                    reader.Read(protocolSizeBytes, 0, 2);
                    var protocolSize  = KafkaProtocolPrimitiveType.GetInt16(protocolSizeBytes);
                    var protocolBytes = new byte[protocolSize];
                    reader.Read(protocolBytes, 0, protocolSize);
                    GroupInfos[i].Protocol = Encoding.UTF8.GetString(protocolBytes);

                    // [Members]
                    var membersSizeBytes = new byte[4];
                    reader.Read(membersSizeBytes, 0, 4);
                    var numOfMembers = KafkaProtocolPrimitiveType.GetInt32(membersSizeBytes);
                    GroupInfos[i].Members = new KafkaProtocolConsumerDescribeGroupsResponseMemberInfo[numOfMembers];
                    for (int j = 0; j < numOfMembers; j++)
                    {
                        GroupInfos[i].Members[j] = new KafkaProtocolConsumerDescribeGroupsResponseMemberInfo();

                        // memberid
                        var memberIdSizeBytes = new byte[2];
                        reader.Read(memberIdSizeBytes, 0, 2);
                        var memberIdSize = KafkaProtocolPrimitiveType.GetInt16(memberIdSizeBytes);
                        var memberBytes  = new byte[memberIdSize];
                        reader.Read(memberBytes, 0, memberIdSize);
                        GroupInfos[i].Members[j].MemberId = Encoding.UTF8.GetString(memberBytes);

                        // client id
                        var clientIdSizeBytes = new byte[2];
                        reader.Read(clientIdSizeBytes, 0, 2);
                        var clientIdSize  = KafkaProtocolPrimitiveType.GetInt16(clientIdSizeBytes);
                        var clientIdBytes = new byte[clientIdSize];
                        reader.Read(clientIdBytes, 0, clientIdSize);
                        GroupInfos[i].Members[j].ClientId = Encoding.UTF8.GetString(clientIdBytes);

                        // client host
                        var clientHostSizeBytes = new byte[2];
                        reader.Read(clientHostSizeBytes, 0, 2);
                        var clientHostSize  = KafkaProtocolPrimitiveType.GetInt16(clientHostSizeBytes);
                        var clientHostBytes = new byte[clientHostSize];
                        reader.Read(clientHostBytes, 0, clientHostSize);
                        GroupInfos[i].Members[j].ClientHost = Encoding.UTF8.GetString(clientHostBytes);

                        // member metadata
                        var memberMetadataSizeBytes = new byte[4];
                        reader.Read(memberMetadataSizeBytes, 0, 4);
                        var memberMetadataSize  = KafkaProtocolPrimitiveType.GetInt32(memberMetadataSizeBytes);
                        var memberMetadataBytes = new byte[memberMetadataSize];
                        reader.Read(memberMetadataBytes, 0, memberMetadataSize);
                        GroupInfos[i].Members[j].MemberMetadata = memberMetadataBytes;

                        // member assignment
                        var memberAssignmentSizeBytes = new byte[4];
                        reader.Read(memberAssignmentSizeBytes, 0, 4);
                        var memberAssignmentSize  = KafkaProtocolPrimitiveType.GetInt32(memberAssignmentSizeBytes);
                        var memberAssignmentBytes = new byte[memberAssignmentSize];
                        reader.Read(memberAssignmentBytes, 0, memberAssignmentSize);
                        GroupInfos[i].Members[j].MemberAssignment = memberAssignmentBytes;
                    }
                }
            }
        }