private void RespondMessage(TrackedStream source)
        {
            var correlationId = source.ReadInt32();

            if (!this.pendingRequests.TryRemove(correlationId, out var request))
            {
                source.DiscardRemainingData();
                return;
            }

            var message = (IResponse)Activator.CreateInstance(request.ResponseType) !;

            if (message is IResponseV2)
            {
                _ = source.ReadTaggedFields();
            }

            message.Read(source);

            if (source.Size != source.Position)
            {
                throw new Exception("Some data was not read from response");
            }

            request.CompletionSource.TrySetResult(message);
        }
        private async Task ListenStream()
        {
            while (!this.stopTokenSource.IsCancellationRequested)
            {
                try
                {
                    var messageSize = await this.WaitForMessageSizeAsync().ConfigureAwait(false);

                    if (messageSize <= 0)
                    {
                        continue;
                    }

                    using var tracked = new TrackedStream(this.stream, messageSize);
                    this.RespondMessage(tracked);
                }
                catch (OperationCanceledException)
                {
                    return;
                }
            }
        }
Beispiel #3
0
        public void Read(Stream source)
        {
            var size = source.ReadInt32();

            if (size == 0)
            {
                return;
            }

            using var tracked         = new TrackedStream(source, size);
            this.BaseOffset           = tracked.ReadInt64();
            this.BatchLength          = tracked.ReadInt32();
            this.PartitionLeaderEpoch = tracked.ReadInt32();
            this.Magic           = (byte)tracked.ReadByte();
            this.Crc             = tracked.ReadInt32();
            this.Attributes      = tracked.ReadInt16();
            this.LastOffsetDelta = tracked.ReadInt32();
            this.FirstTimestamp  = tracked.ReadInt64();
            this.MaxTimestamp    = tracked.ReadInt64();
            this.ProducerId      = tracked.ReadInt64();
            this.ProducerEpoch   = tracked.ReadInt16();
            this.BaseSequence    = tracked.ReadInt32();
            this.Records         = tracked.ReadArray <Record>();
            tracked.DiscardRemainingData();

            // The code below calculates the CRC32c
            // var size = source.ReadInt32();
            //
            // if (size == 0)
            //     return;
            //
            // var data = ArrayPool<byte>.Shared.Rent(size);
            //
            // try
            // {
            //     source.Read(data, 0, size);
            //     using var tmp = new MemoryStream(data, 0, size);
            //     this.BaseOffset = tmp.ReadInt64();
            //     this.BatchLength = tmp.ReadInt32();
            //     this.PartitionLeaderEpoch = tmp.ReadInt32();
            //     this.Magic = (byte) tmp.ReadByte();
            //     this.Crc = tmp.ReadInt32();
            //
            //     var crc = (int) Crc32CHash.Compute(
            //         data,
            //         (int)tmp.Position,
            //         this.BatchLength - 4 - 1 - 4);
            //
            //     if (crc != this.Crc)
            //     {
            //         throw new Exception("Corrupt message");
            //     }
            //
            //     this.Attributes = tmp.ReadInt16();
            //     this.LastOffsetDelta = tmp.ReadInt32();
            //     this.FirstTimestamp = tmp.ReadInt64();
            //     this.MaxTimestamp = tmp.ReadInt64();
            //     this.ProducerId = tmp.ReadInt64();
            //     this.ProducerEpoch = tmp.ReadInt16();
            //     this.BaseSequence = tmp.ReadInt32();
            //     this.Records = tmp.ReadArray<Record>();
            // }
            // finally
            // {
            //     ArrayPool<byte>.Shared.Return(data);
            // }
        }