BroadcastBlock.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // BroadcastBlock.cs
  2. //
  3. // Copyright (c) 2011 Jérémie "garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. #if NET_4_0 || MOBILE
  25. using System;
  26. using System.Threading.Tasks;
  27. using System.Collections.Generic;
  28. using System.Collections.Concurrent;
  29. namespace System.Threading.Tasks.Dataflow
  30. {
  31. public sealed class BroadcastBlock<T> : IPropagatorBlock<T, T>, ITargetBlock<T>, IDataflowBlock, ISourceBlock<T>, IReceivableSourceBlock<T>
  32. {
  33. static readonly DataflowBlockOptions defaultOptions = new DataflowBlockOptions ();
  34. CompletionHelper compHelper = CompletionHelper.GetNew ();
  35. BlockingCollection<T> messageQueue = new BlockingCollection<T> ();
  36. MessageBox<T> messageBox;
  37. MessageVault<T> vault;
  38. DataflowBlockOptions dataflowBlockOptions;
  39. readonly Func<T, T> cloner;
  40. MessageOutgoingQueue<T> outgoing;
  41. TargetBuffer<T> targets = new TargetBuffer<T> ();
  42. DataflowMessageHeader headers = DataflowMessageHeader.NewValid ();
  43. public BroadcastBlock (Func<T, T> cloner) : this (cloner, defaultOptions)
  44. {
  45. }
  46. public BroadcastBlock (Func<T, T> cloner, DataflowBlockOptions dataflowBlockOptions)
  47. {
  48. if (dataflowBlockOptions == null)
  49. throw new ArgumentNullException ("dataflowBlockOptions");
  50. this.cloner = cloner;
  51. this.dataflowBlockOptions = dataflowBlockOptions;
  52. this.messageBox = new PassingMessageBox<T> (messageQueue, compHelper, () => outgoing.IsCompleted, BroadcastProcess, dataflowBlockOptions);
  53. this.outgoing = new MessageOutgoingQueue<T> (compHelper, () => messageQueue.IsCompleted);
  54. this.vault = new MessageVault<T> ();
  55. }
  56. public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
  57. T messageValue,
  58. ISourceBlock<T> source,
  59. bool consumeToAccept)
  60. {
  61. return messageBox.OfferMessage (this, messageHeader, messageValue, source, consumeToAccept);
  62. }
  63. public IDisposable LinkTo (ITargetBlock<T> target, bool unlinkAfterOne)
  64. {
  65. return targets.AddTarget (target, unlinkAfterOne);
  66. }
  67. public T ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target, out bool messageConsumed)
  68. {
  69. return cloner(vault.ConsumeMessage (messageHeader, target, out messageConsumed));
  70. }
  71. public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
  72. {
  73. vault.ReleaseReservation (messageHeader, target);
  74. }
  75. public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
  76. {
  77. return vault.ReserveMessage (messageHeader, target);
  78. }
  79. public bool TryReceive (Predicate<T> filter, out T item)
  80. {
  81. return outgoing.TryReceive (filter, out item);
  82. }
  83. public bool TryReceiveAll (out IList<T> items)
  84. {
  85. return outgoing.TryReceiveAll (out items);
  86. }
  87. void BroadcastProcess ()
  88. {
  89. T input;
  90. if (!messageQueue.TryTake (out input) || targets.Current == null)
  91. return;
  92. foreach (var target in targets) {
  93. DataflowMessageHeader header = headers.Increment ();
  94. if (cloner != null)
  95. vault.StoreMessage (header, input);
  96. target.OfferMessage (header, input, this, cloner != null);
  97. // TODO: verify if it's the correct semantic
  98. T save = input;
  99. if (!messageQueue.TryTake (out input))
  100. input = save;
  101. }
  102. }
  103. public void Complete ()
  104. {
  105. messageBox.Complete ();
  106. }
  107. public void Fault (Exception ex)
  108. {
  109. compHelper.Fault (ex);
  110. }
  111. public Task Completion {
  112. get {
  113. return compHelper.Completion;
  114. }
  115. }
  116. }
  117. }
  118. #endif