| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.ComponentModel; |
| | 4 | | using System.Diagnostics.CodeAnalysis; |
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Channels; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | |
|
| | 10 | | using Microsoft.Extensions.Logging; |
| | 11 | |
|
| | 12 | | namespace mkmrk.Channels; |
| | 13 | |
|
| | 14 | | /// <inheritdoc cref="IBroadcastChannelReader{TData,TResponse}" /> |
| | 15 | | public class BroadcastChannelReader<TData, TResponse> : ChannelReader<TData>, IBroadcastChannelReader<TData, TResponse> |
| | 16 | | private readonly RemoveWriterByHashCode _removeReader; |
| | 17 | | private readonly ChannelWriter<TResponse> _responseWriter; |
| | 18 | | private readonly ChannelReader<TData> _dataReader; |
| | 19 | | private bool _isDisposed; |
| | 20 | | private readonly int _writerHash; |
| | 21 | | private readonly ILogger<BroadcastChannelReader<TData, TResponse>> _logger; |
| | 22 | |
|
| 2 | 23 | | internal BroadcastChannelReader( |
| 2 | 24 | | ChannelReader<TData> dataReader, |
| 2 | 25 | | int inputDataWriterHashCode, |
| 2 | 26 | | ChannelWriter<TResponse> responseWriter, |
| 2 | 27 | | RemoveWriterByHashCode removeReaderFunction, |
| 2 | 28 | | ILogger<BroadcastChannelReader<TData, TResponse>> logger |
| 2 | 29 | | ) { |
| 2 | 30 | | this._writerHash = inputDataWriterHashCode; |
| 2 | 31 | | this._logger = logger; |
| 2 | 32 | | this._removeReader = removeReaderFunction; |
| 2 | 33 | | this._responseWriter = responseWriter; |
| 2 | 34 | | this._dataReader = dataReader; |
| 2 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// This is only for Dependency Injection purposes and should not be used by the user. Instead use <see cref="IBroad |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="broadcastChannelWriter"></param> |
| | 41 | | [ EditorBrowsable( EditorBrowsableState.Never ) ] |
| 2 | 42 | | public BroadcastChannelReader( IBroadcastChannelWriter<TData, TResponse> broadcastChannelWriter ) { |
| 2 | 43 | | ArgumentNullException.ThrowIfNull( broadcastChannelWriter ); |
| 2 | 44 | | ( this._dataReader, this._writerHash, this._removeReader, this._responseWriter, this._logger ) = broadcastChanne |
| 2 | 45 | | this._logger.LogTrace( "Registered with Writer: {Writer}", broadcastChannelWriter ); |
| 2 | 46 | | } |
| | 47 | |
|
| | 48 | | /* ************************************************** */ |
| | 49 | |
|
| | 50 | | /// <inheritdoc cref="System.Threading.Channels.ChannelReader{T}.TryRead"/> |
| 2 | 51 | | public override bool TryRead( [ MaybeNullWhen( false ) ] out TData item ) => !this._isDisposed ? this._dataReader.Tr |
| | 52 | |
|
| | 53 | | /// <inheritdoc cref="System.Threading.Channels.ChannelReader{T}.TryPeek"/> |
| 0 | 54 | | public override bool TryPeek( [ MaybeNullWhen( false ) ] out TData item ) => !this._isDisposed ? this._dataReader.Tr |
| | 55 | |
|
| | 56 | | /// <inheritdoc cref="System.Threading.Channels.ChannelReader{T}.WaitToReadAsync"/> |
| | 57 | | public override ValueTask<bool> WaitToReadAsync( CancellationToken cancellationToken = default ) => |
| 2 | 58 | | !this._isDisposed |
| 2 | 59 | | ? this._dataReader.WaitToReadAsync( cancellationToken ) |
| 2 | 60 | | : ThrowHelper.ThrowObjectDisposedException<ValueTask<bool>>( nameof(BroadcastChannelReader<TData, TResponse> |
| | 61 | |
|
| | 62 | | // warning occurs because there is no `yield` statement, but this is a direct return for ChannelReader<T>.ReadAllAsy |
| | 63 | | #pragma warning disable CS8424 |
| | 64 | | // TODO: try: does [AggressiveInlining] help here? |
| | 65 | | /// <inheritdoc cref="System.Threading.Channels.ChannelReader{T}.ReadAllAsync"/> |
| | 66 | | public override IAsyncEnumerable<TData> ReadAllAsync( [ EnumeratorCancellation ] CancellationToken cancellationToken |
| 0 | 67 | | => !this._isDisposed ? this._dataReader.ReadAllAsync( cancellationToken ) : ThrowHelper.ThrowObjectDisposedExcep |
| | 68 | |
|
| | 69 | | #pragma warning restore CS8424 |
| | 70 | |
|
| | 71 | | /// <inheritdoc cref="ChannelWriter{T}.WriteAsync" /> |
| 2 | 72 | | public ValueTask WriteResponseAsync( TResponse response, CancellationToken cancellationToken = default ) => this._re |
| | 73 | |
|
| | 74 | |
|
| | 75 | | /// <inheritdoc cref="ChannelReader{T}.Completion"/> |
| 0 | 76 | | public override Task Completion => !this._isDisposed ? this._dataReader.Completion : ThrowHelper.ThrowObjectDisposed |
| | 77 | |
|
| | 78 | | /// <inheritdoc /> |
| 0 | 79 | | public override int Count => !this._isDisposed ? this._dataReader.Count : ThrowHelper.ThrowObjectDisposedException<i |
| | 80 | |
|
| | 81 | | /// <inheritdoc /> |
| 0 | 82 | | public override bool CanCount => !this._isDisposed ? this._dataReader.CanCount : ThrowHelper.ThrowObjectDisposedExce |
| | 83 | |
|
| | 84 | | /// <inheritdoc /> |
| 0 | 85 | | public override bool CanPeek => !this._isDisposed ? this._dataReader.CanPeek : ThrowHelper.ThrowObjectDisposedExcept |
| | 86 | |
|
| | 87 | |
|
| | 88 | | /* * |
| | 89 | | * As long as I have Dispose (){ if (_disposed) ... } it doesn't matter if a transient service is disposed by the de |
| | 90 | | */ |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Removes reader from BroadcastChannel |
| | 94 | | /// </summary> |
| | 95 | | /// <remarks> |
| | 96 | | /// This method is only needed because if used in Dependency Injection, it might not be disposed when done using, |
| | 97 | | /// which means the Channel would continually be written to without being read, |
| | 98 | | /// wasting potentially significant amounts of memory. |
| | 99 | | /// <p/> |
| | 100 | | /// While the documentation says that a dependent/requesting type should never Dispose of an injected type |
| | 101 | | /// that was created by the ServiceProvider (and the factory pattern can not be used with Open Generic Types), |
| | 102 | | /// it is still ok (and really <b>MUST</b> be done) for the dependent type to Dispose this <see cref="BroadcastChann |
| | 103 | | /// as the Disposed status is tracked and it will not be disposed of twice. |
| | 104 | | /// </remarks> |
| 2 | 105 | | public void Dispose( ) { |
| 2 | 106 | | this.Dispose( true ); |
| 2 | 107 | | GC.SuppressFinalize( this ); |
| 2 | 108 | | } |
| | 109 | |
|
| | 110 | | // ReSharper disable once InconsistentNaming |
| | 111 | | /// <inheritdoc cref="IDisposable.Dispose"/> |
| 2 | 112 | | protected virtual void Dispose( bool disposing ) { |
| 2 | 113 | | this._logger.LogTrace( "Dispose({Disposing}) {Type}", disposing, this.GetType().GenericTypeShortDescriptor( useS |
| 2 | 114 | | if ( this._isDisposed ) { |
| 0 | 115 | | return; |
| | 116 | | } |
| | 117 | |
|
| 2 | 118 | | if ( disposing ) { |
| 2 | 119 | | this._removeReader( _writerHash ); |
| 2 | 120 | | } |
| | 121 | |
|
| 2 | 122 | | this._isDisposed = true; |
| 2 | 123 | | } |
| | 124 | |
|
| | 125 | | /// <inheritdoc /> |
| | 126 | | // NULL checking is required here, as this could be called from within the constructor before the properties are set |
| | 127 | | // ReSharper disable ConditionalAccessQualifierIsNonNullableAccordingToAPIContract |
| 2 | 128 | | public override string ToString( ) => $"{this.GetType().GenericTypeShortDescriptor( useShortGenericName: false )} [H |
| | 129 | | // ReSharper restore ConditionalAccessQualifierIsNonNullableAccordingToAPIContract |
| | 130 | | } |
| | 131 | |
|
| | 132 | | /// <inheritdoc /> |
| | 133 | | /// <remarks> |
| | 134 | | /// <see cref="BroadcastChannelReader{TData}"/> with a default Response type of <see cref="IBroadcastChannelResponse"/> |
| | 135 | | /// </remarks> |
| | 136 | | public class BroadcastChannelReader<TData> : BroadcastChannelReader<TData, IBroadcastChannelResponse> { |
| | 137 | | /// <inheritdoc /> |
| | 138 | | [ EditorBrowsable( EditorBrowsableState.Never ) ] |
| | 139 | | public BroadcastChannelReader( IBroadcastChannelWriter<TData> broadcastChannelWriter ) |
| | 140 | | : base( broadcastChannelWriter as IBroadcastChannelWriter<TData, IBroadcastChannelResponse> |
| | 141 | | ?? ThrowHelper.ThrowInvalidCastException<IBroadcastChannelWriter<TData>, IBroadcastChannelWriter<TData, |
| | 142 | | } |