Exemple #1
0
        public async ValueTask SendAsync(
            TSendPayload payload,
            CancellationToken cancellationToken = default)
        {
            var buffer = new MemoryStream();

            await using var _ = buffer.ConfigureAwait(false);
            var writer = new KafkaWriter(buffer);

            await using (writer.ConfigureAwait(false))
            {
                await payload
                .WriteToAsync(writer, cancellationToken)
                .ConfigureAwait(false);
            }

            var lengthBuffer = new MemoryStream();

            await using (lengthBuffer
                         .ConfigureAwait(false))
            {
                var lengthWriter = new KafkaWriter(lengthBuffer);
                await using (lengthWriter.ConfigureAwait(false))
                {
                    await lengthWriter
                    .WriteInt32Async(Int32.From((int)buffer.Length), cancellationToken)
                    .ConfigureAwait(false);
                }

                await _networkClient.SendAsync(
                    lengthBuffer
                    .GetBuffer()
                    .AsMemory()
                    .Slice(0, (int)lengthBuffer.Length),
                    cancellationToken)
                .ConfigureAwait(false);
            }

            await _networkClient.SendAsync(
                buffer.GetBuffer().AsMemory()
                .Slice(0, (int)buffer.Length),
                cancellationToken)
            .ConfigureAwait(false);
        }