Esempio n. 1
0
        public bool Read(XmlReader reader)
        {
            if(reader.IsStartElement() && reader.Name == "Fixture") {
                Fixture fixture = new Fixture();

                //...Any attributes go here...
                fixture.AllowFrameSkip = bool.Parse(reader.GetAttribute("allowFrameSkip"));
                fixture.Name = reader.GetAttribute("name");
                // This needs to hold off until after channels are loaded.
                string fixtureDefinitionName = reader.GetAttribute("fixtureDefinitionName");

                if(reader.ElementsExistWithin("Fixture")) { // Entity element
                    // Channels
                    if(reader.ElementsExistWithin("Channels")) { // Container element for child entity
                        ChannelReader<OutputChannel> channelReader = new ChannelReader<OutputChannel>();
                        while(channelReader.Read(reader)) {
                            fixture.InsertChannel(channelReader.Channel);
                        }
                        reader.ReadEndElement(); // Channels
                    }

                    // With channels loaded, the fixture template reference can be set.
                    fixture.FixtureDefinitionName = fixtureDefinitionName;

                    reader.ReadEndElement(); // Fixture

                    this.Fixture = fixture;
                }
                return true;
            }
            return false;
        }
Esempio n. 2
0
File: Taxi.cs Progetto: vonwenm/Jibu
 // The constructor taking the input info channel and the output acknowledge channel.
 public Taxi(ChannelReader<Tuple<int, int, string>> info, ChannelWriter<string> ack)
 {
     customerInfoChannel = info;
     ackChannel = ack;
     timer = new Timer();           
     rand = new Random();
 }
Esempio n. 3
0
 public Dispatch(ChannelReader<Tuple<int, int, string>> customer, int taxis)
 {
     customerChan = customer;
     ackChannel = new Channel<string>();
     taxiChan = new Channel<Tuple<int, int, string>>();
     numTaxis = vacantTaxis = taxis;            
 }
Esempio n. 4
0
 protected override void ReadBody(XmlReader reader)
 {
     // Channels
     if(reader.ElementsExistWithin("Channels")) { // Container element for child entity
         ChannelReader<OutputChannel> channelReader = new ChannelReader<OutputChannel>();
         while(channelReader.Read(reader)) {
             InsertChannel(channelReader.Channel);
         }
         reader.ReadEndElement(); // Channels
     }
 }
Esempio n. 5
0
 public AsyncEnumerableImplChannelThrows(ChannelReader <T> inner)
 {
     _inner = inner;
 }
Esempio n. 6
0
 public async Task StreamDontRead(ChannelReader <string> source)
 {
     while (await source.WaitToReadAsync())
     {
     }
 }
Esempio n. 7
0
 /// <summary>
 /// Channel reader, executing the function once data is available in channel
 /// </summary>
 /// <param name="reader">The channel reader</param>
 /// <param name="cancellationToken">Cancellation token indicating the host is shutting down</param>
 /// <param name="logger">Logger</param>
 protected abstract Task ReaderAsync(ChannelReader <KafkaEventData[]> reader, CancellationToken cancellationToken, ILogger logger);
Esempio n. 8
0
 public Consumer(ChannelReader <string> reader, HardWorker worker)
 {
     _reader = reader;
     _worker = worker;
 }
Esempio n. 9
0
        // ConcurrentTransform

        public static async Task ConcurrentTransform <TIn, TOut>(
            this ChannelReader <TIn> reader,
            ChannelWriter <TOut> writer,
            Func <TIn, TOut> transformer,
            int concurrencyLevel = -1,
            ChannelCompletionMode channelCompletionMode = ChannelCompletionMode.CompleteAndPropagateError,
            CancellationToken cancellationToken         = default)
        {
            if (concurrencyLevel < 0)
            {
                concurrencyLevel = HardwareInfo.GetProcessorCountFactor();
            }
            var       semaphore = new SemaphoreSlim(concurrencyLevel, concurrencyLevel);
            Exception?error     = null;

            async Task Worker()
            {
                try {
                    for (;;)
                    {
                        await semaphore !.WaitAsync(cancellationToken).ConfigureAwait(false);
                        try {
                            if (!await reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false))
                            {
                                break;
                            }
                            if (!reader.TryRead(out var item))
                            {
                                continue;
                            }
                            var newItem = transformer.Invoke(item);
                            await writer.WriteAsync(newItem, cancellationToken).ConfigureAwait(false);
                        }
                        finally {
                            semaphore.Release();
                        }
                    }
                }
                catch (OperationCanceledException) {
                    throw;
                }
                catch (Exception e) {
                    if (channelCompletionMode == ChannelCompletionMode.CompleteAndPropagateError)
                    {
                        error = e;
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            var workers = new Task[concurrencyLevel];

            for (var i = 0; i < concurrencyLevel; i++)
            {
                workers[i] = Task.Run(Worker, cancellationToken);
            }
            await Task.WhenAll(workers).ConfigureAwait(false);

            if ((channelCompletionMode & ChannelCompletionMode.Complete) != 0)
            {
                writer.TryComplete(error);
            }
        }
Esempio n. 10
0
 public Consumer(ChannelReader <string> reader, int identifier, int delay)
 {
     _reader     = reader;
     _identifier = identifier;
     _delay      = delay;
 }
Esempio n. 11
0
        private async Task Run(ChannelReader <BlockGraph> reader)
        {
            while (await reader.WaitToReadAsync())
            {
                while (reader.TryRead(out var data))
                {
                    var entries = new Entry[data.Deps.Count];
                    GraphMutex.WaitOne();
                    var max   = data.Block.Round;
                    var round = Round;
                    for (var i = 0; i < data.Deps.Count; i++)
                    {
                        var dep = data.Deps[i];

                        Debug.WriteLine($"Dep: block.id={dep.Block}");

                        var depMax = dep.Block.Round;
                        var rcheck = false;
                        var e      = new Entry(dep.Block, dep.Prev);
                        if (dep.Block.Round != 1)
                        {
                            e.Deps    = new BlockID[dep.Deps.Count + 1];
                            e.Deps[0] = dep.Prev;
                            Array.Copy(dep.Deps.ToArray(), 0, e.Deps, 1, dep.Deps.Count);
                            var prevMax = (ulong)0;
                            if (Max.ContainsKey(dep.Prev))
                            {
                                prevMax = Max[dep.Prev];
                                rcheck  = true;
                            }
                            else if (prevMax > depMax)
                            {
                                depMax = prevMax;
                            }
                        }
                        else
                        {
                            e.Deps = dep.Deps.ToArray();
                        }
                        entries[i] = e;
                        foreach (var link in dep.Deps)
                        {
                            if (!Max.ContainsKey(link))
                            {
                                rcheck = true;
                            }
                            else
                            {
                                var linkMax = Max[link];
                                if (linkMax > depMax)
                                {
                                    depMax = linkMax;
                                }
                            }
                        }
                        if (rcheck && round > depMax)
                        {
                            depMax = round;
                        }
                        Max[dep.Block] = depMax;
                        if (depMax > max)
                        {
                            max = depMax;
                        }
                    }
                    var rcheck2 = false;
                    if (data.Block.Round != 1)
                    {
                        if (!Max.ContainsKey(data.Prev))
                        {
                            rcheck2 = true;
                        }
                        else
                        {
                            var pmax = Max[data.Prev];
                            if (pmax > max)
                            {
                                max = pmax;
                            }
                        }
                    }
                    if (rcheck2 && round > max)
                    {
                        max = round;
                    }
                    Max[data.Block] = max;
                    Blocks.Add(new BlockInfo(data, max));
                    GraphMutex.ReleaseMutex();
                    foreach (var e in entries)
                    {
                        Process(e);
                    }
                    var self = new Entry(data.Block, data.Prev)
                    {
                        Deps = new BlockID[data.Deps.Count + 1]
                    };
                    self.Deps[0] = data.Prev;
                    for (var i = 0; i < data.Deps.Count; i++)
                    {
                        var dep = data.Deps[i];
                        self.Deps[i + 1] = dep.Block;
                    }
                    Process(self);
                }
            }
        }
Esempio n. 12
0
 public FilteringChannelReader(ChannelReader <T> source, Func <T, bool> predicate)
 {
     _source    = source ?? throw new ArgumentNullException(nameof(source));
     _predicate = predicate ?? throw new ArgumentNullException(nameof(predicate));
     Contract.EndContractBlock();
 }
Esempio n. 13
0
        public async ValueTask Init(ChannelReader <byte[]> outboundChan, ILocalAdapter localAdapter, CancellationToken cancellationToken = default)
        {
            if (localAdapter.Destination.TransportProtocol == TransportProtocol.Udp)
            {
                throw UdpNotSupportedException;
            }
            var dev         = NetworkInformation.GetInternetConnectionProfile().NetworkAdapter;
            var connectTask = socket.ConnectAsync(new HostName(server), port.ToString(), SocketProtectionLevel.PlainSocket, dev).AsTask(cancellationToken).ConfigureAwait(false);

            var destination    = localAdapter.Destination;
            var dstPort        = destination.Port;
            var dstPortStrSize = CountDigit(dstPort);
            var dstHostStrSize = destination.Host.Size;
            int headerLen      = HEADER1.Length + dstHostStrSize + 1 + dstPortStrSize + HEADER2.Length;
            var firstSeg       = sendArrayPool.Rent(headerLen);

            try
            {
                HEADER1.CopyTo(firstSeg, 0);
                destination.Host.CopyTo(firstSeg.AsSpan(HEADER1.Length));
                firstSeg[HEADER1.Length + dstHostStrSize] = (byte)':';
                while (dstPortStrSize-- > 0)
                {
                    firstSeg[HEADER1.Length + dstHostStrSize + 1 + dstPortStrSize] = (byte)(dstPort % 10 + '0');
                    dstPort /= 10;
                }
                HEADER2.CopyTo(firstSeg, headerLen - HEADER2.Length);

                // Connect and perform handshake
                await connectTask;
                inputStream  = socket.InputStream;
                outputStream = socket.OutputStream;
                await outputStream.WriteAsync(firstSeg.AsBuffer(0, headerLen)).AsTask(cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                sendArrayPool.Return(firstSeg);
            }
            var responseBuf = sendArrayPool.Rent(HEAD_BUFFER_LEN);

            try
            {
                var resBuf = await inputStream.ReadAsync(responseBuf.AsBuffer(), HEAD_BUFFER_LEN, InputStreamOptions.Partial).AsTask(cancellationToken).ConfigureAwait(false);

                uint responseLen = resBuf.Length;
                if (responseLen < 14)
                {
                    throw new InvalidOperationException("Remote response too short");
                }
                if ((responseBuf[9] == (byte)'2') && (responseBuf[10] == (byte)'0') && (responseBuf[11] == (byte)'0'))
                {
                    // 200 objk
                }
                else
                {
                    var code = 100 * (responseBuf[9] - '0') + 10 * (responseBuf[10] - '0') + responseBuf[11] - '0';
                    throw new InvalidOperationException("Remote status code: " + code.ToString());
                }
                bool foundHeader = false;
                for (int headerStart = 12; headerStart < responseLen - 3; headerStart++)
                {
                    if (responseBuf[headerStart] == '\r')
                    {
                        if (responseBuf[headerStart + 1] == '\n')
                        {
                            if (responseBuf[headerStart + 2] == '\r')
                            {
                                if (responseBuf[headerStart + 3] == '\n')
                                {
                                    foundHeader = true;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (!foundHeader)
                {
                    throw new InvalidOperationException("Unrecognized remote header: " + Encoding.UTF8.GetString(responseBuf, 0, (int)responseLen));
                }
            }
            finally
            {
                sendArrayPool.Return(responseBuf);
            }
            // Initial data?
        }
Esempio n. 14
0
 public Consumer(ChannelReader <T> reader)
 {
     _reader = reader;
 }
 public IConsumerManager Create(ChannelReader <string> reader)
 {
     return(new ConsumerManager(2, reader, EmbeddedFileUtils.GetExcludedWords()));
 }
Esempio n. 16
0
 public static IAsyncEnumerator <object?> MakeAsyncEnumeratorFromChannel <T>(ChannelReader <T> channel, CancellationToken cancellationToken = default)
 {
     return(new ChannelAsyncEnumerator <T>(channel, cancellationToken));
 }
Esempio n. 17
0
        public void ReadWriteVariations(
            Func <ChannelOptions, Channel <int> > channelCreator,
            ChannelOptions options,
            Func <ChannelReader <int>, Task <bool> > readDelegate,
            Func <ChannelWriter <int>, int, Task> writeDelegate)
        {
            Channel <int>         channel        = channelCreator(options);
            ChannelReader <int>   reader         = channel.Reader;
            ChannelWriter <int>   writer         = channel.Writer;
            BoundedChannelOptions boundedOptions = options as BoundedChannelOptions;
            bool shouldReadAllWrittenValues      = boundedOptions == null || boundedOptions.FullMode == BoundedChannelFullMode.Wait;

            List <Task> taskList = new List <Task>();

            int readerTasksCount;
            int writerTasksCount;

            if (options.SingleReader)
            {
                readerTasksCount = 1;
                writerTasksCount = options.SingleWriter ? 1 : MaxTaskCounts - 1;
            }
            else if (options.SingleWriter)
            {
                writerTasksCount = 1;
                readerTasksCount = MaxTaskCounts - 1;
            }
            else
            {
                readerTasksCount = MaxTaskCounts / 2;
                writerTasksCount = MaxTaskCounts - readerTasksCount;
            }

            int readCount = 0;

            for (int i = 0; i < readerTasksCount; i++)
            {
                taskList.Add(Task.Run(async delegate
                {
                    try
                    {
                        while (true)
                        {
                            if (!await readDelegate(reader))
                            {
                                break;
                            }
                            Interlocked.Increment(ref readCount);
                        }
                    }
                    catch (ChannelClosedException)
                    {
                    }
                }));
            }

            int numberToWriteToQueue = -1;
            int remainingWriters     = writerTasksCount;

            for (int i = 0; i < writerTasksCount; i++)
            {
                taskList.Add(Task.Run(async delegate
                {
                    int num = Interlocked.Increment(ref numberToWriteToQueue);
                    while (num < MaxNumberToWriteToChannel)
                    {
                        await writeDelegate(writer, num);
                        num = Interlocked.Increment(ref numberToWriteToQueue);
                    }

                    if (Interlocked.Decrement(ref remainingWriters) == 0)
                    {
                        writer.Complete();
                    }
                }));
            }

            Task.WaitAll(taskList.ToArray());

            if (shouldReadAllWrittenValues)
            {
                Assert.Equal(MaxNumberToWriteToChannel, readCount);
            }
            else
            {
                Assert.InRange(readCount, 0, MaxNumberToWriteToChannel);
            }
        }
Esempio n. 18
0
    public static InvocationRequest Stream(CancellationToken cancellationToken, Type resultType, string invocationId,
                                           ILoggerFactory loggerFactory, HubConnection hubConnection, out ChannelReader <object?> result)
    {
        var req = new Streaming(cancellationToken, resultType, invocationId, loggerFactory, hubConnection);

        result = req.Result;
        return(req);
    }
Esempio n. 19
0
        public async Task <string> DoubleStreamUpload(ChannelReader <string> letters, ChannelReader <int> numbers)
        {
            var total = await Sum(numbers);

            var word = await UploadWord(letters);

            return(string.Format("You sent over <{0}> <{1}s>", total, word));
        }
Esempio n. 20
0
 public static IAsyncEnumerable <object> MakeCancelableAsyncEnumerableFromChannel <T>(ChannelReader <T> channel, CancellationToken cancellationToken = default)
 {
     return(MakeCancelableAsyncEnumerable(channel.ReadAllAsync(), cancellationToken));
 }
        private static async Task <List <ReplaceOneModel <BsonDocument> > > ReadModelsFromChannelAsync(ChannelReader <ReplaceOneModel <BsonDocument> > reader)
        {
            var models = new List <ReplaceOneModel <BsonDocument> >();

            await foreach (var model in reader.ReadAllAsync())
            {
                models.Add(model);
            }
            return(models);
        }
Esempio n. 22
0
 public static T MustRead <T>(this ChannelReader <T> reader)
 {
     return(reader.TryRead(out T value) ?
            value :
            throw new InvalidOperationException("nothing to read"));
 }
Esempio n. 23
0
 public ChannelReaderSource(ChannelReader <T> reader)
 {
     _reader = reader;
     Outlet  = new Outlet <T>("channelReader.out");
     Shape   = new SourceShape <T>(Outlet);
 }
Esempio n. 24
0
 public WebSocketChannel(ChannelReader <WebSocketMessage> input, ChannelWriter <WebSocketMessage> output)
 {
     _input  = input;
     _output = output;
 }
        private async Task <bool> WriteResultsAsync(TContext rootContext)
        {
            int currentIndex = 0;
            var itemsSeen    = new HashSet <int>();

            ChannelReader <int> reader = _resultsWritingChannel.Reader;

            // Wait until there is work or the channel is closed.
            while (await reader.WaitToReadAsync())
            {
                // Loop while there is work to do.
                while (reader.TryRead(out int item))
                {
                    Debug.Assert(!itemsSeen.Contains(item));
                    itemsSeen.Add(item);

                    // This condition can occur if currentIndex moves
                    // ahead in array processing due to operations
                    // against it by other threads. For this case,
                    // since the relevant file has already been
                    // processed, we just ignore this notification.
                    if (currentIndex > item)
                    {
                        break;
                    }

                    TContext context = default;
                    try
                    {
                        context = _fileContexts[currentIndex];

                        while (context?.AnalysisComplete == true)
                        {
                            CachingLogger cachingLogger = ((CachingLogger)context.Logger);
                            IDictionary <ReportingDescriptor, IList <Result> > results = cachingLogger.Results;

                            if (results?.Count > 0)
                            {
                                if (context.Hashes != null)
                                {
                                    Debug.Assert(_run != null);
                                    int index = _run.GetFileIndex(new ArtifactLocation()
                                    {
                                        Uri = context.TargetUri
                                    });

                                    _run.Artifacts[index].Hashes = new Dictionary <string, string>
                                    {
                                        { "sha-256", context.Hashes.Sha256 },
                                    };
                                }

                                foreach (KeyValuePair <ReportingDescriptor, IList <Result> > kv in results)
                                {
                                    foreach (Result result in kv.Value)
                                    {
                                        rootContext.Logger.Log(kv.Key, result);
                                    }
                                }
                            }

                            if (cachingLogger.ToolNotifications != null)
                            {
                                foreach (Notification notification in cachingLogger.ToolNotifications)
                                {
                                    rootContext.Logger.LogToolNotification(notification);
                                }
                            }

                            if (cachingLogger.ConfigurationNotifications != null)
                            {
                                foreach (Notification notification in cachingLogger.ConfigurationNotifications)
                                {
                                    rootContext.Logger.LogConfigurationNotification(notification);
                                }
                            }

                            RuntimeErrors |= context.RuntimeErrors;

                            _fileContexts[currentIndex] = default;

                            context       = currentIndex < (_fileContexts.Count - 1)
                                ? context = _fileContexts[currentIndex + 1]
                                : default;

                            currentIndex++;
                        }
                    }
                    catch (Exception e)
                    {
                        context        = default;
                        RuntimeErrors |= Errors.LogUnhandledEngineException(rootContext, e);
                        ThrowExitApplicationException(context, ExitReason.ExceptionWritingToLogFile, e);
                    }
                }
            }

            Debug.Assert(currentIndex == _fileContexts.Count);
            Debug.Assert(itemsSeen.Count == _fileContexts.Count);

            return(true);
        }
Esempio n. 26
0
        private async Task <bool> MergeSarifLogsAsync()
        {
            if (_options.SplittingStrategy != SplittingStrategy.PerRule)
            {
                this._idToSarifLogMap[""] = new SarifLog()
                {
                    Runs = new List <Run>()
                };
            }

            ChannelReader <SarifLog> reader = _mergeLogsChannel.Reader;

            // Wait until there is work or the channel is closed.
            while (await reader.WaitToReadAsync())
            {
                // Loop while there is work to do.
                while (reader.TryRead(out SarifLog sarifLog))
                {
                    foreach (Run run in sarifLog.Runs)
                    {
                        if (_options.SplittingStrategy == SplittingStrategy.PerRule || !_options.MergeEmptyLogs)
                        {
                            if (run.Results == null || run.Results.Count == 0)
                            {
                                continue;
                            }
                        }

                        IList <Result> cachedResults = run.Results;
                        run.Results = null;

                        Run emptyRun = run.DeepClone();
                        run.Results = cachedResults;

                        if (run.Results != null)
                        {
                            foreach (Result result in run.Results)
                            {
                                string key = _options.SplittingStrategy == SplittingStrategy.PerRule
                                    ? result.RuleId
                                    : string.Empty;

                                if (!_idToSarifLogMap.TryGetValue(key, out SarifLog splitLog))
                                {
                                    splitLog = _idToSarifLogMap[key] = new SarifLog()
                                    {
                                        Runs = new List <Run>()
                                    };
                                }

                                key = CreateRuleKey(result.RuleId, emptyRun);

                                if (!_ruleIdToRunsMap.TryGetValue(key, out Run splitRun))
                                {
                                    emptyRun.Results = new List <Result>();
                                    splitRun         = _ruleIdToRunsMap[key] = emptyRun;
                                    splitLog.Runs.Add(splitRun);
                                    _ruleIdToResultsMap[key] = new HashSet <Result>(Result.ValueComparer);
                                }

                                if (!_ruleIdToResultsMap[key].Contains(result))
                                {
                                    _ruleIdToResultsMap[key].Add(result);
                                    splitRun.Results.Add(result);
                                }
                            }
                        }
                    }
                }
            }
            return(true);
        }
Esempio n. 27
0
 public JoiningChannelReader(ChannelReader <TList> source, bool singleReader) : base(source, singleReader)
 {
 }
Esempio n. 28
0
 public Task UploadIgnoreItems(ChannelReader <string> source)
 {
     // Wait for an item to appear first then return from the hub method to end the invocation
     return(source.WaitToReadAsync().AsTask());
 }
Esempio n. 29
0
 /// <summary>
 /// Joins collections of the same type into a single channel reader in the order provided.
 /// </summary>
 /// <typeparam name="T">The result type.</typeparam>
 /// <param name="source">The source reader.</param>
 /// <param name="singleReader">True will cause the resultant reader to optimize for the assumption that no concurrent read operations will occur.</param>
 /// <returns>A channel reader containing the joined results.</returns>
 public static ChannelReader <T> Join <T>(this ChannelReader <IEnumerable <T> > source, bool singleReader = false)
 => new JoiningChannelReader <IEnumerable <T>, T>(source, singleReader);
Esempio n. 30
0
 public ChannelAsyncEnumerator(ChannelReader <T> channel, CancellationToken cancellationToken)
 {
     _channel           = channel;
     _cancellationToken = cancellationToken;
 }
        /// <summary>
        ///   Enumerates the events as they become available in the associated channel.
        /// </summary>
        ///
        /// <typeparam name="T">The type of data contained in the channel.</typeparam>
        ///
        /// <param name="reader">The <see cref="ChannelReader{T}" /> instance that this method was invoked on, and from which events are to be sourced.</param>
        /// <param name="maximumWaitTime">The maximum amount of time to wait to for an event to be available before emitting an empty item; if <c>null</c>, empty items will not be emitted.</param>
        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> instance to signal the request to cancel enumeration.</param>
        ///
        /// <returns>An asynchronous enumerator that can be used to iterate over events as they are available.</returns>
        ///
        public static async IAsyncEnumerable <T> EnumerateChannel <T>(this ChannelReader <T> reader,
                                                                      TimeSpan?maximumWaitTime,
                                                                      [EnumeratorCancellation] CancellationToken cancellationToken)
        {
            Argument.AssertNotNull(reader, nameof(reader));

            if (maximumWaitTime.HasValue)
            {
                Argument.AssertNotNegative(maximumWaitTime.Value, nameof(maximumWaitTime));
            }

            CancellationToken waitToken = cancellationToken;
            var waitSource = default(CancellationTokenSource);

            try
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    if (reader.TryRead(out T result))
                    {
                        waitSource?.CancelAfter(Timeout.Infinite);
                        yield return(result);
                    }
                    else if (reader.Completion.IsCompleted)
                    {
                        // If the channel was marked as final, then await the completion task to surface any exceptions.

                        await reader.Completion.ConfigureAwait(false);

                        break;
                    }
                    else
                    {
                        try
                        {
                            if (maximumWaitTime.HasValue)
                            {
                                if ((waitSource == null) || (waitSource.IsCancellationRequested))
                                {
                                    waitSource?.Dispose();
                                    waitSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
                                }

                                waitSource.CancelAfter(maximumWaitTime.Value);
                                waitToken = waitSource.Token;
                            }

                            // Wait for an item to be available in the channel; if it becomes so, then
                            // reset the loop so that it can be read and emitted.

                            if (await reader.WaitToReadAsync(waitToken).ConfigureAwait(false))
                            {
                                waitSource?.CancelAfter(Timeout.Infinite);
                                continue;
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            // This is thrown when the wait token expires.  It may be caused by the maximum wait time
                            // being exceeded or the main cancellation token being set.  Ignore this as an expected
                            // case; if the iteration was canceled, it will be detected in the body of the loop and
                            // appropriate action taken.

                            waitSource?.Dispose();
                            waitSource = null;
                        }

                        // If the wait token was set, but the main cancellation token was not, then the wait time was
                        // exceeded and a default item needs to be emitted.

                        if ((waitToken.IsCancellationRequested) && (!cancellationToken.IsCancellationRequested))
                        {
                            yield return(default);
Esempio n. 32
0
 public Barber(ChannelReader<Customer> customers)
 {
     this.customers = customers;
 }
Esempio n. 33
0
 /// <summary>
 /// Joins collections of the same type into a single channel reader in the order provided.
 /// </summary>
 /// <typeparam name="T">The result type.</typeparam>
 /// <param name="source">The source reader.</param>
 /// <param name="singleReader">True will cause the resultant reader to optimize for the assumption that no concurrent read operations will occur.</param>
 /// <returns>A channel reader containing the joined results.</returns>
 public static ChannelReader <T> Join <T>(this ChannelReader <T[]> source, bool singleReader = false)
 => new JoiningChannelReader <T[], T>(source, singleReader);
Esempio n. 34
0
 /// <summary>
 /// Joins collections of the same type into a single channel reader in the order provided.
 /// </summary>
 /// <typeparam name="T">The result type.</typeparam>
 /// <param name="source">The source reader.</param>
 /// <param name="singleReader">True will cause the resultant reader to optimize for the assumption that no concurrent read operations will occur.</param>
 /// <returns>A channel reader containing the joined results.</returns>
 public static ChannelReader <T> Join <T>(this ChannelReader <ICollection <T> > source, bool singleReader = false)
 => new JoiningChannelReader <ICollection <T>, T>(source, singleReader);
Esempio n. 35
0
 public static IAsyncEnumerable <T> AsAsyncEnumerable <T>(this ChannelReader <T> reader, CancellationToken cancellationToken = default)
 => reader.ReadAllAsync(cancellationToken);