Пример #1
0
        private async Task <ComplexSocket> GetPassiveComplexSocketAsync(ComplexSocket controlComplexSocket,
                                                                        CancellationToken cancellationToken)
        {
            var success = await this.SendAndLogAsync(controlComplexSocket,
                                                     cancellationToken,
                                                     "PASV");

            if (!success)
            {
                return(null);
            }

            var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                         cancellationToken);

            if (!ftpReply.Success)
            {
                return(null);
            }

            var ipEndPoint = FtpClientHelper.ParseIPEndPoint(ftpReply);

            if (ipEndPoint == null)
            {
                return(null);
            }

            var transferComplexSocket = ComplexSocket.CreateForTransfer(this,
                                                                        ipEndPoint);

            return(transferComplexSocket);
        }
Пример #2
0
        private async Task <bool> GotoParentDirectoryAsync(ComplexSocket controlComplexSocket,
                                                           FtpFileSystemObject ftpFileSystemObject,
                                                           CancellationToken cancellationToken,
                                                           bool createDirectoryIfNotExists = false)
        {
            var ftpDirectory     = ftpFileSystemObject.GetParentFtpDirectory();
            var directoryChanges = FtpClientHelper.DirectoryChanges(this._currentFtpDirectory,
                                                                    ftpDirectory);

            foreach (var directoryChange in directoryChanges)
            {
                if (string.Equals(directoryChange,
                                  FtpFileSystemObject.ParentChangeCommand))
                {
                    var success = await this.SendAndLogAsync(controlComplexSocket,
                                                             cancellationToken,
                                                             "CDUP");

                    if (success)
                    {
                        var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                                     cancellationToken);

                        if (ftpReply.Success)
                        {
                            this._currentFtpDirectory = this._currentFtpDirectory.GetParentFtpDirectory();
                        }
                    }
                }
                else
                {
                    var success = await this.ChangeWorkingDirectoryAsync(controlComplexSocket,
                                                                         directoryChange,
                                                                         cancellationToken,
                                                                         createDirectoryIfNotExists);

                    if (!success)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Пример #3
0
        private async Task <FtpFeatures> GetFtpFeaturesAsync(ComplexSocket controlComplexSocket,
                                                             CancellationToken cancellationToken)
        {
            var success = await this.SendAndLogAsync(controlComplexSocket,
                                                     cancellationToken,
                                                     "FEAT");

            if (!success)
            {
                return(FtpFeatures.Unknown);
            }
            var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                         cancellationToken);

            if (!ftpReply.Success)
            {
                return(FtpFeatures.Unknown);
            }

            var messages    = ftpReply.Messages;
            var ftpFeatures = FtpClientHelper.ParseFtpFeatures(messages);

            return(ftpFeatures);
        }
Пример #4
0
        public FtpClient()
        {
            this.Encoding       = Encoding.UTF8;
            this.ConnectTimeout = TimeSpan.FromSeconds(30);
            this.ReceiveTimeout = TimeSpan.FromSeconds(30);
            this.SendTimeout    = TimeSpan.FromMinutes(5);
            this.SocketClientAccessPolicyProtocol = SocketClientAccessPolicyProtocol.Http;

            this._ftpFeatures = new Lazy <FtpFeatures>(() =>
            {
                var controlComplexSocket = this._controlComplexSocket;
                {
                    var success = controlComplexSocket.Send("FEAT",
                                                            this.Encoding,
                                                            this.SendTimeout);
                    if (!success)
                    {
                        return(FtpFeatures.Unknown);
                    }
                }
                {
                    var complexResult = controlComplexSocket.Receive(this.Encoding,
                                                                     this.ReceiveTimeout);
                    var success = complexResult.Success;
                    if (!success)
                    {
                        return(FtpFeatures.Unknown);
                    }

                    var messages    = complexResult.Messages;
                    var ftpFeatures = FtpClientHelper.ParseFtpFeatures(messages);

                    return(ftpFeatures);
                }
            });
        }