Ejemplo n.º 1
0
 public FTPDataConnection(IFTPConnection conn)
 {
     _conn = conn;
     _transType = conn.TransferType;
     set_transfer_type(conn, _transType);
     _conn.Lock();
     set_up_data_connection(conn);
 }
Ejemplo n.º 2
0
		/// <summary>
		/// Retrieves a remote file
		/// </summary>
		/// <param name="remoteFileName">The remote filename</param>
		/// <param name="localFileName">The local filename</param>
		/// <param name="type">The transfer type</param>
		public override void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type)
		{
			var ftStruct = new FileTransferStruct
			               	{
			               		LocalFileName = localFileName,
			               		RemoteFileName = remoteFileName,
			               		Type = type
			               	};

			_getFileTransfersQueue.Enqueue(ftStruct);

			ThreadPool.QueueUserWorkItem(GetFileFromQueue);
		}
Ejemplo n.º 3
0
        private void SetTransferType(FTPFileTransferType type)
        {
            switch (type)
            {
            case FTPFileTransferType.ASCII:
                SetMode("TYPE A");
                break;

            case FTPFileTransferType.Binary:
                SetMode("TYPE I");
                break;

            default:
                throw new Exception("Invalid File Transfer Type");
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 下载断点续传
        /// </summary>
        /// <param name="remoteFileName"></param>
        /// <param name="stream"></param>
        /// <param name="type"></param>
        public void GetStreamContinue(string remoteFileName, Stream stream, FTPFileTransferType type)
        {
            TcpListener listner = null;
            TcpClient client = null;
            NetworkStream networkStream = null;
            ArrayList tempMessageList = null;

            int returnValue = 0;

            //Begin 增加断点续传功能 发送REST命令给FTP服务器
            tempMessageList = new ArrayList();
            tempMessageList = SendCommand("REST "+stream.Length);
            returnValue = GetMessageReturnValue((string)tempMessageList[0]);
            if (returnValue != 350)
            {
                throw new Exception((string)tempMessageList[0]);
            }
            //End 增加断点续传功能 发送REST命令给FTP服务器

            tempMessageList = new ArrayList();

            string returnValueMessage = "";

            LockTcpClient();

            SetTransferType(type);

            if (this.mode == FTPMode.Active)
            {
                listner = CreateDataListner();
                listner.Start();
            }
            else
            {
                client = CreateDataClient();
            }

            tempMessageList = new ArrayList();
            tempMessageList = SendCommand("RETR " + remoteFileName);
            returnValue = GetMessageReturnValue((string)tempMessageList[0]);
            if (!(returnValue == 150 || returnValue == 125))
            {
                throw new Exception((string)tempMessageList[0]);
            }

            if (this.mode == FTPMode.Active)
            {
                client = listner.AcceptTcpClient();
            }

            networkStream = client.GetStream();

            Byte[] buffer = new Byte[BLOCK_SIZE];
            int bytes = 0;

            bool read = true;
            while (read)
            {
                bytes = (int)networkStream.Read(buffer, 0, buffer.Length);
                stream.Write(buffer, 0, bytes);
                if (bytes == 0)
                {
                    read = false;
                }
            }

            networkStream.Close();
            client.Close();

            if (this.mode == FTPMode.Active)
            {
                listner.Stop();
            }

            if (tempMessageList.Count == 1)
            {
                tempMessageList = Read();
                returnValue = GetMessageReturnValue((string)tempMessageList[0]);
                returnValueMessage = (string)tempMessageList[0];
            }
            else
            {
                returnValue = GetMessageReturnValue((string)tempMessageList[1]);
                returnValueMessage = (string)tempMessageList[1];
            }

            if (!(returnValue == 226))
            {
                throw new Exception(returnValueMessage);
            }

            UnlockTcpClient();
        }
Ejemplo n.º 5
0
 public override void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type)
 {
     EnqueueThread(CreateGetFileThread(remoteFileName, localFileName, type));
 }
Ejemplo n.º 6
0
 public override void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type)
 {
     enqueue_thread(create_get_file_thread(remoteFileName, localFileName, type));
 }
Ejemplo n.º 7
0
 private int set_transfer_type(IFTPConnection conn, FTPFileTransferType type)
 {
     int result = 0;
     switch (type)
     {
         case FTPFileTransferType.ASCII:
             result = set_mode(conn, "TYPE A");
             break;
         case FTPFileTransferType.Binary:
             result = set_mode(conn, "TYPE I");
             break;
         default:
             _currentMsg = "Invalid File Transfer Type";
             break;
     }
     return result;
 }
Ejemplo n.º 8
0
 public override void SendFile(string localFileName, string remoteFileName, FTPFileTransferType type)
 {
     EnqueueThread(CreateSendFileThread(localFileName, remoteFileName, type));
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Sends a stream to a remote file
        /// </summary>
        /// <param name="stream">The stream to send</param>
        /// <param name="remoteFileName">The remote file name</param>
        /// <param name="type">The transfer type</param>
        public void SendStream(Stream stream, string remoteFileName, FTPFileTransferType type)
        {
            TcpListener   listener           = null;
            TcpClient     client             = null;
            NetworkStream networkStream      = null;
            var           tempMessageList    = new List <string>();
            int           returnValue        = 0;
            string        returnValueMessage = "";

            lock (_tcpClient)
            {
                SetTransferType(type);

                if (_mode == FTPMode.Active)
                {
                    listener = CreateDataListener();
                    listener.Start();
                }
                else
                {
                    client = CreateDataClient();
                }

                tempMessageList = SendCommand("STOR " + remoteFileName);
                returnValue     = GetMessageReturnValue(tempMessageList[0]);
                if (!(returnValue == 150 || returnValue == 125))
                {
                    throw new Exception(tempMessageList[0]);
                }

                if (_mode == FTPMode.Active)
                {
                    client = listener.AcceptTcpClient();
                }

                networkStream = client.GetStream();

                var buf        = new byte[BlockSize];
                int bytesRead  = 0;
                int totalBytes = 0;

                while (totalBytes < stream.Length)
                {
                    bytesRead  = stream.Read(buf, 0, BlockSize);
                    totalBytes = totalBytes + bytesRead;
                    networkStream.Write(buf, 0, bytesRead);
                }

                stream.Close();

                networkStream.Close();
                client.Close();

                if (_mode == FTPMode.Active)
                {
                    listener.Stop();
                }

                if (tempMessageList.Count == 1)
                {
                    tempMessageList    = Read();
                    returnValue        = GetMessageReturnValue(tempMessageList[0]);
                    returnValueMessage = tempMessageList[0];
                }
                else
                {
                    returnValue        = GetMessageReturnValue(tempMessageList[1]);
                    returnValueMessage = tempMessageList[1];
                }

                if (!(returnValue == 226))
                {
                    throw new Exception(returnValueMessage);
                }
            }
        }
Ejemplo n.º 10
0
		/// <summary>
		/// Connects a stream to remote file
		/// </summary>
		/// <param name="remoteFileName">The remote file name</param>
		/// <param name="stream">The stream to connect to the remote file</param>
		/// <param name="type">The transfer type</param>
		public void GetStream(string remoteFileName, Stream stream, FTPFileTransferType type)
		{
			TcpListener listener = null;
			TcpClient client = null;
			NetworkStream networkStream = null;
			List<string> tempMessageList;
			int returnValue = 0;
			string returnValueMessage = "";

			lock (_tcpClient)
			{
				SetTransferType(type);

				if (_mode == FTPMode.Active)
				{
					listener = CreateDataListener();
					listener.Start();
				}
				else
				{
					client = CreateDataClient();
				}

				tempMessageList = SendCommand("RETR " + remoteFileName);
				returnValue = GetMessageReturnValue(tempMessageList[0]);
				if (!(returnValue == 150 || returnValue == 125))
				{
					throw new Exception(tempMessageList[0]);
				}

				if (_mode == FTPMode.Active)
				{
					client = listener.AcceptTcpClient();
				}

				networkStream = client.GetStream();

				var buffer = new byte[BlockSize];
				int bytesRead = 0;

				bool bRead = true;
				while (bRead)
				{
					bytesRead = networkStream.Read(buffer, 0, buffer.Length);
					stream.Write(buffer, 0, bytesRead);

					if (bytesRead == 0)
					{
						bRead = false;
					}
				}

				stream.Close();

				networkStream.Close();
				client.Close();

				if (_mode == FTPMode.Active)
				{
					listener.Stop();
				}

				if (tempMessageList.Count == 1)
				{
					tempMessageList = Read();
					returnValue = GetMessageReturnValue(tempMessageList[0]);
					returnValueMessage = tempMessageList[0];
				}
				else
				{
					returnValue = GetMessageReturnValue(tempMessageList[1]);
					returnValueMessage = tempMessageList[1];
				}

				if (!(returnValue == 226))
				{
					throw new Exception(returnValueMessage);
				}
			}
		}
Ejemplo n.º 11
0
 public override void GetFile(string remoteFileName, FTPFileTransferType type)
 {
     GetFile(remoteFileName, Path.GetFileName(remoteFileName), type);
 }
Ejemplo n.º 12
0
		/// <summary>
		/// Sends a stream to a remote file
		/// </summary>
		/// <param name="stream">The stream to send</param>
		/// <param name="remoteFileName">The remote file name</param>
		/// <param name="type">The transfer type</param>
		public void SendStream(Stream stream, string remoteFileName, FTPFileTransferType type)
		{
			TcpListener listener = null;
			TcpClient client = null;
			NetworkStream networkStream = null;
			var tempMessageList = new List<string>();
			int returnValue = 0;
			string returnValueMessage = "";

			lock (_tcpClient)
			{
				SetTransferType(type);

				if (_mode == FTPMode.Active)
				{
					listener = CreateDataListener();
					listener.Start();
				}
				else
				{
					client = CreateDataClient();
				}

				tempMessageList = SendCommand("STOR " + remoteFileName);
				returnValue = GetMessageReturnValue(tempMessageList[0]);
				if (!(returnValue == 150 || returnValue == 125))
				{
					throw new Exception(tempMessageList[0]);
				}

				if (_mode == FTPMode.Active)
				{
					client = listener.AcceptTcpClient();
				}

				networkStream = client.GetStream();

				var buf = new byte[BlockSize];
				int bytesRead = 0;
				int totalBytes = 0;

				while (totalBytes < stream.Length)
				{
					bytesRead = stream.Read(buf, 0, BlockSize);
					totalBytes = totalBytes + bytesRead;
					networkStream.Write(buf, 0, bytesRead);
				}

				stream.Close();

				networkStream.Close();
				client.Close();

				if (_mode == FTPMode.Active)
				{
					listener.Stop();
				}

				if (tempMessageList.Count == 1)
				{
					tempMessageList = Read();
					returnValue = GetMessageReturnValue(tempMessageList[0]);
					returnValueMessage = tempMessageList[0];
				}
				else
				{
					returnValue = GetMessageReturnValue(tempMessageList[1]);
					returnValueMessage = tempMessageList[1];
				}

				if (!(returnValue == 226))
				{
					throw new Exception(returnValueMessage);
				}
			}
		}
Ejemplo n.º 13
0
		/// <summary>
		/// Sends a file to the remote server
		/// </summary>
		/// <param name="localFileName">The local filename</param>
		/// <param name="remoteFileName">The remote filename</param>
		/// <param name="type">The transfer type</param>
		public virtual void SendFile(string localFileName, string remoteFileName, FTPFileTransferType type)
		{
			using (var file = new FileStream(localFileName, FileMode.Open))
			{
				SendStream(file, remoteFileName, type);
			}
		}
Ejemplo n.º 14
0
 /// <summary>
 /// Retrieves a remote file
 /// </summary>
 /// <param name="remoteFileName">The remote file name</param>
 /// <param name="localFileName">The local file name</param>
 /// <param name="type">The transfer type</param>
 public virtual void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type)
 {
     GetStream(remoteFileName, new FileStream(localFileName, FileMode.Create), type);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Retrieves a remote file
 /// </summary>
 /// <param name="remoteFileName">The remote file name</param>
 /// <param name="type">The transfer type</param>
 public virtual void GetFile(string remoteFileName, FTPFileTransferType type)
 {
     GetFile(remoteFileName, Path.GetFileName(remoteFileName), type);
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Connects a stream to remote file
        /// </summary>
        /// <param name="remoteFileName">The remote file name</param>
        /// <param name="stream">The stream to connect to the remote file</param>
        /// <param name="type">The transfer type</param>
        public void GetStream(string remoteFileName, Stream stream, FTPFileTransferType type)
        {
            TcpListener   listener      = null;
            TcpClient     client        = null;
            NetworkStream networkStream = null;
            List <string> tempMessageList;
            int           returnValue        = 0;
            string        returnValueMessage = "";

            lock (_tcpClient)
            {
                SetTransferType(type);

                if (_mode == FTPMode.Active)
                {
                    listener = CreateDataListener();
                    listener.Start();
                }
                else
                {
                    client = CreateDataClient();
                }

                tempMessageList = SendCommand("RETR " + remoteFileName);
                returnValue     = GetMessageReturnValue(tempMessageList[0]);
                if (!(returnValue == 150 || returnValue == 125))
                {
                    throw new Exception(tempMessageList[0]);
                }

                if (_mode == FTPMode.Active)
                {
                    client = listener.AcceptTcpClient();
                }

                networkStream = client.GetStream();

                var buffer    = new byte[BlockSize];
                int bytesRead = 0;

                bool bRead = true;
                while (bRead)
                {
                    bytesRead = networkStream.Read(buffer, 0, buffer.Length);
                    stream.Write(buffer, 0, bytesRead);

                    if (bytesRead == 0)
                    {
                        bRead = false;
                    }
                }

                stream.Close();

                networkStream.Close();
                client.Close();

                if (_mode == FTPMode.Active)
                {
                    listener.Stop();
                }

                if (tempMessageList.Count == 1)
                {
                    tempMessageList    = Read();
                    returnValue        = GetMessageReturnValue(tempMessageList[0]);
                    returnValueMessage = tempMessageList[0];
                }
                else
                {
                    returnValue        = GetMessageReturnValue(tempMessageList[1]);
                    returnValueMessage = tempMessageList[1];
                }

                if (!(returnValue == 226))
                {
                    throw new Exception(returnValueMessage);
                }
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// 上传断点续传
 /// </summary>
 /// <param name="localFileName"></param>
 /// <param name="remoteFileName"></param>
 /// <param name="type"></param>
 public virtual void SendFileContinue(string localFileName, string remoteFileName, FTPFileTransferType type)
 {
     FileStream fs = new FileStream(localFileName, FileMode.Open);
     SendStream(fs, remoteFileName, type);
     fs.Close();
 }
Ejemplo n.º 18
0
		/// <summary>
		/// Retrieves a remote file
		/// </summary>
		/// <param name="remoteFileName">The remote file name</param>
		/// <param name="localFileName">The local file name</param>
		/// <param name="type">The transfer type</param>
		public virtual void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type)
		{
			GetStream(remoteFileName, new FileStream(localFileName, FileMode.Create), type);
		}
Ejemplo n.º 19
0
        /// <summary>
        /// 上传 断点续传
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="remoteFileName"></param>
        /// <param name="type"></param>
        public void SendStreamContinue(Stream stream, string remoteFileName, FTPFileTransferType type)
        {
            LockTcpClient();
            TcpListener listner = null;
            TcpClient client = null;
            NetworkStream networkStream = null;
            ArrayList tempMessageList = new ArrayList();
            int returnValue = 0;
            string returnValueMessage = "";
            tempMessageList = new ArrayList();

            SetTransferType(type);

            if (this.mode == FTPMode.Active)
            {
                listner = CreateDataListner();
                listner.Start();
            }
            else
            {
                client = CreateDataClient();
            }

            #region 断点续传
            //需要修改
            long startPosition = 0;
            //Send REST 命令
            tempMessageList = SendCommand("REST " + startPosition);
            returnValue = GetMessageReturnValue((string)tempMessageList[0]);
            if (returnValue != 350)
            {
                throw new Exception((string)tempMessageList[0]);
            }
            tempMessageList = SendCommand("APPE " + remoteFileName);
            returnValue = GetMessageReturnValue((string)tempMessageList[0]);
            if (!(returnValue == 150 || returnValue == 125))
            {
                throw new Exception((string)tempMessageList[0]);
            }
            #endregion

            //tempMessageList = SendCommand("STOR " + remoteFileName);
            //returnValue = GetMessageReturnValue((string)tempMessageList[0]);
            //if (!(returnValue == 150 || returnValue == 125))
            //{
            //    throw new Exception((string)tempMessageList[0]);
            //}

            if (this.mode == FTPMode.Active)
            {
                client = listner.AcceptTcpClient();
            }

            networkStream = client.GetStream();

            Byte[] buffer = new Byte[BLOCK_SIZE];
            int bytes = 0;
            int totalBytes = 0;

            while (totalBytes < stream.Length)
            {
                bytes = (int)stream.Read(buffer, 0, BLOCK_SIZE);
                totalBytes = totalBytes + bytes;
                networkStream.Write(buffer, 0, bytes);
            }

            networkStream.Close();
            client.Close();

            if (this.mode == FTPMode.Active)
            {
                listner.Stop();
            }

            if (tempMessageList.Count == 1)
            {
                tempMessageList = Read();
                returnValue = GetMessageReturnValue((string)tempMessageList[0]);
                returnValueMessage = (string)tempMessageList[0];
            }
            else
            {
                returnValue = GetMessageReturnValue((string)tempMessageList[1]);
                returnValueMessage = (string)tempMessageList[1];
            }

            if (!(returnValue == 226))
            {
                throw new Exception(returnValueMessage);
            }
            UnlockTcpClient();
        }
Ejemplo n.º 20
0
 public override void SendFile(string localFileName, string remoteFileName, FTPFileTransferType type)
 {
     enqueue_thread(create_send_file_thread(localFileName, remoteFileName, type));
 }
Ejemplo n.º 21
0
 public override void SendFile(string localFileName, FTPFileTransferType type)
 {
     SendFile(localFileName, Path.GetFileName(localFileName), type);
 }
Ejemplo n.º 22
0
        private Thread create_send_file_thread(string localFileName, string remoteFileName, FTPFileTransferType type)
        {
            var ft = new FileTransferStruct {LocalFileName = localFileName, RemoteFileName = remoteFileName, Type = type};
            this.sendFileTransfersQueue.Enqueue(ft);

            var thread = new Thread(new ThreadStart(SendFileFromQueue))
                             {
                                 Name = "GetFileFromQueue " + localFileName + ", " + remoteFileName + ", " + type.ToString()
                             };
            ;
            return thread;
        }
Ejemplo n.º 23
0
        //------------------------Private methods: -------------------------------------------------
        private Thread CreateGetFileThread(string remoteFileName, string localFileName, FTPFileTransferType type)
        {
            FileTransferStruct ft = new FileTransferStruct();

            ft.LocalFileName  = localFileName;
            ft.RemoteFileName = remoteFileName;
            ft.Type           = type;
            this.getFileTransfersQueue.Enqueue(ft);

            Thread thread = new Thread(new ThreadStart(GetFileFromQueue));

            thread.Name = "GetFileFromQueue " + remoteFileName + ", " + localFileName + ", " + type.ToString();;
            return(thread);
        }
Ejemplo n.º 24
0
        public void GetStream(string remoteFileName, Stream stream, FTPFileTransferType type)
        {
            TcpListener   listner            = null;
            TcpClient     client             = null;
            NetworkStream networkStream      = null;
            ArrayList     tempMessageList    = new ArrayList();
            int           returnValue        = 0;
            string        returnValueMessage = "";

            LockTcpClient();

            SetTransferType(type);

            if (this.mode == FTPMode.Active)
            {
                listner = CreateDataListner();
                listner.Start();
            }
            else
            {
                client = CreateDataClient();
            }

            tempMessageList = new ArrayList();
            tempMessageList = SendCommand("RETR " + remoteFileName);
            returnValue     = GetMessageReturnValue((string)tempMessageList[0]);
            if (!(returnValue == 150 || returnValue == 125))
            {
                throw new Exception((string)tempMessageList[0]);
            }

            if (this.mode == FTPMode.Active)
            {
                client = listner.AcceptTcpClient();
            }

            networkStream = client.GetStream();

            Byte[] buffer = new Byte[BLOCK_SIZE];
            int    bytes  = 0;

            bool read = true;

            while (read)
            {
                bytes = (int)networkStream.Read(buffer, 0, buffer.Length);
                stream.Write(buffer, 0, bytes);
                if (bytes == 0)
                {
                    read = false;
                }
            }

            networkStream.Close();
            client.Close();

            if (this.mode == FTPMode.Active)
            {
                listner.Stop();
            }

            if (tempMessageList.Count == 1)
            {
                tempMessageList    = Read();
                returnValue        = GetMessageReturnValue((string)tempMessageList[0]);
                returnValueMessage = (string)tempMessageList[0];
            }
            else
            {
                returnValue        = GetMessageReturnValue((string)tempMessageList[1]);
                returnValueMessage = (string)tempMessageList[1];
            }

            if (!(returnValue == 226))
            {
                throw new Exception(returnValueMessage);
            }

            UnlockTcpClient();
        }
Ejemplo n.º 25
0
        private Thread create_get_file_thread(string remoteFileName, string localFileName, FTPFileTransferType type)
        {
            FileTransferStruct ft = new FileTransferStruct();
            ft.LocalFileName = localFileName;
            ft.RemoteFileName = remoteFileName;
            ft.Type = type;
            this.getFileTransfersQueue.Enqueue(ft);

            Thread thread = new Thread(new ThreadStart(get_file_from_queue));
            thread.Name = "GetFileFromQueue " + remoteFileName + ", " + localFileName + ", " + type.ToString();;
            return thread;
        }
Ejemplo n.º 26
0
 /// <summary>
 /// 设置传输类型
 /// </summary>
 /// <param name="type"></param>
 private void SetTransferType(FTPFileTransferType type)
 {
     switch (type)
     {
         case FTPFileTransferType.ASCII:
             SetMode("TYPE A");
             break;
         case FTPFileTransferType.Binary:
             SetMode("TYPE I");
             break;
         default:
             throw new Exception("Invalid File Transfer Type");
     }
 }
Ejemplo n.º 27
0
 public override void GetFile(string remoteFileName, FTPFileTransferType type)
 {
     GetFile(remoteFileName, Path.GetFileName(remoteFileName), type);
 }
Ejemplo n.º 28
0
 public virtual void GetFile(string remoteFileName, FTPFileTransferType type)
 {
     GetFile(remoteFileName, Path.GetFileName(remoteFileName), type);
 }
Ejemplo n.º 29
0
 public override void SendFile(string localFileName, FTPFileTransferType type)
 {
     SendFile(localFileName, Path.GetFileName(localFileName), type);
 }
Ejemplo n.º 30
0
 /// <summary>
 /// 下载断点续传
 /// </summary>
 /// <param name="remoteFileName"></param>
 /// <param name="localFileName"></param>
 /// <param name="type"></param>
 public virtual void GetFileContinue(string remoteFileName, string localFileName, FTPFileTransferType type)
 {
     FileStream fs = new FileStream(localFileName, FileMode.Append);
     GetStream(remoteFileName, fs, type);
     fs.Close();
 }