Example #1
0
        public async Task <bool> BeginUploadFile(string name, FileUploadReadInputBytesDelegate readInputDelegate, FileUploadBytesWrittenDelegate bytesWrittenDelegate, FileUploadFinishedDelegate uploadFinishedDelegate, IOTaskIsCanceledDelegate taskCanceled, bool binaryMode = false)
        {
            //check parameter to be null
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("readInputDelegate");
            }
            if (readInputDelegate == null)
            {
                throw new ArgumentNullException("readInputDelegate");
            }
            if (bytesWrittenDelegate == null)
            {
                throw new ArgumentNullException("bytesWrittenDelegate");
            }
            if (uploadFinishedDelegate == null)
            {
                throw new ArgumentNullException("uploadFinishedDelegate");
            }
            if (taskCanceled == null)
            {
                throw new ArgumentNullException("taskCanceled");
            }

            IsIdled = false;

            checkConnected();

            if (_dataChannelSocket == null)
            {
                if (EnterPassiveModeAutomatic)
                {
                    await EnterPassiveMode();
                }
            }

            if (binaryMode)
            {
                EnterBinaryMode();
            }

            string answer;

            if (!TrySendCommandReadAnswer("STOR " + name, out answer))
            {
                return(false);
            }

            if (answer == null)
            {
                return(false);
            }

            if (answer.Trim().StartsWith("425"))
            {
                _dataChannelSocket = null;
                if (EnterPassiveModeAutomatic)
                {
                    await EnterPassiveMode();

                    if (!TrySendCommandReadAnswer("STOR " + name, out answer))
                    {
                        return(false);
                    }

                    if (answer == null)
                    {
                        return(false);
                    }
                }
            }

            if (answer.Trim().StartsWith("150") | answer.Trim().StartsWith("125"))
            {
                beginUploadData(readInputDelegate, bytesWrittenDelegate, new FileUploadFinishedDelegate((bool success) =>
                {
                    try
                    {
                        answer = ReadAnswer(ClientType.ActiveClient);
                    }
                    catch (IOException) { throw; }
                    EnterASCIIMode();
                    IsIdled = true;
                    uploadFinishedDelegate(success && answer.Trim().StartsWith("226"));
                }), taskCanceled);
            }

            return(false);
        }
Example #2
0
        public async void BeginUploadFileWithUniqueName(FileUploadReadInputBytesDelegate readInputDelegate, FileUploadBytesWrittenDelegate bytesWrittenDelegate, FileUploadFinishedDelegate uploadFinishedDelegate, IOTaskIsCanceledDelegate taskCanceled, bool binaryMode = false)
        {
            //check parameter to be null
            if (readInputDelegate == null)
            {
                throw new ArgumentNullException("readInputDelegate");
            }
            if (bytesWrittenDelegate == null)
            {
                throw new ArgumentNullException("bytesWrittenDelegate");
            }
            if (uploadFinishedDelegate == null)
            {
                throw new ArgumentNullException("uploadFinishedDelegate");
            }
            if (taskCanceled == null)
            {
                throw new ArgumentNullException("taskCanceled");
            }

            IsIdled = false;

            checkConnected();

            //check whether the passive client is already connected. If not connect a new one.
            if (_dataChannelSocket == null)
            {
                if (EnterPassiveModeAutomatic)
                {
                    await EnterPassiveMode();
                }
            }

            //enter the binary mode when requested
            if (binaryMode)
            {
                EnterBinaryMode();
            }

            string answer;

            if (!TrySendCommandReadAnswer("STOU", out answer))
            {
                return;
            }

            if (answer == null)
            {
                return;
            }

            if (answer.Trim().StartsWith("425"))
            {
                _dataChannelSocket = null;
                if (EnterPassiveModeAutomatic)
                {
                    await EnterPassiveMode();

                    if (!TrySendCommandReadAnswer("STOU", out answer))
                    {
                        return;
                    }

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

            if (answer.Trim().StartsWith("150") & answer.Trim().StartsWith("125"))
            {
                beginUploadData(readInputDelegate, bytesWrittenDelegate, new FileUploadFinishedDelegate((bool success) =>
                {
                    try
                    {
                        answer = ReadAnswer(ClientType.ActiveClient);
                    }
                    catch (IOException) { throw; }
                    EnterASCIIMode();
                    IsIdled = true;
                    uploadFinishedDelegate(success && answer.Trim().StartsWith("226"));
                }), taskCanceled);
            }
        }
Example #3
0
        /// <summary>
        /// Internal method to download the content of the download's client connection.
        /// </summary>
        /// <param name="bytesReadDelegate">A <see cref="FileDownloadBytesReadDelegate"/> which is called everytime an array of bytes has been read from the server. /></param>
        /// <param name="downloadFinished">A <see cref="FileDownloadFinished"/> which is called after the entire download has been completed.</param>
        /// <param name="taskCanceled">A <see cref="IOTaskIsCanceledDelegate"/> which is called before each reading cycle to cancel the download.</param>>
        private void beginDownloadData(FileDownloadBytesReadDelegate bytesReadDelegate, FileDownloadFinished downloadFinished, IOTaskIsCanceledDelegate taskCanceled)
        {
            ulong totalReadBytes = 0;
            int   readbytes      = 1;

            byte[] buffer = new byte[10240];

            while (readbytes > 0)
            {
                if (taskCanceled())
                {
                    break;
                }
                try
                {
                    readbytes = _dataChannelSocketReader.Read(buffer, 0, 1024);
                }
                catch (Exception) { break; }
                finally
                {
                    totalReadBytes += (ulong)readbytes;
                    bytesReadDelegate(readbytes, totalReadBytes, buffer);
                }
            }

            _dataChannelSocket.Dispose();
            _dataChannelSocket = null;

            string answer = string.Empty;

            try
            {
                answer = ReadAnswer(ClientType.ActiveClient);
            }
            catch (IOException) { throw; }

            downloadFinished(totalReadBytes);
        }
Example #4
0
        /// <summary>
        /// Downloads a file.
        /// </summary>
        /// <param name="name">The name of the file.</param>
        /// <param name="bytesReadDelegate">A <see cref="FileDownloadBytesReadDelegate"/> which is called everytime an array of bytes has been read from the server. /></param>
        /// <param name="downloadFinished">A <see cref="FileDownloadFinished"/> which is called after the entire download has been completed.</param>
        /// <param name="taskCanceled">A <see cref="IOTaskIsCanceledDelegate"/> which is called before each reading cycle to cancel the download.</param>
        /// <param name="binaryMode">Indicates whether the file should be downloaded in binary or text mode.</param>
        public async void BeginDownloadFile(string name, FileDownloadBytesReadDelegate bytesReadDelegate, FileDownloadFinished downloadFinished, IOTaskIsCanceledDelegate taskCanceled, bool binaryMode = false)
        {
            IsIdled = false;

            checkConnected();

            if (binaryMode)
            {
                EnterBinaryMode();
            }

            if (_dataChannelSocket == null)
            {
                if (EnterPassiveModeAutomatic)
                {
                    await EnterPassiveMode();
                }
            }


            string answer;

            if (!TrySendCommandReadAnswer("RETR " + name, out answer))
            {
                return;
            }

            if (answer == null)
            {
                return;
            }

            if (answer.Trim().StartsWith("425"))
            {
                _dataChannelSocket = null;
                if (EnterPassiveModeAutomatic)
                {
                    await EnterPassiveMode();

                    if (!TrySendCommandReadAnswer("RETR " + name, out answer))
                    {
                        return;
                    }

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

            if (answer.Trim().StartsWith("150") | answer.Trim().StartsWith("125"))
            {
                beginDownloadData(bytesReadDelegate, new FileDownloadFinished((ulong readbytes) =>
                {
                    EnterASCIIMode();
                    IsIdled = true;
                    downloadFinished(readbytes);
                }), taskCanceled);
            }
        }
Example #5
0
        private void beginUploadData(FileUploadReadInputBytesDelegate readInputDelegate, FileUploadBytesWrittenDelegate bytesWrittenDelegate, FileUploadFinishedDelegate uploadFinishedDelegate, IOTaskIsCanceledDelegate taskCanceled)
        {
            ulong totalWrittenBytes = 0;

            byte[] buffer;

            try
            {
                buffer = readInputDelegate(1024);

                while (buffer.Length != 0)
                {
                    //break the loop when the task is marked as canceled
                    if (taskCanceled())
                    {
                        break;
                    }

                    _dataChannelSocketWriter.Write(buffer);
                    _dataChannelSocketWriter.Flush();
                    totalWrittenBytes += (ulong)buffer.Length;
                    bytesWrittenDelegate(totalWrittenBytes);

                    //read new bytes from the input to send them to the server in the next iteration
                    buffer = readInputDelegate(1024);
                }

                //dispose client to close connection
                _dataChannelSocket.Dispose();
                _dataChannelSocket = null;

                //inform the caller that the upload is finished
                uploadFinishedDelegate(true);
            }
            catch (Exception)
            {
                //if client is not null close and dispose it as well
                if (_dataChannelSocket != null)
                {
                    _dataChannelSocket.Dispose();
                    _dataChannelSocket = null;
                }

                //inform the caller about the failure of the upload
                uploadFinishedDelegate(false);
            }
        }
Example #6
0
 public async void BeginUploadFile(string absoluteFileName, FileUploadReadInputBytesDelegate readInputDelegate, FileUploadBytesWrittenDelegate bytesWrittenDelegate, FileUploadFinishedDelegate uploadFinishedDelegate, IOTaskIsCanceledDelegate taskCanceled, bool binaryMode = false)
 {
     await(await getIdledConnection()).BeginUploadFile(absoluteFileName, readInputDelegate, bytesWrittenDelegate, uploadFinishedDelegate, taskCanceled, binaryMode);
 }
Example #7
0
 public async void BeginDownloadFile(string absoluteFileName, FileDownloadBytesReadDelegate bytesReadDelegate, FileDownloadFinished downloadFinished, IOTaskIsCanceledDelegate taskCanceled, bool binaryMode = false)
 {
     (await getIdledConnection()).BeginDownloadFile(absoluteFileName, bytesReadDelegate, downloadFinished, taskCanceled, binaryMode);
 }