Ejemplo n.º 1
0
        public override Task SendAsync(int correlationId, ReusableMemoryStream buffer, bool acknowledge)
        {
            if (_forceErrors)
            {
                var count = Interlocked.Increment(ref _count);

                if (count % 3 == 0)
                {
                    OnReceiveError(new SocketException((int)SocketError.Interrupted));
                    return(Task.FromResult(true));
                }

                if (count == 1 || count % 4 == 0)
                {
                    var tcs = new TaskCompletionSource <Void>();
                    tcs.SetException(new SocketException((int)SocketError.Interrupted));
                    return(tcs.Task);
                }
            }

            if (acknowledge)
            {
                var response = new ReusableMemoryStream(null);
                buffer.WriteTo(response);

                if (_responseDelayMs > 0)
                {
                    var tcs   = new TaskCompletionSource <bool>();
                    var timer = new Timer(_ =>
                    {
                        OnResponse(correlationId, response);
                        tcs.SetResult(true);
                    }, null, _responseDelayMs, -1);
                    _timers.Enqueue(timer);
                    return(tcs.Task);
                }

                OnResponse(correlationId, response);
            }
            return(Task.FromResult(true));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Compress a given stream using a given compression codec
        /// </summary>
        /// <param name="uncompressedStream"> The initial stream we want to compress</param>
        /// <param name="compressedStream"> The stream that want to put the compressed data in (should be empty before calling the method).</param>
        /// <param name="compression"> The compression we want to use.</param>
        /// <returns></returns>
        internal static void CompressStream(ReusableMemoryStream uncompressedStream,
                                            ReusableMemoryStream compressedStream, CompressionCodec compression)
        {
            if (compression == CompressionCodec.None)
            {
                throw new ArgumentException("Compress a stream only when you want compression.");
            }

            switch (compression)
            {
            case CompressionCodec.Gzip:
                using (var gzip = new GZipStream(compressedStream, CompressionMode.Compress, true))
                {
                    uncompressedStream.WriteTo(gzip);
                }

                break;

            case CompressionCodec.Lz4:
                KafkaLz4.Compress(compressedStream, uncompressedStream.GetBuffer(),
                                  (int)uncompressedStream.Length);
                break;

            case CompressionCodec.Snappy:
#if NETSTANDARD1_3
                throw new NotImplementedException();
#else
                compressedStream.SetLength(SnappyCodec.GetMaxCompressedLength((int)uncompressedStream.Length));
                {
                    int size = SnappyCodec.Compress(uncompressedStream.GetBuffer(), 0, (int)uncompressedStream.Length,
                                                    compressedStream.GetBuffer(), 0);
                    compressedStream.SetLength(size);
                }
#endif
                break;
            }
        }