Ejemplo n.º 1
0
        public async Task <T> SendRequestAsync <T>(BaseRequest <T> request, CancellationToken token)
        {
            await clientLock.WaitAsync(token).ConfigureAwait(false);

            try
            {
                encoder.Reset();
                var marker = encoder.PrepareForLength();
                request.Encode(encoder)
                .WriteLength(marker);

                await stream.WriteAsync(buffer, 0, encoder.Offset, token).ConfigureAwait(false);

                if (request.ExpectResponse)
                {
                    var length = await ReceiveResponseAsync(token).ConfigureAwait(false);

                    var result = request.Decode(decoder);
                    return(result);
                }
                else
                {
                    return(default(T));
                }
            }
            finally
            {
                clientLock.Release();
            }
        }