private TcpPackage CreateSubscriptionPackage()
 {
     var dto = new ClientMessage.SubscribeToStream(_streamId, _resolveLinkTos);
     return new TcpPackage(TcpCommand.SubscribeToStream,
                           _userCredentials != null ? TcpFlags.Authenticated : TcpFlags.None,
                           _correlationId,
                           _userCredentials != null ? _userCredentials.Username : null,
                           _userCredentials != null ? _userCredentials.Password : null,
                           dto.Serialize());
 }
        public Task<EventStoreSubscription> Subscribe(string streamId, bool resolveLinkTos, Action<ResolvedEvent> eventAppeared, Action subscriptionDropped)
        {
            var id = Guid.NewGuid();

            var eventStoreSubscription = new EventStoreSubscription(id, streamId, this, eventAppeared, subscriptionDropped);
            var subscriptionTaskPair = new SubscriptionTaskPair(eventStoreSubscription);
            if (!_subscriptions.TryAdd(id, subscriptionTaskPair))
                throw new Exception("Failed to add subscription. Concurrency failure.");

            var subscribe = new ClientMessage.SubscribeToStream(streamId, resolveLinkTos);
            var pkg = new TcpPackage(TcpCommand.SubscribeToStream, id, subscribe.Serialize());
            _connection.EnqueueSend(pkg.AsByteArray());

            return subscriptionTaskPair.TaskCompletionSource.Task;
        }
예제 #3
0
        public Task Subscribe(string stream, Action<RecordedEvent, Position> eventAppeared, Action subscriptionDropped)
        {
            var id = Guid.NewGuid();
            var source = new TaskCompletionSource<object>();

            if (_subscriptions.TryAdd(id, new Subscription(source, id, stream, eventAppeared, subscriptionDropped)))
            {
                var subscribe = new ClientMessage.SubscribeToStream(stream);
                var pkg = new TcpPackage(TcpCommand.SubscribeToStream, id, subscribe.Serialize());
                _connection.EnqueueSend(pkg.AsByteArray());
            }
            else
            {
                source.SetException(new Exception("Failed to add subscription. Concurrency failure."));
            }

            return source.Task;
        }