Esempio n. 1
0
        public async Task <EnumeratorResult <ChunkedStream> > MoveNextAsync(CancellationToken token)
        {
            var message = _streamFactory.Create();

            try
            {
                while (true)
                {
                    var buffer = message.StartWrite();
                    var res    = await _receive(buffer, token);

                    if (res.IsClosed)
                    {
                        break;
                    }
                    message.EndWrite(buffer, res.Count);

                    if (res.EndOfMessage)
                    {
                        var rval = EnumeratorResult.CreateSuccessed(message);
                        message = null;
                        return(rval);
                    }
                }
            }
            finally
            {
                message?.Dispose();
            }

            return(EnumeratorResult.CreateFailed <ChunkedStream>());
        }
Esempio n. 2
0
        private async Task StartBatching(CancellationToken token)
        {
            using (var buff = new MemoryStream())
                while (await _batchBlock.OutputAvailableAsync(token))
                {
                    if (!_batchBlock.TryReceive(out var chunk))
                    {
                        continue;
                    }
                    var keys = new byte[chunk.Length][];

                    for (var i = 0; i < chunk.Length; i++)
                    {
                        buff.SetLength(0);
                        ProtoBuf.Serializer.Serialize(buff, chunk[i]);
                        keys[i] = buff.ToArray();
                    }
                    var message = new RequestMessageModel()
                    {
                        Keys = keys
                    };
                    using (var send = _streamFactory.Create())
                    {
                        ProtoBuf.Serializer.Serialize(send, message);
                        await _client.SendAsync(send, token);
                    }
                }
        }
Esempio n. 3
0
        private static async Task Send(IInteract context, BufferBlock <ResponseMessageModel> messages, CancellationToken token)
        {
            while (await messages.OutputAvailableAsync(token))
            {
                using (var stream = _streamFactory.Create())
                {
                    var message = await messages.ReceiveAsync(token);

                    ProtoBuf.Serializer.Serialize(stream, message);
                    await context.Send(stream, token);
                }
            }
        }