private void ReadPage(StreamRevision startRevision) { Guid correlationId = Guid.NewGuid(); _bus.Publish(new ClientMessage.ReadStreamEventsForward( correlationId, correlationId, new ContinuationEnvelope(OnMessage, _semaphore, _cancellationToken), _streamName, startRevision.ToInt64(), (int)Math.Min(ReadBatchSize, _maxCount), _resolveLinks, _requiresLeader, default, _user, expires: _deadline));
private void ReadPage(StreamRevision startRevision, Func <Message, CancellationToken, Task> onMessage) { Guid correlationId = Guid.NewGuid(); Log.Verbose( "Subscription {subscriptionId} to {streamName} reading next page starting from {nextRevision}.", _subscriptionId, _streamName, startRevision); _bus.Publish(new ClientMessage.ReadStreamEventsForward( correlationId, correlationId, new ContinuationEnvelope(onMessage, _semaphore, _cancellationToken), _streamName, startRevision.ToInt64(), ReadBatchSize, _resolveLinks, _requiresLeader, default,
private void ReadPage(StreamRevision startRevision, ulong readCount = 0) { Guid correlationId = Guid.NewGuid(); _bus.Publish(new ClientMessage.ReadStreamEventsForward( correlationId, correlationId, new ContinuationEnvelope(OnMessage, _semaphore, _cancellationToken), _streamName, startRevision.ToInt64(), (int)Math.Min(ReadBatchSize, _maxCount), _resolveLinks, _requiresLeader, null, _user, expires: _deadline)); async Task OnMessage(Message message, CancellationToken ct) { if (message is ClientMessage.NotHandled notHandled && RpcExceptions.TryHandleNotHandled(notHandled, out var ex)) { _channel.Writer.TryComplete(ex); return; } if (!(message is ClientMessage.ReadStreamEventsForwardCompleted completed)) { _channel.Writer.TryComplete( RpcExceptions.UnknownMessage <ClientMessage.ReadStreamEventsForwardCompleted>(message)); return; } switch (completed.Result) { case ReadStreamResult.Success: var nextStreamPosition = (ulong)completed.NextEventNumber; foreach (var @event in completed.Events) { if (readCount >= _maxCount) { await _channel.Writer.WriteAsync(new ReadResp { StreamPosition = new() { LastStreamPosition = (ulong)completed.LastEventNumber, NextStreamPosition = nextStreamPosition } }, ct).ConfigureAwait(false); _channel.Writer.TryComplete(); return; } await _channel.Writer.WriteAsync(new ReadResp { Event = ConvertToReadEvent(_uuidOption, @event), }, ct).ConfigureAwait(false); nextStreamPosition = (ulong)@event.OriginalEvent.EventNumber; readCount++; }
private void ReadPage(StreamRevision startRevision, ulong readCount = 0) { Guid correlationId = Guid.NewGuid(); _bus.Publish(new ClientMessage.ReadStreamEventsForward( correlationId, correlationId, new ContinuationEnvelope(OnMessage, _semaphore, _cancellationToken), _streamName, startRevision.ToInt64(), (int)Math.Min(ReadBatchSize, _maxCount), _resolveLinks, _requiresLeader, null, _user, expires: _deadline)); async Task OnMessage(Message message, CancellationToken ct) { if (message is ClientMessage.NotHandled notHandled && RpcExceptions.TryHandleNotHandled(notHandled, out var ex)) { _channel.Writer.TryComplete(ex); return; } if (!(message is ClientMessage.ReadStreamEventsForwardCompleted completed)) { _channel.Writer.TryComplete( RpcExceptions.UnknownMessage <ClientMessage.ReadStreamEventsForwardCompleted>(message)); return; } switch (completed.Result) { case ReadStreamResult.Success: if (readCount == 0 && _compatibility >= 1) { if (completed.Events.Length == 0) { var firstStreamPosition = StreamRevision.FromInt64(completed.NextEventNumber); if (startRevision != firstStreamPosition) { await _channel.Writer.WriteAsync(new() { FirstStreamPosition = firstStreamPosition }, ct).ConfigureAwait(false); } } } foreach (var @event in completed.Events) { if (readCount >= _maxCount) { break; } await _channel.Writer.WriteAsync(new() { Event = ConvertToReadEvent(_uuidOption, @event) }, ct).ConfigureAwait(false); readCount++; } if (!completed.IsEndOfStream && readCount < _maxCount) { ReadPage(StreamRevision.FromInt64(completed.NextEventNumber), readCount); return; } if (_compatibility >= 1) { await _channel.Writer.WriteAsync(new() { LastStreamPosition = StreamRevision.FromInt64(completed.LastEventNumber) }, ct).ConfigureAwait(false); } _channel.Writer.TryComplete(); return; case ReadStreamResult.NoStream: await _channel.Writer.WriteAsync(new() { StreamNotFound = new() { StreamIdentifier = _streamName } }, _cancellationToken).ConfigureAwait(false); _channel.Writer.TryComplete(); return; case ReadStreamResult.StreamDeleted: _channel.Writer.TryComplete(RpcExceptions.StreamDeleted(_streamName)); return; case ReadStreamResult.AccessDenied: _channel.Writer.TryComplete(RpcExceptions.AccessDenied()); return; default: _channel.Writer.TryComplete(RpcExceptions.UnknownError(completed.Result)); return; } } }