Path: redb-route/redb.Route.GenericFile/RemoteFileProducer.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.GenericFile/RemoteFileProducer.cs ---- using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Core; namespace redb.Route.GenericFile; /// /// Base class for remote file producers (SFTP, FTP). Extends /// with connection lifecycle management: auto-connect before write, reconnect on failure, /// disconnect after write if configured. /// /// Concrete options type inheriting . public abstract class RemoteFileProducer : GenericFileProducer where TOptions : RemoteFileEndpointOptions { private readonly IRemoteFileOperations _remoteOps; /// Remote file operations with connection lifecycle. protected IRemoteFileOperations RemoteOperations => _remoteOps; /// Creates a remote file producer. protected RemoteFileProducer(IEndpoint endpoint, TOptions options, IRemoteFileOperations operations) : base(endpoint, options, operations) { ArgumentNullException.ThrowIfNull(operations); _remoteOps = operations; } /// /// Connects to the remote server and auto-creates the target directory if configured. /// protected override async Task BeforeWriteAsync(string targetDir, CancellationToken ct) { await EnsureConnectedAsync(ct).ConfigureAwait(false); await base.BeforeWriteAsync(targetDir, ct).ConfigureAwait(false); } /// /// Disconnects from the remote server if the Disconnect option is set. /// protected override Task OnWriteCompletedAsync(IExchange exchange, string targetPath, CancellationToken ct) { if (Options.Disconnect) DisconnectClient(); return Task.CompletedTask; } /// public override async Task Stop(CancellationToken ct = default) { DisconnectClient(); await base.Stop(ct).ConfigureAwait(false); } // ═══════════════════════════════════════════════════════════════════ // CONNECTION MANAGEMENT // ═══════════════════════════════════════════════════════════════════ /// /// Ensures a connection is active, reconnecting with retry if necessary. /// protected async Task EnsureConnectedAsync(CancellationToken ct) { if (_remoteOps.IsConnected) return; var attempts = 0; while (true) { try { await _remoteOps.DisconnectAsync(ct).ConfigureAwait(false); await _remoteOps.ConnectAsync(ct).ConfigureAwait(false); return; } catch (Exception ex) when (++attempts <= Options.MaximumReconnectAttempts) { Logger?.LogWarning(ex, "Producer reconnect to {Host}:{Port} failed, attempt {Attempt}/{Max}", Options.Host, Options.Port, attempts, Options.MaximumReconnectAttempts); await Task.Delay(Options.ReconnectDelay, ct).ConfigureAwait(false); } } } /// /// Safely disconnects from the remote server. /// protected void DisconnectClient() { try { if (_remoteOps.IsConnected) _remoteOps.DisconnectAsync(CancellationToken.None).ConfigureAwait(false).GetAwaiter().GetResult(); } catch (Exception ex) { Logger?.LogDebug(ex, "Producer: error during disconnect"); } } }