public async Task <FtpStreamChannel> OpenPassiveDataStreamAsync(FtpDataConnectionType connectionType, FtpRequest request) { FtpPassiveModeResponse reply; if (connectionType == FtpDataConnectionType.EPSV || connectionType == FtpDataConnectionType.AutoPassive) { if (!(reply = await SetExtendedPassiveMode()).Success) { // if we're connected with IPv4 and data channel type is AutoPassive then fallback to IPv4 if (reply.Type == FtpResponseType.PermanentNegativeCompletion && connectionType == FtpDataConnectionType.AutoPassive && LocalEndPoint.Type == HostNameType.Ipv4) { return(await OpenPassiveDataStreamAsync(FtpDataConnectionType.PASV, request)); } throw new FtpCommandException(reply); } } else { if (LocalEndPoint.Type != HostNameType.Ipv4) { throw new FtpException("Only IPv4 is supported by the PASV command. Use EPSV instead."); } if (!(reply = await SetPassiveMode()).Success) { throw new FtpCommandException(reply); } } var passiveConnection = new FtpStreamChannel(); await passiveConnection.ConnectAsync(reply.HostName, reply.ServiceName); // NOTE: Maybe it is better to just to open the passive channel, and execute the command else where.. if (!(await ExecuteAsync <FtpResponse>(request)).Success) { passiveConnection.Dispose(); throw new FtpCommandException(reply); } return(passiveConnection); }
/// <summary> /// Disconnect from the server /// </summary> public async Task DisconnectAsync() { if (m_Stream == null || !m_Stream.IsConnected) { return; } try { await m_Stream.DisconnectAsync(); } catch (IOException e) { System.Diagnostics.Debug.WriteLine("IOException thrown closing control connectin: " + e); } finally { m_Stream.Dispose(); } }