コード例 #1
0
        /// <summary>
        /// Sets up a simple process that distributes the input to the output channels
        /// </summary>
        /// <returns>An awaitable task.</returns>
        /// <param name="input">The data source.</param>
        /// <param name="output">The channels to distribute the data to.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public static Task ScatterAsync <T>(IReadChannel <T> input, IWriteChannel <T>[] output, ScatterGatherPolicy policy = ScatterGatherPolicy.Any)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (output == null || output.Any(x => x == null))
            {
                throw new ArgumentNullException(nameof(output));
            }

            return(AutomationExtensions.RunTask(
                       new { Input = input, Output = output },
                       async self =>
            {
                if (policy == ScatterGatherPolicy.Any)
                {
                    while (true)
                    {
                        await MultiChannelAccess.WriteToAnyAsync(await self.Input.ReadAsync(), self.Output);
                    }
                }
                else
                {
                    var ix = 0;
                    while (true)
                    {
                        await self.Output[ix].WriteAsync(await self.Input.ReadAsync());
                        ix = (ix + 1) % output.Length;
                    }
                }
            }
                       ));
        }
コード例 #2
0
ファイル: MultiChannelSet.cs プロジェクト: bbants/cocol
 /// <summary>
 /// Writes to any of the channels.
 /// </summary>
 /// <param name="value">The value to write into the channel.</param>
 /// <param name="timeout">The maximum time to wait for any channel to become ready.</param>
 public Task <IWriteChannel <T> > WriteToAnyAsync(T value, TimeSpan timeout)
 {
     if (m_priority == MultiChannelPriority.Fair)
     {
         return(MultiChannelAccess.WriteToAnyAsync(
                    m_sortedChannels.NotifyUsed,
                    value,
                    m_sortedChannels.Channels,
                    timeout,
                    MultiChannelPriority.First
                    ));
     }
     else
     {
         return(MultiChannelAccess.WriteToAnyAsync(
                    null,
                    value,
                    m_channels,
                    timeout,
                    m_priority
                    ));
     }
 }