Exemple #1
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);
            }
        }
Exemple #2
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);
            }
        }
Exemple #3
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);
        }
Exemple #4
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);
 }