private async Task WriteResponseAsync(
            IFtpConnection connection,
            IFtpResponse response,
            CancellationToken cancellationToken)
        {
            var networkStreamFeature = connection.Features.Get <INetworkStreamFeature>();
            var encoding             = connection.Features.Get <IEncodingFeature>().Encoding;

            var writer = networkStreamFeature.Output;

            object?token = null;

            do
            {
                var line = await response.GetNextLineAsync(token, cancellationToken)
                           .ConfigureAwait(false);

                if (line.HasText)
                {
                    _logger?.LogDebug("{Response}", line.Text);
                    var data   = encoding.GetBytes($"{line.Text}\r\n");
                    var memory = writer.GetMemory(data.Length);
                    data.AsSpan().CopyTo(memory.Span);
                    writer.Advance(data.Length);
                    var flushResult = await writer.FlushAsync(cancellationToken);

                    if (flushResult.IsCanceled || flushResult.IsCompleted)
                    {
                        break;
                    }
                }

                if (!line.HasMoreData)
                {
                    break;
                }

                token = line.Token;
            }while (token != null);

#pragma warning disable CS0618 // Typ oder Element ist veraltet
            if (response.AfterWriteAction != null)
            {
                var nextResponse = await response.AfterWriteAction(connection, cancellationToken)
                                   .ConfigureAwait(false);

                if (nextResponse != null)
                {
                    await WriteResponseAsync(connection, nextResponse, cancellationToken)
                    .ConfigureAwait(false);
                }
            }
#pragma warning restore CS0618 // Typ oder Element ist veraltet

            await writer.FlushAsync(cancellationToken)
            .ConfigureAwait(false);
        }
Exemple #2
0
 /// <summary>
 /// Logs a warning message with the data of the <see cref="FtpResponse"/>.
 /// </summary>
 /// <param name="log">The <see cref="ILogger"/> to use.</param>
 /// <param name="response">The <see cref="FtpResponse"/> to log.</param>
 public static void Warn([NotNull] this ILogger log, [NotNull] IFtpResponse response)
 {
     if (response is FtpResponse ftpResponse)
     {
         log.LogWarning("{code} {message}", response.Code, ftpResponse.Message);
     }
     else
     {
         log.LogWarning(response.ToString());
     }
 }
 /// <summary>
 /// Logs a info message with the data of the <see cref="FtpResponse"/>.
 /// </summary>
 /// <param name="log">The <see cref="ILogger"/> to use.</param>
 /// <param name="response">The <see cref="FtpResponse"/> to log.</param>
 public static void Info(this ILogger log, IFtpResponse response)
 {
     if (response is FtpResponse ftpResponse)
     {
         log.LogInformation("{code} {message}", response.Code, ftpResponse.Message);
     }
     else
     {
         log.LogInformation(response.ToString());
     }
 }
        private async Task SendResponseAsync([CanBeNull] IFtpResponse response, CancellationToken cancellationToken)
        {
            if (response == null)
            {
                return;
            }

            var serverCommandFeature = _connection.Features.Get <IServerCommandFeature>();
            await serverCommandFeature.ServerCommandWriter
            .WriteAsync(new SendResponseServerCommand(response), cancellationToken)
            .ConfigureAwait(false);

            if (response.Code == 421)
            {
                await _connection.StopAsync().ConfigureAwait(false);
            }
        }
Exemple #5
0
 /// <summary>
 /// Logs a message with the data of the <see cref="FtpResponse"/>.
 /// </summary>
 /// <param name="log">The <see cref="ILogger"/> to use.</param>
 /// <param name="response">The <see cref="FtpResponse"/> to log.</param>
 /// <remarks>
 /// It logs either a trace, debug, or warning message depending on the
 /// <see cref="FtpResponse.Code"/>.
 /// </remarks>
 public static void Log([NotNull] this ILogger log, [NotNull] IFtpResponse response)
 {
     if (response.Code >= 200 && response.Code < 300)
     {
         log.Trace(response);
     }
     else if (response.Code >= 300 && response.Code < 400)
     {
         log.Info(response);
     }
     else if (response.Code < 200)
     {
         log.Debug(response);
     }
     else
     {
         log.Warn(response);
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SendResponseServerCommand"/> class.
 /// </summary>
 /// <param name="response">The response to send.</param>
 public SendResponseServerCommand([NotNull] IFtpResponse response)
 {
     Response = response;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SendResponseServerCommand"/> class.
 /// </summary>
 /// <param name="response">The response to send.</param>
 public SendResponseServerCommand(IFtpResponse response)
 {
     Response = response;
 }