Exemplo n.º 1
0
        private async Task StartReceive(CancellationToken token)
        {
            EnumeratorResult <ChunkedStream> res;

            while ((res = await _client.ReceiveAsync(token)).Success)
            {
                using (res.Value)
                    using (var reader = new MemoryStreamReader())
                    {
                        var response = ProtoBuf.Serializer.Deserialize <ResponseMessageModel>(res.Value);
                        var items    = new CacheValue <Tk, T> [response.Messages.Count];
                        var idx      = 0;

                        foreach (var message in response.Messages)
                        {
                            reader.SetBuffer(message.Key);
                            reader.SetLength(message.Key.Length);
                            var key = ProtoBuf.Serializer.Deserialize <Tk>(reader);
                            reader.SetBuffer(message.Value);
                            reader.SetLength(message.Value.Length);
                            var val = ProtoBuf.Serializer.Deserialize <T>(reader);
                            items[idx++] = new CacheValue <Tk, T>(key, val, message.ExpiredAtSeconds);
                        }
                        _cache.AddRange(items);
                    }
            }
        }