TransformBlock.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // TransformBlock.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. using System.Collections.Generic;
  23. using System.Collections.Concurrent;
  24. namespace System.Threading.Tasks.Dataflow
  25. {
  26. public sealed class TransformBlock<TInput, TOutput> :
  27. IPropagatorBlock<TInput, TOutput>, IReceivableSourceBlock<TOutput>
  28. {
  29. static readonly ExecutionDataflowBlockOptions defaultOptions = new ExecutionDataflowBlockOptions ();
  30. readonly ExecutionDataflowBlockOptions dataflowBlockOptions;
  31. readonly CompletionHelper compHelper;
  32. readonly BlockingCollection<TInput> messageQueue = new BlockingCollection<TInput> ();
  33. readonly MessageBox<TInput> messageBox;
  34. readonly MessageOutgoingQueue<TOutput> outgoing;
  35. readonly Func<TInput, TOutput> transformer;
  36. public TransformBlock (Func<TInput, TOutput> transformer) : this (transformer, defaultOptions)
  37. {
  38. }
  39. public TransformBlock (Func<TInput, TOutput> transformer, ExecutionDataflowBlockOptions dataflowBlockOptions)
  40. {
  41. if (dataflowBlockOptions == null)
  42. throw new ArgumentNullException ("dataflowBlockOptions");
  43. this.transformer = transformer;
  44. this.dataflowBlockOptions = dataflowBlockOptions;
  45. this.compHelper = CompletionHelper.GetNew (dataflowBlockOptions);
  46. this.messageBox = new ExecutingMessageBox<TInput> (
  47. this, messageQueue, compHelper,
  48. () => outgoing.IsCompleted, TransformProcess, () => outgoing.Complete (),
  49. dataflowBlockOptions);
  50. this.outgoing = new MessageOutgoingQueue<TOutput> (this, compHelper,
  51. () => messageQueue.IsCompleted, () => messageBox.DecreaseCount (),
  52. dataflowBlockOptions);
  53. }
  54. public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
  55. TInput messageValue,
  56. ISourceBlock<TInput> source,
  57. bool consumeToAccept)
  58. {
  59. return messageBox.OfferMessage (messageHeader, messageValue, source, consumeToAccept);
  60. }
  61. public IDisposable LinkTo (ITargetBlock<TOutput> target, DataflowLinkOptions linkOptions)
  62. {
  63. return outgoing.AddTarget (target, linkOptions);
  64. }
  65. public TOutput ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<TOutput> target, out bool messageConsumed)
  66. {
  67. return outgoing.ConsumeMessage (messageHeader, target, out messageConsumed);
  68. }
  69. public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<TOutput> target)
  70. {
  71. outgoing.ReleaseReservation (messageHeader, target);
  72. }
  73. public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<TOutput> target)
  74. {
  75. return outgoing.ReserveMessage (messageHeader, target);
  76. }
  77. public bool TryReceive (Predicate<TOutput> filter, out TOutput item)
  78. {
  79. return outgoing.TryReceive (filter, out item);
  80. }
  81. public bool TryReceiveAll (out IList<TOutput> items)
  82. {
  83. return outgoing.TryReceiveAll (out items);
  84. }
  85. bool TransformProcess ()
  86. {
  87. TInput input;
  88. var dequeued = messageQueue.TryTake (out input);
  89. if (dequeued)
  90. outgoing.AddData (transformer (input));
  91. return dequeued;
  92. }
  93. public void Complete ()
  94. {
  95. messageBox.Complete ();
  96. }
  97. public void Fault (Exception ex)
  98. {
  99. compHelper.RequestFault (ex);
  100. }
  101. public Task Completion {
  102. get {
  103. return compHelper.Completion;
  104. }
  105. }
  106. public int OutputCount {
  107. get {
  108. return outgoing.Count;
  109. }
  110. }
  111. public int InputCount {
  112. get {
  113. return messageQueue.Count;
  114. }
  115. }
  116. public override string ToString ()
  117. {
  118. return NameHelper.GetName (this, dataflowBlockOptions);
  119. }
  120. }
  121. }