Пример #1
0
        public IEnumerable <StoreRecord> EnumerateMessages(string key, long version, int count)
        {
            var records = _appendOnlyStore.ReadRecords(key, version, count);

            foreach (var record in records)
            {
                using (var mem = new MemoryStream(record.Data))
                {
                    // drop attributes
                    var attribs    = _serializer.ReadAttributes(mem);
                    var eventCount = _serializer.ReadCompactInt(mem);
                    var objects    = new object[eventCount];
                    for (int i = 0; i < eventCount; i++)
                    {
                        objects[i] = _serializer.ReadMessage(mem);
                    }
                    yield return(new StoreRecord(key, objects, record.StoreVersion, record.StreamVersion));
                }
            }
        }
Пример #2
0
        public ImmutableEnvelope ReadAsEnvelopeData(byte[] buffer)
        {
            using (var mem = new MemoryStream(buffer))
            {
                var signature = MessageSerializer.ReadCompactInt(mem);
                if (Signature != signature)
                {
                    throw new IOException("Signature bytes mismatch in envelope");
                }

                var frame = StorageFramesEvil.ReadFrame(mem);

                using (var mem1 = new MemoryStream(frame.Bytes))
                {
                    var attributes = MessageSerializer.ReadAttributes(mem1);
                    var item       = MessageSerializer.ReadMessage(mem1);
                    var created    = new DateTime(frame.Stamp, DateTimeKind.Utc);
                    return(new ImmutableEnvelope(frame.Name, created, item, attributes));
                }
            }
        }
Пример #3
0
        protected override async Task Run(CancellationToken cancellationToken)
        {
            Stream stream = null;

            try
            {
                var buffer = Encoding.UTF8.GetBytes(NetworkState.Ok.ToString());
                await Send(cancellationToken, _client.WriteStream, buffer);

                using (var bufferStream = await ToMemoryStream(cancellationToken, _client.ReadStream))
                    _message = await _messageSerializer.ReadMessage <TMessage>(bufferStream);

                if (Message != null && _client.WriteStream.CanWrite)
                {
                    buffer = Encoding.UTF8.GetBytes(NetworkState.Ok.ToString());
                    await Send(cancellationToken, _client.WriteStream, buffer);
                }
                if (IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }
                //TODO:  Check this for proper work
                var streaming = _message as IStreamingMessage;
                if (streaming != null)
                {
                    ulong allReaded = 0;
                    RaiseReport(new ProgressInfo <TMessage>(Message, streaming.StreamLength, allReaded));
                    buffer = new byte[2048];
                    stream = RaiseGetStream(Message) ?? streaming.Stream;
                    do
                    {
                        if (IsCancellationRequested)
                        {
                            throw new OperationCanceledException();
                        }
                        var readed = await _client.ReadStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);

                        await stream.WriteAsync(buffer, 0, readed, cancellationToken);

                        allReaded += (ulong)readed;
                        await stream.FlushAsync(cancellationToken);

                        RaiseReport(new ProgressInfo <TMessage>(Message, streaming.StreamLength, allReaded));
                    } while (allReaded < streaming.StreamLength);
                }
                RaiseSuccess(Message);
            }
            catch (OperationCanceledException)
            {
                RaiseCancelled(Message);
            }
            catch (Exception ex)
            {
                RaiseCatch(ex);
            }

            if (stream != null)
            {
                stream.Dispose();
            }
            await _client.DisconnectAsync();

            _client.Dispose();
        }