// // ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ // ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ // █__ --█ █__ --█ ◄█ - █ // █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ // ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ // │ Copyright (c) JP Dillingham. // │ // │ This program is free software: you can redistribute it and/or modify // │ it under the terms of the GNU Affero General Public License as published // │ by the Free Software Foundation, version 3. // │ // │ This program is distributed in the hope that it will be useful, // │ but WITHOUT ANY WARRANTY; without even the implied warranty of // │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // │ GNU Affero General Public License for more details. // │ // │ You should have received a copy of the GNU Affero General Public License // │ along with this program. If not, see https://www.gnu.org/licenses/. // │ // │ This program is distributed with Additional Terms pursuant to Section 7 // │ of the AGPLv3. See the LICENSE file in the root directory of this // │ project for the complete terms and conditions. // │ // │ https://slskd.org // │ // ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ // │ SPDX-FileCopyrightText: JP Dillingham // │ SPDX-License-Identifier: AGPL-3.0-only // ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ // namespace slskd.Shares { using System; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; /// /// Reads and handles items from a channel. /// public interface IChannelReader { /// /// Gets the that completes when the reader has read all available items from the channel. /// Task Completed { get; } /// /// Gets a value indicating whether the reader has started reading. /// bool Started { get; } /// /// Starts the reader. /// void Start(); } /// /// Shared file cache worker. /// /// The type of the underlying channel. public class ChannelReader : IChannelReader { /// /// Initializes a new instance of the class. /// /// The channel from which the reader will read. /// An to be invoked for each item read from the channel. /// An optional to be invoked if the reader encounters an . /// An optional value indicating whether to automatically start the reader. /// An optional to monitor for cancellation. public ChannelReader( Channel channel, Action handler, Action exceptionHandler = null, bool automaticallyStart = true, CancellationToken cancellationToken = default) { Channel = channel; Handler = handler; ExceptionHandler = exceptionHandler; CancellationToken = cancellationToken; if (automaticallyStart) { Start(); } } /// /// Gets the that completes when the worker has completed all of its work. /// public Task Completed => TaskCompletionSource.Task; /// /// Gets a value indicating whether the reader has started reading. /// public bool Started { get; private set; } private Channel Channel { get; } private Action Handler { get; } private Action ExceptionHandler { get; } private TaskCompletionSource TaskCompletionSource { get; } = new TaskCompletionSource(); private CancellationToken CancellationToken { get; } private object SyncRoot { get; } = new object(); /// /// Starts the reader. /// public void Start() { lock (SyncRoot) { if (!Started) { _ = Read(); Started = true; } } } private async Task Read() { try { while (!Channel.Reader.Completion.IsCompleted) { var item = await Channel.Reader.ReadAsync(CancellationToken); Handler(item); } } catch (ChannelClosedException) { // noop. the channel might close between the time we check and when we go to read; this just means there is no more data. } catch (Exception ex) { ExceptionHandler?.Invoke(ex); TaskCompletionSource.SetException(ex); throw; } finally { TaskCompletionSource.TrySetResult(); } } } }