/// <summary>
        /// Atomically tries to set the first value on the target field and returns
        /// false if its not null.
        /// </summary>
        /// <param name="field"></param>
        /// <param name="first"></param>
        /// <returns></returns>
        public static bool SetOnce(ref ISubscription field, ISubscription first)
        {
            for (;;)
            {
                ISubscription a = Volatile.Read(ref field);
                if (a == Cancelled)
                {
                    first?.Cancel();
                    return(false);
                }

                if (a != null)
                {
                    first?.Cancel();

                    OnSubscribeHelper.ReportSubscriptionSet();
                    return(false);
                }

                if (Interlocked.CompareExchange(ref field, first, null) == null)
                {
                    return(true);
                }
            }
        }
Example #2
0
        public void OnNext(T t)
        {
            if (done)
            {
                return;
            }

            bool b;

            try
            {
                b = predicate(t);
            }
            catch (Exception e)
            {
                done = true;
                s.Cancel();

                actual.OnError(e);
                return;
            }

            if (!b)
            {
                done = true;
                s.Cancel();
                actual.OnComplete();
            }

            actual.OnNext(t);
        }
Example #3
0
            public void OnNext(T t)
            {
                if (done)
                {
                    return;
                }

                long r = Volatile.Read(ref requested);
                long p = produced;

                if (p != r)
                {
                    produced = p + 1;
                    actual.OnNext(t);
                }
                else
                {
                    try
                    {
                        onDrop?.Invoke(t);
                    }
                    catch (Exception ex)
                    {
                        ExceptionHelper.ThrowIfFatal(ex);
                        s.Cancel();
                        OnError(ex);
                    }
                }
            }
Example #4
0
        public void OnNext(T t)
        {
            if (done)
            {
                return;
            }

            C b = buffer;

            if (b == null)
            {
                try
                {
                    b = bufferFactory();
                }
                catch (Exception e)
                {
                    done = true;
                    s.Cancel();

                    actual.OnError(e);
                    return;
                }
                buffer = b;
            }

            b.Add(t);

            if (b.Count == size)
            {
                buffer = default(C);
                actual.OnNext(b);
            }
        }
Example #5
0
 void OnTerminate()
 {
     if (Interlocked.Decrement(ref active) == 0)
     {
         upstream.Cancel();
     }
 }
Example #6
0
        /// <summary>
        /// Atomically set the given ISubscription on a null target or cancel it if the
        /// target is not empty or contains the Cancelled instance.
        /// </summary>
        /// <param name="current">The target field</param>
        /// <param name="s">The new value</param>
        /// <returns>True if successful</returns>
        public static bool SetOnce(ref ISubscription current, ISubscription s)
        {
            if (s == null)
            {
                ReportSubscriptionNull();
                return(false);
            }

            for (;;)
            {
                var a = Volatile.Read(ref current);
                if (a == Cancelled)
                {
                    s.Cancel();
                    return(false);
                }
                if (a != null)
                {
                    s.Cancel();
                    ReportSubscriptionSet();
                    return(false);
                }
                if (Interlocked.CompareExchange(ref current, s, null) == null)
                {
                    return(true);
                }
            }
        }
Example #7
0
        /// <summary>
        /// Atomically sets the single ISubscriber on the target field and requests the accumulated
        /// request amount.
        /// </summary>
        /// <param name="field"></param>
        /// <param name="requested"></param>
        /// <param name="s"></param>
        /// <returns>False if the target field contains the common Cancelled instance or is not null</returns>
        public static bool SingleSetSubscription(ref ISubscription field, ref long requested, ISubscription s)
        {
            ISubscription a = Volatile.Read(ref field);

            if (a == SubscriptionHelper.Cancelled)
            {
                s.Cancel();
                return(false);
            }

            a = Interlocked.CompareExchange(ref field, s, a);

            if (a == SubscriptionHelper.Cancelled)
            {
                s.Cancel();
                return(false);
            }

            if (a == null)
            {
                long r = Interlocked.Exchange(ref requested, 0);

                if (r != 0)
                {
                    s.Request(r);
                }

                return(true);
            }

            s.Cancel();
            OnSubscribeHelper.ReportSubscriptionSet();

            return(false);
        }
Example #8
0
 public void Cancel()
 {
     Volatile.Write(ref cancelled, true);
     upstream.Cancel();
     if (Interlocked.Increment(ref wip) == 1)
     {
         Clear();
     }
 }
Example #9
0
 public void OnNext(T t)
 {
     if (!queue.Offer(t))
     {
         s.Cancel();
         OnError(BackpressureHelper.MissingBackpressureException());
         return;
     }
     Schedule();
 }
Example #10
0
        public void OnComplete()
        {
            if (_subscription == null)
            {
                throw new InvalidOperationException("Cannot call OnComplete before OnSubscribe.");
            }

            _observer.OnCompleted();
            _subscription.Cancel();
        }
Example #11
0
        public void Cancel()
        {
            bp.Cancel();
            s.Cancel();
            worker.Dispose();

            if (bp.Enter())
            {
                q.Clear();
            }
        }
        public void OnNext(T t)
        {
            if (!sds.Value())
            {
                return;
            }

            s.Cancel();

            sds.Complete(false, actual);
        }
Example #13
0
            public void Cancel()
            {
                s.Cancel();
                d.Dispose();
                worker.Dispose();

                if (QueueDrainHelper.Enter(ref wip))
                {
                    queue.Clear();
                    buffer = null;
                }
            }
Example #14
0
            public void OnNext(T t)
            {
                if (done)
                {
                    return;
                }
                K key;

                V value;

                try
                {
                    key = keySelector(t);

                    value = valueSelector(t);
                }
                catch (Exception ex)
                {
                    ExceptionHelper.ThrowIfFatal(ex);
                    s.Cancel();
                    OnError(ex);
                    return;
                }

                GroupUnicast g;
                bool         newGroup = false;

                lock (this)
                {
                    if (!groups.TryGetValue(key, out g))
                    {
                        if (Volatile.Read(ref cancelled) != 0)
                        {
                            return;
                        }

                        Interlocked.Increment(ref groupCount);
                        g = new GroupUnicast(this, key, prefetch);
                        groups.Add(key, g);
                        newGroup = true;
                    }
                }


                g.OnNext(value);

                if (newGroup)
                {
                    queue.Offer(g);
                    Drain();
                }
            }
Example #15
0
 public void OnNext(T t)
 {
     if (sourceMode == FuseableHelper.NONE)
     {
         if (!queue.Offer(t))
         {
             s.Cancel();
             OnError(BackpressureHelper.MissingBackpressureException("Queue is full?!"));
             return;
         }
     }
     Schedule();
 }
Example #16
0
 void InnerDone()
 {
     if (Interlocked.Decrement(ref wip) == 0)
     {
         s.Cancel();
     }
 }
Example #17
0
 public void OnNext(T t)
 {
     hasValue = true;
     value    = t;
     s.Cancel();
     cde.Signal();
 }
        public async Task RequestAndReplies()
        {
            var theOptions = new OptionsWrapper <RabbitMQOptions>(RabbitMQOptions.Default);

            var thePublisher = new RabbitMQIntegrationRequestPublisher(theOptions);

            // Request handler registrieren
            var theServiceCollection = new ServiceCollection();

            theServiceCollection.AddIntegrationRequestHandler(typeof(MyRequestHandler));
            ServiceProvider theServiceProvider = theServiceCollection.BuildServiceProvider();

            var theSubscriber = new RabbitMQIntegrationRequestSubscriber(theOptions, theServiceProvider);

            ISubscription theSubscription = await theSubscriber.SubscribeAsync <MyRequest, MyReply>(topic : "MyTopic");

            // Einen Request senden
            MyReply theReply =
                await thePublisher.PublishAsync <MyRequest, MyReply>(new MyRequest { Topic = "MyTopic", Number = 5 });

            Assert.That(theReply.DoubledNumber, Is.EqualTo(10));

            theSubscription.Cancel();
            theSubscriber.Dispose();

            thePublisher.Dispose();
        }
Example #19
0
 public void OnNext(T element)
 {
     if (hasValue)
     {
         try
         {
             T v = reducer(value, element);
             if (v == null)
             {
                 throw new NullReferenceException("The reducer returned a null value");
             }
             value = v;
         }
         catch (Exception ex)
         {
             upstream.Cancel();
             OnError(ex);
             return;
         }
     }
     else
     {
         hasValue = true;
         value    = element;
     }
 }
        public async Task Timeout()
        {
            var theOptions = new OptionsWrapper <RabbitMQOptions>(RabbitMQOptions.Default);

            var thePublisher = new RabbitMQIntegrationRequestPublisher(theOptions);

            // Request handler registrieren
            var theServiceCollection = new ServiceCollection();

            theServiceCollection.AddIntegrationRequestHandler(typeof(MyDelayRequestHandler));
            ServiceProvider theServiceProvider = theServiceCollection.BuildServiceProvider();

            var theSubscriber = new RabbitMQIntegrationRequestSubscriber(theOptions, theServiceProvider);

            ISubscription theSubscription = await theSubscriber.SubscribeAsync <MyRequest, MyReply>();

            // Einen Request senden
            Assert.ThrowsAsync <TimeoutException>(async() => {
                await thePublisher.PublishAsync <MyRequest, MyReply>(new MyRequest {
                    Number = 5
                }, timeoutSeconds: 1);
            });

            theSubscription.Cancel();
            theSubscriber.Dispose();

            thePublisher.Dispose();
        }
Example #21
0
        public void OnNext(T t)
        {
            if (done)
            {
                return;
            }

            bool b;

            try {
                b = predicate(t);
            }
            catch (Exception e)
            {
                done = true;
                s.Cancel();

                actual.OnError(e);
                return;
            }

            if (b)
            {
                actual.OnNext(t);
            }
            else
            {
                s.Request(1);
            }
        }
        public void OnNext(T t)
        {
            if (done)
            {
                return;
            }

            K k;

            try
            {
                k = keyExtractor(t);
            }
            catch (Exception ex)
            {
                done = true;
                s.Cancel();

                actual.OnError(ex);
                return;
            }
            if (set.Add(k))
            {
                actual.OnNext(t);
            }
            else
            {
                s.Request(1);
            }
        }
Example #23
0
            public void Cancel()
            {
                if (lvCancelled())
                {
                    return;
                }
                Volatile.Write(ref cancelled, true);
                s.Cancel();
                CancelAll();

                if (QueueDrainHelper.Enter(ref wip))
                {
                    var sq = Volatile.Read(ref scalarQueue);
                    sq?.Clear();
                }
            }
        public void OnNext(T t)
        {
            if (done)
            {
                return;
            }

            if (skipping)
            {
                bool b;
                try
                {
                    b = predicate(t);
                }
                catch (Exception e)
                {
                    done = true;
                    s.Cancel();

                    actual.OnError(e);
                    return;
                }
                if (b)
                {
                    s.Request(1);
                    return;
                }

                skipping = false;
            }

            actual.OnNext(t);
        }
Example #25
0
        public virtual void OnSubscribe(ISubscription subscription)
        {
            // As per rule 2.13, we need to throw a `ArgumentNullException` if the `Subscription` is `null`
            if (subscription == null)
            {
                throw new ArgumentNullException(nameof(subscription));
            }

            if (_subscription != null)
            // If someone has made a mistake and added this Subscriber multiple times, let's handle it gracefully
            {
                try
                {
                    subscription.Cancel(); // Cancel the additional subscription
                }
                catch (Exception ex)
                {
                    //Subscription.cancel is not allowed to throw an exception, according to rule 3.15
                    System.Diagnostics.Trace.TraceError(
                        new IllegalStateException(
                            subscription +
                            " violated the Reactive Streams rule 3.15 by throwing an exception from cancel.", ex)
                        .StackTrace);
                }
            }
            else
            {
                // We have to assign it locally before we use it, if we want to be a synchronous `Subscriber`
                // Because according to rule 3.10, the Subscription is allowed to call `onNext` synchronously from within `request`
                _subscription = subscription;
            }
        }
Example #26
0
 public void OnSubscribe(ISubscription s)
 {
     if (Volatile.Read(ref subscribers) == Terminated)
     {
         s.Cancel();
     }
 }
Example #27
0
        internal static bool SetOnce(ref ISubscription subscription, ISubscription next, bool crash = true)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }
            ISubscription current = Interlocked.CompareExchange(ref subscription, next, null);

            if (current == null)
            {
                return(true);
            }
            next?.Cancel();
            if (current != Cancelled)
            {
                var e = new InvalidOperationException("ISubscription already set!");
                if (crash)
                {
                    throw e;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine(e.ToString());
                }
            }
            return(false);
        }
Example #28
0
 public void Cancel()
 {
     if (Interlocked.CompareExchange(ref once, 1, 0) == 0)
     {
         parent.Remove(connection);
         upstream.Cancel();
     }
 }
Example #29
0
 public override void AroundPostStop()
 {
     _state.Remove(Self);
     if (!_canceled)
     {
         _subscription?.Cancel();
     }
     base.AroundPostStop();
 }
 public void OnSubscribe(ISubscription subscription)
 {
     _environment.Debug($"WhiteboxSubscriber.OnSubscribe({subscription})");
     if (_subscription.IsCompleted())
     {
         subscription.Cancel(); // the Probe must also pass subscriber verification
     }
     _probe.RegisterOnSubscribe(new Subscription(subscription));
 }