Example #1
0
        private static async Task <bool> CopyToQueue <T>(this IIterator <T> source, AsyncQueue <T> destination, int count, CancellationToken cancel, bool complete = false)
        {
            try
            {
                for (; count > 0; --count)
                {
                    if (!await source.MoveNext(cancel).ConfigureAwait(false))
                    {
                        if (complete)
                        {
                            destination.OnCompleted();
                        }
                        return(false);
                    }
                    await destination.OnNext(source.Current, cancel).ConfigureAwait(false);
                }

                if (complete)
                {
                    destination.OnCompleted();
                }
            }
            catch (Exception e)
            {
                destination.OnError(e);
                throw;
            }

            return(true);
        }
Example #2
0
        private void FlushCallQueue(Exception e)
        {
            // mark queue as completed to ensure no further inserts can happen
            callQueue.OnCompleted();

            // cancel all other calls
            var remainingCalls = callQueue.Flush();

            if (callQueue.Current != null)
            {
                remainingCalls.Add(callQueue.Current);
            }

            foreach (var call in remainingCalls)
            {
                if (e != null)
                {
                    call.Tcs.TrySetException(e);
                }
                else
                {
                    call.Tcs.TrySetCanceled();
                }
            }
        }
Example #3
0
 public ShareSource(IIterator <T> sourceIterator, int maxQueueLength)
 {
     this.SourceIterator = sourceIterator;
     this.Queue          = new AsyncQueue <T>(maxQueueLength);
     this.readTask       = Read();
     readTask.ContinueWith(t =>
     {
         if (t.IsFaulted)
         {
             Queue.OnError(t.Exception);
         }
         else if (t.IsCompleted)
         {
             Queue.OnCompleted();
         }
         else if (t.IsCanceled)
         {
             Queue.Dispose();
         }
     });
 }
Example #4
0
        async Task BindToSources(IIterator <ISequence <T> > source, CancellationToken cancel)
        {
            try
            {
                while (await source.MoveNext(cancel).ConfigureAwait(false))
                {
                    await Enter(cancel).ConfigureAwait(false);

                    Tuple <IIterator <T>, Task> entry;

                    var iterator = source.Current.Start(source.Context);
                    var readTask = Read(iterator, cancel);

                    lock (readers)
                    {
                        entry = Tuple.Create(iterator, readTask);
                        this.readers.Add(entry);
                    }

                    var t = readTask.Finally(() =>
                    {
                        lock (readers)
                        {
                            StopReader(entry.Item1);
                            readers.Remove(entry);
                        }
                        Leave();
                    });
                }

                await Task.WhenAll(this.ReaderTasks);

                queue.OnCompleted();
            }
            catch (Exception e)
            {
                queue.OnError(e);       // pass exception on to outer iterator
                throw;
            }
        }
Example #5
0
 public void Dispose()
 {
     queue.OnCompleted();
     publishLoopTask.WhenCompleted().Wait();
 }